I'm working on a project with Arduino UNO that needs connection with a cloud server, that is why I'm using ESP8266 module.
I'm already connecting to wi-fi and making GET and POST request but my problem is get the response from these requests.
Here is my code:
/* Doing all these steps to make the request
AT+RST
AT+CWMODE=1
AT+CWJAP="MyNetwork","MyPassword"
AT+CIPMUX=1
AT+CIPSTART=0,"TCP","myserver.com.br",80
AT+CIPSEND=0,270
*/
String data = "data=example";
String command = "POST /webservice/v1/dispositivo/atualizar_data_requisicao HTTP/1.1\r\n";
comando += "Host: " + SERVER + "\r\n";
comando += "User-Agent: Arduino/1.0\r\n";
comando += "Connection: Close\r\n";
comando += "Content-Type: application/x-www-form-urlencoded;\r\n";
comando += "Content-Length: " + String(data.length()) + "\r\n\r\n";
comando += data;
esp8266.println(command);
delay(2000);
String response;
while(esp8266.available()) {
response += esp8266.readStringUntil('\r');
}
Serial.print("Request response: ");
Serial.println(response);
When I run this code the only response I can get is something like:
Recv 270 bxt⸮⸮C⸮
SEND OK
+IPD,0.569:HTTP/1.0 200 OK
But I was expecting a JSON.
Can someone tell me what I am doing wrong and how can I resolve it?
Edit.
Ok, after Code Gorilla helps me (Thank you so much) and a lot of research what I did was program Arduino UNO to send some commands that I invented to serial and program ESP8266 to read the serial and, depending of the command that it reads, do requests and stuff.
ESP8266 is a lot easyer to make requests and have a lot of others possibilities, like HTTPS requests, that you can't do using AT Commands.
When I create this ask I didn't know that I could program directly in ESP8266, but when Code Gorilla told me about it and helps me with some articles I could finally continue with my project.
-
try using the GET method instead of POSTjsotola– jsotola2018年05月21日 22:37:11 +00:00Commented May 21, 2018 at 22:37
-
This variable named "data" is the content of the POSTRicardo Godoz– Ricardo Godoz2018年05月21日 22:38:12 +00:00Commented May 21, 2018 at 22:38
-
1Try posting all your code, not a meaningless snippet.Majenko– Majenko2018年05月21日 22:56:22 +00:00Commented May 21, 2018 at 22:56
-
you expect that after reading a line, the first character of the next line is immediately availableJuraj– Juraj ♦2018年05月22日 07:54:15 +00:00Commented May 22, 2018 at 7:54
-
Why are you using an Uno? Why not just program the ESP8266 directly (using the Arduino IDE and language), much quicker and you don't need to send everything via serial.Code Gorilla– Code Gorilla2018年05月23日 11:45:51 +00:00Commented May 23, 2018 at 11:45
1 Answer 1
String command = "POST /webservice/v1/dispositivo/atualizar_data_requisicao HTTP/1.1\r\n";
comando += "Host: " + SERVER + "\r\n";
...
esp8266.println(command);
I suspect this is just a typo because it shouldn't compile. But you are adding all the HTTP headers to commando not command.
Usually the ? means that the character is unprintable. So to help diagnose the issue display the integer value for each character that is returned. Something LIKE this (I've never used a String
before so length() might not be right)
Serial.print("Request response: ");
Serial.println(response);
for (int index = 0; index < response.length(); ++index)
{
Serial.print (response[index]);
Serial.print (" ");
}
If you want to program the ESP directly then have a look at these guides, they should give you the general idea. Its usually as easy as programming an UNO once you are setup.
http://www.whatimade.today/esp8266-easiest-way-to-program-so-far/ https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/using-arduino-ide https://www.hackster.io/harshmangukiya/how-to-program-esp8266-with-arduino-uno-efb05f
-
I'm sorry, I think that I need to make a new question because I am not expressing my question very well. But your answer seems to be correct, the example shouldn't compile. Thank you!Ricardo Godoz– Ricardo Godoz2018年05月23日 23:35:16 +00:00Commented May 23, 2018 at 23:35
-
Don't make a new question, edit the one above. Use diagrams because then the language doesn't matter so much.Code Gorilla– Code Gorilla2018年05月24日 09:01:54 +00:00Commented May 24, 2018 at 9:01
-
1Thank you so much! I could not continue without your help. I edited the question with what I did to resolve it.Ricardo Godoz– Ricardo Godoz2018年06月06日 19:45:29 +00:00Commented Jun 6, 2018 at 19:45