#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define TOKEN "hhZqt27zIGhbfpyD35XQ"
const int LDR_PIN = 33; // LDR (Photoresistor) connected to Analog Pin A0
const int SWITCH_PIN = 14;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "demo.thingsboard.io";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastRead = 0;
unsigned long lastMsg = 0;
unsigned long lastThreshold = 0;
#define MSG_BUFFER_SIZE (50)
int lightLevel = 0;
int switch_state;
int threshold = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(const char* topic, byte* payload, unsigned int length){
char json[length + 1];
strncpy (json, (char*)payload, length);
json[length] = '\0';
Serial.println("Topic: ");
Serial.println(topic);
Serial.println("Message: ");
Serial.println(json);
StaticJsonDocument<200> jsonBuffer;
DeserializationError data = deserializeJson(jsonBuffer, json);
if (data){
Serial.println("parseObject() failed");
return;
}
threshold = jsonBuffer["params"]["Threshold"];
Serial.print("Nguong:"); Serial.println(threshold);
}
void reconnect(){
while (!client.connected()){
if(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
Serial.print("Connecting to ThingsBoard node ...");
if ( client.connect ("SensorIoT", TOKEN, NULL)){
Serial.println( "[DONE]" );
client.subscribe("v1/devices/me/rpc/request/+");
}
else {
Serial.print("[FAILED] [ rc = " );
Serial.print(client.state() );
Serial.println( " : retrying in 5 seconds]");
delay( 5000 );
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
pinMode(SWITCH_PIN, INPUT);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
unsigned long now = millis();
if (now - lastRead > 500) {
// Read the LDR value
lightLevel = analogRead(LDR_PIN);
int percent = map(lightLevel, 0, 1023, 0, 100);
Serial.print("Light Level: ");
Serial.print(percent);
Serial.println(" %");
lastRead = now;
}
if(now - lastMsg > 5000){
SendData();
lastMsg = now;
}
if(now - lastThreshold > 6000){
Serial.println("Check ThreshHold");
ThresholdUpdate();
lastThreshold = now;
}
client.loop();
}
void SendData(){
lightLevel = analogRead(LDR_PIN);
int percent = map(lightLevel, 0, 1023, 0, 100);
String Light_level = String(percent);
String State;
switch_state = digitalRead(SWITCH_PIN);
if(switch_state == 1){
State = "On";
}else{
State = "Off";
}
String payload = "{";
payload += "\"light_level\":"; payload += Light_level; payload += ",";
payload += "\"switch_status\":"; payload += State;
payload += "}";
char telemetry[100];
payload.toCharArray( telemetry, 100 );
client.publish( "v1/devices/me/telemetry", telemetry );
Serial.println("Send Data Successful");
}
void ThresholdUpdate(){
Serial.print("Threshold Value: ");
Serial.println(threshold);
}