#include <Arduino.h>
/////////////////////////////////
// LCD Libraries
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
////////////////////////////////
// LCD Object Configuration
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void LcdStatusCheck( int LCD_COLS, int LCD_ROWS) {
int status;
//LCD Start
status = lcd.begin(LCD_COLS, LCD_ROWS);
// Check if LCD is working
if (status) // non zero status means it was unsuccesful
{
hd44780::fatalError(status); // does not return
}
// Print a message to the LCD
lcd.setCursor(0, 0); //Set cursor to character 2 on line 0
lcd.print("Initiated");
delay(2000);
}
////////////////////////////////
// WiFi Library
#include <WiFi.h>
#include <HTTPClient.h>
// WiFi Credentials
//* Set these to your desired credentials. */
const char *ssid = "Wokwi-Guest"; //ENTER YOUR WIFI SETTINGS
const char *password = "";
WiFiClient client;
// SetUp Function: WifiSetup()
///////////////////////////////
void WifiSetup() {
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(50);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
int red_pin=18;
int green_pin=5;
void PinModeSetup() {
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
}
#include "HX711.h"
HX711 scale;
void HX711_setup(){
Serial.println("HX710B Demo with HX711 Library");
Serial.println("Initializing the scale");
// parameter "gain" is ommited; the default value 128 is used by the library
// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0
scale.begin(A1, A0);
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //start Serial in case we need to ln debugging info
LcdStatusCheck(LCD_COLS, LCD_ROWS); // Start LCD
PinModeSetup();
//WifiSetup();
HX711_setup();
lcd.clear();
lcd.print("Inventory System");
}
float current,temperature,cycle_time;
void loop() {
// put your main code here, to run repeatedly:
int current_sensor_pin = 34;
pinMode(current_sensor_pin, INPUT);
current = AnalogSensorRead(current_sensor_pin,0,100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Current :");
lcd.setCursor(0,1);
lcd.print(current);
delay(1000);
// put your main code here, to run repeatedly:
int cycle_time_sensor_pin = 32;
pinMode(cycle_time_sensor_pin, INPUT);
cycle_time = AnalogSensorRead(cycle_time_sensor_pin,0,100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Cycle Time :");
lcd.setCursor(0,1);
lcd.print(cycle_time);
delay(1000);
// lcd.setCursor(0,1);
// lcd.print("current :");
// current = AnalogSensorRead(current_sensor_pin,0,100);
// lcd.print(current);
// delay(1000);
// SendRecords();
// if (temperature>70) {
// digitalWrite(red_pin, HIGH);
// digitalWrite(green_pin,LOW);
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Warning: Temp HIGH");
// lcd.setCursor(0,1);
// lcd.print("Aerator On");
// delay(1000);
// }
// if (temperature>40 && temperature<70 && current>40 && current<65){
// digitalWrite(red_pin, LOW);
// digitalWrite(green_pin,HIGH);
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Great System");
// delay(1000);
// }
// if (temperature<40){
// digitalWrite(red_pin, HIGH);
// digitalWrite(green_pin,LOW);
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Warning: Temp LOW");
// lcd.setCursor(0,1);
// lcd.print(" Alerting User");
// delay(1000);
// }
// if (current <40){
// digitalWrite(red_pin, HIGH);
// digitalWrite(green_pin,LOW);
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Warning: Pres. LOW");
// lcd.setCursor(0,1);
// lcd.print(" Sprinkler ON");
// delay(1000);
// }
// if (current>65){
// digitalWrite(red_pin, HIGH);
// digitalWrite(green_pin,LOW);
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Warning: Pres. LOW");
// lcd.setCursor(0,1);
// lcd.print(" Sprinkler OFF");
// delay(1000);
// }
// sendTelegram();
}
//
float weight;
void weight_read(){
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
weight = scale.get_units(10);
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
}
float AnalogSensorRead(int pin, int minX, int maxX) {
// Read Raw Value
float level = analogRead(pin);
Serial.print("Raw level: ");
Serial.println(level);
// Map Raw Value to Required Units
level = map(level, 0, 4095, minX, maxX);
Serial.print("Map level: ");
Serial.println(level);
return level;
}
void SendRecords() {
Serial.print("connecting to ");
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("https://compost.carel.co.zw/api/records");
//http.begin("http://192.168.1.12/test.html"); //HTTP
Serial.print("[HTTP] POST...\n");
String postData;
//Post Data
postData = "sensor_1=" + String(temperature)+ "&sensor_2=" + String(current);
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
// start connection and send HTTP header
auto httpCode = http.POST(postData); //Send the request
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Server Response");
lcd.setCursor(0, 1);
lcd.print("OK: ");
lcd.print(httpCode);
delay(1000);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Server Response");
lcd.setCursor(0, 1);
lcd.print("BAD: ");
lcd.print(httpCode);
delay(1000);
}
http.end();
delay(250);
}
void SendActuator() {
Serial.print("connecting to ");
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("https://compost.carel.co.zw/api/records");
//http.begin("http://192.168.1.12/test.html"); //HTTP
Serial.print("[HTTP] POST...\n");
String postData;
//Post Data
postData = "temperature=" + String(temperature)+ "¤t=" + String(current);
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
// start connection and send HTTP header
auto httpCode = http.POST(postData); //Send the request
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Server Response");
lcd.setCursor(0, 1);
lcd.print("OK: ");
lcd.print(httpCode);
delay(1000);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Server Response");
lcd.setCursor(0, 1);
lcd.print("BAD: ");
lcd.print(httpCode);
delay(1000);
}
http.end();
delay(250);
}
Cycle Time
Pressure
Sensor