/*
* Author:IHSHANA THAHSIN
* Project: RETAIL SHELF MONITORING
* Description:This project integrates ESP32 and STM32 microcontrollers
* with load cells to monitor the weight of products on shelves.
* The ESP32 handles data acquisition and communication, while
* the STM32 updates an LCD with the current weight.
*/
#include <WiFi.h>
#include <ThingSpeak.h>
#include <HX711.h>
#include <LiquidCrystal_I2C.h>
#define LED 2
#define LED2 4
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int LOADCELL_DOUT_PIN = 15;
const int LOADCELL_SCK_PIN = 2;
const float calibration_factor = 100.0;
HX711 scale;
long units;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber = 2279390;
const char * myWriteAPIKey = "DMV9RHKW37I550XH";
int statusCode;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
Serial.println("Hello, ESP32!");
lcd.init();
lcd.backlight();
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
// Generate a random weight value between 0 and 5 kg
float simulated_weight = random(0, 5000) / 1000.0; // Random weight between 0 and 5 kg
// LED control logic
digitalWrite(LED, HIGH);
digitalWrite(LED2, LOW);
delay(500);
digitalWrite(LED, LOW);
digitalWrite(LED2, HIGH);
delay(500);
// Attempt to connect to WiFi if not connected
if(WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(1000);
}
Serial.println("\nConnected.");
}
// Print the simulated weight on the LCD and serial monitor
Serial.print("Weight: ");
lcd.setCursor(2, 0);
lcd.print("Weight :");
lcd.print(simulated_weight);
lcd.print(" kg");
Serial.print(simulated_weight);
Serial.println(" kg");
delay(1000);
// Update ThingSpeak channel with the simulated weight data
ThingSpeak.setField(1, simulated_weight);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(statusCode) { // Successful writing code
Serial.println("Channel update successful.");
} else {
Serial.println("Problem Writing data. HTTP error code :" + String(statusCode));
}
delay(1000);
}