/*
Example from WiFi > WiFiScan
Complete details at https://RandomNerdTutorials.com/esp32-useful-wi-fi-functions-arduino/
*/
#include "WiFi.h"
#include <HTTPClient.h>
String url = "https://test-project-d6cd4-default-rtdb.firebaseio.com/Rimsha/led.json";
#define LED1 22
#define LED2 23
#define LED3 21
#define LED4 19
#define LED5 18
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiServer server(80);
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
WiFi.disconnect();
// delay(100);
initWiFi();
server.begin();
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop() {
HTTPClient http;
http.begin(url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
// digitalWrite(LED1, payload.toInt());
int val = payload.toInt();
if(val<=100){
Serial.println(payload);
digitalWrite(LED1, payload.toInt());
}
else{
digitalWrite(LED1, LOW);
}
if(val<=200 && val>=101){
Serial.println(payload);
digitalWrite(LED2, payload.toInt());
}
else{
digitalWrite(LED2, LOW);
}
if(val<=300 && val>=201){
Serial.println(payload);
digitalWrite(LED3, payload.toInt());
}
else{
digitalWrite(LED3, LOW);
}
if(val<=400 && val>=301){
Serial.println(payload);
digitalWrite(LED4, payload.toInt());
}
else{
digitalWrite(LED4, LOW);
}
if(val<=500 && val>=401){
Serial.println(payload);
digitalWrite(LED5, payload.toInt());
}
else{
digitalWrite(LED5, LOW);
}
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// if (httpResponseCode < 299) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// String payload = http.getString();
// Serial.println(payload);
// digitalWrite(LED2, payload.toInt());
// }
// else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// if (httpResponseCode < 399) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// String payload = http.getString();
// Serial.println(payload);
// digitalWrite(LED3, payload.toInt());
// }
// else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// if (httpResponseCode < 499) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// String payload = http.getString();
// Serial.println(payload);
// digitalWrite(LED4, payload.toInt());
// }
// else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// if (httpResponseCode < 599) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// String payload = http.getString();
// Serial.println(payload);
// digitalWrite(LED5, payload.toInt());
// }
// else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// Free resources
http.end();
}