#include <DHTesp.h>
#include <WiFi.h>
#include <ThingsBoard.h>
#include <Arduino_MQTT_Client.h>
#define pinDht 15
DHTesp dhtSensor;
#define WIFI_AP "Wokwi-GUEST"
#define WIFI_PASS ""
#define TB_SERVER "thingsboard.cloud"
#define TOKEN "6pUBL8KqLHMrPh6moW36"
WiFiClient espClient;
constexpr uint16_t MAX_MESSAGE_SIZE = 1024U;
Arduino_MQTT_Client mqttClient(espClient);
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
double currentLatitude = 40.7128; // Initial latitude
double currentLongitude = -74.0060; // Initial longitude
double speed = 0.001; // Fixed speed in degrees per iteration
double direction = 45.0; // Fixed direction in degrees
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
WiFi.begin(WIFI_AP, WIFI_PASS, 6);
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nFailed to connect to WiFi");
} else {
Serial.println("\nWiFi Connected");
}
delay(1000); // Add a delay after connecting to WiFi
}
void connectToThingsBoard() {
Serial.println("Connecting to ThingsBoard server...");
if (tb.connect(TB_SERVER, TOKEN)) {
Serial.println("Connected to ThingsBoard");
} else {
Serial.println("Failed to connect to ThingsBoard server");
}
}
float generateRandomBoolean() {
return random(0, 1001) / 1000.0;
}
int generateRandomNumber(int min, int max) {
return random(min, max + 1);
}
float generateRandomFloat(float minValue, float maxValue) {
return minValue + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX/(maxValue - minValue)));
}
void sendDataToThingsBoard(float temp, int hum) {
// int pressure = generateRandomNumber(20, 200);
// int battery = 90;
// int signal = 75;
// int speed = 4;
// int direction = 12;
// String area = "[[40.19873979265031, 54.65489086551604],[48.2006883940949, 54.645728477857986],[48.20055623763017, 24.655716985898633],[48.19986971481483, 24.656746954167845]]";
// int volume = generateRandomNumber(100, 900);
// int moisture = generateRandomNumber(0, 90);
// bool isRunning = random(0, 2);
// String deviceStatus = isRunning ? "true" : "false";
// int productionCount = generateRandomNumber(0, 1000);
// String jsonData = "{\"temperature\":" + String(temp) + ", \"humidity\":" + String(hum) +
// ",\"moisture\":" + String(moisture) + ", \"volume\":" + String(volume) +
// ",\"pressure\":" + String(pressure) + ", \"production count\":" + String(productionCount) +
// ",\"battery\":" + String(battery) + ", \"signal\":" + String(signal) +
// ",\"area\":" + String(area) +
// ",\"speed\":" + String(speed) + ", \"direction\":" + String(direction) +
// ", \"latitude\":" + String(currentLatitude) + ", \"longitude\":" + String(currentLongitude) + "}";
float temperature = generateRandomFloat(15.0, 35.0);
float humidity = generateRandomFloat(30.0, 90.0);
float waterFlow = generateRandomFloat(0.0, 100.0);
float waterLevel = generateRandomFloat(0.0, 10.0);
float pH = 14.0;
String jsonData = "{\"pH\":" + String(pH) + "}";
Serial.println(jsonData);
tb.sendTelemetryJson(jsonData.c_str());
}
void generateMovingObjectData() {
// Example implementation: Update latitude and longitude based on fixed speed and direction
// In a real scenario, you might have a more complex logic to update these values
double deltaLatitude = speed * cos(direction * PI / 180.0);
double deltaLongitude = speed * sin(direction * PI / 180.0);
// Update latitude and longitude
currentLatitude += deltaLatitude;
currentLongitude += deltaLongitude;
}
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
dhtSensor.setup(pinDht, DHTesp::DHT22);
connectToWiFi();
connectToThingsBoard();
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temp = generateRandomNumber(0, 50);
int hum = generateRandomNumber(0, 100);
Serial.println("Temperature: " + String(temp) + "°C");
Serial.println("Humidity: " + String(hum) + "%");
sendDataToThingsBoard(temp, hum);
generateMovingObjectData();
delay(3000);
}