Last active
July 30, 2022 11:46
-
-
Save mikbuch/59753429f721f4a93d9592b9f21484c9 to your computer and use it in GitHub Desktop.
ESP8266 web server receiving GET requests and sending POST request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script is avilable as a gist: | |
// https://gist.github.com/mikbuch/59753429f721f4a93d9592b9f21484c9 | |
// ESP8266 web server receiving GET requests and sending POST request | |
// For a more advanced example, see: | |
// * "ESP8266_WebServerAndClient_PlusSerial.ino": https://gist.github.com/mikbuch/a5c7492fc6c29956c5755683ea23d35b | |
/* Requirements: | |
* * "esp8266" board installed. | |
* | |
* In order to install the board, go to: | |
* -- Tools => Board => Boards Manager => Install "esp8266" | |
* It also includes "Wemos" boards ("LOLIN"). | |
* | |
* Then you set the board as: | |
* -- Tools => Board => ESP8266 => Boards LOLIN (Wemos) D1 lite | |
* | |
* Now you can complie and upload the code to your Wemos board. | |
*/ | |
/* Connection status codes ("WiFi.status()"): | |
* 0 : WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses | |
* 1 : WL_NO_SSID_AVAILin case configured SSID cannot be reached | |
* 3 : WL_CONNECTED after successful connection is established | |
* 4 : WL_CONNECT_FAILED if connection failed | |
* 6 : WL_CONNECT_WRONG_PASSWORD if password is incorrect | |
* 7 : WL_DISCONNECTED if module is not configured in station mode | |
* | |
* Source: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#diagnostics | |
*/ | |
/* Instructon on how to use this example on the client side. | |
* | |
* The client side means that you will be sending requests | |
* to the server hosted at ESP8266, and you will be receiving | |
* requests. | |
* | |
* Use a root request (e.g., http://192.168.0.22/), or either of | |
* the two endpoints: | |
* (1) /switch (to switch diode), or | |
* (2) /status (to get the info about diode status: on or off). | |
* For example: http://192.168.0.22/status | |
* ---------------------------------------------------------------------- | |
* | |
*/ | |
/* | |
* Receiving GET requests: | |
* * / (root) -- basic information | |
* * /status -- is the LED diode turned on or off | |
* * /switch -- turn the LED on or off (switch its state) | |
* | |
* Sending POST requests, with plain text JSON in its body. | |
* Caution: create your own POST URL here: http://ptsv2.com/ | |
* -> "New random toilet" | |
* See also: https://stackoverflow.com/a/7194919 | |
* | |
* Sources & references (on the client): | |
* * https://randomnerdtutorials.com/esp8266-nodemcu-http-get-post-arduino/#http-get-2 | |
* * https://techtutorialsx.com/2016/07/21/esp8266-post-requests/ | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <WiFiClient.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
/* SSID is the name of your WiFi network, e.g.: "linksys", or "ab8402932". | |
* You have to set the variable STASSID below. | |
* -- | |
* STAPSK is the password you your WiFi (WLAN) network, i.e., the password | |
* to your router. This variable has to be set as well. | |
* | |
* To sum up, the 4 lines below this block comment should look shomehow | |
* like that: | |
* | |
* #ifndef STASSID | |
* #define STASSID "linksys" | |
* #define STAPSK "@sdjj2325k^#asd" | |
* #endif | |
*/ | |
#ifndef STASSID | |
#define STASSID "<your_WiFi_SSID>" | |
#define STAPSK "<your_WiFi_password>" | |
#endif | |
/* URL to send POST requests, with plain text JSON in its body. | |
* Caution: create your own POST URL here: http://ptsv2.com/ | |
* -> "New random toilet" | |
* See also: https://stackoverflow.com/a/7194919 */ | |
const char* url = "http://ptsv2.com/t/tdi0k-1641503634/post"; | |
const char* ssid = STASSID; | |
const char* password = STAPSK; | |
ESP8266WebServer server(80); | |
ESP8266WiFiMulti WiFiMulti; | |
int led_status = 0; | |
void handleRoot() { | |
Serial.println("Received a root (`/`) request."); | |
Serial.println("LED status is: " + String(led_status)); | |
Serial.println("----------------------------"); | |
// Server response | |
String message = "Use either of the two endpoints:\n"; | |
message += " (1) /switch (to switch diode)\n"; | |
message += " (2) /status (to get the info about diode status: on or off)\n"; | |
message += "For example: 192.168.0.22/status\n"; | |
message += "----------------------------\n"; | |
message += "LED status is now: " + String(led_status) + "\n"; | |
message += "----------------------------"; | |
server.send(200, "text/plain", message); | |
} | |
void handleLedSwitch() { | |
Serial.println("Received a `switch` request."); | |
if (led_status == 0) { | |
digitalWrite(LED_BUILTIN, LOW); | |
led_status = 1; | |
} else { | |
digitalWrite(LED_BUILTIN, HIGH); | |
led_status = 0; | |
} | |
Serial.println("LED status is: " + String(led_status)); | |
Serial.println("----------------------------"); | |
// Server response | |
String message = "Switched LED!\n\n"; | |
message += "Now status of the diode is: "; | |
message += String(led_status); | |
server.send(200, "text/plain", message); | |
} | |
void handleLedStatus() { | |
Serial.println("Received a `status` request."); | |
Serial.println("LED status is: " + String(led_status)); | |
Serial.println("----------------------------"); | |
// Server response | |
String message = "Status of the LED is: "; | |
message += String(led_status); | |
server.send(200, "text/plain", message); | |
} | |
void handleNotFound() { | |
digitalWrite(LED_BUILTIN, LOW); | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i = 0; i < server.args(); i++) { | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
/* ======================================= | |
* | |
* SETUP | |
* | |
* ======================================= | |
*/ | |
void setup(void) { | |
// Reset the serial monitor at the beginning. | |
Serial.flush(); | |
// Initial variables and setup | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, HIGH); | |
Serial.begin(115200); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
/* Wait for the connection | |
* | |
* Connection status codes: | |
* 0 : WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses | |
* 1 : WL_NO_SSID_AVAILin case configured SSID cannot be reached | |
* 3 : WL_CONNECTED after successful connection is established | |
* 4 : WL_CONNECT_FAILED if connection failed | |
* 6 : WL_CONNECT_WRONG_PASSWORD if password is incorrect | |
* 7 : WL_DISCONNECTED if module is not configured in station mode | |
* Source: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#diagnostics | |
*/ | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
Serial.print(WiFi.status()); | |
} | |
Serial.println(""); | |
Serial.println("----------------------------"); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin("esp8266")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", handleRoot); | |
server.on("/switch", handleLedSwitch); | |
server.on("/status", handleLedStatus); | |
server.on("/gif", []() { | |
static const uint8_t gif[] PROGMEM = { | |
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, | |
0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, | |
0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c, | |
0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b | |
}; | |
char gif_colored[sizeof(gif)]; | |
memcpy_P(gif_colored, gif, sizeof(gif)); | |
// Set the background to a random set of colors | |
gif_colored[16] = millis() % 256; | |
gif_colored[17] = millis() % 256; | |
gif_colored[18] = millis() % 256; | |
server.send(200, "image/gif", gif_colored, sizeof(gif_colored)); | |
}); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
Serial.println("----------------------------"); | |
} | |
void loop(void) { | |
server.handleClient(); | |
MDNS.update(); | |
/******************************* | |
* * | |
* WiFi Client * | |
* * | |
* Sending a periodic report * | |
* * | |
*******************************/ | |
// Wait for WiFi connection. | |
if ((WiFiMulti.run() == WL_CONNECTED)) { | |
WiFiClient client; | |
HTTPClient http; | |
Serial.print("\n\n\n"); | |
Serial.print("============================\n"); | |
Serial.print("[HTTP] begin...\n"); | |
// HTTP request start | |
if (http.begin(client, url)) { | |
//Specify content-type header | |
http.addHeader("Content-Type", "text/plain"); | |
Serial.print("[HTTP] POST...\n"); | |
// Start connection and send HTTP header | |
int httpCode = http.POST("{'temp1':'50', 'temp2':'53'}"); | |
// httpCode will be negative on error | |
if (httpCode > 0) { | |
// HTTP header has been send and Server response header has been handled | |
Serial.printf("[HTTP] POST... code: %d\n", httpCode); | |
// file found at server | |
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { | |
String payload = http.getString(); | |
Serial.println(payload); | |
} | |
} else { | |
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} else { | |
Serial.printf("[HTTP] Unable to connect\n"); | |
} | |
} | |
// Send a request every 5 seconds. | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment