logo elektroda
logo elektroda
X
logo elektroda

ESP8266 and LM393 for LED blink counting: a problem with WiFi and the database

gomolec 639 4
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 21022405
    gomolec
    Level 5  
    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();
      }
    }
    
    .
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • #2 21022486
    krzbor
    Level 27  
    Maybe don't do an active wait for the GET result to start with. See how I did it: Link .
  • ADVERTISEMENT
  • #3 21024323
    JacekCz
    Level 42  
    Since it's over HTTP, MySQL shouldn't exist in your thoughts at all when you think about code on the uK

    The place to worry about this is the server part (Python, PHP, Java)
  • ADVERTISEMENT
  • #5 21032285
    xury
    Automation specialist
    Switch to ESP32. You have two cores and an RTOS.
ADVERTISEMENT