tecnologia:iot:esp32_post
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tecnologia:iot:esp32_post [2022/05/06 10:42] – created admin | tecnologia:iot:esp32_post [2022/05/06 15:39] (current) – admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== ESP32 POST ====== | ====== ESP32 POST ====== | ||
| + | Consumo con deep sleep: 0.013A | ||
| + | |||
| + | Cálculo: | ||
| + | 156mWh - 2 horas | ||
| + | 1872mWh dia | ||
| + | pila tiene 11W -> 5.8 dias | ||
| + | |||
| Queremos hacer la lectura de un valor y enviarlo por post. Empezamos por la plantilla de OTA: | Queremos hacer la lectura de un valor y enviarlo por post. Empezamos por la plantilla de OTA: | ||
| - | <codw> | + | <code> |
| #include < | #include < | ||
| #include < | #include < | ||
| Line 54: | Line 61: | ||
| + | Aladimos que haga un post. El tiempo hay que poner el incial igual al final, para que haga una nada mas empezar y luego se espere el tiempo que queramos, en este caso 1 hora | ||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | const char* ssid = " | ||
| + | const char* password = " | ||
| + | |||
| + | IPAddress ip(192, | ||
| + | IPAddress gateway(192, | ||
| + | IPAddress subnet(255, | ||
| + | IPAddress dns(8, | ||
| + | |||
| + | AsyncWebServer server(80); | ||
| + | |||
| + | |||
| + | //Your Domain name with URL path or IP address with path | ||
| + | String serverName = " | ||
| + | |||
| + | // the following variables are unsigned longs because the time, measured in | ||
| + | // milliseconds, | ||
| + | unsigned long lastTime = 3600000; | ||
| + | // Timer set to 10 minutes (600000) | ||
| + | //unsigned long timerDelay = 600000; | ||
| + | // Set timer to 5 seconds (5000) | ||
| + | unsigned long timerDelay = 3600000; | ||
| + | |||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | delay(10); | ||
| + | Serial.println(' | ||
| + | | ||
| + | WiFi.begin(ssid, | ||
| + | WiFi.config(ip, | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(ssid); | ||
| + | |||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(' | ||
| + | } | ||
| + | |||
| + | Serial.println(' | ||
| + | Serial.println(" | ||
| + | Serial.print(" | ||
| + | Serial.println(WiFi.localIP()); | ||
| + | |||
| + | server.on("/", | ||
| + | request-> | ||
| + | }); | ||
| + | |||
| + | AsyncElegantOTA.begin(& | ||
| + | server.begin(); | ||
| + | Serial.println(" | ||
| + | delay(1000); | ||
| + | | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | |||
| + | if ((millis() - lastTime) > timerDelay) { | ||
| + | //Check WiFi connection status | ||
| + | if(WiFi.status()== WL_CONNECTED){ | ||
| + | HTTPClient http; | ||
| + | |||
| + | String serverPath = serverName + "? | ||
| + | | ||
| + | // Your Domain name with URL path or IP address with path | ||
| + | http.begin(serverPath.c_str()); | ||
| + | | ||
| + | // Send HTTP GET request | ||
| + | int httpResponseCode = http.GET(); | ||
| + | | ||
| + | if (httpResponseCode> | ||
| + | Serial.print(" | ||
| + | Serial.println(httpResponseCode); | ||
| + | String payload = http.getString(); | ||
| + | Serial.println(payload); | ||
| + | } | ||
| + | else { | ||
| + | Serial.print(" | ||
| + | Serial.println(httpResponseCode); | ||
| + | } | ||
| + | // Free resources | ||
| + | http.end(); | ||
| + | } | ||
| + | else { | ||
| + | Serial.println(" | ||
| + | } | ||
| + | lastTime = millis(); | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | Añadimos la lectura para que la envíe en el POST: | ||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | const char* ssid = " | ||
| + | const char* password = " | ||
| + | |||
| + | IPAddress ip(192, | ||
| + | IPAddress gateway(192, | ||
| + | IPAddress subnet(255, | ||
| + | IPAddress dns(8, | ||
| + | |||
| + | AsyncWebServer server(80); | ||
| + | |||
| + | |||
| + | //Your Domain name with URL path or IP address with path | ||
| + | String serverName = " | ||
| + | |||
| + | // the following variables are unsigned longs because the time, measured in | ||
| + | // milliseconds, | ||
| + | unsigned long lastTime = 3600000; | ||
| + | // Timer set to 10 minutes (600000) | ||
| + | //unsigned long timerDelay = 600000; | ||
| + | // Set timer to 5 seconds (5000) | ||
| + | unsigned long timerDelay = 3600000; | ||
| + | |||
| + | /////////////////////////////////////////////// | ||
| + | //// ///// | ||
| + | //// | ||
| + | //// ///// | ||
| + | /////////////////////////////////////////////// | ||
| + | const int trigPin = 5; | ||
| + | const int echoPin = 18; | ||
| + | | ||
| + | //define sound speed in cm/uS | ||
| + | #define SOUND_SPEED 0.034 | ||
| + | #define CM_TO_INCH 0.393701 | ||
| + | |||
| + | long duration; | ||
| + | float distanceCm; | ||
| + | float distanceInch; | ||
| + | | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | delay(10); | ||
| + | Serial.println(' | ||
| + | |||
| + | // Definimos pin ultrasonido | ||
| + | pinMode(trigPin, | ||
| + | pinMode(echoPin, | ||
| + | | ||
| + | WiFi.begin(ssid, | ||
| + | WiFi.config(ip, | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(ssid); | ||
| + | |||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(' | ||
| + | } | ||
| + | |||
| + | Serial.println(' | ||
| + | Serial.println(" | ||
| + | Serial.print(" | ||
| + | Serial.println(WiFi.localIP()); | ||
| + | |||
| + | server.on("/", | ||
| + | request-> | ||
| + | }); | ||
| + | |||
| + | AsyncElegantOTA.begin(& | ||
| + | server.begin(); | ||
| + | Serial.println(" | ||
| + | delay(1000); | ||
| + | | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | |||
| + | // Clears the trigPin | ||
| + | digitalWrite(trigPin, | ||
| + | delayMicroseconds(2); | ||
| + | // Sets the trigPin on HIGH state for 10 micro seconds | ||
| + | digitalWrite(trigPin, | ||
| + | delayMicroseconds(10); | ||
| + | digitalWrite(trigPin, | ||
| + | | ||
| + | // Reads the echoPin, returns the sound wave travel time in microseconds | ||
| + | duration = pulseIn(echoPin, | ||
| + | | ||
| + | // Calculate the distance | ||
| + | distanceCm = duration * SOUND_SPEED/ | ||
| + | |||
| + | | ||
| + | if ((millis() - lastTime) > timerDelay) { | ||
| + | //Check WiFi connection status | ||
| + | if(WiFi.status()== WL_CONNECTED){ | ||
| + | HTTPClient http; | ||
| + | |||
| + | String serverPath = serverName + "? | ||
| + | | ||
| + | // Your Domain name with URL path or IP address with path | ||
| + | http.begin(serverPath.c_str()); | ||
| + | | ||
| + | // Send HTTP GET request | ||
| + | int httpResponseCode = http.GET(); | ||
| + | | ||
| + | if (httpResponseCode> | ||
| + | Serial.print(" | ||
| + | Serial.println(httpResponseCode); | ||
| + | String payload = http.getString(); | ||
| + | Serial.println(payload); | ||
| + | } | ||
| + | else { | ||
| + | Serial.print(" | ||
| + | Serial.println(httpResponseCode); | ||
| + | } | ||
| + | // Free resources | ||
| + | http.end(); | ||
| + | } | ||
| + | else { | ||
| + | Serial.println(" | ||
| + | } | ||
| + | lastTime = millis(); | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Ahora hacemos un sleep para que no gaste energía durante esa hora: | ||
| + | < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | const char* ssid = " | ||
| + | const char* password = " | ||
| + | |||
| + | IPAddress ip(192, | ||
| + | IPAddress gateway(192, | ||
| + | IPAddress subnet(255, | ||
| + | IPAddress dns(8, | ||
| + | |||
| + | AsyncWebServer server(80); | ||
| + | |||
| + | |||
| + | //Your Domain name with URL path or IP address with path | ||
| + | String serverName = " | ||
| + | |||
| + | #define uS_TO_S_FACTOR 1000000ULL | ||
| + | #define TIME_TO_SLEEP | ||
| + | RTC_DATA_ATTR int bootCount = 0; | ||
| + | |||
| + | |||
| + | /////////////////////////////////////////////// | ||
| + | //// ///// | ||
| + | //// | ||
| + | //// ///// | ||
| + | /////////////////////////////////////////////// | ||
| + | const int trigPin = 5; | ||
| + | const int echoPin = 18; | ||
| + | | ||
| + | //define sound speed in cm/uS | ||
| + | #define SOUND_SPEED 0.034 | ||
| + | #define CM_TO_INCH 0.393701 | ||
| + | |||
| + | long duration; | ||
| + | float distanceCm; | ||
| + | float distanceInch; | ||
| + | | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | delay(10); | ||
| + | Serial.println(' | ||
| + | |||
| + | // Definimos pin ultrasonido | ||
| + | pinMode(trigPin, | ||
| + | pinMode(echoPin, | ||
| + | | ||
| + | WiFi.begin(ssid, | ||
| + | WiFi.config(ip, | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(ssid); | ||
| + | |||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(' | ||
| + | } | ||
| + | |||
| + | Serial.println(' | ||
| + | Serial.println(" | ||
| + | Serial.print(" | ||
| + | Serial.println(WiFi.localIP()); | ||
| + | |||
| + | server.on("/", | ||
| + | request-> | ||
| + | }); | ||
| + | |||
| + | AsyncElegantOTA.begin(& | ||
| + | server.begin(); | ||
| + | Serial.println(" | ||
| + | delay(1000); | ||
| + | |||
| + | enviaLectura(); | ||
| + | |||
| + | esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); | ||
| + | Serial.println(" | ||
| + | |||
| + | //Go to sleep now | ||
| + | esp_deep_sleep_start(); | ||
| + | } | ||
| + | |||
| + | void loop() {} | ||
| + | |||
| + | |||
| + | String enviaLectura() { | ||
| + | |||
| + | // Clears the trigPin | ||
| + | digitalWrite(trigPin, | ||
| + | delayMicroseconds(2); | ||
| + | // Sets the trigPin on HIGH state for 10 micro seconds | ||
| + | digitalWrite(trigPin, | ||
| + | delayMicroseconds(10); | ||
| + | digitalWrite(trigPin, | ||
| + | | ||
| + | // Reads the echoPin, returns the sound wave travel time in microseconds | ||
| + | duration = pulseIn(echoPin, | ||
| + | | ||
| + | // Calculate the distance | ||
| + | distanceCm = duration * SOUND_SPEED/ | ||
| + | |||
| + | | ||
| + | //Check WiFi connection status | ||
| + | if(WiFi.status()== WL_CONNECTED){ | ||
| + | HTTPClient http; | ||
| + | |||
| + | String serverPath = serverName + "? | ||
| + | | ||
| + | // Your Domain name with URL path or IP address with path | ||
| + | http.begin(serverPath.c_str()); | ||
| + | | ||
| + | // Send HTTP GET request | ||
| + | int httpResponseCode = http.GET(); | ||
| + | | ||
| + | if (httpResponseCode> | ||
| + | Serial.print(" | ||
| + | Serial.println(httpResponseCode); | ||
| + | String payload = http.getString(); | ||
| + | Serial.println(payload); | ||
| + | } | ||
| + | else { | ||
| + | Serial.print(" | ||
| + | Serial.println(httpResponseCode); | ||
| + | } | ||
| + | // Free resources | ||
| + | http.end(); | ||
| + | } | ||
| + | else { | ||
| + | Serial.println(" | ||
| + | } | ||
| + | delay(1000); | ||
| + | return " | ||
| + | } | ||
| + | </ | ||
tecnologia/iot/esp32_post.1651833751.txt.gz · Last modified: by admin
