#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define LDR_PIN 2
#define TEMP_PIN 34
class SensorData {
public:
inline void setTemp(float temp) { this->temp = temp; }
inline void setDark(bool dark) { this->dark = dark; }
private:
bool dark;
float temp; //celsius
};
void initWifi() {
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.print("Connected: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWifi();
pinMode(LDR_PIN, INPUT);
}
void readLdr(SensorData &sd) {
if (digitalRead(LDR_PIN) == LOW) {
sd.setDark(false);
//Serial.println("Light");
} else {
sd.setDark(true);
//Serial.println("Dark");
}
}
void readTemp(SensorData &sd) {
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const unsigned long ADC_MAX = (1 << 12) - 1;
int analogValue = analogRead(TEMP_PIN);
if(analogValue == 0 || analogValue >= ADC_MAX) {
Serial.println("Invalid reading");
return;
}
float celsius = 1 / (log(1 / (1. * ADC_MAX / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.println(celsius);
}
void sendServerReq() {
}
static int cnt = 0;
void loop() {
SensorData sd;
readLdr(sd);
delay(10);
readTemp(sd);
delay(100);
if (cnt == 10) {
//Serial.println("LOL!");
cnt = 0;
}
cnt++;
}