#include <Wire.h>
#include <ThingsBoard.h>
#include <PubSubClient.h>
// #include <ESP8266WiFi.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
//Konfigurasi WiFi
#define WIFI_AP_NAME "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define TOKEN "eA6ycLKvAx39mgXXDNrb"
#define SERIAL_DEBUG_BAUD
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
LiquidCrystal_I2C lcd(0x27, 16, 2); //define I2C address 0x27, 16 column and 2 rows
float flamelevel = 5; // mapped and inverted % of sensor range
//#define greenLED 18 // fire okay or firing
//#define redLED 19 // warning or flashing alert
// empirically relate flame % to actual fire condition (TBD)
const int minSurvive = 15; // minimum level for idle, below is outfire
const int idleLow = 20; // lowest reading for healthy idle
const int idleTarget = 30; // target reading for resting idle
const int firingLow = 70; // lowest reading for actively firing
const int firingHigh = 90; // reading for full firing
void setup() {
Serial.begin(9600); // SERIAL_DEBUG_BAUD);
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
InitWiFi();
lcd.init();
lcd.clear();
lcd.backlight();
Serial.begin(9600);
}
void loop() {
float analogValue = analogRead(A0);
Serial.print("Sensor RAW: ");
Serial.println(analogValue, 0);
flamelevel = map(analogValue, 0, 1024, 100, 0);
Serial.print(flamelevel, 0);
Serial.println("%");
// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}
// Reconnect to ThingsBoard, if needed
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;
}
}
// disabling the lcd commands makes serial print work
lcd.setCursor(0, 0);
lcd.print(F("Flame: "));
if (flamelevel >= firingHigh) { // stoker is fully firing
lcd.print("Full Fire");
tb.sendTelemetryFloat("Deteksi Api :", flamelevel);
}
if ((flamelevel >= firingLow) && (flamelevel < firingHigh)) { // stoker is firing
lcd.print("Firing ");
tb.sendTelemetryFloat("Deteksi Api :", flamelevel);
}
if ((flamelevel < firingLow) && (flamelevel > idleLow) ) { // idle fire
lcd.print("Idle fire ");
tb.sendTelemetryFloat("Deteksi Api :", flamelevel);
}
if ((flamelevel <= idleLow) && (flamelevel >= minSurvive) ) { // low fire
lcd.print("Low fire ");
tb.sendTelemetryFloat("Deteksi Api :", flamelevel);
// trigger stoker run timer = 2 mins?
}
if (flamelevel < minSurvive) { // fire out
lcd.print("FIRE OUT! ");
tb.sendTelemetryFloat("Deteksi Api :", flamelevel);
}
lcd.setCursor(0, 1);
lcd.print(" Level: ");
lcd.print(flamelevel, 0);
lcd.print("% ");
tb.loop();
delay(200);
}
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("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("Connected to AP");
}
}