Sagnik Ghosh
Published © GPL3+

IoT-Based Smart Street Light System

A smart city is an urban area that uses different types of electronic data collection sensors to supply information used to manage assets.

BeginnerFull instructions provided2 hours109,093
IoT-Based Smart Street Light System

Things used in this project

Hardware components

Arduino UNO Wifi Rev.2
Arduino UNO Wifi Rev.2
×1
LED (generic)
LED (generic)
×1
LDR, 1 Mohm
LDR, 1 Mohm
×1
IR Proximity Sensor
Digilent IR Proximity Sensor
×1
ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
Resistor 1k ohm
Resistor 1k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Breadboard, 170 Pin
Breadboard, 170 Pin

Story

Read more

Schematics

Smart iot street light

Code

IOT smart light

Arduino
int  smooth;
int LDR;
int threshold = 40;//sun's intensity
int brightness = 0;
int ledState = 0;

int sensor1 = 11;
int sensor2 = 8;
int sensor3 = 9;

int led1=5;
int led = 6;
int led2=2;

int carPresent = 0;
int carPresent1 = 0;

float beta = 0.65;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(led,OUTPUT);
  pinMode(led1,OUTPUT);
   pinMode(led2,OUTPUT);
}


void loop() {
  smooth = smooth - (beta * (smooth -  analogRead(A0)));
  delay(1);
  LDR = round(((float)smooth / 1023) * 100);
  if (LDR <= 40)
    brightness=0;
  else
  {
    brightness = map(LDR, 40, 100, 0, 255);
  }
  checkSensors();
  if (carPresent == 1)
    {
      ledState = 1;
        digitalWrite(led,HIGH);
        digitalWrite(led1,HIGH); 
        analogWrite(led,brightness);
        analogWrite(led1,brightness);
    }
   else if (carPresent == 0)
    {
      ledState = 0;
       digitalWrite(led,HIGH);
       //digitalWrite(led1,HIGH); 
      analogWrite(led,ledState);
      //analogWrite(led1,ledState);
     if(carPresent1 == 1)
    {
      ledState = 1;
      if(ledState == 1)
      {
     
      analogWrite(led1,brightness);
       analogWrite(led2,brightness);
     }
    }
    
     else if (carPresent1 == 0)
    {
      ledState = 0;
       digitalWrite(led1,HIGH); 
       digitalWrite(led2,HIGH); 
      analogWrite(led1,ledState);
      analogWrite(led2,ledState);
     }
    }
    
String data = (String)ledState+","+(String)brightness+";";
Serial.print(data); 
//  Serial.print(digitalRead(sensor1));
//  Serial.print("\t");
//  Serial.print(digitalRead(sensor2));
//  Serial.print("\t");
//  Serial.print(ledState);
//  Serial.print("\t");
//  Serial.println(brightness);
delay(100);
}

void checkSensors()
{
  if (digitalRead(sensor1) == 0)//Car captured in 1st sensor
  {
 
   if (digitalRead(sensor2) == 1)//Car still didnt reach the 2nd sensor
      carPresent = 1;
  }

  else if (digitalRead(sensor2) == 0)//Car reached the 2nd sensor
  { //No cars detected behind the first car
    if (digitalRead(sensor1) == 1)
    {
      carPresent = 0;
     carPresent1 = 1;
    }

    else if (digitalRead(sensor1) == 0 )
      {
        analogWrite(led,brightness);
         analogWrite(led1,brightness);
         analogWrite(led2,brightness);
        
         digitalWrite(led,HIGH); 
         digitalWrite(led1,HIGH);
          digitalWrite(led2,HIGH);
      }
  }

  
  else if(digitalRead(sensor3) == 0)//car reached the 3rd sensor
  {
    //No cars detected behind the first car 
     if (digitalRead(sensor2) == 1)
    {
      carPresent = 0;
   
    carPresent1 = 0;
    }
     else if (digitalRead(sensor2) == 0 )
      {
       carPresent = 0;
       carPresent1 = 1;
      }
   }
 }

Code part 2

Arduino
#include <ESP8266WiFi.h>

// Network Information
const char* ssid     = "ardent";
const char* password = "12345678";

String ledState = "";
String brightness = "";

char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "NUEBLW9OA58DLL4N";          // Be sure to change this to your channel Write API key
WiFiClient client;

void setup()
{
  Serial.begin( 115200 );   // You may need to adjust the speed depending on your hardware.
  connectWifi();
}
void loop()
{
  filterData();
  HTTPPost( );
  delay( 15000 );
  // If you remove the sleep, be sure to add more delay so you don't post to ThingSpeak too often.

}



int connectWifi()
{
  WiFi.begin( ssid , password );
  while (WiFi.status() != WL_CONNECTED) {
    //Serial.println( "Connecting to WiFi" );
    delay( 2500 );
  }
  //Serial.println( "Connected" );  // Inform the serial monitor
}

void HTTPPost() {

  // This function builds the data string for posting to ThingSpeak and provides the correct format for the wifi client to communicate with ThingSpeak.
  // It will post "numFields" worth of data entries, and take the data from the fieldData parameter passed to it.
  // Be sure to increase numFields to the number of fields you need, and activate the fields in your channel view.

  if (client.connect( thingSpeakAddress , 80 )) {

    // Build the Posting data string.  If you have multiple fields, make sure the sting does not exceed 1440 characters.
    String PostData = "api_key=" + writeAPIKey ;

    PostData += "&field1=" + ledState;
    PostData += "&field2=" + brightness;

    // POST data via HTTP
    client.println( "POST /update HTTP/1.1" );
    client.println( "Host: api.thingspeak.com" );
    client.println( "Connection: close" );
    client.println( "Content-Type: application/x-www-form-urlencoded" );
 
   client.println( "Content-Length: " + String( PostData.length() ) );
    client.println();
    client.println( PostData );
    client.stop();
  }
}

void filterData() {
  if (Serial.available())
  {
    String buffer = "";
    buffer = Serial.readStringUntil(';');
    int i1 = buffer.indexOf(',');
    ledState = buffer[0];
    buffer.remove(0, i1 + 1);
    brightness = buffer;
  }
}

Credits

Sagnik Ghosh

Sagnik Ghosh

4 projects • 38 followers
DIY enthusiast- Crazy Engineer,Mathematics is Love,Projects on Embedded S/W, Electronics, RC Vehicles.

Comments