ESP8266 Witty Cloud LDR + ArduinoIDE + Thingspeak = IoT Light Sensor

Values representing darkness. Daylight would be in the range 300-1000.

Here is a quick example on basic usage of ESP8266 Witty Cloud development board. The Witty Cloud board comes with ESP8266 accompanied by an RGB led, a button and a Light Dependant Resistor. There is also usb chips and connector for easy connectivity. This makes it a great IoT device to start with. Some inputs, some outputs and nice small form factor.

  1. Get ArduinoIDE with ESP8266 extensions, check ESP8266 on NodeMCU board + DS18B20 + ArduinoIDE + ThingSpeak
  2. Connect USB cable, set port and board details
  3. Get Thingspeak.com API-key
  4. Upload code
  5. Enjoy the graph online

More on Witty Cloud: http://www.schatenseite.de/en/2016/04/22/esp8266-witty-cloud-module/

Example code in GitHub: https://github.com/vaasahacklab/esp8266_ldr_thingspeak

/*  ESP8266 LDR ArduinoIDE Thingspeak IoT Example code
 *
 *  This sketch sends data via HTTP GET requests to api.thingspeak.com of the value on
 *  LDR sensor on ESP8266 development board - Wittyboard. You need an api_key from Thingspeak.
 * 
 *  Based on: https://vaasa.hacklab.fi/2016/02/06/esp8266-on-nodemcu-board-ds18b20-arduinoide-thingspeak/
 *  ESP8266 Witty: http://www.schatenseite.de/en/2016/04/22/esp8266-witty-cloud-module/
 */

#include 
#include 

const char* ssid = "VaasaHacklab";
const char* pass = "wifikey";

// RGB or button pins are not used in LDR example
#define RED_PIN      7   // RGB Light pin at Arduino pin 3
#define GREEN_PIN    8   // RGB Light pin at Arduino pin 5
#define BLUE_PIN     9   // RGB Light pin at Arduino pin 6
#define BUTTON_PIN   4   // button
#define LDR_PIN      A0

char host[] = "api.thingspeak.com";
String ApiKey = "THINGSPEAK_API_KEY";
String path = "/update?key=" + ApiKey + "&field1=";

int ldr_value = 0;

void setup() {

  Serial.begin(115200);
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(LDR_PIN, INPUT);

}

void loop() {
  ldr_value = analogRead(LDR_PIN); // read input value and store it
  Serial.println(lrd_value);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  client.print(String("GET ") + path + ldr_value + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: keep-alive\r\n\r\n");
  delay(60000);
}

You may also like...

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.