I am using ngrok in my PC to tunnel my server running in localhost. The ngrok has generated a static http domain for my localhost:80 . Now I need a program in my ESP8266 to send data to this ngrok tunnel using HTTP POST method. I want references for both the programs:
- The Arduino code that runs on ESP8266.
- The Python script that runs on the PC.
This is my ESP8266 reference code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
// Replace with your own WiFi network credentials
const char* ssid = "123";
const char* password = "123456789";
// Replace with the URL of the server you want to send the POST request to
const char* host = "https://ngrok-domain.ngrok.app";
const int port = 80;
void setup() {
// Start the serial communication for debugging
Serial.begin(115200);
// Connect to the WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Create a client object to handle the connection
WiFiClient client;
// Connect to the server
if (!client.connect(host, port)) {
Serial.println("Connection to server failed");
return;
}
// Define the URL and the payload of the POST request
String url = "/path";
String payload = "{\"key1\":\"123\"}";
// Debugging output
Serial.print("Sending POST request to ");
Serial.println(host);
// Send the POST request
client.print("POST " + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + payload.length() + "\r\n" +
"Connection: close\r\n\r\n" +
payload);
// Debugging output: display the response from the server
Serial.println("Response:");
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
// Close the connection
client.stop();
// Wait for 5 seconds before sending another POST request
delay(5000);
}