Hi,
I'm trying to create an automated sorter by weight with ESP32, load cell and two servos. With open code, not commercial.
I'd like to operate it via web page & determine some of the key parameters there.
Issue: webpage not accessible, port 80 closed.
The code compiles & uploads to ESP32.
It connects to WIFI & obtains IP address.
The website however is not accesible, network scanner does not even show port 80 as open. Tried looking online, asked chat gpt, all this probably helped, but did not resolve the issue.
My coding skills are very limited, I've put this code together self learning & combining some codes....over nights across last week.
Any advice is much appreciated, though if this turns out complex to fix I will likely need "spoon feeding". Still dreaming it only requires a simple fix...
#include <ESP32Servo.h>
#include <ESPAsyncWebServer.h>
#include <HX711.h>
#include <WiFi.h>
#include <AsyncTCP.h>
// Define pins for HX711 and servos
#define DOUT_PIN 23
#define CLK_PIN 22
// Create an instance of the HX711 class for the load cell
HX711 scale;
// Initialize variables for weight ranges and item counts
float range1_min = 0;
float range1_max = 0;
float range2_min = 0;
float range2_max = 0;
float range3_min = 0;
float range3_max = 0;
int count_range1 = 0;
int count_range2 = 0;
int count_range3 = 0;
int sorting = 0;
int reset = 0;
unsigned long previousMillis = 0; // variable to store last update time
const long interval = 200; // update interval in milliseconds
Servo myservo1;
Servo myservo2;
const int SERVO1_PIN = 26;
const int SERVO2_PIN = 27;
//const int BUTTON_PIN = 39;
// Replace with your network credentials
const char* ssid = "your SSID";
const char* password = "your PASSWORD";
void setup() {
// Set initial weight ranges
range1_min = 0;
range1_max = 33.33;
range2_min = 33.34;
range2_max = 66.67;
range3_min = 66.68;
range3_max = 100;
Serial.begin(115200);
scale.begin(DOUT_PIN, CLK_PIN);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Initialize servos
myservo1.attach(SERVO1_PIN);
myservo2.attach(SERVO2_PIN);
// Initialize scale and set calibration factor
scale.set_scale();
scale.tare();
scale.set_scale(2280.0);
// Initialize web server
AsyncWebServer server(80);
server.on("/", HTTP_GET, handleRoot);
server.begin();
}
void handleRoot(AsyncWebServerRequest *request) {
String html = "<html><body><h1>Weight Sorting Machine</h1>";
html += "<h1>Weight: %d g</h1><form method='post'>";
html += "<p>Weight Ranges:</p>";
html += "<form method='post' action='/ranges'>";
html += "<label>Range 1: </label>";
html += "<input type='text' name='range1_min' value='" + String(range1_min) + "' placeholder='min'>";
html += "<input type='text' name='range1_max' value='" + String(range1_max) + "' placeholder='max'><br>";
html += "<label>Range 2: </label>";
html += "<input type='text' name='range2_min' value='" + String(range2_min) + "' placeholder='min'>";
html += "<input type='text' name='range2_max' value='" + String(range2_max) + "' placeholder='max'><br>";
html += "<label>Range 3: </label>";
html += "<input type='text' name='range3_min' value='" + String(range3_min) + "' placeholder='min'>";
html += "<input type='text' name='range3_max' value='" + String(range3_max) + "' placeholder='max'><br>";
html += "<input type='submit' name='save_ranges' value='Save'>";
html += "</form>";
html += "<p>Counts:</p>";
html += "<p>Range 1: " + String(count_range1) + "</p>";
html += "<p>Range 2: " + String(count_range2) + "</p>";
html += "<p>Range 3: " + String(count_range3) + "</p>";
html += "<form method='post' action='/reset'>";
html += "<input type='submit' name='reset' value='Reset'>";
html += "</form>";
html += "<form method='post' action='/calibrate'>";
html += "<input type='submit' name='calibrate' value='Calibrate'>";
html += "</form>";
html += "<form method='post' action='/start'>";
html += "<input type='submit' name='start' value='Start Sorting'>";
html += "</form>";
html += "<form method='post' action='/stop'>";
html += "<input type='submit' name='stop' value='Stop Sorting'>";
html += "</form>";
html += "</body></html>";
char buf[256];
unsigned long currentMillis = millis(); // get the current time
if (currentMillis - previousMillis >= interval) { // if the update interval has passed
previousMillis = currentMillis; // save the current time
int weight = scale.get_units();
String html_with_weight = html;
html_with_weight.replace("%d", String(weight));
strcpy(buf, html_with_weight.c_str());
request->send(200, "text/html", buf);
} else { // if the update interval has not passed yet
strcpy(buf, html.c_str()); // send the same HTML as before
request->send(200, "text/html", buf);}
if (request->hasParam("start")) {
sorting = 1;
request->send(200, "text/html", buf);
return;
}
if (request->hasParam("stop")) {
sorting = 0;
request->send(200, "text/html", buf);
return;
}
if (request->hasParam("reset")) {
reset = 1;
request->send(200, "text/html", buf);
return;
}
}
void loop() {
// Wait for button press to start sorting
while (reset == 1) {
delay(100);
// Reset item counts
count_range1 = 0;
count_range2 = 0;
count_range3 = 0;
reset = 0;
}
// Sort items until button is released
while (sorting == 1) {
// Move servo1 to load item
myservo1.write(90);
delay(300);
myservo1.write(80);
delay(1000);
// Read weight and assign to range
float weight = scale.get_units(5);
Serial.print("Weight: ");
Serial.println(weight);
if (weight >= range1_min && weight <= range1_max) {
myservo2.write(0);
count_range1++;
} else if (weight >= range2_min && weight <= range2_max) {
myservo2.write(90);
count_range2++;
} else if (weight >= range3_min && weight <= range3_max) {
myservo2.write(180);
count_range3++;
}
// Wait for item to be removed
myservo1.write(180);
delay(1000);
}
// Move servos to starting position
myservo1.write(0);
myservo2.write(0);
}
// Handle weight range update request
void handleRanges(AsyncWebServerRequest *request) {
range1_min = request->getParam("range1_min")->value().toFloat();
range1_max = request->getParam("range1_max")->value().toFloat();
range2_min = request->getParam("range2_min")->value().toFloat();
range2_max = request->getParam("range2_max")->value().toFloat();
range3_min = request->getParam("range3_min")->value().toFloat();
range3_max = request->getParam("range3_max")->value().toFloat();
request->redirect("/");
}