DEV Community

Cover image for light monitering system with kiwisIot
S. Nishanth
S. Nishanth

Posted on

light monitering system with kiwisIot

I’m a second-year computer science student, and I wanted to understand how a sensor reading gets from a small board to a web page. So I picked the simplest sensor I could find, an LDR light module, and connected it to an ESP8266 NodeMCU. It needs only three wires: VCC to 3V3, GND to GND, and AO to A0.

The board reads the sensor value with analogRead(A0), which gives a number from 0 to 1023. On my module the number goes up when it gets darker, which felt backwards, so I flipped it with 1023 - rawValue. Now a bigger number means more light. I then added three labels: 700 and above is BRIGHT, 300 to 699 is MEDIUM, and anything below that is DARK.

Every 2 seconds, the board sends the number and the label to a KiwisIoT dashboard over Wi-Fi. I used two channels, because the gauge needs a number and the text display needs a word, and they can’t share one channel. Seeing the gauge move for the first time when I covered the sensor with my hand was a great moment. A phone torch pushes it to BRIGHT.

A few things tripped me up. The ESP8266 only works on 2.4 GHz Wi-Fi, so the dashboard stayed offline until I checked that. The board also has just one analog pin, so the AO pin on the sensor is the only one that works for a full range of values. The DO pin only gives on or off, so I left it unconnected.

Right now the project only monitors the light and doesn’t switch anything. My next step is to add a relay so a lamp turns on by itself when it gets dark. I also want to add a chart widget to see the light levels over time.

Tags: iot arduino esp8266 beginners

circuit diagram

coding

/*
 * Project: LDR Light Sensor Module with ESP8266 and KiwisIoT
 * Project ID: KIWISIOT-001
 * Board: ESP8266 NodeMCU
 * Sensor: LDR Sensor Module
 */

#include <ESP8266WiFi.h>
#include <KiwisIoT.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* pass = "YOUR_WIFI_PASSWORD";

const char* topic = "YOUR_DASHBOARD_TOPIC_ID";

#define LDR_PIN A0

KiwisIoT kiwisiot(ssid, pass, topic);

void sendLightData() {

  int rawValue = analogRead(LDR_PIN);

  int lightLevel = 1023 - rawValue;

  String lightStatus;

  if (lightLevel >= 700) {
    lightStatus = "BRIGHT";
  }
  else if (lightLevel >= 300) {
    lightStatus = "MEDIUM";
  }
  else {
    lightStatus = "DARK";
  }

  Serial.println();
  Serial.println("---------- LIGHT MONITORING ----------");

  Serial.print("Raw LDR Value: ");
  Serial.println(rawValue);

  Serial.print("Light Level: ");
  Serial.println(lightLevel);

  Serial.print("Light Status: ");
  Serial.println(lightStatus);

  kiwisiot.send("0", String(lightLevel));

  kiwisiot.send("1", lightStatus);

  Serial.print("Sent to KiwisIoT Channel 0: ");
  Serial.println(lightLevel);

  Serial.print("Sent to KiwisIoT Channel 1: ");
  Serial.println(lightStatus);
}

void setup() {

  Serial.begin(115200);

  Serial.println();
  Serial.println("    LDR LIGHT MONITORING    ");

  pinMode(LDR_PIN, INPUT);

  Serial.println("LDR initialized");

  Serial.println("Connecting to KiwisIoT...");

  kiwisiot.begin();

  Serial.println("KiwisIoT initialized");
  Serial.println("Starting light monitoring...");
}

void loop() {

  kiwisiot.run();

  static unsigned long lastSend = 0;

  if (millis() - lastSend >= 2000) {

    lastSend = millis();

    sendLightData();
  }

  delay(100);
}

Enter fullscreen mode Exit fullscreen mode

Working Prototype

output

my github link:https://github.com/snishanth09122007-bit/light-monitoring-/blob/main/README.md

official Kiwisiot project Github link:https://github.com/Kiwistron/KiwisIoT-Projects/tree/main/esp8266/001-ldr-kiwisiot

The full code, circuit diagram and setup steps are in my GitHub repo. If you’ve built something similar, or have ideas for the relay part, I’d like to hear about it in the comments.

Top comments (0)