Hi everybody.
I want to use 1 ESP32 as an intermediary device, connect to push buttons, each button when used will post the url line of Blynk API, control 1 device connecting Blynk 2.0
The problem is, I’m not a programmer, but just an IOT Blynk practitioner, so I don’t know how to code.
When I apply the below line of code, an error is generated and the device cannot be controlled. I hope the experts will research and rewrite the code accordingly.
Thank you very much.
#include <WiFi.h>
#include <HTTPClient.h>
static uint8_t SwitchPin1 = 13; //D13
const char* ssid = "HIEU VAI THANH";
const char* password = "xxxxxxxxxxxxx";
void setup() {
Serial.begin(115200);
delay(4000); //Delay needed before calling the WiFi.begin
pinMode(SwitchPin1, INPUT_PULLUP);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { //Check for the connection
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
}
void loop() {
if (digitalRead(SwitchPin1) == HIGH) {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http;
http.begin("https://blynk.cloud/external/api/update"); //Specify destination for HTTP request
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
int httpResponseCode = http.POST("token=dMUNVOF6rbo-ZVNm4E8G9CbvpI1qAE8U&v12=1"; //Send the actual POST request
if(httpResponseCode>0){
String response = http.getString(); //Get the response to the request
Serial.println("httpResponseCode = "); //Print return code
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
}else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}else{
Serial.println("Error in WiFi connection");
}
delay(10000); //Send a request every 10 seconds
}
}
ERROR
22:32:10.581 -> Connecting to WiFi..
22:32:11.612 -> Connecting to WiFi..
22:32:12.588 -> Connecting to WiFi..
22:32:12.588 -> Connected to the WiFi network
22:32:13.703 -> httpResponseCode =
22:32:13.703 -> 400
22:32:13.703 -> {"error":{"message":"No token provided."}}
22:32:25.153 -> httpResponseCode =
22:32:25.153 -> 400
22:32:25.153 -> {"error":{"message":"No token provided."}}