Query GET and response JSON --> ESP8266 + arduino uno
Posted: Wed Feb 01, 2017 5:13 pm
Hello everybody,
My initial goal was to call a web service which will send a JSON response which will open or close a solenoid.
However I have trouble to communicate with my serve and receive the JSON response because I have to use an arduino uno + esp8266.
Now the status is that I am calling the URL [url]jsonplaceholder.typicode.com/posts/1[/url] which is supposed to to return a good JSON.
I do not understand why I do not get the response from the server.
I've been struggling a long time and I am wondering if what I would like to do is actually possible.
Thanks in advance
My initial goal was to call a web service which will send a JSON response which will open or close a solenoid.
However I have trouble to communicate with my serve and receive the JSON response because I have to use an arduino uno + esp8266.
Now the status is that I am calling the URL [url]jsonplaceholder.typicode.com/posts/1[/url] which is supposed to to return a good JSON.
I do not understand why I do not get the response from the server.
Code: Select all
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial ESP8266(10, 11);
String NomduReseauWifi = "wifiName";
String MotDePasse = "password";
String cmd;
String server = "jsonplaceholder.typicode.com";
String uri = "/posts/1";
/****************************************************************/
/* INIT */
/****************************************************************/
void setup()
{
Serial.begin(115200);
ESP8266.begin(115200);
Serial.println(); //Clear some garbage that may be printed to the serial console
reset();
connectWifi();
httppost();
}
/****************************************************************/
/* LOOP */
/****************************************************************/
void loop()
{
while (Serial.available()) // forward data from monitor to esp
ESP8266.write(Serial.read());
while (ESP8266.available()) // forward data from esp to monitor
Serial.write(ESP8266.read());
delay(1000);
}
/****************************************************************/
/* ESP8266 INITIALIZATION */
/****************************************************************/
void reset(){
Serial.println("RESETING!");
ESP8266.println("AT+RST");
delay(1000);
if(ESP8266.find("OK") ) Serial.println("Module Reset");
}
/****************************************************************/
/* WIFI INITIALIZATION */
/****************************************************************/
void connectWifi()
{
Serial.println("************************Connection***************************");
String cmd = "AT+CWJAP=\"" +NomduReseauWifi+"\",\"" + MotDePasse + "\"";
ESP8266.println(cmd);
delay(3000);
if(ESP8266.find("OK")) {
Serial.println("Connected!");
}
else {
connectWifi();
Serial.println("Cannot connect to wifi");
}
}
/****************************************************************************************/
/* CALLING THE URL AND GET THE ANSWER FROM THE SERVER */
/***************************************************************************************/
void httppost(){
ESP8266.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
delay(500);
if( ESP8266.find("OK")){
Serial.println("TCP connection ready");
}
delay(3000);
cmd="GET /posts/1\r\n";//"/coiner/feed/?action=call&udi=123456789 HTTP/1.1\r\n";
cmd+="Host: jsonplaceholder.typicode.com\r\n\r\n";
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
ESP8266.print(sendCmd);
Serial.println(cmd.length());
ESP8266.println(52);
delay(200);
StaticJsonBuffer<200> jsonBuffer;
if(ESP8266.find(">")) {
Serial.println("Sending..");
ESP8266.print(cmd);
if(ESP8266.find("SEND OK")) {
Serial.println("Packet sent");
while (ESP8266.available() > 0) {
//Serial.println(ESP8266.read());
String line = ESP8266.readStringUntil('\r');
JsonObject& root = jsonBuffer.parseObject(line);
//Serial.println(line);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
else{
Serial.println("result: ");
Serial.println((const char*)root["title"]);
}
}
}
// close the connection
ESP8266.println("AT+CIPCLOSE");
}
}
I've been struggling a long time and I am wondering if what I would like to do is actually possible.
Thanks in advance