memem

Dependencies:   mbed Sht31 MAX44009

Committer:
kentwong
Date:
Sat Apr 25 06:41:27 2020 +0000
Revision:
12:5a140bcc456f
Parent:
11:3a63d6f65ff5
why u have to go and make things so complicated?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chapman515 0:b1b7113a1441 1 #include "mbed.h"
chapman515 0:b1b7113a1441 2 #include "Sht31.h"
chapman515 0:b1b7113a1441 3 #include "MAX44009.h"
chapman515 0:b1b7113a1441 4 #include "mbed_wait_api.h"
chapman515 0:b1b7113a1441 5 #include <list>
chapman515 0:b1b7113a1441 6
chapman515 0:b1b7113a1441 7 Serial pc(p13,p14);
chapman515 0:b1b7113a1441 8 Sht31 sht31(p9, p10);
chapman515 0:b1b7113a1441 9 MAX44009 myMAX44009 ( p9, p10, MAX44009::MAX44009_ADDRESS_A0_GND, 400000 );
chapman515 0:b1b7113a1441 10 I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL)
chapman515 0:b1b7113a1441 11
chapman515 9:7c53c49afa40 12 InterruptIn smthing_from_BLE(p12);
chapman515 0:b1b7113a1441 13 DigitalOut BLE_Can_receive(p11); // 0 = can recieve
kentwong 10:a9db57e6456e 14 AnalogIn input(p15);
kentwong 10:a9db57e6456e 15 Timer t;
chapman515 0:b1b7113a1441 16 class RGB{
chapman515 0:b1b7113a1441 17 public : int C;
chapman515 0:b1b7113a1441 18 int R;
chapman515 0:b1b7113a1441 19 int G;
chapman515 0:b1b7113a1441 20 int B;
chapman515 0:b1b7113a1441 21 };
chapman515 0:b1b7113a1441 22 class RGBf{
chapman515 0:b1b7113a1441 23 public : float C;
chapman515 0:b1b7113a1441 24 float R;
chapman515 0:b1b7113a1441 25 float G;
chapman515 0:b1b7113a1441 26 float B;
chapman515 0:b1b7113a1441 27 };
chapman515 0:b1b7113a1441 28 class TandH{
chapman515 0:b1b7113a1441 29 public : float t;
chapman515 0:b1b7113a1441 30 float h;
chapman515 0:b1b7113a1441 31 };
kentwong 11:3a63d6f65ff5 32 class myData{
kentwong 11:3a63d6f65ff5 33 public : int timestamp;
kentwong 11:3a63d6f65ff5 34 unsigned short light;
kentwong 11:3a63d6f65ff5 35 unsigned short noise;
kentwong 11:3a63d6f65ff5 36
kentwong 11:3a63d6f65ff5 37 };
chapman515 0:b1b7113a1441 38
chapman515 0:b1b7113a1441 39 RGB RGBdata[10];
chapman515 0:b1b7113a1441 40 TandH THdata[10];
chapman515 0:b1b7113a1441 41 float luxdata[10];
kentwong 11:3a63d6f65ff5 42 myData sleepData[1000];
kentwong 11:3a63d6f65ff5 43 int sleepDataPos=0;
chapman515 0:b1b7113a1441 44 int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead;
kentwong 7:e4984029012e 45 int sendCounter=0; // for sending in auto/ instant measure
kentwong 7:e4984029012e 46 int _30Scounter=0;
chapman515 0:b1b7113a1441 47 bool allfilled = false;
chapman515 0:b1b7113a1441 48 RGBf RGBavg;
chapman515 0:b1b7113a1441 49 TandH THavg;
chapman515 0:b1b7113a1441 50 float luxavg;
chapman515 0:b1b7113a1441 51
kentwong 6:94a26631ec03 52 bool wrote=false;
chapman515 1:3889765eb394 53 DigitalOut led(LED1);
kentwong 6:94a26631ec03 54 char signal[1];
kentwong 11:3a63d6f65ff5 55 //LocalFileSystem local("local"); //Create the local filesystem under the name "local"
chapman515 1:3889765eb394 56
chapman515 9:7c53c49afa40 57 void letsee() {
chapman515 9:7c53c49afa40 58 led=!led;
chapman515 9:7c53c49afa40 59 pc.scanf("%1s", signal);
chapman515 9:7c53c49afa40 60 wrote=true;
chapman515 9:7c53c49afa40 61 }
chapman515 9:7c53c49afa40 62
chapman515 9:7c53c49afa40 63
chapman515 9:7c53c49afa40 64
kentwong 6:94a26631ec03 65
chapman515 0:b1b7113a1441 66 int main()
kentwong 6:94a26631ec03 67 {
kentwong 10:a9db57e6456e 68 set_time(1587744159);
kentwong 10:a9db57e6456e 69
chapman515 9:7c53c49afa40 70 led=1;
kentwong 6:94a26631ec03 71 //pc.read
chapman515 9:7c53c49afa40 72 BLE_Can_receive = 0;
chapman515 0:b1b7113a1441 73 pc.baud ( 19200 );
chapman515 9:7c53c49afa40 74 smthing_from_BLE.fall(&letsee);
kentwong 7:e4984029012e 75
kentwong 6:94a26631ec03 76
kentwong 6:94a26631ec03 77 while(1) {
chapman515 0:b1b7113a1441 78 counter = counter%10;
kentwong 10:a9db57e6456e 79 time_t seconds = time(NULL);
kentwong 10:a9db57e6456e 80 t.start();
kentwong 10:a9db57e6456e 81 float peakToPeak=0, signalMax=0, signalMin=1024;
kentwong 10:a9db57e6456e 82 float _sample;
kentwong 10:a9db57e6456e 83 int start = t.read_ms();
kentwong 10:a9db57e6456e 84 while (t.read_ms() - start < 50) {
kentwong 10:a9db57e6456e 85 _sample=input.read();
kentwong 10:a9db57e6456e 86 if (_sample>signalMax){
kentwong 10:a9db57e6456e 87 signalMax=_sample;
kentwong 10:a9db57e6456e 88 }
kentwong 10:a9db57e6456e 89 else if (_sample<signalMin){
kentwong 10:a9db57e6456e 90 signalMin=_sample;
kentwong 10:a9db57e6456e 91 }
kentwong 10:a9db57e6456e 92 }
kentwong 10:a9db57e6456e 93 int end = t.read_ms();
kentwong 10:a9db57e6456e 94
kentwong 10:a9db57e6456e 95
kentwong 10:a9db57e6456e 96 t.reset();
kentwong 10:a9db57e6456e 97 peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
kentwong 10:a9db57e6456e 98
kentwong 10:a9db57e6456e 99 float volts = peakToPeak * 0.707; // convert to RMS voltage
kentwong 10:a9db57e6456e 100 float first = log10(volts/0.00631)*20;
kentwong 10:a9db57e6456e 101 float second = first + 94 - 44 - 25; //Gain == 11 board gain ==25
kentwong 10:a9db57e6456e 102
kentwong 10:a9db57e6456e 103
kentwong 8:cca99ce7c5bc 104 //time_t seconds = time(NULL);
chapman515 0:b1b7113a1441 105 {
chapman515 0:b1b7113a1441 106 int sensor_addr = 41 << 1;
chapman515 0:b1b7113a1441 107 char id_regval[1] = {146};
chapman515 0:b1b7113a1441 108 char data[1] = {0};
chapman515 0:b1b7113a1441 109 i2c.write(sensor_addr,id_regval,1, true);
chapman515 0:b1b7113a1441 110 i2c.read(sensor_addr,data,1,false);
chapman515 0:b1b7113a1441 111
chapman515 0:b1b7113a1441 112
chapman515 0:b1b7113a1441 113 // Initialize color sensor
chapman515 0:b1b7113a1441 114 char timing_register[2] = {129,192};
chapman515 0:b1b7113a1441 115 //char timing_register[2] = {129,0};
chapman515 0:b1b7113a1441 116 i2c.write(sensor_addr,timing_register,2,false);
chapman515 0:b1b7113a1441 117 char control_register[2] = {143,0};
chapman515 0:b1b7113a1441 118 char temp[2]={0,0};
chapman515 0:b1b7113a1441 119 //char control_register[2] = {143,3};
chapman515 0:b1b7113a1441 120 i2c.write(sensor_addr,control_register,2,false);
chapman515 0:b1b7113a1441 121
chapman515 0:b1b7113a1441 122 char enable_register[2] = {128,3};
chapman515 0:b1b7113a1441 123 i2c.write(sensor_addr,enable_register,2,false);
chapman515 0:b1b7113a1441 124
chapman515 0:b1b7113a1441 125 // Read data from color sensor (Clear/Red/Green/Blue)
chapman515 0:b1b7113a1441 126
chapman515 0:b1b7113a1441 127 char clear_reg[1] = {148};
chapman515 0:b1b7113a1441 128 char clear_data[2] = {0,0};
chapman515 0:b1b7113a1441 129 i2c.write(sensor_addr,clear_reg,1, true);
chapman515 0:b1b7113a1441 130 i2c.read(sensor_addr,clear_data,2, false);
chapman515 0:b1b7113a1441 131
chapman515 0:b1b7113a1441 132 int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
chapman515 0:b1b7113a1441 133
chapman515 0:b1b7113a1441 134 char red_reg[1] = {150};
chapman515 0:b1b7113a1441 135 char red_data[2] = {0,0};
chapman515 0:b1b7113a1441 136 i2c.write(sensor_addr,red_reg,1, true);
chapman515 0:b1b7113a1441 137 i2c.read(sensor_addr,red_data,2, false);
chapman515 0:b1b7113a1441 138
chapman515 0:b1b7113a1441 139 int red_value = ((int)red_data[1] << 8) | red_data[0];
chapman515 0:b1b7113a1441 140
chapman515 0:b1b7113a1441 141 char green_reg[1] = {152};
chapman515 0:b1b7113a1441 142 char green_data[2] = {0,0};
chapman515 0:b1b7113a1441 143 i2c.write(sensor_addr,green_reg,1, true);
chapman515 0:b1b7113a1441 144 i2c.read(sensor_addr,green_data,2, false);
chapman515 0:b1b7113a1441 145
chapman515 0:b1b7113a1441 146 int green_value = ((int)green_data[1] << 8) | green_data[0];
chapman515 0:b1b7113a1441 147
chapman515 0:b1b7113a1441 148 char blue_reg[1] = {154};
chapman515 0:b1b7113a1441 149 char blue_data[2] = {0,0};
chapman515 0:b1b7113a1441 150 i2c.write(sensor_addr,blue_reg,1, true);
chapman515 0:b1b7113a1441 151 i2c.read(sensor_addr,blue_data,2, false);
chapman515 0:b1b7113a1441 152
chapman515 0:b1b7113a1441 153 int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
chapman515 0:b1b7113a1441 154
chapman515 0:b1b7113a1441 155 // print sensor readings
chapman515 0:b1b7113a1441 156 if (allfilled == true){
chapman515 0:b1b7113a1441 157 RGBavg.C = RGBavg.C*10-RGBdata[counter].C;
chapman515 0:b1b7113a1441 158 RGBavg.R = RGBavg.R*10-RGBdata[counter].R;
chapman515 0:b1b7113a1441 159 RGBavg.G = RGBavg.G*10-RGBdata[counter].G;
chapman515 0:b1b7113a1441 160 RGBavg.B = RGBavg.B*10-RGBdata[counter].B;
chapman515 0:b1b7113a1441 161 }
chapman515 0:b1b7113a1441 162 RGBdata[counter].C= clear_value;
chapman515 0:b1b7113a1441 163 RGBdata[counter].R= red_value;
chapman515 0:b1b7113a1441 164 RGBdata[counter].G= green_value;
chapman515 0:b1b7113a1441 165 RGBdata[counter].B= blue_value;
chapman515 0:b1b7113a1441 166 //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
chapman515 0:b1b7113a1441 167 }
chapman515 0:b1b7113a1441 168 {
chapman515 0:b1b7113a1441 169
chapman515 0:b1b7113a1441 170 MAX44009::MAX44009_status_t aux;
chapman515 0:b1b7113a1441 171 MAX44009::MAX44009_vector_data_t myMAX44009_Data;
chapman515 0:b1b7113a1441 172
chapman515 0:b1b7113a1441 173 aux = myMAX44009.MAX44009_Configuration ( MAX44009::CONFIGURATION_CONT_DEFAULT_MODE, MAX44009::CONFIGURATION_MANUAL_DEFAULT_MODE, MAX44009::CONFIGURATION_CDR_CURRENT_NOT_DIVIDED, MAX44009::CONFIGURATION_TIM_800_MS );
chapman515 0:b1b7113a1441 174 aux = myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data );
chapman515 0:b1b7113a1441 175 aux = myMAX44009.MAX44009_GetCurrentDivisionRatio ( &myMAX44009_Data );
chapman515 0:b1b7113a1441 176 aux = myMAX44009.MAX44009_GetIntegrationTime ( &myMAX44009_Data );
chapman515 0:b1b7113a1441 177
chapman515 0:b1b7113a1441 178 if (allfilled == true){
chapman515 0:b1b7113a1441 179 luxavg = luxavg*10-luxdata[counter];
chapman515 0:b1b7113a1441 180 }
chapman515 0:b1b7113a1441 181
chapman515 0:b1b7113a1441 182 luxdata[counter] = myMAX44009_Data.lux;
chapman515 0:b1b7113a1441 183
chapman515 0:b1b7113a1441 184
chapman515 0:b1b7113a1441 185 }
chapman515 0:b1b7113a1441 186
chapman515 0:b1b7113a1441 187 {
chapman515 0:b1b7113a1441 188
chapman515 0:b1b7113a1441 189 //float t = sht31.readTemperature();
chapman515 0:b1b7113a1441 190 //float h = sht31.readHumidity();
chapman515 0:b1b7113a1441 191
chapman515 0:b1b7113a1441 192 if (allfilled == true){
chapman515 0:b1b7113a1441 193 THavg.t = THavg.t*10-THdata[counter].t;
chapman515 0:b1b7113a1441 194 THavg.h = THavg.h*10-THdata[counter].h;
chapman515 0:b1b7113a1441 195 }
chapman515 0:b1b7113a1441 196 THdata[counter].t = sht31.readTemperature();
chapman515 0:b1b7113a1441 197 THdata[counter].h = sht31.readHumidity();
chapman515 0:b1b7113a1441 198
chapman515 0:b1b7113a1441 199 //pc.printf("[TEMP/HUM]");
chapman515 0:b1b7113a1441 200
chapman515 0:b1b7113a1441 201 }
chapman515 0:b1b7113a1441 202 ////////////////////////////////////////////////////////////////////
chapman515 0:b1b7113a1441 203
chapman515 0:b1b7113a1441 204 /*BLE_Can_receive = 0;
chapman515 0:b1b7113a1441 205 wait_ms(5);
chapman515 0:b1b7113a1441 206 pc.printf("counter119\n = %d", counter);
chapman515 0:b1b7113a1441 207 for (int i =0;i<10;i++){
chapman515 0:b1b7113a1441 208 pc.printf("i=%d ,", i);
chapman515 0:b1b7113a1441 209 pc.printf("%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n", RGBdata[i].C, RGBdata[i].R, RGBdata[i].G, RGBdata[i].B,THdata[i].t, THdata[i].h,luxdata[i]);
chapman515 0:b1b7113a1441 210 }
chapman515 0:b1b7113a1441 211 BLE_Can_receive = 1;*/
chapman515 0:b1b7113a1441 212
chapman515 0:b1b7113a1441 213 if (allfilled == true){
chapman515 0:b1b7113a1441 214 RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0);
chapman515 0:b1b7113a1441 215 RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0);
chapman515 0:b1b7113a1441 216 RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0);
chapman515 0:b1b7113a1441 217 RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0);
chapman515 0:b1b7113a1441 218
chapman515 0:b1b7113a1441 219 THavg.t = (THavg.t+THdata[counter].t)/10;
chapman515 0:b1b7113a1441 220 THavg.h = (THavg.h+THdata[counter].h)/10;
chapman515 0:b1b7113a1441 221
chapman515 0:b1b7113a1441 222 luxavg = (luxavg+luxdata[counter])/10;
chapman515 0:b1b7113a1441 223 }
chapman515 0:b1b7113a1441 224 else{
chapman515 0:b1b7113a1441 225
chapman515 0:b1b7113a1441 226 RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C) /(float)(counter+1);
chapman515 0:b1b7113a1441 227 RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R) /(float)(counter+1);
chapman515 0:b1b7113a1441 228 RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G) /(float)(counter+1);
chapman515 0:b1b7113a1441 229 RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B) /(float)(counter+1);
chapman515 0:b1b7113a1441 230
chapman515 0:b1b7113a1441 231 THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1);
chapman515 0:b1b7113a1441 232 THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1);
kentwong 6:94a26631ec03 233
chapman515 0:b1b7113a1441 234 luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1);
chapman515 0:b1b7113a1441 235 }
chapman515 0:b1b7113a1441 236
kentwong 11:3a63d6f65ff5 237
kentwong 10:a9db57e6456e 238 BLE_Can_receive = 0;
kentwong 6:94a26631ec03 239 counter++;
kentwong 12:5a140bcc456f 240 if (counter == 10 &&_30Scounter==0){
kentwong 10:a9db57e6456e 241 _30Scounter=0;
kentwong 11:3a63d6f65ff5 242 // pc.printf("%u,%u\n",(unsigned short)(luxavg*1000),(unsigned short)(second*1000));
kentwong 11:3a63d6f65ff5 243 sleepData[sleepDataPos].timestamp=(int)seconds;
kentwong 11:3a63d6f65ff5 244 sleepData[sleepDataPos].light=(unsigned short)(luxavg*1000);
kentwong 11:3a63d6f65ff5 245 sleepData[sleepDataPos].noise=(unsigned short)(second*1000);
kentwong 11:3a63d6f65ff5 246 sleepDataPos++;
kentwong 10:a9db57e6456e 247 allfilled = true;
kentwong 10:a9db57e6456e 248 }
kentwong 10:a9db57e6456e 249 else
kentwong 8:cca99ce7c5bc 250 if (counter==10){
kentwong 10:a9db57e6456e 251 _30Scounter++;
kentwong 7:e4984029012e 252 allfilled = true;
kentwong 7:e4984029012e 253 }
kentwong 10:a9db57e6456e 254 BLE_Can_receive = 1;
kentwong 6:94a26631ec03 255 if (wrote){
kentwong 10:a9db57e6456e 256 if (signal[0]=='s'){
kentwong 12:5a140bcc456f 257 for (int i=0;i <=sleepDataPos;i++){
kentwong 12:5a140bcc456f 258 if (i==sleepDataPos){
kentwong 12:5a140bcc456f 259 wait_ms(500);
kentwong 12:5a140bcc456f 260 pc.printf("\"send\"");
kentwong 12:5a140bcc456f 261 }
kentwong 12:5a140bcc456f 262 else {
kentwong 12:5a140bcc456f 263 float tempL=sleepData[i].light/1000.0f;
kentwong 12:5a140bcc456f 264 float tempN=sleepData[i].noise/1000.0f;
kentwong 12:5a140bcc456f 265 pc.printf("[\"%d\",\"%f\",\"%f\"],",sleepData[i].timestamp,tempL,tempN);
kentwong 12:5a140bcc456f 266 }
kentwong 11:3a63d6f65ff5 267 }
kentwong 12:5a140bcc456f 268 sleepDataPos=0;
kentwong 7:e4984029012e 269 wrote=false;
kentwong 7:e4984029012e 270 }
kentwong 10:a9db57e6456e 271 else
kentwong 8:cca99ce7c5bc 272 {
kentwong 7:e4984029012e 273 sendCounter++;
kentwong 10:a9db57e6456e 274 pc.printf("%d,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f,%f\n",sendCounter,(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg,second);
kentwong 7:e4984029012e 275 if (sendCounter == 10){ //10 = all element is filled {
kentwong 7:e4984029012e 276 if (signal[0]=='i'){
kentwong 12:5a140bcc456f 277 wait_ms(500);
kentwong 7:e4984029012e 278 pc.printf("end");
kentwong 7:e4984029012e 279 }
kentwong 7:e4984029012e 280 else if (signal[0]=='a')
kentwong 7:e4984029012e 281 {
kentwong 12:5a140bcc456f 282 wait_ms(500);
kentwong 7:e4984029012e 283 pc.printf("aend");
kentwong 7:e4984029012e 284 }
kentwong 7:e4984029012e 285 sendCounter=0;
kentwong 7:e4984029012e 286 wrote=false;
kentwong 7:e4984029012e 287 }
kentwong 6:94a26631ec03 288 }
kentwong 7:e4984029012e 289
kentwong 6:94a26631ec03 290 }
kentwong 6:94a26631ec03 291
kentwong 6:94a26631ec03 292
chapman515 0:b1b7113a1441 293 }
chapman515 0:b1b7113a1441 294 }