-->
Page 1 of 1

Nodemcu sensor polling

PostPosted: Mon Jul 01, 2019 4:27 pm
by ob2s
I have looked at countless examples, but I am not sure why I can't find one that seems so obvious (to me). If you have a simple circuit where a light/temp resistor is connected to a breadboard and nodemcu, I just want to connect to the nodemcu on the same network via tcp and poll/see/collect the value of the resistance. No cloud, no web front end, I just want to make a security-free tcp connection and get that value. I will do it from a linux box, so I could use wget or curl if the value had to be collected from a nodemcu webserver, but a simple socket connection that would just display the current value would be fantastic. I've tried to hire someone to help me, but I can't even clearly state what I need it seems. Does this seem so odd ?
Thanks

Re: Nodemcu sensor polling

PostPosted: Thu Jul 04, 2019 8:10 am
by ob2s
ob2s wrote:Does this seem so odd ?
Thanks

If it takes 3 days to obtain posting approval, then you'll probably render an answer yourself, which I did.
Image

I took the simple authenticationless webserver example with the Arduino IDE ESP8266 extension and put the value of A0 in the root of the webserver. I used the exact hardware setup in the picture above. Here is the code
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// Replace with your network credentials
const char* ssid = "MyFy";
const char* password = "farkomatic";
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
String page = "";
double Lightdata;
void setup(void){
pinMode(A0, INPUT);
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password); //begin WiFi connection
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", [](){
page = String(Lightdata); //publish the value in text
server.send(200, "text/html", page);
});
server.begin();
Serial.println("Web server started!");
}
// loop to read sensor
void loop(void){
Lightdata = analogRead(A0);
delay(1000);
server.handleClient();
}


Here is the simple way I poll the data from the nodemcu (10.1.10.8) using a linux box on the same network.

# value=`wget -q -O - http://10.1.10.8`
# echo $value
44.00
#
That is a daylight value. I can now get the light value on demand. As you were