#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String baseUrl = "https://zeedzero-hardware.herokuapp.com/";
const String userId = "6410546181";
#define led 5
#define ldr 34
#define button 27
void Connect_Wifi();
void GET_time(){
DynamicJsonDocument doc(2048);
HTTPClient http;
http.begin(baseUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
deserializeJson(doc,payload);
Serial.println();
Serial.println((const char*)doc["time"]);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void POST_userId(){
const String url = baseUrl + "users/" + userId;
HTTPClient http;
http.begin(url);
int httpResponseCode = http.POST(url);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void PUT_update(int led_v,int button_v,int ldr_v){
const String url = baseUrl + "users/" + userId + "/update";
String json;
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type","application/json");
DynamicJsonDocument doc(2048);
doc["led"] = led_v;
doc["button"] = button_v;
doc["ldr"] = ldr_v;
serializeJson(doc,json);
Serial.println(json);
int httpResponseCode = http.PUT(json);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void GET_userId(){
const String url = baseUrl + "users/" + userId;
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void setup() {
Serial.begin(115200);
pinMode(led,OUTPUT);
pinMode(ldr,INPUT);
pinMode(button,INPUT_PULLUP);
digitalWrite(led,HIGH);
Connect_Wifi();
delay(200);
POST_userId();
GET_time();
PUT_update(!digitalRead(led),digitalRead(button),analogRead(ldr));
GET_userId();
}
void loop() {
PUT_update(!digitalRead(led),digitalRead(button),analogRead(ldr));
digitalWrite(led,!digitalRead(led));
delay(1000);
/*
digitalWrite(led,!digitalRead(led));
Serial.print(analogRead(ldr));
Serial.print(" ");
Serial.println(digitalRead(button));
delay(2000);
*/
}
void Connect_Wifi(){
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
Serial.print("Fetching " + baseUrl + "... ");
Serial.println();
}