TechnoFabrique
Published © GPL3+

DIY WiFi Light Sensor

The purpose of this project is to create an IoT device with Arduino which sends data to ThingSpeak overWiFi.

IntermediateFull instructions provided8 hours1,315
DIY WiFi Light Sensor

Things used in this project

Hardware components

Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1
Single RGB Led
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×4
Resistor 1k ohm
Resistor 1k ohm
×2
Photo resistor
Photo resistor
×1
Slide Switch
Slide Switch
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Pi_C3_A8ce1+2.5mm+epaisseur.STL

Schematics

Light Sensor - Fritzing

Code

Code snippet #1

C#
// 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>
                               
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);
    }

Credits

TechnoFabrique

TechnoFabrique

3 projects • 0 followers
Student - Engineering School

Comments