/*|-----------------------------------------------------------------------------------|*/
/*|Projekt: Hladinomer - HTTP - FreeRTOS - HC-SR04 / JSN-SR04T / HY-SRF05 |*/
/*|ESP32 (DevKit, Generic) |*/
/*|Autor: Martin Chlebovec (martinius96) |*/
/*|E-mail: [email protected] |*/
/*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
/*|Testovacie webove rozhranie: http://arduino.clanweb.eu/studna_s_prekladom/ |*/
/*|Licencia pouzitia: MIT |*/
/*|Revízia: 2. Január 2022 |*/
/*|-----------------------------------------------------------------------------------|*/
#include <WiFi.h>
#include "NewPingESP8266.h"
const char * ssid = "Wokwi-GUEST"; //MENO WiFi SIETE
const char* host = "arduino.clanweb.eu"; //adresa webservera (doména) na ktorú sa odosielajú dáta
#define pinTrigger 5
#define pinEcho 4
#define maxVzdialenost 450
NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost);
TaskHandle_t Task1; //ULTRASONIC MEASUREMENT
TaskHandle_t Task2; //WIFI HTTP SOCKET
QueueHandle_t q = NULL;
WiFiClient client;
static void Task1code( void * parameter);
static void Task2code( void * parameter);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid); //pripoj sa na wifi siet s heslom
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(F(""));
Serial.println(F("Wifi connected with IP:"));
Serial.println(WiFi.localIP());
q = xQueueCreate(20, sizeof(int));
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"));
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("HTTP Socket task started"));
} else {
Serial.println(F("Queue creation failed"));
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid); //pripoj sa na wifi siet s heslom
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
yield();
}
static void Task1code( void * parameter) {
if (q == NULL) {
Serial.println(F("Queue in 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
delay(300000);
}
}
}
static void Task2code( void * parameter) {
int distance;
if (q == NULL) {
Serial.println(F("Queue in HTTP socket task is not ready"));
return;
}
while (1) {
xQueueReceive(q, &distance, portMAX_DELAY); //read measurement value from Queue and run code below, if no value, WAIT....
client.stop();
if (client.connect(host, 80)) {
Serial.println(F("Connected to server successfully"));
String url = "/studna_s_prekladom/data.php?hodnota=" + String(distance) + "&token=123456789";
client.print(String("GET ") + url + " HTTP/1.0\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP32\r\n" + "Connection: close\r\n\r\n");
Serial.println(F("Datas were sent to server successfully"));
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println(line);
} else {
Serial.println(F("Connection to webserver was NOT successful"));
}
client.stop();
}
}