I am able to get the data on the server with serial monitor command:
AT
OK
AT+CWJAP="PRATIK","pwd"
WIFI DISCONNECTED
WIFI CONNECTED
WIFI GOT IP
OK
AT+CIPSTART="TCP","xx.xxx.xx.xxx",80
CONNECT
OK
AT+CIPSEND=33
OK
GET /json/{"hi":"15432"} HTTP/1.1
busy s....
Recv 33 bytes
SEND OK
AT+CIPCLOSE
P.S : I connect rx of ESP8266 to rx of arduino ( pin 0) and tx of ESP8266 to tx of arduino (pin 1)
For the sequence of the above command in serial monitor at 115200 baud am successfully able to get the data which is {"hi","15432"} on the server. So now I have the sequence of command that works from serial monitor to send the data on the server.
I tried replicating the same in the code which does not work. Please find the code below:
I now connect rx of ESP8266 to 11 and tx of ESP8266 to 10 and run below code:
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
SoftwareSerial esp8266(RX,TX);
long timer = 0;
String AP = "PRATIK";
String PASS = "pwd";
String HOST = "xx.xxx.xx.xxx";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
// test start up
sendCommand("AT",5,(char *)"OK");
sendCommand("AT+RST",5,(char *)"OK");
// Connect to AP using username and pwd
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,(char *)"OK");
}
void loop() {
String valSensor = "{\"HI\":\"HELLO\"}";
String http = "HTTP/1.1";
String getData = "GET /json/"+valSensor+" "+http;
sendCommand("AT+CIPSTART=\"TCP\",\""+ HOST +"\","+ PORT,15,(char *)"OK");
Serial.println(getData);
sendCommand("AT+CIPSEND=" +String(getData.length()),4,(char *)">");
esp8266.println(getData);
delay(5000);
countTrueCommand++;
// close connections
sendCommand("AT+CIPCLOSE",5,(char *)"OK");
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("Sucess");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
The output on the serial monitor is:
0. at command => AT Sucess
- at command => AT+RST Sucess
- at command => AT+CWJAP="PRATIK","hardsal2017" Sucess
- at command => AT+CIPSTART="TCP","xx.xxx.xx.xxx",80 Sucess
GET /json/{"HI":"HELLO"}
- at command => AT+CIPSEND=39 Sucess
- at command => AT+CIPCLOSE Sucess
- at command => AT+CIPSTART="TCP","xx.xxx.xx.xxx",80 Sucess
GET /json/{"HI":"HELLO"}
- at command => AT+CIPSEND=39 Sucess
- at command => AT+CIPCLOSE Fail
- at command => AT+CIPSTART="TCP","xx.xxx.xx.xxx",80 Sucess
GET /json/{"HI":"HELLO"}
- at command => AT+CIPSEND=39 Sucess
- at command => AT+CIPCLOSE Fail
...
...
....
....
Output on the server is:
xxx.xxx.xxx - - [21/Sep/2018 19:25:22] code 400, message Bad request syntax ('AT+CIPSEND=33')
xxx.xxx.xxx - - [21/Sep/2018 19:25:22] "AT+CIPSEND=33" 400 -
xxx.xxx.xxx - - [21/Sep/2018 19:25:35] code 400, message Bad request syntax ('AT+CIPSEND=33')
xxx.xxx.xxx - - [21/Sep/2018 19:25:35] "AT+CIPSEND=33" 400 -
xxx.xxx.xxx - - [21/Sep/2018 19:25:49] code 400, message Bad request syntax ('AT+CIPSEND=33')
xxx.xxx.xxx - - [21/Sep/2018 19:25:49] "AT+CIPSEND=33" 400 -
My code accepts the value in the parameter format: Code :
from flask import Flask, jsonify, request
app = Flask(name)
@app.route('/json/',methods=['GET'])
def search(data):
with open("json.txt", "a") as myfile:
myfile.write(str(data)+ "\n");
return jsonify({'message' : 'It works'})
if name == 'main':
app.run(host='0.0.0.0',debug=True,port=80)
Sending the same command in the code as serial monitor but doesnt work