1

I'm using an arduino uno and ESP8266 (esp01) with the WiFiEsp library to connect to WiFi and POST data, connecting to WiFi works just fine.

I need to send a POST request, but I can't get it to work as I want.

 #include "WiFiEsp.h"
 // Emulate Serial1 on pins 7/8 if not present
 #ifndef HAVE_HWSERIAL1
 #include "SoftwareSerial.h"
 SoftwareSerial Serial1(6, 7); // RX, TX
 #endif
 char ssid[] = "Innovating-Guest"; // your network SSID (name)
 char pass[] = "wireless"; // your network password
 int status = WL_IDLE_STATUS; // the Wifi radio's status

 char server[] = "10.66.240.66";//http://10.66.240.66:8000/loadimage
 WiFiEspClient client; // Initialize the Ethernet client object

 void setup()
 {
 // initialize serial for debugging
 Serial.begin(9600);
 // initialize serial for ESP module
 Serial1.begin(9600);
 // initialize ESP module
 WiFi.init(&Serial1);
 // check for the presence of the shield
 if (WiFi.status() == WL_NO_SHIELD) 
 {
 Serial.println("WiFi shield not present");
 // don't continue
 while (true);
 }

 // attempt to connect to WiFi network
 while ( status != WL_CONNECTED) {
 Serial.print("Attempting to connect to WPA SSID: ");
 Serial.println(ssid);
 // Connect to WPA/WPA2 network
 status = WiFi.begin(ssid, pass);
 }
 // you're connected now, so print out the data
 Serial.println("You're connected to the network");
 printWifiStatus();
 Serial.println();
 Serial.println("Starting connection to server...");
 // if you get a connection, report back via serial
 if (client.connect(server, 8000)) {
 Serial.println("Connected to server");
 String temp= "12.22";
 String humd= "23.33";
 String batt= "100";

 String content ="{\"temperature\": + temp +,\"humidity\":+ humd +,\"batteryPer\": + batt + }";
 client.println("POST /loadimage HTTP/1.1");
 client.println("Host: 10.66.240.66:8000");
 client.println("Accept: */*");
 client.print("Content-Length: ");
 client.println(content.length());
 client.println("Content-Type: application/json");
 client.println();
 client.println(content);
 }
 }
 void loop()
 {

 while (client.available()) {
 char c = client.read();
 Serial.write(c);
 }
 // if the server's disconnected, stop the client
 if (!client.connected()) {
 Serial.println();
 Serial.println("Disconnecting from server...");
 client.stop();
 // do nothing forevermore
 while (true);
  }
 }

 void printWifiStatus()
 {
 // print the SSID of the network you're attached to
 Serial.print("SSID: ");
 Serial.println(WiFi.SSID());

 // print your WiFi shield's IP address
 IPAddress ip = WiFi.localIP();
 Serial.print("IP Address: ");
 Serial.println(ip);

 // print the received signal strength
 long rssi = WiFi.RSSI();
 Serial.print("Signal strength (RSSI):");
 Serial.print(rssi);
 Serial.println(" dBm");
 }

Getting output on serial monitor as in image Can anyone help me to know that why this error is coming.

Thanks in advance.

ASK
  • 31
  • 4
  • 1
    `POST /loadimage HTTP/1.1`, with a space between `POST` and the uri. – hcheung Feb 11 '20 at 09:33
  • @hcheung there is no change when use a space between POST and uri – ASK Feb 11 '20 at 09:45
  • 1
    Nevertheless, `POST /uri HTTP/1.1` is the correct syntax for http POST command. – hcheung Feb 11 '20 at 09:49
  • @hcheung I update with correction but POST is not happens by that. Is there any chnage in my server uri?? can you please help me – ASK Feb 11 '20 at 09:56
  • it looks like the server disconnects. if you can update the AT firmware to AT 1.7 (SDK 3), I recommend my WiFiEspAT library. in debug mode it shows all communication with the AT firmware so it would be simpler to troubleshoot – Juraj Feb 11 '20 at 09:56
  • Also change `client.println("Content-Length: ");` to `client.print("Content-Length: ");` – hcheung Feb 11 '20 at 10:03
  • @Juraj `AT version:1.3.0.0(Jul 14 2016 18:54:01) SDK version:2.0.0(5a875ba) ```How can I change the versionto AT 1.7(SDK 3) – ASK Feb 11 '20 at 10:05
  • @hcheung I also try changing to client.print("Content-Length: "); It s not working now also. – ASK Feb 11 '20 at 10:25
  • I suspect, the problem is on the server side. So, you may use Wireshark or server logs to see, what exact query you are sending. – gbg Feb 11 '20 at 11:08
  • 1
    @gbg thanks for the reply. But the server side is done by another one and only duty i have is to post data obtained in arduino to the server. And when i try to send with nodemcu board it is working. So i think there is no problem with server side. – ASK Feb 11 '20 at 11:37
  • Okay, anyway, you can use the Wireshark to see, what exact bytes sends ESP to the server. To do this, your PC must be in the same WiFi network with the ESP – gbg Feb 11 '20 at 11:46
  • @gbg thanks for the help. Sorry but i can't post data now also. – ASK Feb 11 '20 at 12:53
  • Host should contain the port. So `Host: 10.66.240.66:8000` should be `Host: 10.66.240.66`. Secondly; I think the JSON string isn't generated properly. I think it should be: `String content ="{\"temperature\":"+ temp +",\"humidity\":"+ humd + ",\"batteryPer\":" + batt + "}";` – Gerben Feb 11 '20 at 17:03
  • @Gerben thanks for the reply but by changing the host also there is no chnage. It shows as disconnecting from server now also. I don't have any idea what the problem is. – ASK Feb 12 '20 at 05:40
  • Is there even a problem? You'd expect the server to close the connection once it has received your request and send back a response. The only thing is that you'd expect to get back some response. Try temporarily removing the `while (true);` at the end of the loop and replace it with a `delay(100);`. Then see what happens – Gerben Feb 12 '20 at 14:34
  • @Gerben, this is based on the Arduino WebClient example and the loop() works as it is to receive the HTTP response. but the client doesn't connect here at all – Juraj Feb 12 '20 at 16:09
  • @Gerben I try by removing `while(true);` but same result cames. – ASK Feb 13 '20 at 09:45
  • @Juraj your are absolutely correct. Can you make any solutions other than changing the esp version and library? – ASK Feb 13 '20 at 11:15
  • do you have an USB to TTL Serial adapter? wire the RX pin of the adapter to TX pin of the esp8266 additionally to the wire to Arduino. this way you can see everything the AT firmware sends to Arduino. – Juraj Feb 13 '20 at 13:01
  • @Juraj I dont have an USB to TTL converter with me. – ASK Feb 17 '20 at 08:50

0 Answers0