a

Dependencies:   mbed Sht31 MAX44009

Committer:
kentwong
Date:
Fri Apr 24 04:10:14 2020 +0000
Revision:
8:cca99ce7c5bc
Parent:
7:e4984029012e
ver2

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 1:3889765eb394 12 DigitalOut Batt2SYS(p8);
chapman515 0:b1b7113a1441 13 DigitalOut BLE_Can_receive(p11); // 0 = can recieve
chapman515 0:b1b7113a1441 14
chapman515 0:b1b7113a1441 15 class RGB{
chapman515 0:b1b7113a1441 16 public : int C;
chapman515 0:b1b7113a1441 17 int R;
chapman515 0:b1b7113a1441 18 int G;
chapman515 0:b1b7113a1441 19 int B;
chapman515 0:b1b7113a1441 20 };
chapman515 0:b1b7113a1441 21 class RGBf{
chapman515 0:b1b7113a1441 22 public : float C;
chapman515 0:b1b7113a1441 23 float R;
chapman515 0:b1b7113a1441 24 float G;
chapman515 0:b1b7113a1441 25 float B;
chapman515 0:b1b7113a1441 26 };
chapman515 0:b1b7113a1441 27 class TandH{
chapman515 0:b1b7113a1441 28 public : float t;
chapman515 0:b1b7113a1441 29 float h;
chapman515 0:b1b7113a1441 30 };
chapman515 0:b1b7113a1441 31
chapman515 0:b1b7113a1441 32 RGB RGBdata[10];
chapman515 0:b1b7113a1441 33 TandH THdata[10];
chapman515 0:b1b7113a1441 34 float luxdata[10];
chapman515 0:b1b7113a1441 35
chapman515 0:b1b7113a1441 36 int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead;
kentwong 7:e4984029012e 37 int sendCounter=0; // for sending in auto/ instant measure
kentwong 7:e4984029012e 38 int _30Scounter=0;
chapman515 0:b1b7113a1441 39 bool allfilled = false;
chapman515 0:b1b7113a1441 40 RGBf RGBavg;
chapman515 0:b1b7113a1441 41 TandH THavg;
chapman515 0:b1b7113a1441 42 float luxavg;
chapman515 0:b1b7113a1441 43
kentwong 6:94a26631ec03 44 bool wrote=false;
chapman515 1:3889765eb394 45 DigitalOut led(LED1);
kentwong 6:94a26631ec03 46 char signal[1];
kentwong 8:cca99ce7c5bc 47 //LocalFileSystem local("local"); //Create the local filesystem under the name "local"
chapman515 1:3889765eb394 48
kentwong 6:94a26631ec03 49
chapman515 0:b1b7113a1441 50 int main()
kentwong 6:94a26631ec03 51 {
kentwong 8:cca99ce7c5bc 52 // set_time(1587699064);
kentwong 7:e4984029012e 53
kentwong 6:94a26631ec03 54 //pc.read
chapman515 0:b1b7113a1441 55 pc.baud ( 19200 );
chapman515 1:3889765eb394 56 Batt2SYS = 0;
kentwong 6:94a26631ec03 57
kentwong 7:e4984029012e 58
kentwong 6:94a26631ec03 59
kentwong 6:94a26631ec03 60 while(1) {
chapman515 0:b1b7113a1441 61 counter = counter%10;
kentwong 8:cca99ce7c5bc 62 //time_t seconds = time(NULL);
chapman515 0:b1b7113a1441 63 {
chapman515 0:b1b7113a1441 64 int sensor_addr = 41 << 1;
chapman515 0:b1b7113a1441 65 char id_regval[1] = {146};
chapman515 0:b1b7113a1441 66 char data[1] = {0};
chapman515 0:b1b7113a1441 67 i2c.write(sensor_addr,id_regval,1, true);
chapman515 0:b1b7113a1441 68 i2c.read(sensor_addr,data,1,false);
chapman515 0:b1b7113a1441 69
chapman515 0:b1b7113a1441 70
chapman515 0:b1b7113a1441 71 // Initialize color sensor
chapman515 0:b1b7113a1441 72 char timing_register[2] = {129,192};
chapman515 0:b1b7113a1441 73 //char timing_register[2] = {129,0};
chapman515 0:b1b7113a1441 74 i2c.write(sensor_addr,timing_register,2,false);
chapman515 0:b1b7113a1441 75 char control_register[2] = {143,0};
chapman515 0:b1b7113a1441 76 char temp[2]={0,0};
chapman515 0:b1b7113a1441 77 //char control_register[2] = {143,3};
chapman515 0:b1b7113a1441 78 i2c.write(sensor_addr,control_register,2,false);
chapman515 0:b1b7113a1441 79
chapman515 0:b1b7113a1441 80 char enable_register[2] = {128,3};
chapman515 0:b1b7113a1441 81 i2c.write(sensor_addr,enable_register,2,false);
chapman515 0:b1b7113a1441 82
chapman515 0:b1b7113a1441 83 // Read data from color sensor (Clear/Red/Green/Blue)
chapman515 0:b1b7113a1441 84
chapman515 0:b1b7113a1441 85 char clear_reg[1] = {148};
chapman515 0:b1b7113a1441 86 char clear_data[2] = {0,0};
chapman515 0:b1b7113a1441 87 i2c.write(sensor_addr,clear_reg,1, true);
chapman515 0:b1b7113a1441 88 i2c.read(sensor_addr,clear_data,2, false);
chapman515 0:b1b7113a1441 89
chapman515 0:b1b7113a1441 90 int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
chapman515 0:b1b7113a1441 91
chapman515 0:b1b7113a1441 92 char red_reg[1] = {150};
chapman515 0:b1b7113a1441 93 char red_data[2] = {0,0};
chapman515 0:b1b7113a1441 94 i2c.write(sensor_addr,red_reg,1, true);
chapman515 0:b1b7113a1441 95 i2c.read(sensor_addr,red_data,2, false);
chapman515 0:b1b7113a1441 96
chapman515 0:b1b7113a1441 97 int red_value = ((int)red_data[1] << 8) | red_data[0];
chapman515 0:b1b7113a1441 98
chapman515 0:b1b7113a1441 99 char green_reg[1] = {152};
chapman515 0:b1b7113a1441 100 char green_data[2] = {0,0};
chapman515 0:b1b7113a1441 101 i2c.write(sensor_addr,green_reg,1, true);
chapman515 0:b1b7113a1441 102 i2c.read(sensor_addr,green_data,2, false);
chapman515 0:b1b7113a1441 103
chapman515 0:b1b7113a1441 104 int green_value = ((int)green_data[1] << 8) | green_data[0];
chapman515 0:b1b7113a1441 105
chapman515 0:b1b7113a1441 106 char blue_reg[1] = {154};
chapman515 0:b1b7113a1441 107 char blue_data[2] = {0,0};
chapman515 0:b1b7113a1441 108 i2c.write(sensor_addr,blue_reg,1, true);
chapman515 0:b1b7113a1441 109 i2c.read(sensor_addr,blue_data,2, false);
chapman515 0:b1b7113a1441 110
chapman515 0:b1b7113a1441 111 int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
chapman515 0:b1b7113a1441 112
chapman515 0:b1b7113a1441 113 // print sensor readings
chapman515 0:b1b7113a1441 114 if (allfilled == true){
chapman515 0:b1b7113a1441 115 RGBavg.C = RGBavg.C*10-RGBdata[counter].C;
chapman515 0:b1b7113a1441 116 RGBavg.R = RGBavg.R*10-RGBdata[counter].R;
chapman515 0:b1b7113a1441 117 RGBavg.G = RGBavg.G*10-RGBdata[counter].G;
chapman515 0:b1b7113a1441 118 RGBavg.B = RGBavg.B*10-RGBdata[counter].B;
chapman515 0:b1b7113a1441 119 }
chapman515 0:b1b7113a1441 120 RGBdata[counter].C= clear_value;
chapman515 0:b1b7113a1441 121 RGBdata[counter].R= red_value;
chapman515 0:b1b7113a1441 122 RGBdata[counter].G= green_value;
chapman515 0:b1b7113a1441 123 RGBdata[counter].B= blue_value;
chapman515 0:b1b7113a1441 124 //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
chapman515 0:b1b7113a1441 125 }
chapman515 0:b1b7113a1441 126 {
chapman515 0:b1b7113a1441 127
chapman515 0:b1b7113a1441 128 MAX44009::MAX44009_status_t aux;
chapman515 0:b1b7113a1441 129 MAX44009::MAX44009_vector_data_t myMAX44009_Data;
chapman515 0:b1b7113a1441 130
chapman515 0:b1b7113a1441 131 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 132 aux = myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data );
chapman515 0:b1b7113a1441 133 aux = myMAX44009.MAX44009_GetCurrentDivisionRatio ( &myMAX44009_Data );
chapman515 0:b1b7113a1441 134 aux = myMAX44009.MAX44009_GetIntegrationTime ( &myMAX44009_Data );
chapman515 0:b1b7113a1441 135
chapman515 0:b1b7113a1441 136 if (allfilled == true){
chapman515 0:b1b7113a1441 137 luxavg = luxavg*10-luxdata[counter];
chapman515 0:b1b7113a1441 138 }
chapman515 0:b1b7113a1441 139
chapman515 0:b1b7113a1441 140 luxdata[counter] = myMAX44009_Data.lux;
chapman515 0:b1b7113a1441 141
chapman515 0:b1b7113a1441 142
chapman515 0:b1b7113a1441 143 }
chapman515 0:b1b7113a1441 144
chapman515 0:b1b7113a1441 145 {
chapman515 0:b1b7113a1441 146
chapman515 0:b1b7113a1441 147 //float t = sht31.readTemperature();
chapman515 0:b1b7113a1441 148 //float h = sht31.readHumidity();
chapman515 0:b1b7113a1441 149
chapman515 0:b1b7113a1441 150 if (allfilled == true){
chapman515 0:b1b7113a1441 151 THavg.t = THavg.t*10-THdata[counter].t;
chapman515 0:b1b7113a1441 152 THavg.h = THavg.h*10-THdata[counter].h;
chapman515 0:b1b7113a1441 153 }
chapman515 0:b1b7113a1441 154 THdata[counter].t = sht31.readTemperature();
chapman515 0:b1b7113a1441 155 THdata[counter].h = sht31.readHumidity();
chapman515 0:b1b7113a1441 156
chapman515 0:b1b7113a1441 157 //pc.printf("[TEMP/HUM]");
chapman515 0:b1b7113a1441 158
chapman515 0:b1b7113a1441 159 }
chapman515 0:b1b7113a1441 160 ////////////////////////////////////////////////////////////////////
chapman515 0:b1b7113a1441 161
chapman515 0:b1b7113a1441 162 /*BLE_Can_receive = 0;
chapman515 0:b1b7113a1441 163 wait_ms(5);
chapman515 0:b1b7113a1441 164 pc.printf("counter119\n = %d", counter);
chapman515 0:b1b7113a1441 165 for (int i =0;i<10;i++){
chapman515 0:b1b7113a1441 166 pc.printf("i=%d ,", i);
chapman515 0:b1b7113a1441 167 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 168 }
chapman515 0:b1b7113a1441 169 BLE_Can_receive = 1;*/
chapman515 0:b1b7113a1441 170
chapman515 0:b1b7113a1441 171 if (allfilled == true){
chapman515 0:b1b7113a1441 172 RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0);
chapman515 0:b1b7113a1441 173 RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0);
chapman515 0:b1b7113a1441 174 RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0);
chapman515 0:b1b7113a1441 175 RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0);
chapman515 0:b1b7113a1441 176
chapman515 0:b1b7113a1441 177 THavg.t = (THavg.t+THdata[counter].t)/10;
chapman515 0:b1b7113a1441 178 THavg.h = (THavg.h+THdata[counter].h)/10;
chapman515 0:b1b7113a1441 179
chapman515 0:b1b7113a1441 180 luxavg = (luxavg+luxdata[counter])/10;
chapman515 0:b1b7113a1441 181 }
chapman515 0:b1b7113a1441 182 else{
chapman515 0:b1b7113a1441 183
chapman515 0:b1b7113a1441 184 RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C) /(float)(counter+1);
chapman515 0:b1b7113a1441 185 RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R) /(float)(counter+1);
chapman515 0:b1b7113a1441 186 RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G) /(float)(counter+1);
chapman515 0:b1b7113a1441 187 RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B) /(float)(counter+1);
chapman515 0:b1b7113a1441 188
chapman515 0:b1b7113a1441 189 THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1);
chapman515 0:b1b7113a1441 190 THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1);
kentwong 6:94a26631ec03 191
chapman515 0:b1b7113a1441 192 luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1);
chapman515 0:b1b7113a1441 193 }
chapman515 0:b1b7113a1441 194
kentwong 6:94a26631ec03 195 //pc.printf("test");
kentwong 6:94a26631ec03 196 // pc.printf("Shine Like:%s,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",signal,(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);
chapman515 0:b1b7113a1441 197 BLE_Can_receive = 0;
kentwong 6:94a26631ec03 198 counter++;
kentwong 8:cca99ce7c5bc 199 // if (counter == 10 &&_30Scounter==3){
kentwong 8:cca99ce7c5bc 200 //_30Scounter=0;
kentwong 8:cca99ce7c5bc 201 // FILE *fp = fopen("/local/out.txt", "a");
kentwong 8:cca99ce7c5bc 202 //fprintf(fp, "[%d,%d,%d,%d,%d,%d,%d,%0.001f],\n",seconds,(int)(RGBavg.C+0.5),
kentwong 8:cca99ce7c5bc 203 // (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),(int)THavg.t,(int) THavg.h,luxavg);
kentwong 8:cca99ce7c5bc 204 // fclose(fp);
kentwong 8:cca99ce7c5bc 205 // allfilled = true;
kentwong 8:cca99ce7c5bc 206 // }
kentwong 8:cca99ce7c5bc 207 //else
kentwong 8:cca99ce7c5bc 208 if (counter==10){
kentwong 8:cca99ce7c5bc 209 // _30Scounter++;
kentwong 7:e4984029012e 210 allfilled = true;
kentwong 7:e4984029012e 211 }
chapman515 0:b1b7113a1441 212 BLE_Can_receive = 1;
kentwong 6:94a26631ec03 213 if (wrote){
kentwong 8:cca99ce7c5bc 214 /* if (signal[0]=='s'){
kentwong 7:e4984029012e 215 FILE *fp = fopen("/local/out.txt", "r");
kentwong 7:e4984029012e 216 char str[45];
kentwong 7:e4984029012e 217 while(!feof(fp))
kentwong 7:e4984029012e 218 if(fgets(str,40,fp)!=NULL) {
kentwong 7:e4984029012e 219 pc.printf("%s\n",str);
kentwong 7:e4984029012e 220 //wait_ms(300);
kentwong 7:e4984029012e 221 }
kentwong 7:e4984029012e 222 fclose(fp);
kentwong 7:e4984029012e 223 wrote=false;
kentwong 7:e4984029012e 224 }
kentwong 8:cca99ce7c5bc 225 else */
kentwong 8:cca99ce7c5bc 226 {
kentwong 7:e4984029012e 227 sendCounter++;
kentwong 7:e4984029012e 228 pc.printf("%d,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\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);
kentwong 7:e4984029012e 229 if (sendCounter == 10){ //10 = all element is filled {
kentwong 7:e4984029012e 230 if (signal[0]=='i'){
kentwong 7:e4984029012e 231 pc.printf("end");
kentwong 7:e4984029012e 232 }
kentwong 7:e4984029012e 233 else if (signal[0]=='a')
kentwong 7:e4984029012e 234 {
kentwong 7:e4984029012e 235 pc.printf("aend");
kentwong 7:e4984029012e 236 }
kentwong 7:e4984029012e 237 sendCounter=0;
kentwong 7:e4984029012e 238 wrote=false;
kentwong 7:e4984029012e 239 }
kentwong 6:94a26631ec03 240 }
kentwong 7:e4984029012e 241
kentwong 6:94a26631ec03 242 }
kentwong 6:94a26631ec03 243 else{
kentwong 6:94a26631ec03 244 if(pc.readable() ) {
kentwong 6:94a26631ec03 245 led=!led;
kentwong 6:94a26631ec03 246 pc.scanf("%1s", signal);
kentwong 6:94a26631ec03 247 wrote=true;
kentwong 6:94a26631ec03 248 }
kentwong 6:94a26631ec03 249
kentwong 6:94a26631ec03 250 }
kentwong 6:94a26631ec03 251
kentwong 6:94a26631ec03 252
chapman515 0:b1b7113a1441 253 }
chapman515 0:b1b7113a1441 254 }