Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active May 20, 2023 18:31
Show Gist options
  • Save jenschr/ea59677e615f9c6fb8f6a88ae788e82a to your computer and use it in GitHub Desktop.
Save jenschr/ea59677e615f9c6fb8f6a88ae788e82a to your computer and use it in GitHub Desktop.
ESP32's can use use either a TCP-client or a HTTP Client to do POST requests. This snippet will let you do a POST request directly to an IP address.
#include <WiFi.h>
#include <HTTPClient.h>
// WiFi network name and password:
const char * networkName = "your-ssid";
const char * networkPswd = "your-password";
// Internet address to send POST data to
const char * hostDomain = "10.13.37.158";
const int hostPort = 3001;
const int LED_PIN = LED_BUILTIN;
void connectToWiFi(const char * ssid, const char * pwd)
{
Serial.println("Connecting to WiFi network: " + String(ssid));
WiFi.begin(ssid, pwd); // start connecting to the wifi network
while (WiFi.status() != WL_CONNECTED)
{
// Blink LED while we're connecting:
digitalWrite( LED_PIN, !digitalRead(LED_PIN) );
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void sendDataUsingTcp(const char * host, int port)
{
Serial.println("Connecting to domain: " + String(host));
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port))
{
Serial.println("connection failed");
return;
}
Serial.println("Connected!\n");
// This will send the POST request to the server
String dataToSend = "temp="+String(20.5)+"&humid="+ String(35);
int dataStringLength = dataToSend.length();
client.print((String)"POST /products HTTP/1.1\r\n" +
"Host: " + String(host) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: "+dataStringLength+"\r\n"+
"Connection: close\r\n\r\n"+
dataToSend);
// If something goes wrong, we need a timeout
unsigned long timeout = millis();
while (client.available() == 0)
{
if (millis() - timeout > 5000)
{
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
}
// When we are finished, we close the connection
Serial.println("closing connection");
client.stop();
}
// Alternate way to send data via HTTP POST
void sendDataUsingHttp()
{
HTTPClient http;
String serverUrl = "http://" + String(host) + ":" + String(port) + "/";
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
// Prepare the data & send it
String json = "{\"temperature\":" + String(20.5) + ",\"humidity\":" + String(35) + "}";
int httpCode = http.POST(json);
// Printing if the post request went through or not
if (httpCode > 0) {
String response = http.getString();
Serial.print("HTTP success! Sent: ");
Serial.println(json);
Serial.print("Response: ");
Serial.println(response);
} else {
Serial.println("Error on HTTP request");
}
delay(200);
http.end();
}
void setup()
{
Serial.begin(115200);
delay(5000);
connectToWiFi(networkName, networkPswd);
delay(1000);
sendDataUsingTcp(hostDomain, hostPort); // Connect to server
// sendDataUsingHttp();
}
void loop()
{
// We only post data once :-)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment