Implementing the smart streetlight project

Now that we have completed all the prerequisites, let's implement the project. Open your Arduino IDE and paste the following code.

#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#include <Ethernet.h>
#include <EthernetClient.h>

#include <Wire.h>
#include <Sparkfun_APDS9301_Library.h>
APDS9301 apds;

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
/************************* Ethernet Client Setup *****************************/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(172, 17, 0, 2);
IPAddress myDns(8, 8, 8, 8);


/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME  "yourusername"      //Replace this with your username
#define AIO_KEY       "youradafruitiokey"  //Replace this with your Adafruit IO key


/************ Global State (you don't need to change this!) ******************/

//Set up the ethernet client
EthernetClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1);  }


/****************************** Feeds ***************************************/

// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish light = Adafruit_MQTT_Publish(&mqtt,  AIO_USERNAME "/feeds/light");
Adafruit_MQTT_Publish bulb = Adafruit_MQTT_Publish(&mqtt,  AIO_USERNAME "/feeds/bulb");


/*************************** Sketch Code ************************************/

void setup() {
  Wire.begin();
  Serial.begin(115200);
  apds.begin(0x29);

  Serial.println(F("Smart Bulb with Adafruit IO"));

  // Initialise the Client
  Serial.print(F("\nInit the Client..."));
  Ethernet.begin(mac, ip, myDns);
  delay(1000); //give the ethernet a second to initialize
}


void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  //Get Temperature and Humidity values from SHT21
  float lux = apds.readLuxLevel();

  // Now we can publish stuff!
  Serial.print(F("\nSending lux value: "));
  Serial.print(lux);
  Serial.print("...");
  if (! light.publish(lux++)) {
    Serial.println("Lux Failed");
  } else {
    Serial.println("Lux OK!");
  }

  if (lux < 100) {
    buttonState = 1;
  }
  else {
    buttonState = 0;
  }

  if (buttonState != lastButtonState) {
    if (buttonState == 1) {
      bulb.publish("ON");
      Serial.println("Bulb is now on");
    } else {
      bulb.publish("OFF");
      Serial.println("Bulb is now off");
    }
  }
  delay(50);
  lastButtonState = buttonState;


  // ping the server to keep the mqtt connection alive
  if (! mqtt.ping()) {
    mqtt.disconnect();
  }
  delay(10000);
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
  }
  Serial.println("MQTT Connected!");
}

In the code, replace "yourusername" with your Adafuit IO user name, and "youradafruitiokey" with your Adafruit IO key which are available in the My Key option in Adafruit IO.

Now we need to export the Compiled Binary for this program. Go to Sketch and select Export Compiled Binary. This should save a hex file at the location you have saved this sketch. Now go back to Simuli Virtual Lab and open the new instance we had created for this project. Our workspace will now open. Upload the sketch_name.ino.standard.hex file by clicking on the Arduino and navigating to where the sketch was stored. Now we just need to reset the Arduino and the program will be loaded.

The Arduino starts reading the lux value and sends this data to Adafruit IO via MQTT. The lux values are then plotted on a graph to show the trend in luminosity. When the lux value gets below 100 lux, the program sends a message via MQTT to Adafruit to toggle our switch on.

When the lux value again starts to increase and it increases over 100 lux, the program again sends a message to Adafruit via MQTT telling them to toggle the switch off.

This is a simulation of how smart streetlights can work. You can create any amount of switches in the Adafruit IO dashboard and connect them to the bulb feed to simulate how a network of streetlights would subscribe to the same topic.

Last updated