Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By GengusKahn
#36531 The modified Arduino sketch below allows the Visualisation of the data from the ESP Supply voltage with Humidity, Temperature and Dew Point (NOAA) Measurements via DHT11 using ESP8266-01.

This is posting a response to a GET from the Freeboard IO Site, in the link below, this can be cloned and very quickly you can have your sensor data Visualised......

https://freeboard.io/board/5bOyD6

Tag this to other Sites via the Plugins....

http://freeboard.github.io/freeboard/docs/plugin_example.html

Code: Select all// Now using ESP8266.....
// Sample Arduino Json Web Server
// Created by Benoit Blanchon.
// Heavily inspired by "Web Server" from David A. Mellis and Tom Igoe


#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "DHT.h"
//////////////////////////////
// DHT21 / AMS2301 is at GPIO2
//////////////////////////////
#define DHTPIN 2

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// init DHT; 3rd parameter = 16 works for ESP8266@80MHz
DHT dht(DHTPIN, DHTTYPE,12);

// needed to avoid link error on ram check
extern "C"
{
#include "user_interface.h"
}
ADC_MODE(ADC_VCC);

WiFiServer server(80);
WiFiClient client;
const char* ssid = "Your-SSID";
const char* password = "Your-PASSWORD";
float pfDew,pfHum,pfTemp,pfVcc;
bool readRequest(WiFiClient& client) {
  bool currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      if (c == '\n' && currentLineIsBlank) {
        return true;
      } else if (c == '\n') {
        currentLineIsBlank = true;
      } else if (c != '\r') {
        currentLineIsBlank = false;
      }
    }
  }
  return false;
}

JsonObject& prepareResponse(JsonBuffer& jsonBuffer) {
  JsonObject& root = jsonBuffer.createObject();
  JsonArray& tempValues = root.createNestedArray("temperature");
    tempValues.add(pfTemp);
  JsonArray& humiValues = root.createNestedArray("humidity");
    humiValues.add(pfHum);
  JsonArray& dewpValues = root.createNestedArray("dewpoint");
    dewpValues.add(pfDew);
  JsonArray& EsPvValues = root.createNestedArray("Systemv");
    EsPvValues.add(pfVcc/1000, 3);   
  return root;
}

void writeResponse(WiFiClient& client, JsonObject& json) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Connection: close");
  client.println();

  json.prettyPrintTo(client);
}

void setup() {
delay(2000);
  dht.begin();
  // inital connect
  WiFi.mode(WIFI_STA);
  delay(1000);
    // Connect to WiFi network
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    bool success = readRequest(client);
    if (success) {
         delay(1000);
   pfTemp = dht.readTemperature();   
   pfHum = dht.readHumidity();
   float a = 17.67;
   float b = 243.5;
   float alpha = (a * pfTemp)/(b + pfTemp) + log(pfHum/100);
   pfDew = (b * alpha)/(a - alpha);
   delay(500);
   pfVcc = ESP.getVcc();
      StaticJsonBuffer<500> jsonBuffer;
      JsonObject& json = prepareResponse(jsonBuffer);
      writeResponse(client, json);
    }
    delay(1);
    client.stop();
  }
}
User avatar
By zober
#42700 Thanks for sharing your code!

But i have a problem, maybe you can help me?

My Setup:

ESP 8266-01 with your Code
Linux PC with Apache and Freeboard in the same local Wifi

When i open the URL from the ESP (http://192.168.178.163/jsread) in Chrome Browser, it works and show's me the JSON-code but if i add a new datasource in freeboard with this URL and the settings out of your example it doesn't connect/update. But now it becomes tricky, when i add a datasource from the internet e.g. yours (http://82.5.78.180:93/jsread) it works. First i thought it is a network problem, i created a html site with an iframe linked to the ESP-URL (http://192.168.178.163/jsread) in the same directory as the freeboard on my apache server and it work also. Now i'm runnig out of ideas. Maybe you know a solution or a trick how it works?

Tanks,
Andi

PS: I hope this Thread is not too old to reply.
User avatar
By zober
#43026 After a hard fight i got it working now! :D

The magic word is CORS. Both sides have to use it.

On the apache webserver you can use this guide: http://enable-cors.org/server_apache.html to enable it.
On the ESP its just a line in the HTTP header: client.println("Access-Control-Allow-Origin: *");

Code: Select allvoid writeResponse(WiFiClient& client, JsonObject& json) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Access-Control-Allow-Origin: *");
  client.println("Connection: close");
  client.println();

  json.prettyPrintTo(client);
}


and voila now its working!
User avatar
By Quentin_cb
#48479 I have this example flashed to an ESP8266 12-f. everything works fine, module connects, gets IP address serves json to the the browser but it writes out the chars in the browser very very slowly like a 300 baud modem. I wonder if anyone else has encountered this and if there is a known solution. I would like to build on this example and serve more complex json objects.
Thanks,
Quentin