Hi all!
Thanks for your time in advanced, any guidance and suggestions are appreciated.
I’m a self taught and thanks to many like you with their patience and guidance have learned to use Arduino IDE.
For this project I wanted to use canvas gauges to display data from an analog pot attached to the NodeMcu. After lots of reading from different post I was able to make it work!
Will post the sketch and picture of the gauge when I get home today.
So, since that’s done. Now I would like to get data from an RS485 Modbus RTU device and display it on the gauge. Humm but I have no clue on how to store that data on an array then send it to the server then display it on the gauge.
Yup, JavaScript and html will be used on the server side.
here is the link to the Video.
Analog read to gauge
the Ino file:
const char* htmlfile = "/index.html";
//WiFi Connection configuration
const char *ssid = "My Wifi";
const char *password = "123456789";
ESP8266WebServer server(80);
void handleADC(){
int a = analogRead(A0);
a = map(a,0,1023,0,100);
String adc = String(a);
Serial.println(adc);
server.send(200, "text/plane",adc); //sends data to server
}
void handleRoot(){
server.sendHeader("Location", "/index.html",true); //Redirect to our html web page
server.send(302, "text/plane","");
}
void handleWebRequests(){
if(loadFromSpiffs(server.uri())) return;
String message = "File Not Detected\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
Serial.println(message);
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
//Initialize File System
SPIFFS.begin();
Serial.println("File System Initialized");
Serial.print("Setting AP (Access Point)…");
Serial.println();
//Connect to wifi Network
WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP(ssid, password); //Start HOTspot
//If connection successful show IP address in serial monitor
Serial.print("Connected to: ");
Serial.println(ssid);
IPAddress IP = WiFi.softAPIP();
Serial.print("Server IP address: ");
Serial.println(IP);
//Initialize Webserver
server.on("/",handleRoot);
server.on("/getADC",handleADC); //Reads ADC function is called from out index.html
server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
bool loadFromSpiffs(String path){
String dataType = "text/plain";
if(path.endsWith("/")) path += "index.htm";
if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
else if(path.endsWith(".html")) dataType = "text/html";
else if(path.endsWith(".htm")) dataType = "text/html";
else if(path.endsWith(".css")) dataType = "text/css";
else if(path.endsWith(".js")) dataType = "application/javascript";
else if(path.endsWith(".png")) dataType = "image/png";
else if(path.endsWith(".gif")) dataType = "image/gif";
else if(path.endsWith(".jpg")) dataType = "image/jpeg";
else if(path.endsWith(".ico")) dataType = "image/x-icon";
else if(path.endsWith(".xml")) dataType = "text/xml";
else if(path.endsWith(".pdf")) dataType = "application/pdf";
else if(path.endsWith(".zip")) dataType = "application/zip";
File dataFile = SPIFFS.open(path.c_str(), "r");
if (server.hasArg("download")) dataType = "application/octet-stream";
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
}
dataFile.close();
return true;
}
this is where the analog reading is received and sent to the server:
void handleADC(){
int a = analogRead(A0);
a = map(a,0,1023,0,100);
String adc = String(a);
Serial.println(adc);
server.send(200, "text/plane",adc); //sends data to server
}