I wrote a code to obtain and receive load cell data and be able to control a relay. Both of these on a web browser. This was written combing many examples and right now I've managed to clear all the errors, however, I am still facing a problem. The web browser can never be reached. This is the code:
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include <HX711_ADC.h>
TaskHandle_t Task1;
TaskHandle_t Task2;
const int HX711_dat = 18;
const int HX711_clk = 19;
HX711_ADC LoadCell(HX711_dat, HX711_clk);
const int relay_pin = 26;
const char* wifi_name = "AntanasWIFI";
const char* wifi_pass = "12345678";
void task1Code(void* pvParameters);
void task2Code(void* pvParameters);
AsyncWebServer server(80); // Create an instance of AsyncWebServer
void setup() {
Serial.begin(57600);
pinMode(relay_pin, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_name, wifi_pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
WebSerial.begin(&server);
float calibrationValue = 23.09;
LoadCell.begin();
unsigned long stabilizingtime = 2000;
boolean _tare = true;
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
} else {
LoadCell.setCalFactor(calibrationValue);
Serial.println("Startup is complete");
}
xTaskCreatePinnedToCore(
task1Code,
"Task1",
10000,
NULL,
1,
&Task1,
0);
xTaskCreatePinnedToCore(
task2Code,
"Task2",
10000,
&server, // Pass the server object as a parameter
1,
&Task2,
1);
delay(150);
}
void loop() {
// Allow the tasks to execute
delay(10);
}
void task1Code(void* pvParameters) {
static unsigned long t = 0;
static boolean newDataReady = 0;
const int serialPrintInterval = 30; // increase value to slow down serial print activity
while (1) {
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
float i = LoadCell.getData();
WebSerial.print("Load cell output value: ");
WebSerial.println(i);
newDataReady = 0;
t = millis();
}
}
// receive command from serial terminal, send 't' to initiate tare operation:
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}
// Allow the main task to execute Arduino library functions
delay(10);
}
}
void task2Code(void* pvParameters) {
AsyncWebServer* server = static_cast<AsyncWebServer*>(pvParameters);
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
String html = "<!DOCTYPE HTML>";
html += "<html><body><center><h2>ESP32 Web Server</h2>";
html += "<h3>Click to turn ON/OFF the Relay</h3>";
html += "<a href=\"/RELAYON\"><button>RELAY ON</button></a>";
html += "<a href=\"/RELAYOFF\"><button>RELAY OFF</button></a>";
html += "</center></body></html>";
request->send(200, "text/html", html);
});
server->on("/RELAYON", HTTP_GET, [](AsyncWebServerRequest* request) {
digitalWrite(relay_pin, HIGH);
request->send(200, "text/plain", "Relay turned ON");
});
server->on("/RELAYOFF", HTTP_GET, [](AsyncWebServerRequest* request) {
digitalWrite(relay_pin, LOW);
request->send(200, "text/plain", "Relay turned OFF");
});
while (1) {
// Allow the main task to execute Arduino library functions
delay(10);
}
}
The WiFi credentials are my mobile phone hotspot's info. Both my pc and esp32 are connected - no error here I believe.