#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include "ThingsBoard.h"
#include "DHTesp.h"
#include <ESP32Servo.h>
Servo servo1;
Servo servo2;
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define CURRENT_FIRMWARE_TITLE "TEST"
#define CURRENT_FIRMWARE_VERSION "1.0.0"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
#define TOKEN "sqY8sfWtOHzML1c1Op7q"
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD 115200
#define Led_red 19 //Indikator Tambak Panas
#define Led_green 5 //Indikator Tambak Dingin
#define Led_yellow 18 //Indikator Tambak Normal
const int DHT_PIN = 15;
const int servoPin1 = 14; //Pompa Air Panas
const int servoPin2 = 12; //Pompa Air Dingin
DHTesp dhtSensor;
// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
const unsigned char logoudang [] PROGMEM = {
// Logo Udang
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x38, 0xfe, 0x00, 0xe3, 0xff, 0xf0, 0x00, 0x0c, 0x00, 0x3f, 0xff, 0xf8, 0x1f, 0xe0, 0x7f, 0xfe,
0x38, 0xff, 0xc0, 0xe3, 0xff, 0xf8, 0x01, 0x8c, 0x20, 0x3f, 0xff, 0xf8, 0x3f, 0xf8, 0x7f, 0xfe,
0x38, 0xc7, 0xe0, 0xe3, 0x80, 0x3c, 0x03, 0x8c, 0x30, 0x30, 0x00, 0x38, 0x78, 0x3c, 0x01, 0x80,
0x38, 0xc0, 0xf8, 0xe3, 0x80, 0x0e, 0x07, 0x8c, 0x38, 0x30, 0x00, 0x38, 0xe0, 0x0e, 0x01, 0x80,
0x38, 0xc0, 0x3c, 0xe3, 0x80, 0x07, 0x07, 0x0c, 0x38, 0x30, 0x00, 0x71, 0xc0, 0x07, 0x01, 0x80,
0x38, 0xc0, 0x1e, 0xe3, 0x80, 0x03, 0x0e, 0x0c, 0x1c, 0x30, 0x00, 0xf1, 0x80, 0x07, 0x01, 0x80,
0x38, 0xc0, 0x0f, 0xe3, 0x80, 0x03, 0x8e, 0x0c, 0x0c, 0x30, 0x01, 0xe3, 0x80, 0x03, 0x81, 0x80,
0x38, 0xc0, 0x07, 0xe3, 0x80, 0x03, 0x8c, 0x0c, 0x0e, 0x30, 0x07, 0xc3, 0x80, 0x03, 0x81, 0x80,
0x38, 0xc0, 0x03, 0xe3, 0x80, 0x01, 0x8c, 0x0c, 0x0e, 0x3f, 0xff, 0x83, 0x00, 0x03, 0x81, 0x80,
0x38, 0xc0, 0x03, 0xe3, 0x80, 0x01, 0x8c, 0x0c, 0x0e, 0x3f, 0xff, 0x83, 0x00, 0x01, 0x81, 0x80,
0x38, 0xc0, 0x01, 0xe3, 0x80, 0x01, 0x8c, 0x00, 0x0e, 0x38, 0x0f, 0xc3, 0x80, 0x03, 0x81, 0x80,
0x38, 0xc0, 0x01, 0xe3, 0x80, 0x03, 0x8e, 0x00, 0x0c, 0x30, 0x01, 0xe3, 0x80, 0x03, 0x81, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0x80, 0x03, 0x0e, 0x00, 0x1c, 0x30, 0x00, 0xf1, 0x80, 0x03, 0x01, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0x80, 0x07, 0x07, 0x00, 0x1c, 0x30, 0x00, 0x71, 0xc0, 0x07, 0x01, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0x80, 0x0e, 0x07, 0x80, 0x38, 0x30, 0x00, 0x30, 0xe0, 0x0e, 0x01, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0x80, 0x1e, 0x03, 0xc0, 0xf0, 0x30, 0x00, 0x38, 0xf0, 0x3e, 0x01, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0xff, 0xfc, 0x01, 0xff, 0xe0, 0x3f, 0xff, 0xf8, 0x7f, 0xf8, 0x01, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0xff, 0xf0, 0x00, 0x7f, 0xc0, 0x3f, 0xff, 0xf8, 0x1f, 0xf0, 0x01, 0x80,
0x38, 0xc0, 0x00, 0xe3, 0xff, 0x80, 0x00, 0x0e, 0x00, 0x3f, 0xff, 0xf8, 0x03, 0x80, 0x01, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x00, 0x03, 0xfc, 0x00, 0x38, 0x00, 0x3f, 0xc0, 0x03, 0xfc, 0x01, 0x80, 0x70, 0x0c, 0x0c,
0x07, 0x80, 0x07, 0xfc, 0x00, 0x7c, 0x00, 0x3f, 0xe0, 0x03, 0xfc, 0x01, 0xc0, 0xf0, 0x0e, 0x18,
0x0d, 0x80, 0x06, 0x00, 0x00, 0x6c, 0x00, 0x30, 0x70, 0x03, 0x00, 0x01, 0xc0, 0xf0, 0x07, 0x18,
0x0d, 0xc0, 0x0e, 0x00, 0x00, 0x6c, 0x00, 0x30, 0x38, 0x03, 0x00, 0x01, 0xe1, 0xf0, 0x03, 0x30,
0x1c, 0xc0, 0x0c, 0x00, 0x00, 0xc6, 0x00, 0x30, 0x38, 0x03, 0xf8, 0x01, 0xf1, 0xf0, 0x03, 0xf0,
0x18, 0xe0, 0x0c, 0x00, 0x00, 0xc6, 0x00, 0x30, 0x18, 0x03, 0xf8, 0x01, 0xb3, 0x70, 0x01, 0xe0,
0x38, 0xe0, 0x0c, 0x00, 0x01, 0xc7, 0x00, 0x30, 0x38, 0x03, 0x00, 0x01, 0x9f, 0x70, 0x01, 0xc0,
0x3f, 0xe0, 0x0e, 0x00, 0x01, 0xff, 0x00, 0x30, 0x30, 0x03, 0x00, 0x01, 0x9e, 0x70, 0x00, 0xc0,
0x30, 0x70, 0x07, 0x0c, 0x03, 0x83, 0x80, 0x30, 0x70, 0x03, 0x00, 0x01, 0x8c, 0x70, 0x00, 0xc0,
0x60, 0x30, 0x03, 0xfc, 0x03, 0x01, 0x80, 0x3f, 0xe0, 0x03, 0xfc, 0x01, 0x84, 0x70, 0x00, 0xc0,
0x60, 0x38, 0x01, 0xf8, 0x07, 0x01, 0xc0, 0x3f, 0x80, 0x03, 0xfc, 0x01, 0x80, 0x70, 0x00, 0xc0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x62, 0xc6, 0x45, 0xd9, 0x72, 0x84, 0x06, 0x63, 0x06, 0x4d, 0x89, 0x03, 0x60, 0x49, 0xb7, 0x68,
0x02, 0x84, 0x08, 0x82, 0x22, 0x80, 0x80, 0x82, 0x00, 0x81, 0x41, 0x84, 0x02, 0x50, 0x04, 0x12,
0x0a, 0x84, 0x22, 0x80, 0xa0, 0x6a, 0x00, 0x92, 0x00, 0x81, 0x81, 0x44, 0x01, 0x54, 0x84, 0x64,
0x02, 0x80, 0x00, 0x92, 0x01, 0x00, 0x00, 0x62, 0x20, 0x60, 0x09, 0x03, 0x60, 0x40, 0x84, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// delay(2000);
// display.clearDisplay();
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
void setup() {
servo1.attach(servoPin1, 500, 2400);
servo2.attach(servoPin2, 500, 2400);
pinMode(Led_red, OUTPUT);
pinMode(Led_green, OUTPUT);
pinMode(Led_yellow, OUTPUT);
// initialize serial for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.drawBitmap(0, 0, logoudang, 128, 64, WHITE);
display.display();
delay(5000);
}
void loop() {
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
Serial.println("Sending data...");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(1);
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api
// for more details
TempAndHumidity data = dhtSensor.getTempAndHumidity();
tb.sendTelemetryInt("temperature", data.temperature);
tb.sendTelemetryFloat("humidity", data.humidity);
Serial.print("Suhu : ");
Serial.print(data.temperature);
Serial.print(" Kelembaban : ");
Serial.println(data.humidity);
display.setCursor(0,0);
display.println("Temp : " + String(data.temperature, 1) + " C");
display.setCursor(0,15);
display.println("Inisialisasi");
tb.loop();
tb.loop();
// Menggerakkan Servo
//Suhu Panas
if (data.temperature >= 34) {
servo1.write(180);
servo2.write(0);
delay(1000);
Serial.println("Suhu Panas, Pompa Air Dingin Menyala :");
digitalWrite(Led_red, HIGH);
digitalWrite(Led_green, LOW);
digitalWrite(Led_yellow, LOW);
display.setCursor(0,15);
display.println("Suhu Panas, Pompa Air Dingin Menyala");
}
//Suhu Dingin
else if (data.temperature < 26){
servo1.write(0);
servo2.write(180);
Serial.println("Suhu Dingin, Pompa Air Panas Menyala ");
digitalWrite(Led_red, LOW);
digitalWrite(Led_green, HIGH);
digitalWrite(Led_yellow, LOW);
display.setCursor(0,15);
display.println("Suhu Dingin, Pompa Air Panas Menyala ");
}
//Suhu Sedang
else {
servo1.write(0);
servo2.write(0);
Serial.println("Suhu Normal, Semua Pompa Mati ");
digitalWrite(Led_green, LOW);
digitalWrite(Led_red, LOW);
digitalWrite(Led_yellow, HIGH);
display.setCursor(0,15);
display.println("Suhu Normal, Semua Pompa Mati ");
}
display.display();
}