#include <DHTesp.h> // DHT for ESP32 library
#include <WiFi.h> // WiFi control for ESP32
#include "ThingsBoard.h"
#include <DS1307RTC.h>
#include <ESP32Servo.h>
#include "TimeLib.h"
#define SERIAL_OPTION 0
#define SCL_PIN 22
#define SDA_PIN 21
#define DHT_PIN 15
const int servoPin = 18;
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define WIFI_AP_NAME "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define THINGSBOARD_ACCESSTOKEN "SfTEgEv8I1xMqpJ0FCg8"
WiFiClient espClient;
ThingsBoard tb(espClient);
Servo myservo;
tmElements_t tm;
DHTesp dht;
int control_Hour1 = 0;
int status = WL_IDLE_STATUS;
// deklarasi array untuk pin pendingin yang dikontrol melalui thingsboard
uint8_t aircond_control[] = { 13, 12 };
// deklarasi array untuk pin led yang dikontrol melalui thingsboard
uint8_t leds_control[] = { 2, 4 };
int i;
// Main application loop delay
int quant = 20;
float control_tem = 0;
int control_lamp1 = 0;
int control_lamp2 = 0;
int pendingin = 0;
int pemanas = 0;
// Period of sending a temperature/humidity data.
int send_delay = 2000;
// Time passed after temperature/humidity data was sent, milliseconds.
int send_passed = 0;
// Set to true if application is subscribed for the RPC messages.
bool subscribed = false;
float temperature = 0;
float humidity = 0;
int pos = 0;
void connect()
{
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("Connected to AP");
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
RPC_Response processHour1Change(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
// Process data
control_Hour1 = data;
Serial.print("Atur waktu ke: ");
Serial.println(control_Hour1);
return String(control_Hour1);
}
RPC_Response processGetHour1(const RPC_Data &data)
{
Serial.println("Received the get value method");
return String(control_Hour1);
}
RPC_Response processTempChange(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
// Process data
control_tem = data;
Serial.print("Atur suhu ke: ");
Serial.println(control_tem);
return String(control_tem);
}
RPC_Response processGetTemp(const RPC_Data &data)
{
Serial.println("Received the get value method");
return String(control_tem);
}
RPC_Response processSwitch1Change(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
// Process data
control_lamp1 = data;
Serial.println(control_lamp1);
return String(control_lamp1);
}
RPC_Response processGetSwitch1(const RPC_Data &data)
{
Serial.println("Received the get value method");
return String(control_lamp1);
}
RPC_Response processSwitch2Change(const RPC_Data &data)
{
Serial.println("Received the set delay RPC method");
// Process data
control_lamp2 = data;
Serial.println(control_lamp2);
return String(control_lamp2);
}
RPC_Response processGetSwitch2(const RPC_Data &data)
{
Serial.println("Received the get value method");
return String(control_lamp2);
}
// RPC handlers
RPC_Callback callbacks[] = {
{ "setValue", processTempChange },
{ "getValue", processGetTemp },
{ "setHour1", processHour1Change },
{ "getHour1", processGetHour1 },
{ "setSwitch1", processSwitch1Change },
{ "getSwitch1", processGetSwitch1 },
{ "setSwitch2", processSwitch2Change },
{ "getSwitch2", processGetSwitch2 },
};
void setup() {
Serial.begin(115200);
connect();
myservo.attach(18, 500, 2400);
// Inisialiasi pinmode
for (size_t i = 0; i < COUNT_OF(aircond_control); ++i) {
pinMode(aircond_control[i], OUTPUT);
}
for (size_t i = 0; i < COUNT_OF(leds_control); ++i) {
pinMode(leds_control[i], OUTPUT);
}
// Initialize temperature sensor
dht.setup(DHT_PIN, DHTesp::DHT22);
}
void loop()
{
delay(quant);
send_passed += quant;
// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
connect();
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;
}
digitalWrite(leds_control[0], control_lamp1);
digitalWrite(leds_control[1], control_lamp2);
// Check if it is a time to send DHT22 temperature and humidity
if (send_passed > send_delay) {
Serial.println();
Serial.print("Sending data... ");
TempAndHumidity lastValues = dht.getTempAndHumidity();
if (isnan(lastValues.humidity) || isnan(lastValues.temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if (RTC.read(tm)) {
Serial.println("Sekarang Jam ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Tanggal ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
if(tm.Hour == control_Hour1 && tm.Minute == 00)
{
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
Serial.println("Alat Penyiram Aktif");
}
}
}
if(control_lamp1 == 1)
{
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
Serial.println("Alat Penyiram Aktif");
}
}
if(control_lamp1 == 0)
{
myservo.write(0);
}
temperature = lastValues.temperature;
Serial.print("Suhu: ");
Serial.print(temperature);
humidity = lastValues.humidity;
Serial.print(" Kelembaban: ");
Serial.println(humidity);
tb.sendTelemetryFloat("suhu", temperature);
tb.sendTelemetryFloat("kelembaban", humidity);
if (temperature < control_tem) {
// Nyalakan pemanas
pendingin = 0;
pemanas = 1;
}
else if(temperature > control_tem){
// Nyalakan pendingin
pendingin = 1;
pemanas = 0;
}
else{
pendingin = 0;
pemanas = 0;
}
digitalWrite(aircond_control[0], pemanas);
digitalWrite(aircond_control[1], pendingin);
}
send_passed = 0;
}
tb.loop();
}