//COBA USE CASE 2 OLEH KELOMPOK 2 //
// MONITORING KOLAM UDANG VANAME // sampling data kolam sisi utara .
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include "ThingsBoard.h"
#include <ESP32Servo.h>
// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
#define TOKEN "oQohIDoYdLGlB3AQlDu1" //GANTI SESUAI AKUN TB ANDA
#define THINGSBOARD_SERVER "thingsboard.cloud" // FIXED
const int heatPin = 12;
const int bzrPin = 4;
const int servoPin = 5;
const int relayPin = 2;
//SENSOR DHT22
const int DHT_PIN = 15;
DHTesp dhtSensor;
Servo servo;
int pos = 0;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
//LCD 16X2
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void stdbyScreen() {
LCD.setCursor(8, 0);
LCD.println("SMART");
LCD.setCursor(0, 1);
LCD.println("POUND-V1");
}
//wifi
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to AP");
spinner();
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to AP");
spinner();
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(heatPin, OUTPUT);
pinMode(bzrPin, OUTPUT);
servo.attach(servoPin, 500, 2400);
pinMode(relayPin, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
InitWiFi();
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000); // this speeds up the simulation
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...");
// 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);
Serial.println("data kolam sisi 1 terkirim . . . .");
checkSuhu();
lcdMonitor();
//tb.loop();
tb.loop();
}
void checkSuhu(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if(data.temperature < 26){
digitalWrite(heatPin, HIGH);
digitalWrite(bzrPin, HIGH);
LCD.setCursor(8, 0);
LCD.println("WARNING");
LCD.setCursor(0, 1);
LCD.println("SUHU DINGIN");
// LCD.Clear();
}else if(data.temperature > 34){
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
LCD.println("WARNING");
LCD.setCursor(0, 1);
LCD.println("SUHU PANAS");
//LCD.Clear();
}else{
digitalWrite(relayPin, HIGH);
lcdMonitor();
}
}
void lcdMonitor(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
LCD.setCursor(8, 0);
LCD.println("SMRTPND-V1");
LCD.setCursor(0, 1);
LCD.println("suhu : ");
LCD.print(data.temperature);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
relay1:NO2
relay1:NC2
relay1:P2
relay1:COIL2
relay1:NO1
relay1:NC1
relay1:P1
relay1:COIL1
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
led1:A
led1:C
r1:1
r1:2
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
bz1:1
bz1:2
led2:A
led2:C
r2:1
r2:2
servo1:GND
servo1:V+
servo1:PWM