#include "HX711.h"
#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h> // Include the MQTT library
#define DHTPIN 22
#define DHTTYPE DHT22 // Type of DHT sensor
#define LDR_PIN 33
// Virtual MQTT Broker Details
const char* mqtt_server = "wokwi-mqtt-broker";
const int mqtt_port = 1883;
const char* mqtt_username = "hajer";
const char* mqtt_password = "123";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "localhost";
WiFiClient espClient;
PubSubClient client(espClient);
HX711 scale;
DHT dht(DHTPIN, DHTTYPE);
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
typedef struct struct_message {
char a[32];
} struct_message;
struct_message myData;
esp_now_peer_info_t peerInfo;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(9600);
Serial.println("HX710B Demo with HX711 Library");
Serial.println("Initializing the scale");
pinMode(LDR_PIN, INPUT);
scale.begin(26, 27);
scale.set_scale(2280.f);
scale.tare();
dht.begin();
setup_wifi(); // Connect to WiFi
client.setServer(mqtt_server, mqtt_port); // Set MQTT broker
// Optional: set MQTT credentials
// client.setCredentials(mqtt_username, mqtt_password);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (digitalRead(LDR_PIN) == LOW) {
Serial.println("Light in cargo");
} else {
Serial.println("Dark");
}
delay(100);
float load = scale.get_units(10);
Serial.print("Load: ");
Serial.print(load, 1);
Serial.println(" Kg");
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
sprintf(myData.a, "%.2f", temperature); // Changed to two decimal places for temperature
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
delay(5000);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP32Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}