#include <WiFi.h> // WiFi control for ESP32
//#include <ThingsBoard.h> // ThingsBoard SDK
#include "ThingsBoard.h"
#define WIFI_AP_NAME "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define THINGSBOARD_ACCESSTOKEN "3MEzvNDH3wHgtQ2Co1CN" //Ubah Token
#define SERIAL_DEBUG_BAUD 115200
#define POT_PIN 34
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
#include "DHTesp.h"
const int DHT_PIN = 22;
DHTesp dhtSensor;
// Array with LEDs that should be controlled from ThingsBoard, one by one
uint8_t leds_control[] = {14, 12};
// Set to true if application is subscribed for the RPC messages.
bool subscribed = false;
// LED number that is currenlty ON.
int current_led = 0;
// Start - Modified By Revi - 31 Juli 2022
#include <ESP32Servo.h>
#define LDRPIN 33
#define servoPin 25
Servo myservo;
#define minCahaya 2045 // = 100 lux
#define maxCahaya 1291 // = 300 lux
unsigned long d1 = 100;
unsigned long lastD1 = 0;
unsigned long d2 = 400;
unsigned long lastD2 = 0;
unsigned long d3 = 200;
unsigned long lastD3 = 0;
int analogValue = 0;
int dataPot = 0;
int potValue = 0;
const float GAMMA = 0.7;
const float RL10 = 50;
float voltage = 0;
float resistance = 0;
float lux = 0;
TempAndHumidity data;
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
RPC_Response processSetGpioState(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
int pin = data["pin"];
bool enabled = data["enabled"];
if (pin < COUNT_OF(leds_control)) {
Serial.print("Setting LED ");
Serial.print(pin);
Serial.print(" to state ");
Serial.println(enabled);
digitalWrite(leds_control[pin], enabled);
}
return String("{\"" + String(pin) + "\": " + String(enabled?"true":"false") + "}");
//return RPC_Response(data["pin"], (bool)data["enabled"]);
}
//Reset system ESP 32
RPC_Response processReset(const RPC_Data &data)
{
delay(4000);
ESP.restart();
return RPC_Response(NULL);
}
RPC_Response processGetGpioState(const RPC_Data &data)
{
Serial.println("Received the get GPIO RPC method");
String respStr = "{";
for (size_t i = 0; i < COUNT_OF(leds_control); ++i) {
int pin = leds_control[i];
Serial.print("Getting LED ");
Serial.print(pin);
Serial.print(" state ");
bool ledState = digitalRead(pin);
Serial.println(ledState);
respStr += String("\"" + String(i) + "\": " + String(ledState?"true":"false") + ", ");
}
respStr = respStr.substring(0, respStr.length() - 2);
respStr += "}";
return respStr;
}
// RPC handlers
RPC_Callback callbacks[] = {
{ "setGpioStatus", processSetGpioState },
{ "getGpioStatus", processGetGpioState },
{ "rpcReset", processReset },
};
int h = 0;
int t = 0;
int LDRVal = 0;
// End
void setup() {
Serial.begin(SERIAL_DEBUG_BAUD);
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
InitWiFi();
pinMode(POT_PIN, INPUT);
pinMode(14,OUTPUT);
// Pinconfig
for (size_t i = 0; i < COUNT_OF(leds_control); ++i) {
pinMode(leds_control[i], OUTPUT);
}
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
// Modified By Revi - 31 Juli 2022
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);// Standard 50hz servo
myservo.attach(servoPin, 500, 2400);
pinMode(LDRPIN, INPUT);
// End
}
void loop() {
unsigned long currentMillis = millis();
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}
// Reconnect to ThingsBoard, if needed
if (!tb.connected()) {
subscribed = false;
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(THINGSBOARD_ACCESSTOKEN);
if (!tb.connect(THINGSBOARD_SERVER, THINGSBOARD_ACCESSTOKEN)) {
Serial.println("Failed to connect");
return;
}
}
// Subscribe for RPC, if needed
if (!subscribed) {
Serial.println("Subscribing for RPC... ");
// Perform a subscription. All consequent data processing will happen in
// callbacks as denoted by callbacks[] array.
if (!tb.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
Serial.println("Failed to subscribe for RPC");
return;
}
Serial.println("Subscribe done");
subscribed = true;
}
LDRVal = analogRead(LDRPIN);
voltage = LDRVal / 4095. * 5;
resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// lcd.clear();
lcd.setCursor(0,0);
lcd.print("Suhu : ");
lcd.print(t);
lcd.print(" C ");
lcd.setCursor(0,1);
lcd.print("Humidity : ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0,2);
lcd.print("klbpn tanah : ");
lcd.print(potValue);
lcd.setCursor(0,3);
lcd.print("Sinar : ");
lcd.print(lux);
lcd.print(" lux");
if (lux >= maxCahaya) {
myservo.write(0);
// lcd.clear();
// lcd.print("Atap Tertutup");
} else {
myservo.write(90);
// lcd.clear();
// lcd.print("Atap Terbuka");
}
// ***** READ POTENTIOMETER AS SOIL SENSOR ***** //
dataPot = analogRead(POT_PIN);
potValue = map(dataPot, 0, 800, 0, 255);
Serial.print("Kelembapan Tanah : ");
Serial.println(potValue);
for (int i = 13; i <= 20; i++) {
lcd.setCursor(i,2);
lcd.print(" ");
}
if(potValue <= 500) {
digitalWrite(14, HIGH);
Serial.println("Pompa Air hidup");
} else {
digitalWrite(14, LOW);
Serial.println("Pompa Air Mati");
}
if ((currentMillis - lastD2) > d2) {
lastD2 = currentMillis;
data = dhtSensor.getTempAndHumidity();
t = data.temperature;
h = data.humidity;
Serial.print("Suhu : ");
Serial.print(t);
Serial.print(" Kelembaban : ");
Serial.println(h);
}
if ((currentMillis - lastD3) > d3) {
lastD3 = currentMillis;
sendData();
}
}
void InitWiFi() {
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
Serial.println("Connected to AP");
}
}
void sendData() {
Serial.println("Sending data...");
if (!tb.connected()) {
subscribed = false;
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(THINGSBOARD_ACCESSTOKEN);
if (!tb.connect(THINGSBOARD_SERVER, THINGSBOARD_ACCESSTOKEN)) {
Serial.println("Failed to connect");
return;
}
}
tb.sendTelemetryInt("temperature", t);
tb.sendTelemetryFloat("humidity", h);
tb.sendTelemetryFloat("Kelembapan Tanah", potValue);
tb.sendTelemetryInt("kecerahan", lux);
if(potValue <= 500) {
tb.sendTelemetryString("Pompa Air","Hidup");
} else {
tb.sendTelemetryString("Pompa Air","Mati");
}
if (LDRVal >= maxCahaya) {
tb.sendTelemetryString("ACTUATOR ATAP","Tertutup");
} else {
tb.sendTelemetryString("ACTUATOR ATAP","Terbuka");
}
Serial.println("Data Sended...");
}