F

Dependencies:   mbed Sht31 MAX44009

Committer:
chapman515
Date:
Fri Apr 24 13:33:28 2020 +0000
Revision:
9:7c53c49afa40
Parent:
8:cca99ce7c5bc
Child:
10:a9db57e6456e
FYP

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