Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*|-------------------------------------------------------------------------------|*/
- /*|Project: Ultrasonic sensor node - HTTPS - FreeRTOS - HC-SR04 / JSN-SR04T |*/
- /*|ESP32 (DevKit, Generic) |*/
- /*|Author: Martin Chlebovec (martinius96) |*/
- /*|E-mail: martinius96@gmail.com |*/
- /*|Project info: https://martinius96.github.io/hladinomer-studna-scripty/en/ |*/
- /*|Test web interface: https://hladinomer.eu/?lang=en |*/
- /*|Buy me coffee: https://paypal.me/chlebovec |*/
- /*|Revision: 31. Oct. 2024 |*/
- /*|-------------------------------------------------------------------------------|*/
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <NewPingESP8266.h>
- const char * ssid = "MY_WIFI"; //WiFi SSID name
- const char * password = "MY_WIFI_PASSWORD"; //WiFi password
- const char* host = "hladinomer.eu"; //host (server)
- String url = "/data.php"; //URL address to to target PHP file
- #define pinTrigger 22
- #define pinEcho 23
- #define maxVzdialenost 450
- NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost);
- TaskHandle_t Task1; //ULTRASONIC MEASUREMENT
- TaskHandle_t Task2; //HTTPS POST request task
- QueueHandle_t q = NULL; //Queue handler
- WiFiClientSecure client; //Secured client object for HTTPS connection
- static void Task1code( void * parameter);
- static void Task2code( void * parameter);
- //Root CA cert - ISRG Root X1
- const static char* test_root_ca PROGMEM = \
- "-----BEGIN CERTIFICATE-----\n" \
- "MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \
- "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \
- "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \
- "WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \
-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\
- "ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ\n" \
- "3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK\n" \
- "NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5\n" \
- "ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n" \
- "TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n" \
- "jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n" \
- "oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n" \
- "4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n" \
- "mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n" \
- "emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n" \
- "-----END CERTIFICATE-----\n";
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password); //connect to WiFi
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(F("."));
- }
- Serial.println(F(""));
- Serial.println(F("Wifi connected with IP:"));
- Serial.println(WiFi.localIP());
- client.setCACert(test_root_ca);
- q = xQueueCreate(20, sizeof(int)); //create Queue of 20 INT values
- if (q != NULL) {
- Serial.println(F("Queue FIFO buffer is created"));
- vTaskDelay(1000 / portTICK_PERIOD_MS); //wait for a second
- xTaskCreatePinnedToCore(
- Task1code, /* Task function. */
- "Task1", /* name of task. */
- 10000, /* Stack size of task */
- NULL, /* parameter of the task */
- 1, /* priority of the task */
- &Task1, /* Task handle to keep track of created task */
- 1); /* pin task to core 1 */
- Serial.println(F("Ultrasonic measurement task started - pinned to APP CPU"));
- xTaskCreatePinnedToCore(
- Task2code, /* Task function. */
- "Task2", /* name of task. */
- 10000, /* Stack size of task */
- NULL, /* parameter of the task */
- 1, /* priority of the task */
- &Task2, /* Task handle to keep track of created task */
- 0); /* pin task to core 0 */
- Serial.println(F("HTTPS POST request task started - pinned to PRO CPU"));
- } else {
- Serial.println(F("Queue creation failed"));
- }
- }
- 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(F("."));
- }
- yield();
- }
- static void Task1code( void * parameter) {
- if (q == NULL) {
- Serial.println(F("Queue in ultrasonic Measurement task is not ready"));
- return;
- }
- while (1) {
- int distance = sonar.ping_cm();
- delay(50);
- Serial.print(F("Test measurement: "));
- Serial.print(distance);
- Serial.println(F(" cm"));
- if (distance > 0) {
- distance = 0;
- for (int i = 0; i < 10; i++) {
- distance += sonar.ping_cm();
- delay(50);
- }
- distance = distance / 10;
- Serial.print(F("Distance to water level is: "));
- Serial.print(distance);
- Serial.println(F(" cm."));
- xQueueSend(q, (void *)&distance, (TickType_t )0); //add the measurement value to Queue
- for (int countdown = 300; countdown >= 0; countdown--) {
- Serial.print(F("Next measurement in: "));
- Serial.print(countdown);
- Serial.println(F(" seconds"));
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- }
- }
- }
- }
- static void Task2code( void * parameter) {
- int distance;
- if (q == NULL) {
- Serial.println(F("Queue in HTTP socket task is not ready"));
- return;
- }
- while (1) {
- xQueuePeek(q, &distance, portMAX_DELAY); //read measurement value from Queue and run code below, if no value, WAIT....
- String data = "hodnota=" + String(distance) + "&token=123456789";
- client.stop(); //close all opened connections
- if (client.connect(host, 443)) {
- Serial.println(F("Connected to server successfully"));
- 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("Datas were sent to server successfully"));
- xQueueReset(q); //EMPTY QUEUE, IF REQUEST WAS SUCCESSFUL, OTHERWISE RUN REQUEST AGAIN
- while (client.connected()) {
- String line = client.readStringUntil('\n');
- if (line == "\r") {
- break;
- }
- }
- String line = client.readStringUntil('\n');
- } else {
- Serial.println(F("Connection to webserver was NOT successful"));
- }
- client.stop(); //close all opened connections
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement