Skip to content
Snippets Groups Projects
Commit 118cf0e0 authored by 13515029 - Finiko Kasula Novenda's avatar 13515029 - Finiko Kasula Novenda
Browse files

Add Connect Client and Time on main 2

parent ae2ee90b
No related merge requests found
#include <Bridge.h>
#include <HttpClient.h> #include <HttpClient.h>
#include <Wire.h> #include <Wire.h>
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <TimeLib.h>
const int ledPin = 2; const int ledPin = 2;
const int buttonPin = 4; const int buttonPin = 4;
...@@ -44,15 +46,21 @@ const byte digit_pattern[10] = ...@@ -44,15 +46,21 @@ const byte digit_pattern[10] =
unsigned int counter = 0; unsigned int counter = 0;
bool isSuccess; bool isSuccess;
int year; int years;
int humidity; int humidity;
int light; int light;
char json[] = "{\"success\":true,\"year\":3,\"humidity\":5,\"light\":10}"; 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() { void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
Serial.begin(9600); Serial.begin(9600);
InitializeClient();
Connect("https://tranquil-citadel-63668.herokuapp.com/api/status");
pinMode(buttonPin, INPUT); pinMode(buttonPin, INPUT);
...@@ -82,7 +90,8 @@ void loop() { ...@@ -82,7 +90,8 @@ void loop() {
digit_base = 10; digit_base = 10;
// get the value to be displayed and update one digit // 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.setCursor(0,0);
lcd.autoscroll(); lcd.autoscroll();
...@@ -165,7 +174,7 @@ void ReadJson() { ...@@ -165,7 +174,7 @@ void ReadJson() {
return; return;
} }
isSuccess = jsonReader["success"]; isSuccess = jsonReader["success"];
year = jsonReader["year"]; years = jsonReader["year"];
humidity = jsonReader["humidity"]; humidity = jsonReader["humidity"];
light = jsonReader["light"]; light = jsonReader["light"];
...@@ -176,7 +185,7 @@ void ReadJson() { ...@@ -176,7 +185,7 @@ void ReadJson() {
char* GenerateJSONString() { char* GenerateJSONString() {
char buff[12]; char buff[12];
char* totalString, yearString, humidityString, lightString; char* totalString, yearString, humidityString, lightString;
yearString = itoa(year,buff,10); yearString = itoa(years,buff,10);
humidityString = itoa(humidity,buff,10); humidityString = itoa(humidity,buff,10);
lightString = itoa(light,buff,10); lightString = itoa(light,buff,10);
...@@ -184,3 +193,51 @@ char* GenerateJSONString() { ...@@ -184,3 +193,51 @@ char* GenerateJSONString() {
return (totalString); 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);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment