กลับไปหน้ารวมไฟล์
diy-wifi-light-sensor-d526d0-en.md

The purpose of this project is to create an Internet of Things device with Arduino, which sends data to ThingSpeak via WiFi, and creates real-time graphics. ThingSpeak stores and retrieves data from things using the HTTP protocol over the Internet.

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

Eventually, the whole device will be housed inside a small box (3D-printed + wood) which will make our IoT project fully wearable.

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

Step 1: Demonstration

Step 2: What We Need

  • 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)

Step 3: Build the IoT & Technical Implementation

This project, a DIY WiFi Light Sensor, reveals the hidden layers of simple light-to-cloud interaction. The hardware infrastructure consists of several key layers:

  • Sensing Layer: The Photoresistor (LDR) acts as the eye of your sensor. Its resistance varies from ~500 Ohm (full light) to ~50 kOhm (full dark), creating a variable analog signal.
  • Signal Conditioning Layer: The analog input of the Feather Huzzah has a limitation of 1V. We use a voltage divider circuit (with the specified resistors) to ensure the voltage from the 3.3V supply to the photoresistor never exceeds 1V at the ADC pin.
  • Conversion & Wireless Interface Layer: The ESP8266 on the Feather Huzzah is the core. Its built-in 10-bit ADC (Analog-to-Digital Converter) translates the conditioned analog sensor signal (0-1V) into a digital value between 0 and 1023. This microcontroller also acts as the bridge to your home WiFi network.
  • Communication & Cloud Layer: Data is sent rhythmically to the ThingSpeak cloud platform via WiFi for real-time visualization and logging.
  • User Feedback Layer: An RGB LED provides visual status. It shows red during WiFi connection attempts and a blue/green blink sequence when successfully sending data.

Build Steps:

  • Connect the 3.7V Li-Po battery to a switcher to power the board.
  • Create the 1V-max voltage divider from the 3.3V supply to safely power the photoresistor for the analog input.
  • Connect the RGB LED to digital pins 12, 13, and 14 for R, G, and B respectively to provide user feedback.

/!\ Important: Respect the ESP8266's current limits: max 12 mA per pin and 85mA for the entire chip.

Step 4: Send Data to ThingSpeak

In the Arduino code that follows, we send the luminosity data to ThingSpeak. You must add your channel ID and the SSID/password of your internet connection.

The interaction logic is as follows:

  1. Initialize Hardware & Cloud Sync: The code sets up the sensor, LED pins, WiFi connection, and links to your ThingSpeak channel using your unique Write API Key.
  2. Execution Loop: The ESP8266 constantly reads the analog pin, converting the digital value (0 to 1023) to a percentage between 1 and 100 (inverted so 0% is dark and 100% is bright).
  3. Cloud Update & Feedback: This percentage value is sent to your ThingSpeak channel. The LED blinks to confirm the data transmission.

As shown in the project video, you can build several real-time graphs on ThingSpeak, such as a gauge and a time-series chart, to visualize how luminosity changes.

Step 5: 3D-Printed Socle / STL File

Once we built 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.

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

Future Expansion Ideas

This project is a perfect foundation for an interactive light-tracking tool and can be expanded in several ways:

  • OLED Status Dashboard: Add a small OLED display to show the current light percentage and WiFi signal strength locally.
  • Multi-Sensor Network: Connect several light sensors to independently monitor different rooms or zones.
  • Mobile App Interface: Create a companion mobile app to view and track light levels remotely.
  • Adjustable Thresholds: Add a potentiometer to manually adjust the light level threshold that triggers an alert or specific LED color.

Step 7: Thank You !

Thanks you very much! If you liked this post, please check out our other presentation on tutorials!

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

To follow our tutorials: http://bit.ly/2vYQwL7

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "1x Arduino IDE"
  - "1x ThingSpeak Cloud Platform"
author: "TechnoFabrique"
category: "Sensors & Environment"
components:
  - "1x Li-Ion Battery 1000mAh"
  - "2x Resistor 1k ohm"
  - "1x Single RGB Led"
  - "1x Slide Switch"
  - "1x Photo resistor"
  - "1x Adafruit Feather HUZZAH with ESP8266 WiFi"
  - "4x Resistor 2.21k ohm"
description: "The purpose of this project is to create an IoT device with Arduino which sends data to ThingSpeak overWiFi."
difficulty: "Intermediate"
documentationLinks: []
downloadableFiles: []
encryptedPayload: "U2FsdGVkX1/xQIQdEmSc3zp66ns2fV+JNSPuWBHOgarom2JEnXrhKALcB9Ubuqr/km3asX3IT44Zh/kcvBBXnibxMP4Vt0oswUBNqNhEVjs="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/diy-wifi-light-sensor-d526d0_cover.png"
lang: "en"
likes: 1
passwordHash: "f1a4a0bf72885e446fc2d4bef8b1282f7e4559300ebe63980215750b7e380373"
price: 2450
seoDescription: "Build a DIY WiFi Light Sensor with Arduino and ThingSpeak. Learn to send IoT data over WiFi in this simple tutorial."
tags:
  - "weather"
  - "wearables"
  - "lights"
  - "smart appliances"
  - "data collection"
  - "internet of things"
title: "DIY WiFi Light Sensor"
tools: []
videoLinks:
  - "https://www.youtube.com/embed/3LGoo4bnWXE"
views: 4518