Technická podpora projektu - hlásenie SW problémov, bugov: martinius96@gmail.com
Knižnice pre projekt - dostupné na Githube: Knižnice pre hladinomer
  • Obsah priečinka /src/ po extrahovaní .zip archívu nakopírovať do: C:/Users/[User]/Dokumenty/Arduino/libraries - Windows 10


  • Zdrojové kódy pre projekt s pripojením na túto webovú lokalitu - dostupné na Githube - Od 30. Júla dostupné sketche pre ULTRA LOW POWER mód pre ESP8266, ESP32, deep sleep. - Pozri schému zapojenia pre danú revíziu: Programy pre mikrokontroléry (Tester, HTTP, HTTPS spojenie)
    Pri ULP móde pre ESP8266 je nutné pri nahrávaní programu odpojiť prepojku medzi GPIO16 (D0) a RST! Po nahratí programu je nutné dať prepojku späť a vykonať manuálny reset! Inak nebude prebudenie WAKE signálom fungovať!
    Zdrojové kódy pre ESP8266 - HTTPS sú navrhnuté pre verziu Arduino Core 3.0.2 (posledná stable verzia - December 2021)!

    HTTP - Arduino + Ethernet / ESP8266 / ESP32 - StandBy


    Programové implementácie pre odosielanie údajov na toto webové rozhranie.
  • Implementácie pre všetky tri druhy kompatibilných mikrokontrólerov sú obsiahnuté v jednom zdrojovom kóde, na základe vybratej dosky v Arduino IDE sa kompiluje tá časť programu, ktorá prislúcha danej doske.
  • Pre Arduino je natívne zvolené DHCP (s možnosťou odkomentovania častí so statickou IPv4 adresou), defaultne zvolený Ethernet shield Wiznet W5100 (pre W5500 / ENC28J60) nutné v zdrojovom kóde odkomentovať knižnicu a zakomentovať pôvodnú Ethernet
  • Pre ESP8266 a ESP32 je nutné v zdrojovom kóde doplniť SSID a heslo WiFi siete. Využívajú IPv4 priradenú z DHCP.
  • V zdrojovom kóde sú použité vývody mikrokontrolérov na základe schémy zapojenia tu na stránke pre pripojenie ultrazvukového senzora vzdialenosti HC-SR04 / JSN-SR04T.

  • Dôležité údaje vyžadujúce zmenu / pozornosť pre kompatibilitu s vašou sieťou a hardvérom sú v zdrojovom kóde vyznačené ČERVENOU FARBOU!

     
    /*|-----------------------------------------------------------------------------------|*/
    /*|Projekt: Hladinomer - HTTP - Compact - HC-SR04 / JSN-SR04T / HY-SRF05              |*/
    /*|Arduino + Ethernet (W5100 / W5500, ENC28J60), ESP8266 (NodeMCU), ESP32 (DevKit)    |*/
    /*|Autor: Martin Chlebovec (martinius96)                                              |*/
    /*|E-mail: martinius96@gmail.com                                                      |*/
    /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
    /*|Testovacie webove rozhranie: http://arduino.clanweb.eu/studna_s_prekladom/         |*/
    /*|Knižnice NewPing, ESP8266NewPing a Ethernet2 je dostupná v Github repozitári:      |*/
    /*|https://github.com/martinius96/hladinomer-studna-scripty/ - stihnuť a rozbaliť     |*/
    /*|Obsah priečinka /src/ nakopírovať do C:/Users/User/Dokumenty/Arduino/libraries/    |*/
    /*|Na toto webove rozhranie posiela mikrokontroler data                               |*/
    /*|Na zaklade zvolenej platformy v Arduino IDE sa vykona kompilacia podla direktiv    |*/
    /*|Licencia pouzitia: MIT                                                             |*/
    /*|Revízia: 19. Februar 2021                                                          |*/
    /*|-----------------------------------------------------------------------------------|*/
    
    const char* host = "arduino.clanweb.eu"; //adresa webservera (doména) na ktorú sa odosielajú dáta
    String url = "/studna_s_prekladom/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápis
    //Pre testovacie webove rozhranie sa data odosielaju na: arduino.clanweb.eu/studna_s_prekladom/data.php (HTTP POST ONLY)
    
    //Kompatibilne mikrokontrolery z rady Arduino: Uno, Nano, Mega (R3)
    #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    //HLAVICKOVE SUBORY PRE ARDUINO A ETHERNET SHIELD + watchdog
    #include <avr\wdt.h>
    #include <SPI.h>
    
    
    
    //VOLBA ETHERNET MODULU / SHIELDU K ARDUINU Z RADY WIZNET / ENC (Microchip)
    ///////////////////////////////////////////////////////////////////////////////////////
    
    

    #include <Ethernet.h> //Ethernet shield Wiznet W5100 - zakomentovat ak sa nepouziva //#include <Ethernet2.h> //Ethernet modul Wiznet W5500 - zakomentovat ak sa nepouziva //#include <Ethernet3.h> //Ethernet modul WIZ850io / USR-ES1 (Wiznet W5500 V2) - zakomentovat ak sa nepouziva //#include <UIPEthernet.h> //Ethernet modul ENC28J60 - zakomentovat ak sa nepouziva

    /////////////////////////////////////////////////////////////////////////////////////// //PREMENNE, HLAVICKOVY SUBOR, OBJEKT PRE HC-SR04 / JSN-SR04T #include <NewPing.h> #define pinTrigger 5 #define pinEcho 6 #define maxVzdialenost 450 NewPing sonar(pinTrigger, pinEcho, maxVzdialenost); byte mac[] = { 0xAA, 0xBB, 0xCA, 0xDC, 0xEE, 0xDE }; //IPAddress ip(192, 168, 0, 2); //IPAddress dnServer(192, 168, 0, 1); //IPAddress gateway(192, 168, 0, 1); //IPAddress subnet(255, 255, 255, 0); unsigned long timer = 0; EthernetClient client; #elif defined(ESP8266) #include <ESP8266WiFi.h> #include <NewPingESP8266.h> #define pinTrigger 5 //D1 #define pinEcho 4 //D2 #define maxVzdialenost 450 NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); #elif defined(ESP32) #include <WiFi.h> #include <NewPingESP8266.h> #include "esp_system.h" #define pinTrigger 22 //D22 #define pinEcho 23 //D23 #define maxVzdialenost 450 NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); const int wdtTimeout = 30000; //time in ms to trigger the watchdog hw_timer_t *timer = NULL; void IRAM_ATTR resetModule() { ets_printf("reboot\n"); esp_restart(); } #endif #if defined(ESP32) || defined(ESP8266)

    const char * ssid = "WIFI_MENO_SIETE"; //MENO WiFi SIETE

    const char * password = "WIFI_HESLO_SIETE"; //HESLO WiFi SIETE

    WiFiClient client; unsigned long timer2 = 0; #endif void setup() { Serial.begin(115200); #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) if (Ethernet.begin(mac) == 0) { Serial.println(F("Konfigurujem Ethernet adapter")); Ethernet.begin(mac); // pre DHCP //Ethernet.begin(mac, ip); //pre staticku IPv4 //Ethernet.begin(mac, ip, dns); //pre staticku IPv4 //Ethernet.begin(mac, ip, dns, gateway); //pre staticku IPv4 //Ethernet.begin(mac, ip, dns, gateway, subnet); //pre staticku IPv4 //IBA PRE Ethernet3.h !!! POZOR --> INY BEGIN ORDER PARAMETROV.... : //Ethernet.begin(mac, ip, subnet, gateway, dns); } Serial.print(F("Priradena IP: ")); Serial.println(Ethernet.localIP()); wdt_enable(WDTO_8S); //watchdog on 8 sec trigger #elif defined(ESP32) || defined(ESP8266) WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print(F(".")); } Serial.println(""); Serial.println(F("Wifi pripojene s IP:")); Serial.println(WiFi.localIP()); #endif #if defined(ESP8266) ESP.wdtEnable(30000); //30s SW watchdog #elif defined(ESP32) timer = timerBegin(0, 80, true); //timer 0, div 80 timerAttachInterrupt(timer, &resetModule, true); //attach callback timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us timerAlarmEnable(timer); //enable interrupt #endif } void loop() { #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) wdt_reset(); //Feed watchdog /* if (Ethernet.begin(mac) == 0) { Ethernet.begin(mac); // pre DHCP //Ethernet.begin(mac, ip); //pre staticku IPv4 //Ethernet.begin(mac, ip, dns); //pre staticku IPv4 //Ethernet.begin(mac, ip, dns, gateway); //pre staticku IPv4 //Ethernet.begin(mac, ip, dns, gateway, subnet); } */ if ((millis() - timer) >= 300000 || timer == 0) { //rutina raz za 5 minut timer = millis(); //NEPOUZIVAT MOMENTALNE, SPOSOBUJE PROBLEMY A ZACYKLENIE SIETE

    Ethernet.maintain(); //pouzivat iba ak sa vyuziva DHCP bez statickej IP adresy, inak zakomentovat

    int vzdialenost = sonar.ping_cm(); delay(50); if (vzdialenost > 0) { vzdialenost = 0; for (int i = 0; i < 10; i++) { vzdialenost += sonar.ping_cm(); delay(50); } wdt_reset(); vzdialenost = vzdialenost / 10; Serial.print(F("Vzdialenost medzi senzorom a predmetom je: ")); Serial.print(vzdialenost); Serial.println(F(" cm.")); client.stop(); //ukoncenie vsetkych existujucich spojeni String data = "hodnota=" + String(vzdialenost) + "&token=123456789"; if (client.connect(host, 80)) { client.println("POST " + url + " HTTP/1.0"); client.println("Host: " + (String)host); client.println(F("User-Agent: EthernetW5100")); //Moznost pouzit iny USER agent client.println(F("Connection: close")); client.println(F("Content-Type: application/x-www-form-urlencoded;")); client.print(F("Content-Length: ")); client.println(data.length()); client.println(); client.println(data); Serial.println(F("Data uspesne odoslane na webove rozhranie")); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } String line = client.readStringUntil('\n'); } else { Serial.println(F("Pripojenie zlyhalo...")); delay(500); timer = 0; //vynulujeme timer, znovu nameriame a vykoname request } client.stop(); } else { Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah.")); delay(500); timer = 0; //vynulujeme timer, znovu nameriame a vykoname request } } #endif #if defined(ESP32) || defined(ESP8266) if (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom } #endif #if defined(ESP8266) while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } ESP.wdtFeed(); //feed watchdog yield(); //nechaj ESP dokoncit WiFi stack if ((millis() - timer2) >= 300000 || timer2 == 0) { timer2 = millis(); int vzdialenost = sonar.ping_cm(); delay(50); if (vzdialenost > 0) { vzdialenost = 0; for (int i = 0; i < 10; i++) { vzdialenost += sonar.ping_cm(); delay(50); } vzdialenost = vzdialenost / 10; Serial.print(F("Vzdialenost medzi senzorom a predmetom je: ")); Serial.print(vzdialenost); Serial.println(F(" cm.")); client.stop(); String data = "hodnota=" + String(vzdialenost) + "&token=123456789"; if (client.connect(host, 80)) { client.println("POST " + url + " HTTP/1.0"); client.println("Host: " + (String)host); client.println(F("User-Agent: ESP8266")); client.println(F("Connection: close")); client.println(F("Content-Type: application/x-www-form-urlencoded;")); client.print(F("Content-Length: ")); client.println(data.length()); client.println(); client.println(data); Serial.println(F("Data uspesne odoslane na web")); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } String line = client.readStringUntil('\n'); } else { Serial.println(F("Pripojenie zlyhalo...")); delay(500); timer2 = 0; //vynulujeme timer, znovu nameriame a vykoname request } client.stop(); } else { Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah.")); delay(500); client.stop(); timer2 = 0; //vynulujeme timer, znovu nameriame a vykoname request } } #elif defined(ESP32) while (WiFi.status() != WL_CONNECTED) { timerWrite(timer, 0); //reset timer (feed watchdog) delay(500); Serial.print(F(".")); } timerWrite(timer, 0); //reset timer (feed watchdog) yield(); //nechaj ESP dokoncit WiFi stack if ((millis() - timer2) >= 300000 || timer2 == 0) { timer2 = millis(); int vzdialenost = sonar.ping_cm(); delay(50); if (vzdialenost > 0) { vzdialenost = 0; for (int i = 0; i < 10; i++) { vzdialenost += sonar.ping_cm(); delay(50); } vzdialenost = vzdialenost / 10; Serial.print(F("Vzdialenost medzi senzorom a predmetom je: ")); Serial.print(vzdialenost); Serial.println(F(" cm.")); client.stop(); String data = "hodnota=" + String(vzdialenost) + "&token=123456789"; if (client.connect(host, 80)) { client.println("POST " + url + " HTTP/1.0"); client.println("Host: " + (String)host); client.println(F("User-Agent: ESP32")); client.println(F("Connection: close")); client.println(F("Content-Type: application/x-www-form-urlencoded;")); client.print(F("Content-Length: ")); client.println(data.length()); client.println(); client.println(data); Serial.println(F("Data uspesne odoslane na web")); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } String line = client.readStringUntil('\n'); } else { Serial.println(F("Pripojenie zlyhalo...")); delay(500); timer2 = 0; //vynulujeme timer, znovu nameriame a vykoname request } client.stop(); } else { Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah.")); delay(500); client.stop(); timer2 = 0; //vynulujeme timer, znovu nameriame a vykoname request } } #endif }

    HTTP - ESP8266 / ESP32 - StandBy + Over The Air (OTA) Over-The-Air firmware update


    /*|-----------------------------------------------------------------------------------|*/
    /*|Projekt: Hladinomer - HTTP - OTA - HC-SR04 / JSN-SR04T / HY-SRF05                  |*/
    /*|ESP8266 (NodeMCU, Wemos D1 Mini, Generic), ESP32 (DevKit, Generic)                 |*/
    /*|Autor: Martin Chlebovec (martinius96)                                              |*/
    /*|E-mail: martinius96@gmail.com                                                      |*/
    /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
    /*|Testovacie webove rozhranie: http://arduino.clanweb.eu/studna_s_prekladom/         |*/
    /*|Knižnica ESP8266NewPing je dostupná v Github repozitári:                           |*/
    /*|https://github.com/martinius96/hladinomer-studna-scripty/ - stihnuť a rozbaliť     |*/
    /*|Obsah priečinka /src/ nakopírovať do C:/Users/User/Dokumenty/Arduino/libraries/    |*/
    /*|Na toto webove rozhranie posiela mikrokontroler data                               |*/
    /*|Na zaklade zvolenej platformy v Arduino IDE sa vykona kompilacia podla direktiv    |*/
    /*|Licencia pouzitia: MIT                                                             |*/
    /*|Revízia: 26. Februar 2021                                                          |*/
    /*|-----------------------------------------------------------------------------------|*/
          
    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>
    #include <NewPingESP8266.h>
    
    

    const char * ssid = "WIFI_MENO_SIETE"; //MENO WiFi SIETE

    const char * password = "WIFI_HESLO_SIETE"; //HESLO WiFi SIETE

    const char* host = "arduino.clanweb.eu"; //adresa webservera (doména) na ktorú sa odosielajú dáta String url = "/studna_s_prekladom/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápis //Pre testovacie webove rozhranie sa data odosielaju na: arduino.clanweb.eu/studna_s_prekladom/data.php (HTTP POST ONLY) unsigned long timer2 = 0; #if defined(ESP32) #include <WiFi.h> #include <ESPmDNS.h> #include "esp_system.h" #define pinTrigger 22 #define pinEcho 23 #define maxVzdialenost 450 NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); const int wdtTimeout = 30000; //time in ms to trigger the watchdog hw_timer_t *timer = NULL; void IRAM_ATTR resetModule() { ets_printf("reboot\n"); esp_restart(); } #elif defined(ESP8266) #include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <ArduinoOTA.h> #include <NewPingESP8266.h> #define pinTrigger 5 //D1 #define pinEcho 4 //D2 #define maxVzdialenost 450 NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); #endif WiFiClient client; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } ArduinoOTA.onStart([]() { Serial.println(F("Start")); }); ArduinoOTA.onEnd([]() { Serial.println(F("\nEnd")); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println(F("")); Serial.println(F("Wifi pripojene s IP:")); Serial.println(WiFi.localIP()); #if defined(ESP32) timer = timerBegin(0, 80, true); //timer 0, div 80 timerAttachInterrupt(timer, &resetModule, true); //attach callback timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us timerAlarmEnable(timer); //enable interrupt #elif defined(ESP8266) ESP.wdtEnable(30000); #endif } void loop() { if (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom } while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } #if defined(ESP32) timerWrite(timer, 0); //reset timer (feed watchdog) #elif defined(ESP8266) ESP.wdtFeed(); #endif ArduinoOTA.handle(); yield(); if ((millis() - timer2) >= 300000 || timer2 == 0) { timer2 = millis(); int vzdialenost = sonar.ping_cm(); delay(50); if (vzdialenost > 0) { vzdialenost = 0; for (int i = 0; i < 10; i++) { vzdialenost += sonar.ping_cm(); delay(50); } vzdialenost = vzdialenost / 10; Serial.print(F("Vzdialenost medzi senzorom a predmetom je: ")); Serial.print(vzdialenost); Serial.println(F(" cm.")); String data = "hodnota=" + String(vzdialenost) + "&token=123456789"; if (client.connect(host, 80)) { client.println("POST " + url + " HTTP/1.0"); client.println("Host: " + (String)host); client.println(F("User-Agent: ESP")); client.println(F("Connection: close")); client.println(F("Content-Type: application/x-www-form-urlencoded;")); client.print(F("Content-Length: ")); client.println(data.length()); client.println(); client.println(data); Serial.println(F("Data uspesne odoslane na web")); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } String line = client.readStringUntil('\n'); } else { Serial.println(F("Pripojenie zlyhalo...")); delay(500); timer2 = 0; //reset pocitadla, vykoname opakovany request } } else { Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah.")); delay(500); timer2 = 0; //reset pocitadla, vykoname opakovany request } client.stop(); } }

    HTTP - ESP8266 / ESP32 - Deep Sleep - Ultra-Low Power (ULP) Battery-friendly deep sleep ULP mode


    Pri ESP8266 (NodeMCU, Wemos D1 Mini, Generic) je nutné prepojiť RST S GPIO16 (D0) po nahratí programu a vykonať reštart cez EN (RST) tlačidlo, inak prebudenie a Deep Sleep nebude fungovať!
    /*|-----------------------------------------------------------------------------------|*/
    /*|Projekt: Hladinomer - HTTP - ULP - DEEP SLEEP - HC-SR04 / JSN-SR04T / HY-SRF05     |*/
    /*|ESP8266 (NodeMCU, Wemos D1 Mini, Generic), ESP32 (DevKit, Generic)                 |*/
    /*|Autor: Martin Chlebovec (martinius96)                                              |*/
    /*|E-mail: martinius96@gmail.com                                                      |*/
    /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
    /*|Testovacie webove rozhranie: http://arduino.clanweb.eu/studna_s_prekladom/         |*/
    /*|Knižnica ESP8266NewPing je dostupná v Github repozitári:                           |*/
    /*|https://github.com/martinius96/hladinomer-studna-scripty/ - stihnuť a rozbaliť     |*/
    /*|Obsah priečinka /src/ nakopírovať do C:/Users/User/Dokumenty/Arduino/libraries/    |*/
    /*|Na toto webove rozhranie posiela mikrokontroler data                               |*/
    /*|Na zaklade zvolenej platformy v Arduino IDE sa vykona kompilacia podla direktiv    |*/
    /*|Licencia pouzitia: MIT                                                             |*/
    /*|Revízia: 26. Februar 2021                                                          |*/
    /*|-----------------------------------------------------------------------------------|*/
    

    const char * ssid = "WIFI_MENO_SIETE"; //MENO WiFi SIETE

    const char * password = "WIFI_HESLO_SIETE"; //HESLO WiFi SIETE

    const char* host = "arduino.clanweb.eu"; //adresa webservera (doména) na ktorú sa odosielajú dáta String url = "/studna_s_prekladom/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápis //Pre testovacie webove rozhranie sa data odosielaju na: arduino.clanweb.eu/studna_s_prekladom/data.php (HTTP POST ONLY) #include <NewPingESP8266.h> #if defined(ESP32) #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 300 /* Time ESP32 will go to sleep (in seconds) */ #include <WiFi.h> #define pinTrigger 22 #define pinEcho 23 #define maxVzdialenost 450 #elif defined(ESP8266) #include <ESP8266WiFi.h> #define pinTrigger 5 //D1 #define pinEcho 4 //D2 #define maxVzdialenost 450 #endif NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); WiFiClient client; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print(F(".")); } Serial.println(F("")); Serial.println(F("Wifi pripojene s IP:")); Serial.println(WiFi.localIP()); int vzdialenost = sonar.ping_cm(); delay(50); if (vzdialenost > 0) { vzdialenost = 0; for (int i = 0; i < 10; i++) { vzdialenost += sonar.ping_cm(); delay(50); } vzdialenost = vzdialenost / 10; Serial.print(F("Vzdialenost medzi senzorom a predmetom je: ")); Serial.print(vzdialenost); Serial.println(F(" cm.")); String data = "hodnota=" + String(vzdialenost) + "&token=123456789"; client.stop(); if (client.connect(host, 80)) { client.println("POST " + url + " HTTP/1.0"); client.println("Host: " + (String)host); client.println(F("User-Agent: ESP")); client.println(F("Connection: close")); client.println(F("Content-Type: application/x-www-form-urlencoded;")); client.print(F("Content-Length: ")); client.println(data.length()); client.println(); client.println(data); Serial.println(F("Data uspesne odoslane na web")); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { break; } } String line = client.readStringUntil('\n'); } else { Serial.println(F("Pripojenie zlyhalo...")); } } else { Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah.")); } client.stop(); #if defined(ESP32) esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds"); esp_deep_sleep_start(); #elif defined(ESP8266) Serial.println(F("Entering deep sleep mode for 300 seconds")); ESP.deepSleep(300e6); #endif } void loop() { }

    Sigfox UART MODEM 868MHz - Arduino - Sigfox Backend Sigfox LPWAN IoT network supported


    /*|-----------------------------------------------------------------------------------|*/
    /*|Projekt: Hladinomer - HTTP - Sigfox 868 MHz UART - HC-SR04 / JSN-SR04T / HY-SRF05  |*/
    /*|Arduino, ESP8266 (NodeMCU), ESP32 (DevKit)                                         |*/
    /*|Autor: Martin Chlebovec (martinius96)                                              |*/
    /*|E-mail: martinius96@gmail.com                                                      |*/
    /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
    /*|Testovacie webove rozhranie: http://arduino.clanweb.eu/studna_s_prekladom/         |*/
    /*|Knižnice NewPing, ESP8266NewPing sú dostupné v Github repozitári:                  |*/
    /*|https://github.com/martinius96/hladinomer-studna-scripty/ - stihnuť a rozbaliť     |*/
    /*|Obsah priečinka /src/ nakopírovať do C:/Users/User/Dokumenty/Arduino/libraries/    |*/
    /*|Na toto webove rozhranie posiela mikrokontroler data                               |*/
    /*|Na zaklade zvolenej platformy v Arduino IDE sa vykona kompilacia podla direktiv    |*/
    /*|Licencia pouzitia: MIT                                                             |*/
    /*|Revízia: 26. Februar 2021                                                          |*/
    /*|-----------------------------------------------------------------------------------|*/
    
    /*|---------------------------------------------------------------------|*/
    /*|Inštrukcie pre nastavenie Sigfox Modemu na stránkach Sigfox backend: |*/
    /*|Callbacks --> NEW --> Custom callback                                |*/
    /*|UPLINK, TYPE: DATA, CHANNEL: URL                                     |*/
    /*|Do Custom payload config napíšeme: cislo1::uint:16                   |*/
    /*|Do URL pattern: http://arduino.clanweb.eu/studna_s_prekladom/data.php|*/
    /*|PRIPADNE: (pripadne vas URL webservera s možnostou TLS - SSL)        |*/
    /*|V HTTP metóde zvolíme: POST (jediná podporovaná metóda Hladinomeru)  |*/
    /*|Do Body (tela správy) doplníme:                                      |*/
    /*|hodnota={customData#cislo1}&token=123456789                          |*/
    /*|Mozno odosielat aj systemove udaje - cislo spravy, RSSI, GEO UDAJE   |*/
    /*|Do Content-Type: application/x-www-form-urlencoded                   |*/
    /*|---------------------------------------------------------------------|*/
    
    //HLAVICKOVE SUBORY watchdog a software serial
    #include <avr\wdt.h>
    #include <SoftwareSerial.h>
    
    // nastavenie softverovej zbernice pre Sigfox Modem
    #define TX 7
    #define RX 8
    SoftwareSerial Sigfox(RX, TX);
    
    //PREMENNE, HLAVICKOVY SUBOR, OBJEKT PRE HC-SR04 / JSN-SR04T
    #include <NewPing.h>
    //#include <NewPingESP8266.h> // pre ESP8266, ESP32
    #define pinTrigger    5
    #define pinEcho       6
    #define maxVzdialenost 450
    NewPing sonar(pinTrigger, pinEcho, maxVzdialenost);
    //NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); // pre ESP8266, ESP32
    unsigned long timer = 0;
    
    
    void setup() {
      Sigfox.begin(9600); //SoftwareSerial
      Serial.begin(115200);
      wdt_enable(WDTO_8S);
    }
    
    void loop() {
      wdt_reset();
      if (Sigfox.available()) {
        Serial.write(Sigfox.read()); //MOZNO VYSKUSAT AT prikaz, odpoved OK
      }
      if (Serial.available()) {
        Sigfox.write(Serial.read());
      }
      if ((millis() - timer) >= 660000 || timer == 0) { //rutina raz za 11 minut (limit 140 sprav za den), odosle sa 130 správ za deň (24h)
        timer = millis();
        Sigfox.println(); //Wakeup from Light sleep via ‘\n’ (ASCII 10)
        //Sigfox.print('\n'); //Wakeup from Light sleep via ‘\n’ (ASCII 10) - ekvivalent
        unsigned int vzdialenost = sonar.ping_cm();
        delay(50);
        if (vzdialenost > 0) {
          vzdialenost = 0;
          for (int i = 0; i < 10; i++) {
            vzdialenost += sonar.ping_cm();
            delay(50);
          }
          wdt_reset();
          vzdialenost = vzdialenost / 10;
          Serial.print(F("Vzdialenost medzi senzorom a predmetom je: "));
          Serial.print(vzdialenost);
          Serial.println(F(" cm."));
          char sprava[4]; //4B sprava, max mozna 12B
          unsigned int cislo1 = vzdialenost;
          sprintf(sprava, "%04X", cislo1);
          Serial.print(F("Do Sigfox siete odosielam tento payload: "));
          Serial.print(cislo1);
          Serial.print(F(", hexa tvar: "));
          Serial.println(sprava);
          wdt_reset();
          Sigfox.print(F("AT$SF="));
          Sigfox.println(sprava);
          wdt_reset();
          delay(1000);
          Sigfox.print(F("AT$P=1")); //Light sleep (Send a break (‘\n’) to wake up.)
          //Sigfox.print(F("AT$P=2")); //Deep sleep (power-on reset needed for wake up)
        } else {
          Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah."));
          timer = 0; //vynulujeme timer, vykoname nove meranie
        }
      }
    }
      

    HTTP - ESP32 - ESP-IDF 4.X - StandBy Real-time operating system FreeRTOS


    /*|-----------------------------------------------------------------------------------|*/
    /*|Projekt: Hladinomer - HTTP - HC-SR04 / JSN-SR04T / HY-SRF05                        |*/
    /*|ESP32 (DevKit, Generic) - ESP-IDF v4.2 (4.0 compatible)                            |*/
    /*|Autor: Martin Chlebovec (martinius96)                                              |*/
    /*|E-mail: martinius96@gmail.com                                                      |*/
    /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
    /*|Testovacie webove rozhranie: http://arduino.clanweb.eu/studna_s_prekladom/         |*/
    /*|Revízia: 4. Jun 2021                                                               |*/
    /*|-----------------------------------------------------------------------------------|*/
    
    /* HTTP GET Example using plain POSIX sockets
    
       This example code is in the Public Domain (or CC0 licensed, at your option.)
    
       Unless required by applicable law or agreed to in writing, this
       software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
       CONDITIONS OF ANY KIND, either express or implied.
    */
    #include <string.h>
    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "freertos/queue.h"
    #include "esp_system.h"
    #include "esp_wifi.h"
    #include "esp_event.h"
    #include "esp_log.h"
    #include "nvs_flash.h"
    #include "protocol_examples_common.h"
    
    #include "lwip/err.h"
    #include "lwip/sockets.h"
    #include "lwip/sys.h"
    #include "lwip/netdb.h"
    #include "lwip/dns.h"
    
    #include "ultrasonic.h"
    #include "driver/dac.h"
    
    /* Constants that aren't configurable in menuconfig */
    
    #define MAX_DISTANCE_CM 450 // 5m max
    #define GPIO_TRIGGER	22
    #define GPIO_ECHO	23
    // Webserver
    #define WEB_SERVER "arduino.clanweb.eu"
    #define WEB_PORT "80"
    
    
    static const char *TAG = "http_request";
    static const char *TAG2 = "ultrasonic_measurement";
    
    QueueHandle_t  q=NULL;
    static void ultrasonic(void *pvParamters)
    {
    	ultrasonic_sensor_t sensor = {
    		.trigger_pin = GPIO_TRIGGER,
    		.echo_pin = GPIO_ECHO
    	};
    
    	ultrasonic_init(&sensor);
      uint32_t distance = 0;
        if(q == NULL){
            printf("Queue is not ready \n");
            return;
        }
    	while (true) {
      uint32_t avg_distance = 0;
      int index_loop = 1;
      while(index_loop<=10){
    		esp_err_t res = ultrasonic_measure_cm(&sensor, MAX_DISTANCE_CM, &distance);
    		if (res != ESP_OK) {
    			printf("Error: ");
    			switch (res) {
    				case ESP_ERR_ULTRASONIC_PING:
    					printf("Cannot ping (device is in invalid state)\n");
    					break;
    				case ESP_ERR_ULTRASONIC_PING_TIMEOUT:
    					printf("Ping timeout (no device found)\n");
    					break;
    				case ESP_ERR_ULTRASONIC_ECHO_TIMEOUT:
    					printf("Echo timeout (i.e. distance too big)\n");
    					break;
    				default:
    					printf("%d\n", res);
    			}
    		} else {
    			printf("Measurement %d: %d cm\n", index_loop, distance);
           avg_distance +=  distance;
          index_loop++;
    		}
        }
          avg_distance = avg_distance / 10;
          distance  = avg_distance;
        xQueueSend(q,(void *)&distance,(TickType_t )0); // add the counter value to the queue
                for(int countdown = 300; countdown >= 0; countdown--) {
                ESP_LOGI(TAG2, "%d... ", countdown);
                vTaskDelay(1000 / portTICK_PERIOD_MS);
            }
    	}
    }
    
    static void http_get_task(void *pvParameters)
    {
        const struct addrinfo hints = {
            .ai_family = AF_INET,
            .ai_socktype = SOCK_STREAM,
        };
        struct addrinfo *res;
        struct in_addr *addr;
        int s, r;
        char recv_buf[64];
        uint32_t distance;
         if(q == NULL){
            printf("Queue is not ready \n");
            return;
        }
        while(1) {
        xQueueReceive(q,&distance,portMAX_DELAY); 
        char REQUEST [1000];
    	  char values [250];
    	  sprintf(values, "hodnota=%d&token=123456789", distance);
        sprintf (REQUEST, "POST /studna_s_prekladom/data.php HTTP/1.0\r\nHost: "WEB_SERVER"\r\nUser-Agent: ESP32\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded;\r\nContent-Length:%d\r\n\r\n%s\r\n",strlen(values),values);
            int err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res);
    
            if(err != 0 || res == NULL) {
                ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
                vTaskDelay(1000 / portTICK_PERIOD_MS);
                continue;
            }
    
            /* Code to print the resolved IP.
    
               Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
            addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
            ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr));
    
            s = socket(res->ai_family, res->ai_socktype, 0);
            if(s < 0) {
                ESP_LOGE(TAG, "... Failed to allocate socket.");
                freeaddrinfo(res);
                vTaskDelay(1000 / portTICK_PERIOD_MS);
                continue;
            }
            ESP_LOGI(TAG, "... allocated socket");
    
            if(connect(s, res->ai_addr, res->ai_addrlen) != 0) {
                ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
                close(s);
                freeaddrinfo(res);
                vTaskDelay(4000 / portTICK_PERIOD_MS);
                continue;
            }
    
            ESP_LOGI(TAG, "... connected");
            freeaddrinfo(res);
    
            if (write(s, REQUEST, strlen(REQUEST)) < 0) {
                ESP_LOGE(TAG, "... socket send failed");
                close(s);
                vTaskDelay(4000 / portTICK_PERIOD_MS);
                continue;
            }
            ESP_LOGI(TAG, "... socket send success");
    
            struct timeval receiving_timeout;
            receiving_timeout.tv_sec = 5;
            receiving_timeout.tv_usec = 0;
            if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout,
                    sizeof(receiving_timeout)) < 0) {
                ESP_LOGE(TAG, "... failed to set socket receiving timeout");
                close(s);
                vTaskDelay(4000 / portTICK_PERIOD_MS);
                continue;
            }
            ESP_LOGI(TAG, "... set socket receiving timeout success");
    
            /* Read HTTP response */
            do {
                bzero(recv_buf, sizeof(recv_buf));
                r = read(s, recv_buf, sizeof(recv_buf)-1);
                for(int i = 0; i < r; i++) {
                    putchar(recv_buf[i]);
                }
            } while(r > 0);
    
            ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d.", r, errno);
            close(s);
            ESP_LOGI(TAG, "Starting again!");
        }
    }
    
    void app_main(void)
    {
        ESP_ERROR_CHECK( nvs_flash_init() );
        ESP_ERROR_CHECK(esp_netif_init());
        ESP_ERROR_CHECK(esp_event_loop_create_default());
    
        /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
         * Read "Establishing Wi-Fi or Ethernet Connection" section in
         * examples/protocols/README.md for more information about this function.
         */
        ESP_ERROR_CHECK(example_connect());
         q=xQueueCreate(20,sizeof(unsigned long));
        if(q != NULL){
            printf("Queue is created\n");
            vTaskDelay(1000/portTICK_PERIOD_MS); //wait for a second
            xTaskCreate(&ultrasonic, "ultrasonic", 2048, NULL, 5, NULL);
            printf("producer task  started\n");
            xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
            printf("consumer task  started\n");
        }else{
            printf("Queue creation failed");
        }    
    
    }
    
    
    
    

    HTTP - SIM7000E - Arduino - NB-IoT (experimental) NB-IoT (NarrowBand – Internet of Things) supported


    /*|------------------------------------------------------------------------------------|*/
    /*|Projekt: Hladinomer - HTTP - SIM7000 - NB-IoT - HC-SR04 / JSN-SR04T / HY-SRF05      |*/
    /*|Arduino, ESP8266 (NodeMCU), ESP32 (DevKit)                                          |*/
    /*|Autor: Ing. Martin Chlebovec (martinius96)                                          |*/
    /*|E-mail: martinius96@gmail.com                                                       |*/
    /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/  |*/
    /*|Testovacie webove rozhranie pre HTTP: http://arduino.clanweb.eu/studna_s_prekladom/ |*/
    /*|Testovacie webove rozhranie pre HTTPS: https://hladinomer.000webhostapp.com/        |*/
    /*|Knižnice NewPing, ESP8266NewPing sú dostupné v Github repozitári:                   |*/
    /*|https://github.com/martinius96/hladinomer-studna-scripty/ - stihnuť a rozbaliť      |*/
    /*|Obsah priečinka /src/ nakopírovať do C:/Users/User/Dokumenty/Arduino/libraries/     |*/
    /*|Na toto webove rozhranie posiela mikrokontroler data                                |*/
    /*|Na zaklade zvolenej platformy v Arduino IDE sa vykona kompilacia podla direktiv     |*/
    /*|Licencia pouzitia: MIT                                                              |*/
    /*|Revízia: 13. Januar 2022                                                            |*/
    /*|------------------------------------------------------------------------------------|*/
    
    #include <DFRobot_SIM7000.h>
    
    #define PIN_TX     7
    #define PIN_RX     8
    #include <SoftwareSerial.h>
    
    //This URL is use for post data to webapp
    #define POSTURL   "arduino.clanweb.eu/studna_s_prekladom/data.php"
    
    // nastavenie softverovej zbernice pre SIM7000E modul
    SoftwareSerial     mySerial(PIN_RX, PIN_TX);
    DFRobot_SIM7000    sim7000;
    //PREMENNE, HLAVICKOVY SUBOR, OBJEKT PRE HC-SR04 / JSN-SR04T
    #include <NewPing.h>
    //#include <NewPingESP8266.h> // pre ESP8266, ESP32
    #define pinTrigger    5
    #define pinEcho       6
    #define maxVzdialenost 450
    NewPing sonar(pinTrigger, pinEcho, maxVzdialenost);
    //NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); // pre ESP8266, ESP32
    unsigned long timer = 0;
    
    
    void setup() {
      int signalStrength, dataNum;
      Serial.begin(115200);
      while (!Serial);
      sim7000.begin(mySerial);
      Serial.println("Turn ON SIM7000......");
      if (sim7000.turnON()) {                                  //Turn ON SIM7000
        Serial.println("Turn ON !");
      }
    
      Serial.println("Set baud rate......");
      while (1) {
        if (sim7000.setBaudRate(19200)) {                    //Set SIM7000 baud rate from 115200 to 19200 reduce the baud rate to avoid distortion
          Serial.println("Set baud rate:19200");
          break;
        } else {
          Serial.println("Faile to set baud rate");
          delay(1000);
        }
      }
      Serial.println("Check SIM card......");
      if (sim7000.checkSIMStatus()) {                          //Check SIM card
        Serial.println("SIM card READY");
      } else {
        Serial.println("SIM card ERROR, Check if you have insert SIM card and restart SIM7000");
        while (1);
      }
    
      Serial.println("Set net mode......");
      while (1) {
        if (sim7000.setNetMode(GPRS)) {                      //Set net mod GPRS
          Serial.println("Set GPRS mode");
          break;
        } else {
          Serial.println("Fail to set mode");
          delay(1000);
        }
      }
    
      Serial.println("Get signal quality......");
      signalStrength = sim7000.checkSignalQuality();           //Check signal quality from (0-30)
      Serial.print("signalStrength =");
      Serial.println(signalStrength);
      delay(500);
      Serial.println("Attaching service......");
      while (1) {
        if (sim7000.attacthService()) {                      //Open the connection
          Serial.println("Attach service");
          break;
        } else {
          Serial.println("Fail to Attach service");
          delay(1000);
        }
      }
    
      Serial.println("Init http......");
      while (1) {
        if (sim7000.httpInit(GPRS)) {                        //Init http service
          Serial.println("HTTP init !");
          break;
        } else {
          Serial.println("Fail to init http");
        }
      }
      wdt_enable(WDTO_8S);
    }
    
    void loop() {
      wdt_reset();
      if ((millis() - timer) >= 660000 || timer == 0) { //rutina raz za 11 minut (limit 140 sprav za den), odosle sa 130 správ za deň (24h)
        timer = millis();
        unsigned int vzdialenost = sonar.ping_cm();
        delay(50);
        if (vzdialenost > 0) {
          vzdialenost = 0;
          for (int i = 0; i < 10; i++) {
            vzdialenost += sonar.ping_cm();
            delay(50);
          }
          vzdialenost = vzdialenost / 10;
          Serial.print(F("Vzdialenost medzi senzorom a predmetom je: "));
          Serial.print(vzdialenost);
          Serial.println(F(" cm."));
          wdt_reset();
          String data = "hodnota=" + String(vzdialenost) + "&token=123456789";
          while (1) {
            if (sim7000.httpPost(POSTURL, data)) {           //HTTP POST
              Serial.println("Post successed");
              break;
            } else {
              Serial.println("Fail to post");
            }
          }
    
        } else {
          Serial.println(F("Vzdialenost medzi predmetom a senzorom je mimo rozsah."));
          timer = 0; //vynulujeme timer, vykoname nove meranie
        }
        /*
          Serial.println("Disconnect");
          sim7000.httpDisconnect();                                //Disconnect
          Serial.println("Close net work");
          sim7000.closeNetwork();                                  //Close net work
          Serial.println("Turn off SIM7000");
          sim7000.turnOFF();                                       //Turn OFF SIM7000
        */
      }
    }