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......


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


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

// needed to avoid link error on ram check
extern "C"
#include "user_interface.h"

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");
  JsonArray& humiValues = root.createNestedArray("humidity");
  JsonArray& dewpValues = root.createNestedArray("dewpoint");
  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");


void setup() {
  // inital connect
    // Connect to WiFi network
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED)

void loop() {
  WiFiClient client = server.available();
  if (client) {
    bool success = readRequest(client);
    if (success) {
   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);
   pfVcc = ESP.getVcc();
      StaticJsonBuffer<500> jsonBuffer;
      JsonObject& json = prepareResponse(jsonBuffer);
      writeResponse(client, json);
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 ( 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 ( it works. First i thought it is a network problem, i created a html site with an iframe linked to the ESP-URL ( 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?


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");


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.