diff --git a/src/main_2/main_2.ino b/src/main_2/main_2.ino index 8100dce194d7424802b7a5c99eb2e4f746b17603..514ca9d75e52d6880b0478d0c1bd1c39531b8f20 100644 --- a/src/main_2/main_2.ino +++ b/src/main_2/main_2.ino @@ -1,7 +1,9 @@ +#include <Bridge.h> #include <HttpClient.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <ArduinoJson.h> +#include <TimeLib.h> const int ledPin = 2; const int buttonPin = 4; @@ -44,15 +46,21 @@ const byte digit_pattern[10] = unsigned int counter = 0; bool isSuccess; -int year; +int years; int humidity; int light; char json[] = "{\"success\":true,\"year\":3,\"humidity\":5,\"light\":10}"; + +#define TIME_MSG_LEN 11 // time sync to PC is HEADER and unix time_t as ten ascii digits +#define TIME_HEADER 255 // Header tag for serial time sync message + void setup() { // put your setup code here, to run once: Serial.begin(9600); + InitializeClient(); + Connect("https://tranquil-citadel-63668.herokuapp.com/api/status"); pinMode(buttonPin, INPUT); @@ -82,7 +90,8 @@ void loop() { digit_base = 10; // get the value to be displayed and update one digit - update_one_digit(counter % digit_base); + //update_one_digit(counter % digit_base); + update_one_digit(GetCurrentTime()); lcd.setCursor(0,0); lcd.autoscroll(); @@ -165,7 +174,7 @@ void ReadJson() { return; } isSuccess = jsonReader["success"]; - year = jsonReader["year"]; + years = jsonReader["year"]; humidity = jsonReader["humidity"]; light = jsonReader["light"]; @@ -176,7 +185,7 @@ void ReadJson() { char* GenerateJSONString() { char buff[12]; char* totalString, yearString, humidityString, lightString; - yearString = itoa(year,buff,10); + yearString = itoa(years,buff,10); humidityString = itoa(humidity,buff,10); lightString = itoa(light,buff,10); @@ -184,3 +193,51 @@ char* GenerateJSONString() { return (totalString); } +int GetCurrentTime() { + if (Serial.available()) { + GetPCTime(); + } + if(timeStatus() != timeNotSet) { + return(year()); + } else { + return (0); + } +} + + +void GetPCTime() { + // if time available from serial port, sync the DateTime library + while(Serial.available() >= TIME_MSG_LEN ){ // time message + if( Serial.read() == TIME_HEADER ) { + time_t pctime = 0; + for(int i=0; i < TIME_MSG_LEN -1; i++){ + char charRead= Serial.read(); + if( charRead >= '0' && charRead <= '9') { + pctime = (10 * pctime) + (charRead - '0') ; // convert digits to a number + } + } + setTime(pctime); + } + } +} + +void InitializeClient() { + pinMode(13, OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); + //Serial.begin(9600); + //while(!Serial); +} + +void Connect(const char* url) { + HttpClient client; + client.get(url); + + while (client.available()) { + const char* clientRead = client.read(); + Serial.print(clientRead); + } + Serial.flush(); + + //delay(5000); +}