#include <WiFi.h>
#include <ThingsBoard.h>
#include <Server_Side_RPC.h>
#include <Arduino_MQTT_Client.h>
#include <DHT.h>
#define SSID "Wokwi-GUEST"
#define PASS ""
#define TOKEN "ESP32PICO"
#define TBSERVER "demo.thingsboard.io"
#define TBPORT 1883
#define MAXSEND 256
#define MAXRECEIVE 256
#define MAXSUBSCRIPTION 10
#define MAXRESPONSE 10
#define DHTPIN 22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
WiFiClient esp;
Arduino_MQTT_Client mqttClient(esp);
Server_Side_RPC<MAXSUBSCRIPTION, MAXRESPONSE> rpc;
const std::array<IAPI_Implementation*, 1U> apis = {
&rpc
};
ThingsBoard tb(mqttClient, MAXRECEIVE, MAXSEND, Default_Max_Stack_Size, apis);
int quant = 20;
int led_delay = 1000;
int send_delay = 5000;
int led_passed = 0; // Time passed after LED was turned ON, milliseconds.
int send_passed = 0; // Time passed after temperature/humidity data was sent, milliseconds.
bool subscribed = false;
int current_led = 0;
int leds_cycling[] = { 27, 26, 25 };
int leds_control[] = { 19, 18, 17 };
String leds[] = { "Red", "Green", "Blue" };
int total_leds_cycling = sizeof(leds_cycling)/sizeof(0[leds_cycling]);
int total_leds_control = sizeof(leds_control)/sizeof(0[leds_control]);
void setup() {
for (int i = 0; i < total_leds_cycling; ++i) {
pinMode(leds_cycling[i], OUTPUT);
}
for (int i = 0; i < total_leds_control; ++i) {
pinMode(leds_control[i], OUTPUT);
}
digitalWrite(leds_control[1], HIGH); //just to test GPIO Control Panel update
dht.begin();
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin(SSID, PASS, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
delay(quant);
led_passed += quant;
send_passed += quant;
if (led_passed > led_delay) {
digitalWrite(leds_cycling[current_led], LOW);
led_passed = 0;
current_led = current_led >= 2 ? 0 : (current_led + 1);
digitalWrite(leds_cycling[current_led], HIGH);
}
//DHTupdate();
if (!tb.connected()) {
Serial.printf("Connecting to: (%s) with token (%s)\n", TBSERVER, TOKEN);
if (!tb.connect(TBSERVER, TOKEN, TBPORT)) {
Serial.println("Failed to connect");
return;
}
}
if (!subscribed) {
Serial.print("Subscribing for RPC...");
const std::array<RPC_Callback, MAXSUBSCRIPTION> callbacks = {
RPC_Callback{ "getValue", processGetDelay },
RPC_Callback{ "setValue", processDelayChange },
RPC_Callback{ "getGpioStatus", processGpioStatus },
RPC_Callback{ "setGpioStatus", processSwitchChange }
};
if (!rpc.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
Serial.println("Failed!");
return;
}
Serial.println("Done!");
subscribed = true;
}
tb.loop();
}
void processSwitchChange(const JsonVariantConst &data, JsonDocument &response) {
Serial.println("-> Switch LED");
int pin = data["pin"];
bool enabled = data["enabled"];
Serial.print("---> " + leds[pin-1] + " to ");
Serial.println(enabled ? "ON" : "OFF");
digitalWrite(leds_control[pin-1], enabled);
response[String(pin)] = enabled;
}
void processGpioStatus(const JsonVariantConst &data, JsonDocument &response) {
Serial.println("-> Updating GPIO Control Panel");
for (int i = 0; i < total_leds_control; ++i) {
int pin = leds_control[i];
Serial.print("---> LED " + leds[i] + " : ");
bool ledState = digitalRead(pin);
Serial.println(ledState ? "ON" : "OFF");
response[String(i+1)] = ledState;
}
}
void processDelayChange(const JsonVariantConst &data, JsonDocument &response) {
led_delay = data;
Serial.println("-> Received new delay value : " + String(led_delay));
response.set(led_delay);
}
void processGetDelay(const JsonVariantConst &data, JsonDocument &response)
{
Serial.println("-> Sending back current delay value : " + String(led_delay));
response.set(led_delay);
}
void DHTupdate() {
if (send_passed > send_delay) {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
else {
Serial.println("-> Sending temperature and humidity");
Serial.println("---> t = " + String(t) + "\th = " + String(h));
tb.connect(TBSERVER, TOKEN);
tb.sendTelemetryData("temperature", t);
tb.sendTelemetryData("humidity", h);
}
send_passed = 0;
}
}