// ESP32 use http post to run API Blynk
// https://community.blynk.cc/t/esp32-use-http-post-to-run-api-blynk/65887
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME "ESP32LED"
#define BLYNK_AUTH_TOKEN ""
#include <WiFi.h>
#include <HTTPClient.h>
#define LED1_PIN 4
#define LED2_PIN 2
#define SwitchPin1 13 //D13
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
float humidity=0; // 宣告一個資料型態為float的全域變數humidity
float temperature=0; // 宣告一個資料型態為float的全域變數temperature
long lastTime = 0 , startTime = 0;
String response;
void SendMessage(String message);
void setup(void) {
pinMode(LED1_PIN,OUTPUT);
pinMode(LED2_PIN,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
pinMode(SwitchPin1, INPUT_PULLUP);
// Debug console
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
delay(4000); // Delay needed before calling the WiFi.begin
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) { //Check for the connection
Serial.println("Connecting to WiFi..");
delay(1000);
}
Serial.println("Connected to the WiFi network");
for(int i=0;i<3;i++)
{
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);
digitalWrite(LED_BUILTIN,LOW);
delay(1000);
}
}
void loop(void) {
char url[100];
// if (digitalRead(SwitchPin1) == HIGH) {
long currTime = millis();
if (currTime - lastTime > 10000) {
lastTime = currTime;
humidity = random(100);
temperature = random(50);
Serial.println("-----------------------");
// http.begin("http://api.ipify.org/"); // Specify destination for HTTP request
// https://blynk.cloud/external/api/update?token={token}&pin={pin}&value={value}
sprintf(url , "https://blynk.cloud/external/api/update?token=%s&V5=%.2f",BLYNK_AUTH_TOKEN ,humidity);
Serial.println("message = " + (String)url);
SendMessage(url);
delay(1000);
}
// https://{server_address}/external/api/get?token={token}&{pin}
// https://blynk.cloud/external/api/get?token=XoNXUD1WSva6Jmuj9HPyeBr5Sx8ogbWj&v0
sprintf(url , "https://blynk.cloud/external/api/get?token=%s&V1",BLYNK_AUTH_TOKEN);
Serial.println("message = " + (String)url);
SendMessage(url);
if (response == "0") {
digitalWrite(LED2_PIN,HIGH);
Serial.println("LED2 on V1: off");
} else {
digitalWrite(LED2_PIN,LOW);
Serial.println("LED2 on V1: on");
}
// }
delay(1000); //Send a request every 1 seconds
}
void SendMessage(String url) {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http; // 創建HTTPClient對象
http.begin(url); // Specify destination for HTTP request
http.addHeader("Content-Type", "text/plain"); // Specify content-type header
int httpCode = http.GET();
//int httpCode = http.POST("");
Serial.println("http Response Code=" + String(httpCode)); //Print HTTP return code //200 is OK
if (httpCode == HTTP_CODE_OK) { // 如果取得資料成功200
response = http.getString(); // Get the response to the request
Serial.println(response); // Print request answer
}else{
Serial.println("Error on sending POST: " + (String)httpCode);
}
http.end(); // Free resources
}else{
Serial.println("Error in WiFi connection");
}
}
Loading
ssd1306
ssd1306