I wanted to make a blink counter for an LED placed on an electricity meter using an esp8266 and an LM393 light sensor. I have written a program that counts the blinks and periodically sends the data to a local server to save it in a database (sql). So far the only way I have to save the data is with Apache and MySql. Unfortunately sending this data to the server causes the loop to stop for a while, thus skipping some blinks. As such esp doesn't have any multithreading so I don't know how to bite on this topic. Perhaps someone knows of, has had a similar problem and can help me? I also have a problem with the wifi connection itself, when I use the ESP8266WiFi.h library the computer kind of loses range after a while, with the ESP8266WiFiMulti.h library there is no such problem, but I don't know if it's just that esp doesn't reconnect automatically (this probably also slows down the program).
.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "time.h"
// WiFi network
const char* ssid = "";
const char* password = "";
// API
const char* serverIP = "192.168.43.1";
const int serverPort = 8080;
const char* serverPath = "/energy.php";
// NTP Server
const char* ntpServer = "tempus1.gum.gov.pl";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
// Timer for updating the database
unsigned long previousMillis = 0;
const long interval = 10000;//60000;
// Inpulse Counter
const int analogInPin = A0;
bool was_dark = false;
int counter = 0;
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return(0);
}
time(&now);
return now;
}
void sendToServer() {
Serial.println("[HTTP] begin...\n");
WiFiClient client;
HTTPClient http;
String request = serverPath;
request += "?date=" + String(getTime());
request += "&value=" + String(counter++);
Serial.print("Generating request: ");
Serial.println(request);
if (!http.begin(client, serverIP, serverPort, request)) {
Serial.println("Connection failed");
Serial.print("Wifi status: ");
Serial.println(WiFi.status());
return;
}
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
counter = 0;
return;
}
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void loop() {
Serial.println(WiFi.status());
int value = analogRead(analogInPin);
if (value > 900) {
was_dark = true;
return;
}
if (value > 90 || !was_dark) {
return;
}
counter++;
was_dark = false;
Serial.print("Counter: ");
Serial.println(counter);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sendToServer();
}
}