Advertisement
martinius96

ESP32 - WiFi sensor IoT Node /w ultrasonic distance sensor

Dec 20th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*|-------------------------------------------------------------------------------|*/
  3. /*|Project: Ultrasonic sensor node - HTTPS - FreeRTOS - HC-SR04 / JSN-SR04T       |*/
  4. /*|ESP32 (DevKit, Generic)                                                        |*/
  5. /*|Author: Martin Chlebovec (martinius96)                                         |*/
  6. /*|E-mail: martinius96@gmail.com                                                  |*/
  7. /*|Project info: https://martinius96.github.io/hladinomer-studna-scripty/en/      |*/
  8. /*|Test web interface: https://hladinomer.eu/?lang=en                             |*/
  9. /*|Buy me coffee: https://paypal.me/chlebovec                                     |*/
  10. /*|Revision: 31. Oct. 2024                                                        |*/
  11. /*|-------------------------------------------------------------------------------|*/
  12.  
  13. #include <WiFi.h>
  14. #include <WiFiClientSecure.h>
  15. #include <NewPingESP8266.h>
  16.  
  17. const char * ssid = "MY_WIFI"; //WiFi SSID name
  18. const char * password = "MY_WIFI_PASSWORD"; //WiFi password
  19.  
  20. const char* host = "hladinomer.eu"; //host (server)
  21. String url = "/data.php"; //URL address to to target PHP file
  22.  
  23. #define pinTrigger    22
  24. #define pinEcho       23
  25. #define maxVzdialenost 450
  26. NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost);
  27.  
  28. TaskHandle_t Task1; //ULTRASONIC MEASUREMENT
  29. TaskHandle_t Task2; //HTTPS POST request task
  30. QueueHandle_t  q = NULL; //Queue handler
  31.  
  32. WiFiClientSecure client; //Secured client object for HTTPS connection
  33. static void Task1code( void * parameter);
  34. static void Task2code( void * parameter);
  35.  
  36. //Root CA cert - ISRG Root X1
  37. const static char* test_root_ca PROGMEM = \
  38.     "-----BEGIN CERTIFICATE-----\n" \
  39.     "MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \
  40.     "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \
  41.     "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \
  42.     "WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \
  43.     "ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n" \
  44.     "MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n" \
  45.     "h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n" \
  46.     "0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U\n" \
  47.     "A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW\n" \
  48.     "T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH\n" \
  49.     "B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC\n" \
  50.     "B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv\n" \
  51.     "KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn\n" \
  52.     "OlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTn\n" \
  53.     "jh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbw\n" \
  54.     "qHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CI\n" \
  55.     "rU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV\n" \
  56.     "HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq\n" \
  57.     "hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL\n" \
  58.     "ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ\n" \
  59.     "3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK\n" \
  60.     "NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5\n" \
  61.     "ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n" \
  62.     "TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n" \
  63.     "jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n" \
  64.     "oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n" \
  65.     "4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n" \
  66.     "mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n" \
  67.     "emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n" \
  68.     "-----END CERTIFICATE-----\n";
  69.  
  70. void setup() {
  71.   Serial.begin(115200);
  72.   WiFi.begin(ssid, password); //connect to WiFi
  73.   while (WiFi.status() != WL_CONNECTED) {
  74.     delay(500);
  75.     Serial.print(F("."));
  76.   }
  77.   Serial.println(F(""));
  78.   Serial.println(F("Wifi connected with IP:"));
  79.   Serial.println(WiFi.localIP());
  80.   client.setCACert(test_root_ca);
  81.   q = xQueueCreate(20, sizeof(int)); //create Queue of 20 INT values
  82.   if (q != NULL) {
  83.     Serial.println(F("Queue FIFO buffer is created"));
  84.     vTaskDelay(1000 / portTICK_PERIOD_MS); //wait for a second
  85.     xTaskCreatePinnedToCore(
  86.       Task1code,   /* Task function. */
  87.       "Task1",     /* name of task. */
  88.       10000,       /* Stack size of task */
  89.       NULL,        /* parameter of the task */
  90.       1,           /* priority of the task */
  91.       &Task1,      /* Task handle to keep track of created task */
  92.       1);          /* pin task to core 1 */
  93.     Serial.println(F("Ultrasonic measurement task started - pinned to APP CPU"));
  94.     xTaskCreatePinnedToCore(
  95.       Task2code,   /* Task function. */
  96.       "Task2",     /* name of task. */
  97.       10000,       /* Stack size of task */
  98.       NULL,        /* parameter of the task */
  99.       1,           /* priority of the task */
  100.       &Task2,      /* Task handle to keep track of created task */
  101.       0);          /* pin task to core 0 */
  102.     Serial.println(F("HTTPS POST request task started - pinned to PRO CPU"));
  103.   } else {
  104.     Serial.println(F("Queue creation failed"));
  105.   }
  106. }
  107.  
  108. void loop() {
  109.   if (WiFi.status() != WL_CONNECTED) {
  110.     WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom
  111.   }
  112.   while (WiFi.status() != WL_CONNECTED) {
  113.     delay(500);
  114.     Serial.print(F("."));
  115.   }
  116.   yield();
  117.  
  118. }
  119.  
  120. static void Task1code( void * parameter) {
  121.   if (q == NULL) {
  122.     Serial.println(F("Queue in ultrasonic Measurement task is not ready"));
  123.     return;
  124.   }
  125.   while (1) {
  126.     int distance = sonar.ping_cm();
  127.     delay(50);
  128.     Serial.print(F("Test measurement: "));
  129.     Serial.print(distance);
  130.     Serial.println(F(" cm"));
  131.     if (distance > 0) {
  132.       distance = 0;
  133.       for (int i = 0; i < 10; i++) {
  134.         distance += sonar.ping_cm();
  135.         delay(50);
  136.       }
  137.       distance = distance / 10;
  138.       Serial.print(F("Distance to water level is: "));
  139.       Serial.print(distance);
  140.       Serial.println(F(" cm."));
  141.       xQueueSend(q, (void *)&distance, (TickType_t )0); //add the measurement value to Queue
  142.       for (int countdown = 300; countdown >= 0; countdown--) {
  143.         Serial.print(F("Next measurement in: "));
  144.         Serial.print(countdown);
  145.         Serial.println(F(" seconds"));
  146.         vTaskDelay(1000 / portTICK_PERIOD_MS);
  147.       }
  148.     }
  149.   }
  150. }
  151. static void Task2code( void * parameter) {
  152.   int distance;
  153.   if (q == NULL) {
  154.     Serial.println(F("Queue in HTTP socket task is not ready"));
  155.     return;
  156.   }
  157.   while (1) {
  158.     xQueuePeek(q, &distance, portMAX_DELAY); //read measurement value from Queue and run code below, if no value, WAIT....
  159.     String data = "hodnota=" + String(distance) + "&token=123456789";
  160.     client.stop(); //close all opened connections
  161.     if (client.connect(host, 443)) {
  162.       Serial.println(F("Connected to server successfully"));
  163.       client.println("POST " + url + " HTTP/1.0");
  164.       client.println("Host: " + (String)host);
  165.       client.println(F("User-Agent: ESP"));
  166.       client.println(F("Connection: close"));
  167.       client.println(F("Content-Type: application/x-www-form-urlencoded;"));
  168.       client.print(F("Content-Length: "));
  169.       client.println(data.length());
  170.       client.println();
  171.       client.println(data);
  172.       Serial.println(F("Datas were sent to server successfully"));
  173.       xQueueReset(q); //EMPTY QUEUE, IF REQUEST WAS SUCCESSFUL, OTHERWISE RUN REQUEST AGAIN
  174.       while (client.connected()) {
  175.         String line = client.readStringUntil('\n');
  176.         if (line == "\r") {
  177.           break;
  178.         }
  179.       }
  180.       String line = client.readStringUntil('\n');
  181.     } else {
  182.       Serial.println(F("Connection to webserver was NOT successful"));
  183.     }
  184.     client.stop(); //close all opened connections
  185.   }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement