Názov knižnice | Funkcia knižnice | Stiahnuť |
---|---|---|
NewPing |
Knižnica pre AVR mikrokontroléry (ATmega) Arduino Uno / Nano / Mega. Umožňuje vykonávať meranie ultrazvukovými senzormi vzdialenosti US-02X, IOE-SR0X, HC-SR0X, HY-SRF0X, JSN-SR0XT, DYP-ME007. |
Stiahnuť |
NewPingESP8266 |
Knižnica pre ESP8266 a ESP32 mikrokontroléry. Umožňuje vykonávať meranie ultrazvukovými senzormi vzdialenosti US-02X, IOE-SR0X, HC-SR0X, HY-SRF0X, JSN-SR0XT, DYP-ME007. |
Stiahnuť |
Ethernet2 |
Knižnica pre AVR mikrokontroléry (ATmega) Arduino Uno / Nano / Mega. Umožňuje komunikáciu s Ethernet modulom Wiznet W5200 až W5500 cez SPI rozhranie. |
Stiahnuť |
Ethernet3 |
Knižnica pre AVR mikrokontroléry (ATmega) Arduino Uno / Nano / Mega. Umožňuje komunikáciu s Ethernet modulom Wiznet W5500 V2 - USR-ES1 cez SPI rozhranie. |
Stiahnuť |
UIPEthernet |
Knižnica pre AVR mikrokontroléry (ATmega) Arduino Uno / Nano / Mega. Umožňuje komunikáciu s Ethernet modulom ENC28J60 od Microchipu cez SPI rozhranie. |
Stiahnuť |
/*|------------------------------------------------------------------------------------|*/ /*|Projekt: Hladinomer - HTTPS - Compact - HC-SR04 / JSN-SR04T / HY-SRF05 |*/ /*|ESP8266 (NodeMCU, Wemos D1 Mini), ESP32 (DevKit, Standalone) |*/ /*|Autor: 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/ |*/ /*|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 iba po HTTPS |*/ /*|Pre Arduino + Ethernet HTTPS nie je podporované! Možno využiť len HTTP variant! |*/ /*|Na zaklade zvolenej platformy v Arduino IDE sa vykona kompilacia podla direktiv |*/ /*|Licencia pouzitia: MIT |*/ /*|Revízia: 15. December 2021 |*/ /*|------------------------------------------------------------------------------------|*/ const char* host = "hladinomer.000webhostapp.com"; //adresa webservera (doména) na ktorú sa odosielajú dáta String url = "/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápisconst char * ssid = "WIFI_NAME"; //MENO WiFi SIETE const char * password = "WIFI_PASSWORD"; //HESLO WiFi SIETE
//Root CA cert --> CERTIFIKÁT CERTIFIKAČNEJ AUTORITY, KTORÁ VYDALA CERTIFIKÁT VÁŠ WEBSERVER v .pem formáte //DST ROOT CA X3 EXAMPLE (https://i.imgur.com/fvw4huT.png)const static char* test_root_ca PROGMEM = \ "-----BEGIN CERTIFICATE-----\n" \ "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \ "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \ "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \ "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \ "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" \ "9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" \ "CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" \ "nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" \ "43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" \ "T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" \ "gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" \ "BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" \ "TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" \ "DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \ "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \ "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \ "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \ "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ "-----END CERTIFICATE-----\n";
#include <WiFiClientSecure.h> #if defined(ESP8266) #include <ESP8266WiFi.h> #include <NewPingESP8266.h> #define pinTrigger 5 //D1 #define pinEcho 4 //D2 #define maxVzdialenost 450 X509List cert(test_root_ca); #elif defined(ESP32) #include <WiFi.h> #include <NewPingESP8266.h> #include "esp_system.h" #define pinTrigger 22 //D22 #define pinEcho 23 //D23 #define maxVzdialenost 450 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 WiFiClientSecure client; NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); unsigned long timer2 = 0; 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(""); Serial.println(F("Wifi pripojene s IP:")); Serial.println(WiFi.localIP()); #if defined(ESP8266) // Set time via NTP, as required for x.509 validation configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 * 3600 * 2) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println(""); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); Serial.printf("Using certificate: %s\n", test_root_ca); client.setTrustAnchors(&cert); ESP.wdtEnable(30000); //30s SW watchdog #elif defined(ESP32) client.setCACert(test_root_ca); 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 (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom } #if defined(ESP8266) while (WiFi.status() != WL_CONNECTED) { ESP.wdtFeed(); //feed watchdog 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.")); String data = "hodnota=" + String(vzdialenost) + "&token=123456789"; if (client.connect(host, 443)) { 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, 443)) { 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 }
/*|------------------------------------------------------------------------------------|*/ /*|Projekt: Hladinomer - HTTPS - 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 pre HTTP: 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: 15. December 2021 |*/ /*|------------------------------------------------------------------------------------|*/ #include <WiFiUdp.h> #include <ArduinoOTA.h> #include <NewPingESP8266.h> #include <WiFiClientSecure.h> const char* host = "hladinomer.000webhostapp.com"; //adresa webservera (doména) na ktorú sa odosielajú dáta String url = "/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápisconst char * ssid = "WIFI_NAME"; //MENO WiFi SIETE const char * password = "WIFI_PASSWORD"; //HESLO WiFi SIETE
//Root CA cert --> CERTIFIKÁT CERTIFIKAČNEJ AUTORITY, KTORÁ VYDALA CERTIFIKÁT VÁŠ WEBSERVER v .pem formáte //DST ROOT CA X3 EXAMPLE (https://i.imgur.com/fvw4huT.png)const static char* test_root_ca PRO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kIoyQY/Esr\n" \ "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \ "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \ "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \ "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ "-----END CERTIFICATE-----\n";
#if defined(ESP32) #include <WiFi.h> #include <ESPmDNS.h> #include "esp_system.h" #define pinTrigger 22 #define pinEcho 23 #define maxVzdialenost 450 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 X509List cert(test_root_ca); #endif NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); unsigned long timer2 = 0; WiFiClientSecure 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) client.setCACert(test_root_ca); 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) // Set time via NTP, as required for x.509 validation configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 * 3600 * 2) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println(""); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); Serial.printf("Using certificate: %s\n", test_root_ca); client.setTrustAnchors(&cert); 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, 443)) { 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(); } }
/*|-----------------------------------------------------------------------------------|*/ /*|Projekt: Hladinomer - HTTPS - 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/ |*/ /*|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: 15. December 2021 |*/ /*|-----------------------------------------------------------------------------------|*/ const char* host = "hladinomer.000webhostapp.com"; //adresa webservera (doména) na ktorú sa odosielajú dáta String url = "/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápisconst char * ssid = "WIFI_NAME"; //MENO WiFi SIETE const char * password = "WIFI_PASSWORD"; //HESLO WiFi SIETE
//Root CA cert --> CERTIFIKÁT CERTIFIKAČNEJ AUTORITY, KTORÁ VYDALA CERTIFIKÁT VÁŠ WEBSERVER v .pem formáte //DST ROOT CA X3 EXAMPLE (https://i.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MucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \ "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \ "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \ "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \ "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ "-----END CERTIFICATE-----\n";
#include <NewPingESP8266.h> #include <WiFiClientSecure.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 X509List cert(test_root_ca); #endif NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost); WiFiClientSecure 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()); #if defined(ESP32) client.setCACert(test_root_ca); #elif defined(ESP8266) // Set time via NTP, as required for x.509 validation configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 * 3600 * 2) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println(""); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); Serial.printf("Using certificate: %s\n", test_root_ca); client.setTrustAnchors(&cert); #endif 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, 443)) { 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() { }
/*|------------------------------------------------------------------------------------|*/ /*|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 pre HTTP: 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: 1. April 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: https://vasweb.sk/cesta/data.php (+ 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 } } }
/*|-----------------------------------------------------------------------------------|*/ /*|Projekt: Hladinomer - HTTPS - HC-SR04 / JSN-SR04T / HY-SRF05 |*/ /*|ESP32 (DevKit, Generic) - ESP-IDF v4.3.X |*/ /*|Autor: Martin Chlebovec (martinius96) |*/ /*|E-mail: martinius96@gmail.com |*/ /*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/ /*|Testovacie webove rozhranie HTTPS: https://hladinomer.000webhostapp.com/ |*/ /*|Revízia: 20. Január 2022 |*/ /*|-----------------------------------------------------------------------------------|*/ #include <string.h> #include <stdlib.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" #include "protocol_examples_common.h" #include "esp_netif.h" #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" #include "lwip/netdb.h" #include "lwip/dns.h" #include "mbedtls/platform.h" #include "mbedtls/net_sockets.h" #include "mbedtls/esp_debug.h" #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/error.h" #include "mbedtls/certs.h" #include "esp_crt_bundle.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 /* Constants that aren't configurable in menuconfig */ #define WEB_SERVER "hladinomer.000webhostapp.com" #define WEB_PORT "443" static const char *TAG = "https_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++; } } esp_err_t res = ultrasonic_measure_cm(&sensor, MAX_DISTANCE_CM, &distance); if (res == ESP_OK) { avg_distance = avg_distance / 10; distance = avg_distance; xQueueSend(q,(void *)&distance,(TickType_t )0); // add the value to the queue } for(int countdown = 300; countdown >= 0; countdown--) { ESP_LOGI(TAG2, "%d... ", countdown); vTaskDelay(1000 / portTICK_PERIOD_MS); } } } static void https_get_task(void *pvParameters) { uint32_t distance; if(q == NULL){ printf("Queue is not ready \n"); return; } char buf[512]; int ret, flags, len; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ssl_context ssl; mbedtls_x509_crt cacert; mbedtls_ssl_config conf; mbedtls_net_context server_fd; mbedtls_ssl_init(&ssl); mbedtls_x509_crt_init(&cacert); mbedtls_ctr_drbg_init(&ctr_drbg); ESP_LOGI(TAG, "Seeding the random number generator"); mbedtls_ssl_config_init(&conf); mbedtls_entropy_init(&entropy); if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)) != 0) { ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", ret); abort(); } ESP_LOGI(TAG, "Attaching the certificate bundle..."); ret = esp_crt_bundle_attach(&conf); if(ret < 0) { ESP_LOGE(TAG, "esp_crt_bundle_attach returned -0x%x\n\n", -ret); abort(); } ESP_LOGI(TAG, "Setting hostname for TLS session..."); /* Hostname set here should match CN in server certificate */ if((ret = mbedtls_ssl_set_hostname(&ssl, WEB_SERVER)) != 0) { ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret); abort(); } ESP_LOGI(TAG, "Setting up the SSL/TLS structure..."); if((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret); goto exit; } /* MBEDTLS_SSL_VERIFY_OPTIONAL is bad for security, in this example it will print a warning if CA verification fails but it will continue to connect. You should consider using MBEDTLS_SSL_VERIFY_REQUIRED in your own code. */ mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL); mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); #ifdef CONFIG_MBEDTLS_DEBUG mbedtls_esp_enable_debug_log(&conf, CONFIG_MBEDTLS_DEBUG_LEVEL); #endif if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", -ret); goto exit; } while(1) { xQueueReceive(q,&distance,portMAX_DELAY); char REQUEST [1000]; char values [250]; sprintf(values, "hodnota=%d&token=123456789", distance); sprintf (REQUEST, "POST /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); mbedtls_net_init(&server_fd); ESP_LOGI(TAG, "Connecting to %s:%s...", WEB_SERVER, WEB_PORT); if ((ret = mbedtls_net_connect(&server_fd, WEB_SERVER, WEB_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) { ESP_LOGE(TAG, "mbedtls_net_connect returned -%x", -ret); goto exit; } ESP_LOGI(TAG, "Connected."); mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); ESP_LOGI(TAG, "Performing the SSL/TLS handshake..."); while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { ESP_LOGE(TAG, "mbedtls_ssl_handshake returned -0x%x", -ret); goto exit; } } ESP_LOGI(TAG, "Verifying peer X.509 certificate..."); if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) { /* In real life, we probably want to close connection if ret != 0 */ ESP_LOGW(TAG, "Failed to verify peer certificate!"); bzero(buf, sizeof(buf)); mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", flags); ESP_LOGW(TAG, "verification info: %s", buf); } else { ESP_LOGI(TAG, "Certificate verified."); } ESP_LOGI(TAG, "Cipher suite is %s", mbedtls_ssl_get_ciphersuite(&ssl)); ESP_LOGI(TAG, "Writing HTTP request..."); size_t written_bytes = 0; do { ret = mbedtls_ssl_write(&ssl, (unsigned char *)REQUEST + written_bytes, strlen(REQUEST) - written_bytes); if (ret >= 0) { ESP_LOGI(TAG, "%d bytes written", ret); written_bytes += ret; } else if (ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret != MBEDTLS_ERR_SSL_WANT_READ) { ESP_LOGE(TAG, "mbedtls_ssl_write returned -0x%x", -ret); goto exit; } } while(written_bytes < strlen(REQUEST)); ESP_LOGI(TAG, "Reading HTTP response..."); do { len = sizeof(buf) - 1; bzero(buf, sizeof(buf)); ret = mbedtls_ssl_read(&ssl, (unsigned char *)buf, len); if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) continue; if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { ret = 0; break; } if(ret < 0) { ESP_LOGE(TAG, "mbedtls_ssl_read returned -0x%x", -ret); break; } if(ret == 0) { ESP_LOGI(TAG, "connection closed"); break; } len = ret; ESP_LOGD(TAG, "%d bytes read", len); /* Print response directly to stdout as it is read */ for(int i = 0; i < len; i++) { putchar(buf[i]); } } while(1); mbedtls_ssl_close_notify(&ssl); exit: mbedtls_ssl_session_reset(&ssl); mbedtls_net_free(&server_fd); if(ret != 0) { mbedtls_strerror(ret, buf, 100); ESP_LOGE(TAG, "Last error was: -0x%x - %s", -ret, buf); } putchar('\n'); // JSON output doesn't have a newline at end static int request_count; ESP_LOGI(TAG, "Completed %d requests", ++request_count); 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("Measurement task started\n"); xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL); printf("HTTPS socket task started\n"); }else{ printf("Queue creation failed"); } }