Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By seaurchin
#72014 I know its an old thread I will highly appreciate if someone can please answer this !

I figured out that
Code: Select allclient.println("Content-Type: application/json");
in combination with
Code: Select alljson.prettyPrintTo(client);
does the actual job of output the json when we access the ip or ip/jsread.

However I want to change this behaviour to just the ip to return

Code: Select allclient.println("Content-Type: text/html");
and instead of ip/jsread I want it to be ip/json to return
Code: Select allclient.println("Content-Type: application/json");


how to do that ?
User avatar
By GengusKahn
#73522 Hi there.....

Use this...(Faster....)Substitute your own variable data, sPath is the requested uri .........

Code: Select allelse if(sPath=="/json")   
   {
     ulJReqcount++;
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Connection: close");
 // client.println("Connection: keep-alive");
  client.println("Access-Control-Allow-Origin: *");
  client.println("Access-Control-Allow-Methods: GET");
  client.println("Access-Control-Allow-Headers: Content-Type");
  client.println("Access-Control-Max-Age: 500");
  client.println();
     sResponse  = ("{\r\n\"temperature\": [\r\n");
     sResponse += pfTemp;
     sResponse += ("\r\n],\r\n\"humidity\": [\r\n");
     sResponse += pfHum;
     sResponse += ("\r\n],\r\n\"dewpoint\": [\r\n");
     sResponse += pfDew;
     sResponse += ("\r\n],\r\n\"Systemv\": [\r\n");
     sResponse += pfVcC/1000;
     sResponse += ("\r\n],\r\n\"Low_Hum_Today\": [\r\n");
     sResponse += pfHumLow;
     sResponse += ("\r\n],\r\n\"High_Hum_Today\": [\r\n");
     sResponse += pfHumHigh;
     sResponse += ("\r\n],\r\n\"Low_Temp_Today\": [\r\n");
     sResponse += pfTempLow;
     sResponse += ("\r\n],\r\n\"High_Temp_Today\": [\r\n");
     sResponse += pfTempHigh;
     sResponse += ("\r\n],\n\"Jsreqs\": [\r\n");
     sResponse += ulJReqcount;
     sResponse += ("\r\n],\r\n\"Uptime\": [\r\n\"");
     sResponse += duration1;
     sResponse += ("\"\r\n],\r\n\"TiMestamp\": [\r\n\"");
     sResponse += TMnow;
     sResponse += ("\"\r\n]\r\n}");
    // Send the JSON response to the client
    client.println(sResponse);     

   }