#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "HX711.h"
#include <WiFi.h>
#include <ThingSpeak.h>
/*
Exemple use case for smart inventory management. A microntroller is connected to
a pressure sensor measuring the weight of the stock. The weight samples are
then sent to a ThingSpeak channel via wifi. The samples can then be interpreted by
a custom AI model to analyse and predict how to manage the stocks (W.I.P.).
*/
// OLED display size
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// OLED reset pin (not used)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// HX711 pins
#define LOADCELL_DOUT_PIN 23
#define LOADCELL_SCK_PIN 22
// Create an instance of HX711
HX711 scale;
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak settings
unsigned long channelID = 2647402; // Replace with your ThingSpeak Channel ID
const char* writeAPIKey = "04INH9HZUOGOKYP1"; // Replace with your ThingSpeak Write API Key
WiFiClient client;
// Calibration factor (example)
float calibration_factor = 21000.0 / 50.0; // Adjust this value to calibrate the scale
void setup() {
Serial.begin(115200);
Serial.println("HX711 calibration");
// Initialize HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
// Tare the scale (set to zero with no load)
Serial.println("Taring the scale...");
scale.set_scale(); // Reset scale to default with no calibration
scale.tare(); // Tare the scale to zero
// Set the initial calibration factor
scale.set_scale(calibration_factor);
// Initialize OLED display (use correct SDA and SCL pins)
Wire.begin(19, 21); // SDA = GPIO 19, SCL = GPIO 21
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the display buffer
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Connecting...");
display.display();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
display.clearDisplay();
display.setCursor(0, 0);
display.print("WiFi Connected");
display.display();
delay(2000);
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Check if HX711 is ready
if (scale.is_ready()) {
// Read the weight (with calibration factor)
float weight = scale.get_units(10); // Average of 10 readings
Serial.print("Weight (kg): ");
Serial.println(weight, 2); // Display weight with 2 decimal places
// Send the weight data to ThingSpeak
ThingSpeak.setField(1, weight); // Set field 1 with weight data
// Write the data to ThingSpeak
int x = ThingSpeak.writeFields(channelID, writeAPIKey);
if (x == 200) {
Serial.println("Data sent to ThingSpeak.");
} else {
Serial.println("Error sending data to ThingSpeak: " + String(x));
}
// Display weight on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.print("Weight: ");
display.println(weight, 2);
display.display();
} else {
Serial.println("Waiting for HX711 to be ready...");
}
delay(20000); // Send data every 20 seconds
}