/*
Curso Online NodeMCU ESP32 FreeRTOS
Autor: Fernando Simplicio
www.microgenios.com.br
--Este Projeto tem por objetivo criar duas threads no FreeRTOS.
--Cada thread é responsável em enviar uma string pela UART do ESP32 de maneira concorrente.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
/*
* WiFi;
*/
#define WIFISSID "Wokwi-GUEST"
#define PASSWORD ""
static WiFiMulti wifiMulti;
/*
* Função de inicialização da Serial e Oled;
*/
static void wifi_init( void )
{
Serial.begin(115200);
for(uint8_t t = 4; t > 0; t--)
{
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(500);
}
wifiMulti.addAP( WIFISSID, PASSWORD );
for(;;){
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
} else break;
}
}
static void vTask_http( void *pvParameters )
{
Serial.println( "Task1 Init..." );
for( ;; )
{
if( (wifiMulti.run() == WL_CONNECTED) )
{
HTTPClient http;
http.begin("http://api.thingspeak.com/update?api_key=D48A9UAR7NS3S94S&field1=0");
http.addHeader( "Content-Type", "application/json" );
http.addHeader( "Connection", "close" );
int httpCode = http.GET();
if( httpCode > 0 )
{
Serial.printf("HTTP_CODE_RETURN: %d\n", httpCode );
if( httpCode == HTTP_CODE_OK )
{
String payload = http.getString();
Serial.println( payload.c_str() );
}
} else {
Serial.printf("[HTTP] POST.. falha, error: %s", http.errorToString(httpCode).c_str() );
}
http.end();
}
vTaskDelay( 20000 / portTICK_PERIOD_MS );
}
}
void setup( void )
{
/**
* Inicializa WiFi;
*/
wifi_init();
/**
* Cria a tarefa responsável em requisitar o servidor thingspeak;
*/
xTaskCreate(vTask_http, "Task 1", 1024*8, NULL, 2, NULL);
}
/**
* Task loop;
*/
void loop( void )
{
vTaskDelay( 100 / portTICK_PERIOD_MS );
}