/*
Lab Test Jan 2024
Name: Min Khant Maw Soe
Class: DEE1
Date: 19/1/24
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include "HX711.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;
float calibration_factor = 420; // this calibration factor is adjusted according to my load cell
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_2706308g"; // yourStudentID must be unique
int PORTNUM = 1883;
long previousMillisA = 0;
int intervalA = 1000;
int GLED = 18;
int RLED = 23;
int YLED = 19;
long previousMillisY = 0;
int intervalY = 2000;
int LEDStateY = 1;
void setup_wifi()
{
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED )
{
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Given IP by the router to ESP32 is ");
Serial.println(WiFi.localIP());
}
void MQTTSubscribe()
{
// Subsccribe to your topics here
client.subscribe("ADC/LT51G/2706308g_weight");
}
void connectMQTT()
{
while(!client.connected() )
{
Serial.println("Connecting to MQTT ...");
if (client.connect(espClientName) ) //, mqttUser, mqttPassword) )
{
Serial.println("Connected");
MQTTSubscribe();
}
else
{
Serial.print("Failed with state ");
Serial.print(client.state() );
delay(2000);
}
}
}
void callback(String topic, byte* payload, unsigned int length)
{
String messageTemp;
//Serial.print("Message received in topic: ");
//Serial.print(topic);
//Serial.print(" length is: ");
//Serial.println(length);
//Serial.print("Data received from broker: ");
for (int i = 0; i<length; i++)
{
//Serial.print( (char)payload[i] );
messageTemp += (char)payload[i];
}
// Handle your topics and payload here
// Example
if (topic == "ADC/LT51G/2706308g_weight")
{
// Temperature
if (messageTemp == "Red")
{
digitalWrite(RLED, LOW);
}
else
{
digitalWrite(RLED, HIGH);
}
if (messageTemp == "Yellow")
{
digitalWrite(YLED, LOW);
}
else
{
digitalWrite(YLED, HIGH);
}
if (messageTemp == "Green")
{
digitalWrite(GLED, LOW);
}
else
{
digitalWrite(GLED, HIGH);
}
}
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup()
{
Serial.begin(9600);
delay(5000);
pinMode(YLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //Adjust to this calibration factor
scale.tare();
setup_wifi();
setup_MQTT();
}
void loop()
{
long currentMillisA = millis();
float weight;
if (currentMillisA - previousMillisA >= intervalA)
{
previousMillisA = currentMillisA;
weight = scale.get_units();
Serial.print("Weight in kg: ");
Serial.println(String(weight,1));
Serial.println("---");
// weight
if (weight >= 2.0 && weight <= 4.0){
// Publish the analog value to the specified MQTT topic
String mqttTopic = "ADC/LT51G/2706308g_weight";
String payload = String("Yellow");
client.publish(mqttTopic.c_str(), payload.c_str());
}
else if (weight < 2.0)
{
// Publish the analog value to the specified MQTT topic
String mqttTopic = "ADC/LT51G/2706308g_weight";
String payload = String("Green");
client.publish(mqttTopic.c_str(), payload.c_str());
}
else if (weight > 4.0)
{
// Publish the analog value to the specified MQTT topic
String mqttTopic = "ADC/LT51G/2706308g_weight";
String payload = String("Red");
client.publish(mqttTopic.c_str(), payload.c_str());
}
}
if ( !client.connected() )
{
connectMQTT();
}
client.loop();
}