#include <WiFi.h>
#include <PubSubClient.h>
#include <DHTesp.h>
#include <HX711.h>
const int DHT_PIN = 15;
DHTesp dht;
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = "";
const char* mqtt_server = "test.mosquitto.org"; // MQTT broker server address (you can use "mqtt.eclipse.org" as a public broker for testing)
#define DATA_PIN 15
#define CLOCK_PIN 2
HX711 hx711;
long units;
int buzzer = 13;
int statusCode;
int n = 0;
int i;
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
float weight = 0;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("Connected to MQTT broker");
client.publish("/ThinkIOT/Publish", "Welcome");
client.subscribe("/ThinkIOT/Subscribe");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
pinMode(2, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dht.setup(DHT_PIN, DHTesp::DHT22);
WiFi.mode(WIFI_STA);
hx711.begin(DATA_PIN, CLOCK_PIN);
hx711.set_scale(420.0983);
hx711.tare();
pinMode(buzzer, OUTPUT);
}
void loop() {
units = hx711.get_units();
int v = map(units, 0, 50, 0, 3500);
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected to WiFi.");
}
if (!client.connected()) {
reconnect();
}
client.loop();
// `if (digitalRead(BUTTON_PIN) == LOW) {
// // Button is pressed, take some action here //
// Add your button action here // if (digitalRead(BUTTON_PIN) == LOW) { //
// Button is pressed, take some action here //
// buttonPressed = true; // }
// else if (buttonPressed) { //
// Button was pressed, trigger an action //
// Add your button action here //
// buttonPressed = false;
// Reset the button flag // } // }
unsigned long now = millis();
if (now - lastMsg > 2000) { // Publish data command
lastMsg = now;
TempAndHumidity data = dht.getTempAndHumidity();
// Simulate weight data (replace this with your actual weight reading logic)
// weight += 0.1; // Increment the weight value
if (v > 2500) {
i = -1;
tone(buzzer, 1000);
n = 1;
} else if (v < 500) {
i = 0;
noTone(buzzer);
n = 2;
} else {
i = v;
noTone(buzzer);
n = 2;
}
Serial.print("Weight: ");
Serial.println(i);
// Convert weight to a string
String weightStr = String(i);
// Publish weight data to MQTT topic
if (client.connected()) {
client.publish("/ThinkIOT/weight", weightStr.c_str());
Serial.print("Published to MQTT: ");
Serial.println(weightStr);
} else {
Serial.println("MQTT not connected, unable to publish.");
}
}
}