#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include "ThingsBoard.h"
#include <ESP32Servo.h>
#include "DHTesp.h"
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define CURRENT_FIRMWARE_TITLE "TEST"
#define CURRENT_FIRMWARE_VERSION "1.0.0"
#define SSID "Wokwi-GUEST"
#define PASS ""
#define TOKEN "tWKVySgfnUqTVlsDCFaG"
#define THINGSBOARD_SERVER "demo.thingsboard.io"
#define SERIAL_DEBUG_BAUD 115200
#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);
#define lampu 2
#define LDR_PIN 34
const unsigned char indobot [] PROGMEM = {
// 'INDOBOT-LOGO-kecil, 128x64px
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
};
const float GAMMA = 0.7;
const float RL10 = 50;
boolean lamp;
int status = WL_IDLE_STATUS;
// Set to true if application is subscribed for the RPC messages.
bool subscribed = false;
WiFiClient client;
ThingsBoard tb(client);
Servo myservo;
const int DHT_PIN = 13;
DHTesp dhtSensor;
void reconnect(){
status = WiFi.status();
if(status!=WL_CONNECTED){
WiFi.begin(SSID, PASS);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("Connected to AP");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(SERIAL_DEBUG_BAUD);
myservo.attach(15);
pinMode(lampu, OUTPUT);
digitalWrite(lampu, LOW);
WiFi.disconnect();
WiFi.begin("Wokwi-GUEST", "");
while ((!(WiFi.status() == WL_CONNECTED))) {
delay(300);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.drawBitmap(0, 0, indobot, 128, 64, WHITE);
display.display();
delay(5000);
}
void loop() {
delay(1000);
if(WiFi.status() != WL_CONNECTED){
reconnect();
}
if(!tb.connected()) {
Serial.println("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);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
int analogValue = analogRead(LDR_PIN);
float voltage = analogValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
display.setCursor(0,0);
display.println("Temp : " + String(data.temperature, 1) + " C");
display.setCursor(0,15);
display.println("Hum: " + String(data.humidity, 2) + " %");
display.setCursor(0, 30);
display.println("Lux: " + String(lux, 2) + " Lux");
if ((data.temperature >= 28) || (lux >= 1800) || (data.humidity <= 45)){
myservo.write(90);
tb.sendTelemetryString("Roof", "Close");
digitalWrite(lampu, LOW);
lamp = false;
tb.sendTelemetryInt("lamp", lamp);
delay(15);
} else if((data.temperature <= 24) || (lux <= 1600) || (data.humidity >= 64)){
myservo.write(0);
tb.sendTelemetryString("Roof", "Open");
digitalWrite(lampu, HIGH);
lamp = true;
tb.sendTelemetryInt("lamp", lamp);
delay(15);
} else if((24 <= data.temperature <= 28) || (1600 <= lux <= 1800) || (64>= data.humidity >= 45)) {
myservo.write(0);
tb.sendTelemetryString("Roof", "Open");
digitalWrite(lampu, LOW);
lamp = false;
tb.sendTelemetryInt("lamp", lamp);
delay(15);
}
tb.sendTelemetryInt("temperature", data.temperature);
tb.sendTelemetryFloat("humidity", data.humidity);
tb.sendTelemetryFloat("Lux", lux);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Lux: " + String(lux, 2) + "Lux");
Serial.println("---");
display.display();
tb.loop();
}
Loading
ssd1306
ssd1306