#include "WiFi.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>
#include <DHTesp.h> //DHT sensor library for ESPx
static char *ssid = "Wokwi-GUEST";
static char *pass = "";
#define LED1 15
#define LED2 2
#define SW1 12
#define SW2 13
#define SPK 32
//LDR Photoresistor Sensor Module (T88)
#define AnalogIN 33
#define GAMMA 0.7
#define RL10 50
//Temperature and Humidity Sensor Module (DHT)
#define DHT_PIN 25
DHTesp dhtSensor;
Servo Myservo1, Myservo2, Myservo3; // https://wokwi.com/projects/398557335194012673
const byte ServoPin1 = 19;
const byte ServoPin2 = 18;
const byte ServoPin3 = 5;
byte GO = 0;
String MessageTMP = "";
void Function1() {
digitalWrite(LED1, 1); GO = 2;
}
void Function2() {
digitalWrite(LED1, 1); GO = 1;
}
void Beep(int F,int L) {
for (int i = 0; i <= L; i++) {
digitalWrite(SPK, 1); delayMicroseconds(F);
digitalWrite(SPK, 0); delayMicroseconds(F);
}
}
void setup() {
Serial.begin(115200);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(SPK, OUTPUT);
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
attachInterrupt(SW1, Function1, FALLING);
attachInterrupt(SW2, Function2, FALLING);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Myservo1.attach(ServoPin1);
Myservo2.attach(ServoPin2);
Myservo3.attach(ServoPin3);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi .");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.'); delay(100);
}
Serial.println(WiFi.localIP());
}
// https://docs.google.com/spreadsheets/d/1rxxUN-9b81o5--xSpeghYOKFeKWsULGeGBvPBdl-2yk
// https://x.thunkable.com/copy/70a66df71fc73b67b59e7931f85858e2
void loop() {
float voltage = analogRead(AnalogIN) / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
//Serial.println("illuminance " + String(lux, 2) + " lux");
TempAndHumidity DHT = dhtSensor.getTempAndHumidity();
//Serial.println("Temp: " + String(DHT.temperature, 2) + "Ā°C");
//Serial.println("Humidity: " + String(DHT.humidity, 1) + "%");
String URL = "https://script.google.com/macros/s/AKfycbxHPcLeIX5eOydXdVy1h2RAErkzojNW4_Cc3lgoHZUE7MMvEb_WEbJAFpqm19tPt1uRdg/exec";
URL = URL + "?i1=" + String(lux, 2) + "&i2=" + String(DHT.temperature, 2);
if (GO == 1) {
URL = URL + "&i4=0"; Beep(800, 5);
}
else if (GO == 2) {
URL = URL + "&i4=1"; Beep(500, 5);
}
GO = 0;
HTTPClient http;
http.begin(URL);
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
if (httpCode == 200) {
String Message = http.getString();
if (!MessageTMP.equals(Message)){
MessageTMP = Message;
StaticJsonDocument<200> JP;
deserializeJson(JP, Message);
int j1 = JP["o1"];
int j2 = JP["o2"];
int j3 = JP["o3"];
int j4 = JP["o4"];
Myservo1.write(map(j1,0.1,100881.09,0,180));
Myservo2.write(map(j2,-40,80,0,180));
Myservo3.write(j3);
if (j4 >= 1) {
digitalWrite(LED2, 1);
}
else {
digitalWrite(LED2, 0);
}
Beep(1000, 4);
}
digitalWrite(LED1, 0);
} else {
Serial.println("HTTP ERROR " + String(httpCode));
Beep(2000, 10); Beep(300, 50);
delay(1000);
}
}
SW1.ON
SW2.OFF
š
Wait
Light
Temp.
Level
ā ā ā ā ā ā ā
ā ā .ā ā ā ā ā ā ā ā .
āā ā ā ā ā ā ā ā ā ā ā ā