0
0
Drone-programmingHow-ToBeginner · 4 min read

IoT Project for Smart Street Lighting: Setup and Example

A smart street lighting IoT project uses microcontrollers like ESP32, light sensors, and MQTT protocol to control lights based on ambient brightness. Devices send sensor data to a broker, which triggers lights to turn on or off automatically.
📐

Syntax

This project uses the MQTT protocol to communicate between street lights and a central controller. The main parts are:

  • MQTT Broker: Server that routes messages.
  • Publisher: Device sending sensor data.
  • Subscriber: Device receiving commands to turn lights on/off.
  • Sensor Reading: Code to measure light intensity.

Basic MQTT publish syntax in Python:

client.publish(topic, message)

Basic subscribe syntax:

client.subscribe(topic)
python
import paho.mqtt.client as mqtt

# MQTT broker address
broker = "test.mosquitto.org"

# Create client
client = mqtt.Client()

# Connect to broker
client.connect(broker)

# Publish example
client.publish("streetlight/sensor", "light_level:300")

# Subscribe example
client.subscribe("streetlight/command")
💻

Example

This example shows a simple ESP32 microcontroller code that reads a light sensor and publishes the light level to an MQTT broker. It also subscribes to commands to turn the street light on or off.

cpp
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "test.mosquitto.org";

WiFiClient espClient;
PubSubClient client(espClient);

const int lightSensorPin = 34; // Analog pin
const int streetLightPin = 2;  // Output pin for light

void setup_wifi() {
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  String message;
  for (int i = 0; i < length; i++) {
    message += (char)payload[i];
  }
  if (String(topic) == "streetlight/command") {
    if (message == "ON") {
      digitalWrite(streetLightPin, HIGH);
    } else if (message == "OFF") {
      digitalWrite(streetLightPin, LOW);
    }
  }
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect("ESP32Client")) {
      client.subscribe("streetlight/command");
    } else {
      delay(5000);
    }
  }
}

void setup() {
  pinMode(streetLightPin, OUTPUT);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  int lightLevel = analogRead(lightSensorPin);
  char msg[50];
  snprintf(msg, 50, "light_level:%d", lightLevel);
  client.publish("streetlight/sensor", msg);
  delay(5000);
}
Output
The ESP32 connects to WiFi and MQTT broker, publishes light sensor readings every 5 seconds, and listens for ON/OFF commands to control the street light.
⚠️

Common Pitfalls

  • Not handling MQTT reconnection can cause lost messages.
  • Incorrect sensor pin or calibration leads to wrong light readings.
  • Using blocking delays can stop MQTT client from processing messages.
  • Not securing MQTT broker can expose control to unauthorized users.

Always use non-blocking code and secure your network.

cpp
/* Wrong: Using delay blocks MQTT client loop */
void loop() {
  delay(5000); // Blocks MQTT processing
  client.publish("topic", "message");
}

/* Right: Use millis() for timing without blocking */
unsigned long lastPublish = 0;
void loop() {
  client.loop();
  if (millis() - lastPublish > 5000) {
    client.publish("topic", "message");
    lastPublish = millis();
  }
}
📊

Quick Reference

Key points for smart street lighting IoT project:

  • Use MQTT for lightweight messaging.
  • Read ambient light with analog sensors.
  • Publish sensor data regularly.
  • Subscribe to commands to control lights.
  • Handle network reconnections gracefully.
  • Secure MQTT broker with authentication if possible.

Key Takeaways

Use MQTT protocol to send sensor data and receive control commands for street lights.
Read ambient light levels with sensors to automate light switching.
Avoid blocking code to keep MQTT communication responsive.
Secure your MQTT broker to prevent unauthorized access.
Handle network disconnections by reconnecting automatically.