Hi everyone, I created a people counter laser barrier where at each step a counter increases in number, this code is inserted into the esp8266 01 through the pin GPIO2, through the example code helloserver of esp8266webserver I would like to be able to visualize in real time the counter , at each increment of the counter it must be updated on the page created, I don't know how to display this counter, can someone help me? this is my code, where do I insert the counter to display it on the server's web page?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "fifiddu"
#define STAPSK "xxxxxxx"
#endif
const int Inp2=2;//to the GPio2 esp8266
int counter;// the counter of the laser barrier
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
digitalWrite(led, 0);
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\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 += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void) {
pinMode(Inp2,INPUT);
digitalWrite(Inp2,HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
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());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
if (digitalRead(Inp2)==LOW) {DealWithSwitchPress();};
};
void DealWithSwitchPress()
{
delay(100);
counter++;
Serial.println(counter);
while (digitalRead(Inp2)==LOW){;};//do nothing while waiting
delay(100);
server.handleClient();
MDNS.update();
}
First please format your code with Ctrl-T,
secondwhile (digitalRead(Inp2)==LOW){;};//do nothing while waitingthis will cause a wdt reset if the loop is longer than 2.5 seconds.
third, have you considered just doing this ?
Thanks Deva for the answer, could you bring back all the working code instead of the small piece of code? I'm not very practical to enter the only code you posted, another interesting thing would be to be able to view a graph of the statistics where I display the time and date and the percentage of people entering to make a statistic and save it , however I understand that I ask too much, I just need to be able to view the counter on the server's web page.
Ok so i removed a couple of 'stray' ; 's from the code (this is not Java ! ) moved a few things to loop() and moved functions around for cosmetic reasons and commented out which we don't really need.
another interesting thing would be to be able to view a graph of the statistics where I display the time and date and the percentage of people entering to make a statistic and save it , however I understand that I ask too much
yep, gigs and collabs would be the place for it.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "fifiddu"
#define STAPSK "xxxxxxx"
#endif
const int Inp2 = 2; //to the GPio2 esp8266
int counter;// the counter of the laser barrier
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = 13;
void setup(void) {
pinMode(Inp2, INPUT);
digitalWrite(Inp2, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
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());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
// having another way to do something similar within the code
/*server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});*/
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient(); // both these lines should be in loop()
// MDNS.update(); // and this one is not really required
if (digitalRead(Inp2) == LOW) {
DealWithSwitchPress();
}
}
void DealWithSwitchPress() {
delay(100);
counter++;
Serial.println(counter);
while (digitalRead(Inp2) == LOW) {
yield(); // prevents wdt reset
}
delay(100);
}
// these are the callback functions
/*void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
digitalWrite(led, 0);
}*/
void handleRoot() {
digitalWrite(led, 1);
String s="Counted ";
s+=String(counter, DEC);
s+=" people";
server.send(200, "text/plain", s);
digitalWrite(led, 0);
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\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 += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
personally i never really bother about the 'not found' if it's not found it isn't no need to tell people that you can't find it really.
works, but there is a problem .... until the laser barrier is interrupted, the web page cannot be reached, when the barrier is interrupted the counter marks the passage and is displayed on the web page, but as soon as the beam does not is more hindered then the server's web page cannot be reached.
fifiddu:
until the laser barrier is interrupted, the web page cannot be reached, when the barrier is interrupted the counter marks the passage and is displayed on the web page, but as soon as the beam does not is more hindered then the server's web page cannot be reached.
if (digitalRead(Inp2) == LOW) {
DealWithSwitchPress();
}
is it possible the input is active 'HIGH' iow if the laser is not interrupted is digitalRead(Inp2) LOW or HIGH ?
as 'INPUT_PULLUP' ? (old method ?)
If it's a photo-electric transistor it would make sense for it to go "LOW" when hit with the laser, anyway i just had another thought, maybe you have to 'un-comment' // MDNS.update(); (i'm using a modified version of <ESP8266mDNS.h> myself.
I apologize for the late reply, but I was in the hospital and therefore unable to try the code, as it is possible to try, anyway always thanks for your cooperation.