#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <ThingsBoard.h>
#include <DHTesp.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 "GfAOLkydxQP3OiiWPXpy"
#define THINGSBOARD_SERVER "thingsboard.cloud"
const char *TOKEN_DEVICE[] = {"GfAOLkydxQP3OiiWPXpy", "rBjHF5ZTyoT2YEzR3cnZ", "I1T3eP1dufELmScl4kpL"};
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD 115200
const int DHT_PIN = 13;
uint8_t location_id = 0;
DHTesp dhtSensor;
// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
bool subscribed = false;
void InitWiFi();
void reconnect();
RPC_Response processSwitchChange(const RPC_Data &data);
const size_t callbacks_size = 1;
// RPC_Callback callbacks[callbacks_size] = {
// { "example_set_dial", processDialChange },
// { "example_get_dial", getDial },
// { "example_set_switch", processSwitchChange }
// };
RPC_Callback callbacks[callbacks_size] = {
{ "set_lampu_lahan", processSwitchChange }
};
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");
}
}
RPC_Response processSwitchChange(const RPC_Data &data)
{
boolean switch_state = data;
Serial.print("Received switch state: ");
Serial.println(switch_state);
digitalWrite(LED_BUILTIN, switch_state); // switch led
return RPC_Response("OK", 1);
}
void setup() {
randomSeed(analogRead(A0));
pinMode(LED_BUILTIN, OUTPUT);
// initialize serial for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
if (!tb.connected()) {
subscribed = false;
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN_DEVICE[location_id % 3]);
// if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
if (!tb.connect(THINGSBOARD_SERVER, TOKEN_DEVICE[location_id % 3])) {
Serial.println("Failed to connect");
return;
}
}
if (!subscribed) {
Serial.println("Subscribing for RPC...");
if (!tb.RPC_Subscribe(callbacks, callbacks_size)) {
Serial.println("Failed to subscribe for RPC");
return;
}
Serial.println("Subscribe done");
subscribed = true;
}
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();
long randx = random(100) - 50;
Serial.println(randx);
float hum = constrain(data.humidity + (randx / 10), 0, 100), tempx = constrain(data.temperature + (randx / 10), -10, 100);
tb.sendTelemetryFloat("temperature", tempx);
tb.sendTelemetryFloat("humidity", hum);
Serial.print("Suhu : ");
Serial.print(tempx);
Serial.print(" Kelembaban : ");
Serial.println(hum);
tb.loop();
// tb.loop();
tb.disconnect();
delay(random(3));
location_id++;
}