Issue sending a 130 chars json as response to a get request
Posted: Wed Feb 28, 2018 12:19 pm
Hi guys,
I've an esp8266 connected with an arduino UNO to make an automated watering system based on value get from a soil moisture sensor. Now i'm trying to make a real time console to monitor the situation and power on/off the pump on demand.
I'm able to send get request from a client to the esp and i'm also i'am able to control same sensor connected with the arduino from get request.
Now the problem. After the esp receive and process the request and do what it have to do, it is not able to sent the response to the client, but also if the json that have to be returned is longer than 130 character... I've tried a lot of things but i've no more ideas. can you help me please?
This code as is will not give response to a get request, but if you remove root["test"] json property, it will.
Help me guys
I've an esp8266 connected with an arduino UNO to make an automated watering system based on value get from a soil moisture sensor. Now i'm trying to make a real time console to monitor the situation and power on/off the pump on demand.
I'm able to send get request from a client to the esp and i'm also i'am able to control same sensor connected with the arduino from get request.
Now the problem. After the esp receive and process the request and do what it have to do, it is not able to sent the response to the client, but also if the json that have to be returned is longer than 130 character... I've tried a lot of things but i've no more ideas. can you help me please?
Code: Select all
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#define DEBUG true
#define SSID "****"
#define PASSWORD "****"
SoftwareSerial esp8266(2,3);
int rainPin = A0;
int greenLED = 6;
int redLED = 5;
int orangeLED = 7;
int pump = 8;
int thresholdSoggy = 500;
int thresholdWet = 800;
int thresholdDry = 1000;
int sensorValue;
String soilStatus;
boolean autoModeOn = false;
boolean pumpIsOn = false;
long pumpOnTime = 0;
int defaultPumpOnMax = 15000;
long pumpOnMax = defaultPumpOnMax;
void setup()
{
Serial.begin(9600); //Serial Monitor
esp8266.begin(9600); //ESP8266 Module
//soilMoisture sensor
pinMode(rainPin, INPUT);
//LEDs
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(orangeLED, OUTPUT);
//Pump
pinMode(pump, OUTPUT);
//Turn Off Leds
alert(0);
//turn off pump
turnPumpOn(false);
//set automode on
setAutoModeOn(false);
//setSecurityPumpTime(defaultPumpOnMax);
//Connect ESP8266 to WiFi
connectWiFi(SSID,PASSWORD);
}
void setAutoModeOn(boolean on){
if(on){
autoModeOn = true;
if(DEBUG) Serial.println("Activate AUto Mode");
}else{
autoModeOn = false;
if(DEBUG) Serial.println("Deactivate AUto Mode");
}
}
void setSecurityPumpTime(int interval){
if(interval <= 0) interval = defaultPumpOnMax;
pumpOnMax = interval*1000;
}
void turnPumpOn(boolean on){
if(on){
digitalWrite(pump,HIGH);
pumpIsOn = true;
if(DEBUG) Serial.println("Turn On Pump");
//pumpOnTime = millis();
}else{
digitalWrite(pump,LOW);
pumpIsOn = false;
if(DEBUG) Serial.println("Turn Off Pump");
}
}
void alert(int status){
switch(status){
case 0 : //ALL OFF
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(orangeLED, LOW);
break;
case 1 : //ALL ON
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(orangeLED, HIGH);
break;
case 2 : //OK
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(orangeLED, LOW);
break;
case 3 : //WARN
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(orangeLED, HIGH);
break;
case 4 : //DANGER
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(orangeLED, LOW);
break;
}
}
String soilMoisture(int sensorValue, boolean debug){
String soilDesc;
if(sensorValue <= thresholdSoggy){
soilDesc = "Too much wet";
if(autoModeOn){
alert(3);
turnPumpOn(false);
}
}
else if(sensorValue > thresholdSoggy && sensorValue <= thresholdWet){
soilDesc = "No need Water";
if(autoModeOn){
alert(2);
turnPumpOn(false);
}
}
else if(sensorValue > thresholdWet && sensorValue <= thresholdDry){
soilDesc = "Need Water";
if(autoModeOn){
alert(3);
turnPumpOn(true);
}
}
else if(sensorValue > thresholdDry){
soilDesc = "Dry";
if(autoModeOn){
alert(4);
turnPumpOn(true);
}
}
if(debug) Serial.println(String(sensorValue) + " - " + soilDesc);
delay(500);
return soilDesc;
}
int connectionId;
void loop()
{
/*if(pumpIsOn){
unsigned long currentMillis = millis();
if(currentMillis - pumpOnTime > pumpOnMax) turnPumpOn(false);
}*/
if(autoModeOn){
sensorValue = analogRead(rainPin);
soilStatus = soilMoisture(sensorValue,DEBUG);
}
if(esp8266.available())
{
if(esp8266.find("+IPD,"))
{
delay(300);
String response = "";
connectionId = esp8266.read()-48;
sensorValue = analogRead(rainPin);
soilStatus = soilMoisture(sensorValue,DEBUG);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& soil = jsonBuffer.createObject();
JsonObject& pump = jsonBuffer.createObject();
soil["moisture"] = sensorValue;
soil["status"] = soilStatus;
root["soil"] = soil;
if(esp8266.find("?"))
{
String qString = "";
alert(0);
delay(300);
while(esp8266.available())
{
char c = esp8266.read();
qString += c;
}
int spaceInd = qString.indexOf(0x20);
qString = qString.substring(0,spaceInd+1);
if(DEBUG) Serial.println("Query String: " + qString);
char buf[qString.length()];
qString.toCharArray(buf, sizeof(buf));
char *p = buf;
char *str;
while ((str = strtok_r(p, "&", &p)) != NULL){
String par = String(str);
int ind = par.indexOf(0x3D);
String name = par.substring(0,ind);
String value = par.substring(ind+1,par.length());
if(DEBUG) Serial.println("Parsed parameter: " + name + ", value: " + value);
if(name == "pump")
{
if(value == "on")
{
turnPumpOn(true);
setAutoModeOn(false);
}
else if(value == "off")
{
turnPumpOn(false);
setAutoModeOn(false);
}
}
if(name == "automode")
{
if(value == "on")
{
setAutoModeOn(true);
}
else if(value == "off")
{
setAutoModeOn(false);
}
}
//if(name == "pumponmax") setSecurityPumpTime(value.toInt());
}
}
root["pumpIsOn"] = pumpIsOn;
root["pumpOnMax"] = int(pumpOnMax);
root["autoModeOn"] = autoModeOn;
root["test"] = "Hello WOrld, With this i do not work!";
//root["pump"] = pump;
//if(DEBUG) root.prettyPrintTo(Serial);
Serial.println("\r\n");
root.printTo(response);
esp8266Send(response);
delay(300);
String closeCommand = "AT+CIPCLOSE="; ////////////////close the socket connection////esp command
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,1000,DEBUG);
}
}
}
//////////////////////////////sends data from ESP to webpage///////////////////////////
void esp8266Send(String d)
{
String cipSend = " AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=d.length();
cipSend +="\r\n";
sendData(cipSend,2000,DEBUG);
sendData(d,2000,DEBUG);
}
//////////////gets the data from esp and displays in serial monitor///////////////////////
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug) Serial.print(response); //displays the esp response messages in arduino Serial monitor
return response;
}
boolean connectWiFi(String ssid, String pass){
String sendData(String command, const int timeout, boolean debug);
sendData("AT+RST\r\n",2000,DEBUG); // reset module
sendData("AT+CWMODE=1\r\n",1000,DEBUG); // configure as station
String connString = "AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"\r\n";
sendData(connString,7000,DEBUG); //connect to WiFi
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
return true;
}
This code as is will not give response to a get request, but if you remove root["test"] json property, it will.
Help me guys