#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <ThingsBoard.h>
#include <PubSubClient.h>
#define LDR 36
const float GAMMA = 0.7;
const float RL10 = 50;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
#define TOKEN "qHwAePLkXwM0wx1FgrO7" // Access token of device Display
const char* mqtt_server = "thingsboard.cloud";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
delay(10);
pinMode(LDR, INPUT);
Serial.print("Connect to: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("...");
}
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnect();
}
getData();
delay(5000);
}
void getData() {
int analogValue = analogRead(LDR);
float voltage = analogValue / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.println(lux);
String payload = "{\"Intensitas Cahaya\":";
payload += lux;
payload += "}";
char attributes[100];
payload.toCharArray(attributes, 100);
client.publish("v1/devices/me/telemetry", attributes);
Serial.println(attributes);
}
void reconnect() {
while (!client.connected()) {
status = WiFi.status();
if (status != WL_CONNECTED)