Hello,
I am trying to post a simple text "hello world" to my esp8266. Here is the block I use to Post text.
There is a ESPAsyncWebServer running on the esp8266. It listen the request and show the text in the serial moniter. I have tested it with Postman and there is no problem. But when I use the app on smartphone, the esp8266 recognized the request but recivied no textdata. So I suppose the problem comes from the smartphone.
Here is the code arduino for esp8266.
#include "GFX4dIoD9.h"
#include "ESP8266WiFi.h"#include "Arduino.h"
#include "ESP8266WiFi.h"
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include "Hash.h"#define SENSOR_GREEN 0x00
#define SENSOR_PURPLE 0X01
#define SENSOR_RED 0X02
#define SENSOR_YELLOW 0X03
#define SENSOR_BLUE 0X04#define LAMP_10_P_6EV 0X00
#define LAMP_10_P_0EV 0X01GFX4dIoD9 gfx = GFX4dIoD9();
String s;const char* ssid = "aiRcovIoT";
const char* password = "aircospi";String t = "";
AsyncWebServer server(80); // Create AsyncWebServer object on port 80
const char index_html PROGMEM = R"rawliteral(%TEMPERATURE%)rawliteral";
String processor(const String& var)
{
if (var == "TEMPERATURE")
{
return String(t);
}
return String();
}void conf_106G_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.6eV,Green");
unsigned char hexConfData[2] = {LAMP_10_P_6EV, SENSOR_GREEN};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_106P_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.6eV,Purple");
unsigned char hexConfData[2] = {LAMP_10_P_6EV, SENSOR_PURPLE};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_106R_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.6eV,Red");
unsigned char hexConfData[2] = {LAMP_10_P_6EV, SENSOR_RED};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_106Y_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.6eV,Yellow");
unsigned char hexConfData[2] = {LAMP_10_P_6EV, SENSOR_YELLOW};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_106B_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.6eV,Blue");
unsigned char hexConfData[2] = {LAMP_10_P_6EV, SENSOR_BLUE};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_100G_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.0eV,Green,invalid configuration");
request->send_P(200, "text/plain", "invalid configuration");
}
void conf_100P_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.0eV,Purple");
unsigned char hexConfData[2] = {LAMP_10_P_0EV, SENSOR_PURPLE};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_100R_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.0eV,Red");
unsigned char hexConfData[2] = {LAMP_10_P_0EV, SENSOR_RED};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_100Y_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.0eV,Yellow");
unsigned char hexConfData[2] = {LAMP_10_P_0EV, SENSOR_YELLOW};
Serial.write(hexConfData, 2);
request->send_P(200, "text/plain", "configuration sent");
}
void conf_100B_hander(AsyncWebServerRequest *request)
{
gfx.Cls();
gfx.println("10.0eV,Blue,invalid configuration");
request->send_P(200, "text/plain", "invalid configuration");
}
void time_sync_hander(AsyncWebServerRequest request)
{
//gfx.Cls();
gfx.println("Time Synchronisation...");
//List all parameters
int params = request->params();
Serial.println(params);
for (int i = 0; i < params; i++) {
AsyncWebParameter p = request->getParam(i);
if (p->isFile()) { //p->isPost() is also true
Serial.printf("FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
} else if (p->isPost()) {
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
} else {
Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}request->send(200);
}
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {if (!index) {
gfx.printf("UploadStart: %s\n", filename.c_str());
}
for (size_t i = 0; i < len; i++) {
gfx.print(data[i]);
}
if (final) {
gfx.printf("UploadEnd: %s, %u B\n", filename.c_str(), index + len);
}
}
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {Serial.println("handling Body");
gfx.println("handling Body");
if (!index) {
gfx.printf("BodyStart: %u B\n", total);
Serial.println("BodyStart:");
}
for (size_t i = 0; i < len; i++) {
gfx.print(data[i]);
Serial.printf("%c", data[i]);
}
if (index + len == total) {
gfx.printf("BodyEnd: %u B\n", total);
Serial.println("BodyEnd:");
}
}
void gfx_init()
{
gfx.begin(); // Initialize the display
gfx.Cls();
gfx.ScrollEnable(true);
gfx.BacklightOn(true);
gfx.Orientation(LANDSCAPE);
gfx.SmoothScrollSpeed(5);
gfx.TextColor(WHITE); gfx.Font(2); gfx.TextSize(1);
String inf = "aiRcov-IoT";
gfx.println(inf);
}
void setup() {gfx_init();
Serial.begin(9600);
WiFi.softAP(ssid, password);IPAddress IP = WiFi.softAPIP();
// Route for root / web page
server.on("/", HTTP_GET, (AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperature", HTTP_GET, (AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", String(t).c_str());
});server.on("/10p6ev-green-", HTTP_GET, conf_106G_hander);
server.on("/10p6ev-purple-", HTTP_GET, conf_106P_hander);
server.on("/10p6ev-red-", HTTP_GET, conf_106R_hander);
server.on("/10p6ev-yellow-", HTTP_GET, conf_106Y_hander);
server.on("/10p6ev-blue-", HTTP_GET, conf_106B_hander);server.on("/10p0ev-green-", HTTP_GET, conf_100G_hander);
server.on("/10p0ev-purple-", HTTP_GET, conf_100P_hander);
server.on("/10p0ev-red-", HTTP_GET, conf_100R_hander);
server.on("/10p0ev-yellow-", HTTP_GET, conf_100Y_hander);
server.on("/10p0ev-blue-", HTTP_GET, conf_100B_hander);server.on("/timesync", HTTP_ANY, time_sync_hander, NULL, handleBody);
// Start server
server.begin();}
void loop() {
if (Serial.available())
{
s = Serial.readString();
t = s;
gfx.Cls();
gfx.println(s);
}
}