#include "DHT.h"
#include <Arduino_MQTT_Client.h>
#include <ThingsBoard.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#define THINGSBOARD_ENABLE_PROGMEM 0
#elif defined(ESP32) || defined(RASPBERRYPI_PICO) || defined(RASPBERRYPI_PICO_W)
#include <WiFi.h>
#endif
#define DHTPIN 15
#define DHTTYPE DHT22
float h = 0;
float t = 0;
float s = 0;
DHT dht(DHTPIN, DHTTYPE);
// void turnOnBuzzer(int pinBuzzer);
// void turnOffBuzzer(int pinBuzzer);
constexpr char WIFI_SSID[] = "Wokwi-GUEST";
constexpr char WIFI_PASSWORD[] = "";
// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
constexpr char TOKEN[] = "asgwQGRArfeGbEkuZAAe";
// Thingsboard we want to establish a connection too
constexpr char THINGSBOARD_SERVER[] = "demo.thingsboard.io";
// MQTT port used to communicate with the server, 1883 is the default unencrypted MQTT port.
constexpr uint16_t THINGSBOARD_PORT = 1883U;
// Maximum size packets will ever be sent or received by the underlying MQTT client,
// if the size is to small messages might not be sent or received messages will be discarded
constexpr uint32_t MAX_MESSAGE_SIZE = 1024U;
// Baud rate for the debugging serial connection.
// If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable
constexpr uint32_t SERIAL_DEBUG_BAUD = 115200;
// Initialize underlying client, used to establish a connection
WiFiClient wifiClient;
// Initalize the Mqtt client instance
Arduino_MQTT_Client mqttClient(wifiClient);
// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
constexpr int16_t telemetrySendInterval = 2000U;
uint32_t previousDataSend;
/// @brief Initalizes WiFi connection,
// will endlessly delay until a connection has been successfully established
void InitWiFi() {
Serial.println("Connecting to AP ...");
// Attempting to establish a connection to the given WiFi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
// Delay 500ms until a connection has been succesfully established
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
/// @brief Reconnects the WiFi uses InitWiFi if the connection has been removed
/// @return Returns true as soon as a connection has been established again
const bool reconnect() {
// Check to ensure we aren't connected yet
const wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) {
return true;
}
// If we aren't establish a new connection to the given WiFi network
InitWiFi();
return true;
}
void setup() {
// Initalize serial connection for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
delay(1000);
InitWiFi();
Serial.println(F("DHTxx test!"));
// pinMode(buzzerpin,OUTPUT); // Define the port attribute as output
ledcSetup(0, 5000, 8); // LEDC channel 0, 5000 Hz PWM, 8-bit resolution
dht.begin();
}
void loop() {
delay(2000);
if (!reconnect()) {
return;
}
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, THINGSBOARD_PORT)) {
Serial.println("Failed to connect");
return;
}
// Sending a MAC address as an attribute
tb.sendAttributeData("macAddress", WiFi.macAddress().c_str());
}
h = dht.readHumidity();
h = map(h, 0, 100, 0, 14);
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("pH: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
if (millis() - previousDataSend > telemetrySendInterval) {
previousDataSend = millis();
tb.sendTelemetryData("temperature", t);
tb.sendTelemetryData("pH", h);
Serial.print("temperature ");
Serial.println(t);
Serial.print("pH ");
Serial.println(h);
Serial.println(WiFi.SSID().c_str());
// tb.sendAttributeData("rssi", WiFi.RSSI());
// tb.sendAttributeData("channel", WiFi.channel());
// tb.sendAttributeData("bssid", WiFi.BSSIDstr().c_str());
// tb.sendAttributeData("localIp", WiFi.localIP().toString().c_str());
// tb.sendAttributeData("ssid", WiFi.SSID().c_str());
}
tb.loop();
}