STM32L476
Dependencies: MbedJSONValue SDFileSystem WConstants mbed-dev DS1820 TinyGPSPlus epd1in54
Fork of A_SSL_Main by
Diff: main.cpp
- Revision:
- 11:9b49bd5a5f8c
- Parent:
- 10:a4526c9b8332
- Child:
- 13:0477a745dad2
diff -r a4526c9b8332 -r 9b49bd5a5f8c main.cpp --- a/main.cpp Fri Nov 09 03:12:35 2018 +0000 +++ b/main.cpp Tue Nov 27 13:57:52 2018 +0000 @@ -19,19 +19,22 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////// // Definitions and initialization //////////////////////////////////////////////////////////////////////////////////////////////////////// -#define TX5 PC_10 -#define RX5 PC_11 +#define TX5 PC_12 +#define RX5 PD_2 #define GPSBaud 9600 #define SerialBaud 115200 #define SD_FILE_SYSTEM_H SDFileSystem* fs; FILE* fp; TinyGPSPlus tgps; +Serial serial(USBTX, USBRX,115200); //Local terminal Baud rate +SDFileSystem sd(PB_5, PB_4, PB_3, PB_12, "sd"); //uSD SPI -Serial serial(USBTX, USBRX,112500); //Local terminal Baud rate -DS1820 ds1820(PB_2); //pin name connected to the DS1820 data pin -SDFileSystem sd(PB_5, PB_4, PB_3, PB_12, "sd"); //uSD SPI - +//Temp sensors +#define MAX_PROBES 16 +#define DATA_PIN PB_2 +DS1820* probe[MAX_PROBES]; + //E-ink Display PinName rst; PinName dc; PinName busy; PinName mosi; PinName miso; PinName sclk; PinName cs; unsigned char frame_black[EPD_HEIGHT*EPD_WIDTH/8]; @@ -41,14 +44,23 @@ int deviceID = 1; double longTest,latTest; int tyear,tmonth,tday,thour,tmin,tsec; -float liquidTemp = 0; +double liquidTemp = 0; +double ambientTemp = 0; char cValt[32]; char chr_s[600]; std::string s; const char * parameter; +char * readLastGeo; int msg_index; double geofencesNum; int geotest; +char geo_extract[6]; + +DigitalOut en1(PC_5); + DigitalOut en2(PC_6); + DigitalOut en3(PC_8); + DigitalOut en4(PC_9); + //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -123,18 +135,20 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////// // Temperature sensor read //////////////////////////////////////////////////////////////////////////////////////////////////////// -float getTemp() -{ - float t; - // getTemp(tRead); - if(ds1820.begin()) { - ds1820.startConversion(); - wait(1.0); - t = ds1820.read(); // read temperature - serial.printf("temp = %3.2f\r\n", t); // Display temp on terminal - } else - serial.printf("No DS1820 sensor found!\r\n"); - return t; +double getTemp(int device) { + // Initialize the probe array to DS1820 objects + int num_devices = 0; + double temp; + while(DS1820::unassignedProbe(DATA_PIN)) { + probe[num_devices] = new DS1820(DATA_PIN); + num_devices++; + if (num_devices == MAX_PROBES) + break; + } + probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready + temp = probe[device]->temperature(); + wait(0.01); + return temp; } //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -142,7 +156,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////// // Epd display //////////////////////////////////////////////////////////////////////////////////////////////////////// -int Display(float t, int g) +int Display(double t, int g) { Epd epd = Epd(PB_5, PB_4, PB_3, PA_8, PC_4, PC_7, PB_10); @@ -188,21 +202,26 @@ } } -int read_file(char* adr_file)//fct to read a file +char* read_file(char* adr_file)//fct to read a file { - char c; FILE *file; serial.printf("\r\nRead: %s\r\n", adr_file); file = fopen(adr_file, "r"); - if (file) { - while (!feof(file)) { - c = getc(file); - serial.putc(c); - } - fclose(file); - return 0; //success + + if(!file) { + error("Could not open file!\n"); + //return -1; } - return -1; + + char buffer[600]; + while(fgets(buffer, 600, file)) { + wait(1); + } + printf("Line: %s\n", buffer); + fclose(file); + + return buffer; //success + } int add_data(char* adr_flie, char* msg)//fct to update a file @@ -226,7 +245,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////// //ToDo: pass arguments, currently some dummy values //geoFenceNum: the geofence entered or left, value: 1(entered), 2(left) -string jsonSerializeDeviceToSystem(int deviceID, float longTest, float latTest, int geoFenceNum, int value, float liquidTemp, int tday, int tmonth, int tyear, int thour, int tmin, int tsec) +string jsonSerializeDeviceToSystem(int deviceID, double longTest, double latTest, int geoFenceNum, int value, double liquidTemp, double ambientTemp, int tday, int tmonth, int tyear, int thour, int tmin, int tsec) { MbedJSONValue statusReport; std::string s; @@ -241,6 +260,7 @@ statusReport["geoFenceNum"] = geoFenceNum; statusReport["geoFenceEnteryDeparture"] = value; statusReport["container"] = liquidTemp; + statusReport["Ambient"] = ambientTemp; statusReport["heater"] = 34.5; statusReport["batteryVoltage"] = 3.67; statusReport["network"] = "Tele2"; @@ -272,6 +292,7 @@ int main() { + en1=1;en2=1;en3=1;en4=1; Serial GPSSerial(TX5, RX5); GPSSerial.baud(GPSBaud); wait(0.001); @@ -280,29 +301,35 @@ //ToDo: To be replaced by the value received when subscribed to the Iot hub //Subscribe and save the message in system_message - system_message = "{\"config\":[1.0,1.0,240.0,5.0],\"route1\":[1.0,0.0,1.0,1.0,1.0,1.0,54.5983852,-1.5708491,54.5969549,-1.5663735],\"route2\":[2.0,0.0,1.0,1.0,1.0,1.0,54.6542957,-1.4459836,54.6495902,-1.4430425],\"route3\":[3.0,0.0,1.0,1.0,1.0,1.0,54.7051416,-1.5638412,54.7101814,-1.5615844],\"route4\":[4.0,0.0,1.0,1.0,1.0,1.0,54.6298560,-1.3059736,54.6267899,-1.3075833],\"route5\":[5.0,0.0,1.0,1.0,1.0,2.0,5.0,54.6710093,-1.4587418,54.6730758,-1.4461951,54.6672642,-1.4436423,54.6678548,-1.4562232,54.6710093,-1.4587418]}"; + system_message = "{\"config\":[1.0,1.0,240.0,5.0],\"route1\":[1.0,0.0,1.0,1.0,1.0,1.0,54.5983852,-1.5708491,54.5969549,-1.5663735],\"route2\":[2.0,0.0,1.0,1.0,1.0,1.0,54.6542957,-1.4459836,54.6495902,-1.4430425],\"route3\":[3.0,0.0,1.0,1.0,1.0,1.0,54.7051416,-1.5638412,54.7101814,-1.5615844],\"route4\":[4.0,0.0,1.0,1.0,1.0,1.0,54.6298560,-1.3059736,54.6267899,-1.3075833],\"route5\":[5.0,1.0,1.0,1.0,1.0,2.0,5.0,54.6710093,-1.4587418,54.6730758,-1.4461951,54.6672642,-1.4436423,54.6678548,-1.4562232,54.6710093,-1.4587418]}"; - - //Variables wait(1); serial.printf("Silent Hub v1.0 ........................\r\n"); serial.printf("SystemCoreClock is %d Hz...........\r\n", SystemCoreClock); wait (1); + int sleepCounter = 0; + //new_file("/sd/HeatingStationLog.txt"); + while(1) { - + sleepCounter++; + + if(tgps.encode(GPSSerial.getc())) { + tgps.encode(GPSSerial.getc()); latTest = tgps.location.lat(); longTest = tgps.location.lng(); //Location tyear = tgps.date.year(); tmonth = tgps.date.month(); tday = tgps.date.day(); //Date thour = tgps.time.hour(); tmin = tgps.time.minute(); tsec = tgps.time.second(); //Time - + //this part is just for local terminal monitoring serial.printf("\r\nLocation: %3.6f, %3.6f, Date: %d-%d-%d, Time: %d:%d:%d \r\n", latTest, longTest, tday, tmonth, tyear, thour, tmin, tsec); - - liquidTemp = getTemp(); + ambientTemp = getTemp(0); + printf("Ambient Temperature %3.1foC\r\n", ambientTemp); + liquidTemp = getTemp(1); + printf("liquid Temperature %3.1foC\r\n", liquidTemp); Display(liquidTemp, geotest); wait(5); //little delay to prevent double writing @@ -316,11 +343,13 @@ int count=1; int count2; double geo_lat_c; double geo_long_c; double geo_lat_e; double geo_long_e; + while ((count <= geofencesNum)&&(geotest == 0)) //Can also check latTest,longTest: to make sure we have a fix //while ((count <= geofencesNum)&&(geotest == 0)&& ((latTest != 0)||(longTest != 0))) { - char geo_extract[6]; + + sprintf(geo_extract,"route%d",count); parameter = geo_extract; msg_index = 5; @@ -354,11 +383,13 @@ serial.printf("\r\nGeofence number = %d: \r\nIn geofence = %d:\r\n", count, geotest); } count++; + } - if (geotest == 1) //ToDo &&make sure the last publish handshake was successful + count = count--; + if (geotest == 0) //ToDo &&make sure the last publish handshake was successful { //msg to be saved on SD and published, will overwrite last saved message - s = jsonSerializeDeviceToSystem(deviceID, longTest, latTest, count, geotest, liquidTemp, tday, tmonth, tyear, thour, tmin, tsec); + s = jsonSerializeDeviceToSystem(deviceID, longTest, latTest, count, geotest, liquidTemp, ambientTemp, tday, tmonth, tyear, thour, tmin, tsec); sprintf(chr_s,s.c_str()); Epd_EN = 1; new_file("/sd/DeviceToSystemLog.txt"); @@ -371,13 +402,37 @@ new_file("/sd/LastGeofence.txt"); add_data("/sd/LastGeofence.txt",cValGeoNum); read_file("/sd/LastGeofence.txt"); + serial.printf("\n\rEnd\n"); + wait(1); Epd_EN = 0; + + //To do: if timer >= x, sleep, then sysreset; + //NVIC_SystemReset(); + //If it is a heating station log temperature } - + new_file("/sd/HeatingStationLog.txt"); + readLastGeo = read_file("/sd/LastGeofence.txt"); + int intVal = atoi(readLastGeo); + serial.printf("\r\nlast Geo in int = %d:",intVal); + sprintf(geo_extract,"route%d",intVal); + if (jsonParseSystemToDevice(geo_extract,1) == 1.0); //Check if it is a heating station + { + char liq[32];char amb[32]; + //const char seperator = ","; + sprintf(liq,"%.2f", liquidTemp); + add_data("/sd/HeatingStationLog.txt",liq); + add_data("/sd/HeatingStationLog.txt",","); + sprintf(amb,"%.2f", ambientTemp); + add_data("/sd/HeatingStationLog.txt",amb); + add_data("/sd/HeatingStationLog.txt",","); + read_file("/sd/HeatingStationLog.txt"); + } + + } if (millis() > 5000 && tgps.charsProcessed() < 10)