Experimenting with Thingspeak and ESP8266

A friend recently gave me an ESP12E module to play with. I’ve previously played with the ESP8266 at Open Source Hardware Camp 2015 when Omer Kilic ran an ESP8266 workshop. The workshop focused on NodeMCU and LUA.

My immediate thought there was to use LUA again and I built some NodeMCU firmware to experiment with different LUA scripts.  It worked pretty well.  Later I wanted to have a go with the NodeMCU support in the Arduino IDE.  That turns out to be very nicely done :-)

I’ve therefore added a couple of DS18B20 digital one-wire temperature sensors to the board and made use of the OneWire, DallasTemperature and Thingspeak libraries in order to collect data and push it to the web.

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include “ThingSpeak.h”

const int LDR = A0; //ADC0
const int BUTTON = 4; //GPIO4
const int RED = 15;  //GPIO15
const int GREEN = 12; //GPIO12
const int BLUE = 13; //GPIO13
const int ONE_WIRE_BUS_PIN = 2; //GPIO2
const int ONE_WIRE_BUS_PWR = 0; //GPIO0

unsigned long myChannelNumber = YourThingspeakChannelNumber;
const char * myWriteAPIKey = “YourThingspeakAPI”;

OneWire ow(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&ow);
WiFiClient wifiClient;

#define SSID “YourSSID”
#define PASS “YourPASS”

int status = WL_IDLE_STATUS;

void setup()
{
pinMode(LDR, INPUT);
pinMode(BUTTON, INPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(ONE_WIRE_BUS_PWR, OUTPUT);

digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(ONE_WIRE_BUS_PWR, HIGH); //we are powering the sensor from a GPIO to make wiring easier

Serial.begin(115200);
Serial.println();

//connect to wifi network
WiFi.begin(SSID, PASS);
Serial.print(“Connecting to “);
Serial.println(SSID);

while (WiFi.status() != WL_CONNECTED){
delay(250);
digitalWrite(RED, HIGH);
delay(250);
digitalWrite(RED, LOW);
Serial.print(“.”);
}

Serial.println();

Serial.print(“Connected, IP address: “);
Serial.println(WiFi.localIP());
Serial.print(“Signal strength (RSSI):”);
Serial.print(WiFi.RSSI());
Serial.println(” dBm”);

digitalWrite(BLUE, HIGH);

ThingSpeak.begin(wifiClient);
sensors.begin();
}

void loop(){
float light;

// check for incoming bytes from the server and print them to serial port
while (wifiClient.available()) {
char c = wifiClient.read();
Serial.write(c);
}

Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); //globally read the temperature sensors
Serial.println(“DONE”);

Serial.print(“Temperature for Device 0 (Outside) is: “);
float tempOutsideC = sensors.getTempCByIndex(0);
ThingSpeak.setField(2, tempOutsideC);
Serial.println(tempOutsideC);

Serial.print(“Temperature for Device 1 (Fish) is: “);
float tempFishC = sensors.getTempCByIndex(1);
ThingSpeak.setField(1, tempFishC);
Serial.println(tempFishC);

Serial.print(“Light percent is: “);
light = analogRead(LDR);
//convert light to percent
light = light * 100;
light = light / 1024;
ThingSpeak.setField(3, light);
Serial.println(light);

digitalWrite(BLUE, LOW);
digitalWrite(GREEN, HIGH);

Serial.print(“Sending data to Thingspeak…”);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println(“DONE”);
Serial.println(“”);

digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, LOW);
delay(20000);    //must not push data more often that 15 seconds!
}

One Response to Experimenting with Thingspeak and ESP8266

  • tom says:

    Martin, This looks fantastic, I’m currently seeing your fish tank temperature going up as it’s getting dark outside – must either be the heating in your house coming on or lamp turning on in the tank… :)

    I hope to have a go at something similar soon, thanks for posting this up so I and others can refer to it.

    tom

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>