#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 "lbC0VCNnzFr6hUOK6hUT"
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 generateFloat2(float min, float max) {
// std::random_device rd; // obtain a random number generator
// std::mt19937 gen(rd()); // seed the generator with a random device
// std::uniform_real_distribution<> dis(min, max); // define the range for random floats
// return dis(gen); // generate a random float within the specified range
// }
float generateRandomFloat() {
int randomValue = random(500, 1001);
return map(randomValue, 500, 1000, 0.5, 1.0);
}
float generateRandomFloat(float min, float max) {
int randomValue = random(0, 1001); // Generate random integer between 0 and 1000
return map(randomValue, 0, 1000, min, max);
}
int generateRandomNumber(int min, int max) {
return random(min, max + 1);
}
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) + "}";
String machineName = "Gasket_beta";
String machineId = "b1s2y3f6g5h6j9k8g9";
String latitude = "19.186475875346186";
String longitude = "72.95680780307147";
String xPos = "0.23";
String yPos = "0.24";
String operatorName = "Rohit";
String operatorId = "aITs8945";
int productionId = 452451254;
String partId = "geps5v9hjkhg4fsddg";
int standardCycleTime = 3;
int partPerCycle = generateRandomNumber(10,15);
int noOfCycle = generateRandomNumber(5,10);
int rejectionQuantity = generateRandomNumber(0,3);
float availability = generateRandomNumber(50,100);
float performance = generateRandomNumber(50,100);
int activePowerConsumption = generateRandomNumber(1, 10);
float machineTemprature = generateRandomFloat(50.0,70.0);
int pressure = generateRandomNumber(410, 430);
int machineVibration = generateRandomNumber(220, 230);
int energyConsumption = generateRandomNumber(1, 10);
String area = "[[19.186432275614465, 72.9565229949931],[19.186411718007538, 72.9569090045943],[19.18705818821269, 72.9569071722072],[19.187059835240884, 72.95648698920218]]";
float productionQuantity = noOfCycle * partPerCycle;
float quality = ((productionQuantity - rejectionQuantity)/productionQuantity)*100;
int oee = ( availability * performance * quality ) / 10000;
// int lineVoltage_VYB = generateRandomNumber(410, 430);
// int lineVoltage_VRB = generateRandomNumber(410, 430);
// int phaseVoltage_VRN = generateRandomNumber(220, 230);
// int phaseVoltage_VYN = generateRandomNumber(220, 230);
// int phaseVoltage_VBN = generateRandomNumber(220, 230);
// int lineCurrent_IY = generateRandomNumber(50, 70);
// int lineCurrent_IB = generateRandomNumber(50, 70);
// int lineCurrent_IN = generateRandomNumber(0, 3);
// int frequency = 50;
// Serial.println(powerFactor);
String jsonData = "{\"machineName\":" + String(machineName) + ", \"machineId\":" + String(machineId) +
",\"latitude\":" + String(latitude) + ", \"longitude\":" + String(longitude) +
",\"operatorName\":" + String(operatorName) + ", \"operatorId\":" + String(operatorId) +
",\"productionId\":" + String(productionId) + ", \"partId\":" + String(partId) +
",\"standardCycleTime\":" + String(standardCycleTime) + ", \"partPerCycle\":" + String(partPerCycle) +
",\"noOfCycle\":" + String(noOfCycle) + ", \"rejectionQuantity\":" + String(rejectionQuantity) +
",\"availability\":" + String(availability) + ",\"performance\":" + String(performance) +
",\"machineTemprature\":" + String(machineTemprature) + ", \"activePowerConsumption\":" + String(activePowerConsumption) +
", \"energyConsumption\":" + String(energyConsumption) + ", \"pressure\":" + String(pressure) +
", \"productionQuantity\":" + String(productionQuantity) + ", \"quality\":" + String(quality) +
", \"oee\":" + String(oee) + ", \"xPos\":" + String(xPos) + ", \"yPos\":" + String(yPos) +
", \"area\":" + String(area) + ", \"machineVibration\":" + String(machineVibration) + "}";
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);
}