Problems receiving Data from my NodeMCU ESP12 in Server Mode
Posted: Sun May 13, 2018 11:27 am
Hello there,
recently I came across the nodeMCU board with the ESP8266 integrated in it. So I wanted to implement it in one of my projects.
I have a CO2 Sensor from which I get a PWM signal. I converted the PWM Signal to a CO2 concentration value from type String.
In addition I watched tutorials about setting the ESP8266 up to a webserver. The minimal programm for setting up a webserver was working, but when I try to implement my function in the standard code I can't connect to the network. Does someone have some ideas how to implement my code in the Server.
Here is my program for the sensor:
int aPin = 0;
long unsigned val, t1, t2, t3, htime, ltime, Cppm;
void loop()
{
server.handleClient();
val = analogRead(aPin);
while (val < 200)
{
delay(0.1);
val = analogRead(aPin);
}
t1 = millis();
while (val >= 200)
{
delay(0.1);
val = analogRead(aPin);
}
t2 = millis();
htime = t2 - t1;
while (val <= 200)
{
delay(0.1);
val = analogRead(aPin);
}
t3 = millis();
ltime = t3 - t2;
Cppm = 2000 * (htime - 2) / (htime + ltime - 4);
Serial.println(Cppm);
String Cppm = Cppm;
}
And that is the program I used to put my function in:
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" +
"\r\n";
return htmlPage;
}
void loop()
{
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
delay(100); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
}
}
recently I came across the nodeMCU board with the ESP8266 integrated in it. So I wanted to implement it in one of my projects.
I have a CO2 Sensor from which I get a PWM signal. I converted the PWM Signal to a CO2 concentration value from type String.
In addition I watched tutorials about setting the ESP8266 up to a webserver. The minimal programm for setting up a webserver was working, but when I try to implement my function in the standard code I can't connect to the network. Does someone have some ideas how to implement my code in the Server.
Here is my program for the sensor:
int aPin = 0;
long unsigned val, t1, t2, t3, htime, ltime, Cppm;
void loop()
{
server.handleClient();
val = analogRead(aPin);
while (val < 200)
{
delay(0.1);
val = analogRead(aPin);
}
t1 = millis();
while (val >= 200)
{
delay(0.1);
val = analogRead(aPin);
}
t2 = millis();
htime = t2 - t1;
while (val <= 200)
{
delay(0.1);
val = analogRead(aPin);
}
t3 = millis();
ltime = t3 - t2;
Cppm = 2000 * (htime - 2) / (htime + ltime - 4);
Serial.println(Cppm);
String Cppm = Cppm;
}
And that is the program I used to put my function in:
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" +
"\r\n";
return htmlPage;
}
void loop()
{
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
delay(100); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
}
}