an esp8266 server can be as simple as this, it reads the analogue pin every 20 sec once connected to a wifi net.
it responds with the last read value to a get request from anywhere. the requester has the appropriate web page stored locally.
using a GET request like this.
be warned when you master this stuff you may chuck all your pic/pbp stuff in the hard waste.
i'm turning everything into little servers. with ntp time keeping , ota updates no need for displays or icsp or serial ports
it make pics stuff look awkward and very limited. you can mix and merge the data from multiple sources to get one dreamed of
functionality
function readdata() {
$.get("http://yourhostname/t24")
.done(function (data) {
if (data) {
tot = (data.data/ 10);
$("#label").text( tot.toFixed(1));
}
}).fail(function () {
console.log("The was a problem retrieving the total yield");
});
}
Code:
/*
ESP8266
wemos d1
*/
#include <ESP8266WebServer.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
ESP8266WebServer server(80); // create a web server on port 80
unsigned int lastsample , samplerate=20000;
float rawdata;
void setup() {
Serial.begin(19200);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("ssid1", "pass1");
wifiMulti.addAP("ssid2", "pass2");
Serial.println("Connecting Wifi...");
while (wifiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
startServer();
}
void loop() {
if (millis() - lastsample > samplerate) {// things to do every 1 sec
rawdata = analogRead(A0);
lastsample=millis();
}
yield();
server.handleClient(); // run the server
}
void startServer() { // Start a HTTP server with a file read handler and an upload handler
server.on ("/t24", T24H);
server.onNotFound(handleNotFound); // if someone requests any other file or page, go to function 'handleNotFound'
server.begin(); // start the HTTP server
Serial.println("HTTP server started.");
}
/*__________________________________________________________SERVER_HANDLER__________________________________________________________*/
void T24H() {
char buff[10];
String gd = "{\"data\":";
dtostrf( rawdata, 3, 1, buff);
gd += buff;
gd +="}";
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-control-allow-headers", "Authorization, Content-Type,x-requested-with");
server.sendHeader("Access-control-allow-methods", "GET,PUT,HEAD");
server.sendHeader("Content-Type", " application/json");
server.send(200, "application/json", gd);
}
void handleNotFound() { // if the requested file or page doesn't exist, return a 404 not found error
server.send(404, "text/plain", "404: File Not Found");
}
Bookmarks