DIY - Wifi Light Sensor - Wifi Communication to ThingSpeak [Arduino / ESP8266 / ThingSpeak]

by TechnoFabrique in Circuits > Wearables

3286 Views, 3 Favorites, 0 Comments

DIY - Wifi Light Sensor - Wifi Communication to ThingSpeak [Arduino / ESP8266 / ThingSpeak]

Cover 3.6.png

The purpose of this project is to create a IoT (Internet of Things) device with arduino which send data to ThingSpeak by WiFi, and create real-time graphics.

ThingSpeak store and retrieve data from things using the HTTPprotocol over the Internet.

We are going to create a tiny device (60x90 mm - 2.36x3.54 inches) with the Feather Huzzah card from Adafruit and send data to ThingSpeak using the ESP8266 WiFi device. We are going to send the data from our light sensor made with a photoresistor.

Eventually, the whole device will hold in a tiny box (3D printed + wood) which will make our IoT project fully wearable.

ref : https://en.wikipedia.org/wiki/ThingSpeak

Demonstration

DIY Light Sensor - Wifi communication to ThingSpeak [Arduino / ESP8266 / ThingSpeak]

What We Need

SensoTeck 0.0.1 - Light_schema.png

- Adafruit Feather Huzzah ESP8266

- Battery Li-po 3.7V 550mAh (or more capacity)

- Photoresistor

- Resistance 1kOhm (x4), 2.2kOhms (x2)

- Led RGB

- tiny switcher

- Wire

- 3D printer (optional)

Build the IoT

SensoTeck 0.0.1 - Light.png

--> Connect the 3.7V Li-Po battery to a switcher to power the board.

--> We get a 3.3V supply to power our photoresistor. The analog input of the Feather Huzzah has a limitation of 1V, so we cut the power from 3.3V to 1V max. Our photoresistor value vary from 500 Ohm to 50 KOhm (full dark to full light). The resistor allow staying analog input under 1V.

--> We supply a RGB led in order to show to the user that the connection between the card and the website is going well. In the code, we show a red light when the device isnt connected, and a blink of a blue and a green light, when it's sending data (see the video). The digital output 12, 13, 14 supply the R,G,B led.

/!\ The max per pin is 12 mA and the max for the entire package is 85mA.

Send Data to ThingSpeak

ThingSpeak graph 0.2.png

In the arduino code that follow, we send the luminosity data to TeamSpeak. Add your channel ID and the SSID/password of your internet connection in the arduino code.

The analog pin give arduino a digital number (0 to 1024). We translate this number between 1 to 100.

As we can see, we can build several graphs such as a real-time gauge, and a graph with settings. Here we can see that the luminosity is getting lower and lower as the time goes.

3D Printed Socle / STL File

Socle v2.0-1.png

Once we builted the IoT device, we will put the whole thing in a tiny box.

The base is made with a 3D printing machine, with PLA.

The top of the box is made of wood.

The Code

<p>// Light Sensor data transmitted to ThingSpeak via Wifi<br>// Inspired from the Hackster project : https://www.hackster.io/glicmich/adafruit-feather-huzzah-with-esp8266-wifi-8009d2</p><p>// Librairies</p><p>#include <ESP8266Wifi.h>
#include <WifiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ThingSpeak.h>
</p><p>                                 
const char* ssid = "your_ssid_box";               // Wireless SID
const char* password = "your_wifi_password";                // Wireless Passcode</p><p>ESP8266WebServer server(80); // http server</p><p>// what digital pin we're connected to
int led_r= 13; // red led connected to the digital pin 13
int led_g = 12; // green led connected to the digital pin 12
int led_b = 14; // blue led connected to the digital pin 14</p><p>int luminosity = A0; // photoresistor connected to the INPUT (analog pin 0)</p><p>WiFiClient  client;
unsigned int myChannelNumber = 546348;           // Channel Number from   // ThingSpeak IoT
const char * myWriteAPIKey = "*****************";     // Write API Key
                                                
void handleRoot() 
    {</p><p>    delay(1000);
    server.send(200, "text/plain", "Hello world !");</p><p>    delay(1000);
    }</p><p>void handleNotFound()
    {</p><p>    String message = "File Not Found\n\n";
    message += "URI: ";
    message += server.uri();
    message += "\nMethod: ";
    message += (server.method() == HTTP_GET)?"GET":"POST";
    message += "\nArguments: ";
    message += server.args();
    message += "\n";
   
    for (uint8_t i=0; i</p><p>void setup(void)
    {
    Serial.println("AM2302 test!");
    pinMode(led_r,OUTPUT);
    pinMode(led_g,OUTPUT);
    pinMode(led_b,OUTPUT);
    digitalWrite(led_r,LOW);
    digitalWrite(led_g,LOW);
    digitalWrite(led_b,LOW);
    pinMode(luminosity,INPUT);</p><p>    Serial.begin(115200);
    WiFi.begin(ssid, password);
    ThingSpeak.begin(client);
    Serial.println("");</p><p>  // Wait for connection
    while (WiFi.status() != WL_CONNECTED) 
        {
        delay(500);
        Serial.print(".");
        // RGB red light
        digitalWrite(led_r,HIGH);
        digitalWrite(led_g,LOW);
        digitalWrite(led_b,LOW);
        }
  
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    
    if (MDNS.begin("esp8266")) 
        {
        Serial.println("MDNS responder started");
        }</p><p>    server.on("/", handleRoot);</p><p>    server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });</p><p>    server.onNotFound(handleNotFound);</p><p>    server.begin();
    Serial.println("HTTP server started");
    }</p><p>void loop(void)
    {
    server.handleClient();
    // Blink led from white to blue
    digitalWrite(led_r,HIGH);
    digitalWrite(led_g,HIGH);
    digitalWrite(led_b,HIGH);
    delay(300);
    digitalWrite(led_r,LOW);
    digitalWrite(led_g,LOW);
    digitalWrite(led_b,HIGH);
    delay(300);
    digitalWrite(led_r,HIGH);
    digitalWrite(led_g,HIGH);
    digitalWrite(led_b,HIGH);
    delay(300);
    digitalWrite(led_r,LOW);
    digitalWrite(led_g,LOW);
    digitalWrite(led_b,HIGH);
    delay(300);
    digitalWrite(led_r,HIGH);
    digitalWrite(led_g,HIGH);
    digitalWrite(led_b,HIGH);
    delay(300);
    digitalWrite(led_r,LOW);
    digitalWrite(led_g,LOW);
    digitalWrite(led_b,HIGH);
    // Sensor reading 
    luminosity = analogRead(A0);
    // Light sensor in % / reverse in order to have 0% for LOW light and 100% for high light
    luminosity = 100 - map(luminosity,0,1024,0,100);
    Serial.print(luminosity);
    Serial.println(" %");
    ThingSpeak.setField(1, luminosity); // Field 1 , sending the variable luminosity
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    }</p>

Thank You !

Thanks you very much !

If you liked this post, please come check our other presentation on Instructables !

If you want to visit our website : http://bit.ly/2viP7No

To follow us on Instructables: http://bit.ly/2viP7No

Hope you appreciated ...