Implementing the Smart Lamp project.

Once everything has been configured, open your Arduino IDE and paste the following code:

#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

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

bool mqttConnected = false;

/************************* Ethernet Client Setup *****************************/
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

//Uncomment the following, and set to a valid ip if you don't have dhcp available.
IPAddress iotIP (109, 74, 198, 92);
//Uncomment the following, and set to your preference if you don't have automatic dns.
IPAddress dnsIP (8, 8, 8, 8);
//If you uncommented either of the above lines, make sure to change "Ethernet.begin(mac)" to "Ethernet.begin(mac, iotIP)" or "Ethernet.begin(mac, iotIP, dnsIP)"


/************************* 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 ***************************************/

// Setup a feed called 'assistant-lamp' for subscribing to changes.
Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/assistant-lamp");

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

void setup() {
  Serial.begin(115200);
  pinMode(7, OUTPUT);

  digitalWrite(7, HIGH);
  delay(2000);
  digitalWrite(7, LOW);
  Serial.println(F("Smart Lamp with Google Assistant"));

  // Initialise the Client
  Serial.print(F("\nInit the Client..."));
  Ethernet.begin(mac, iotIP, dnsIP);
  delay(1000); //give the ethernet a second to initialize
  
  mqtt.subscribe(&lamp);
}


void loop() {

  while (!mqttConnected) {
    MQTT_connect();
  }

  // this is our 'wait for incoming subscription packets' busy subloop
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(1000))) {
    if (subscription == &lamp) {
      Serial.print(F("Got: "));

      char *received = (char *) lamp.lastread;
      Serial.println(received);
      Serial.println(lamp.lastread[0]);
      if (lamp.lastread[0] == 49) {
        digitalWrite(7, HIGH);
        Serial.println(F("Turn On Light"));
      }
      else if (lamp.lastread[0] == 48) {
        digitalWrite(7, LOW);
        Serial.println(F("Turn Off Light"));
      }
    }
  }

  // ping the server to keep the mqtt connection alive
  if ((millis() % 1000) == 0) {
    if (! mqtt.ping()) {
      //mqtt.disconnect();
      mqttConnected = false;
    }
  }
}

// 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()) {
    Serial.println(F("Adafruit MQTT was already 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!");
  mqttConnected = true;
}

In the above code, replace "yourusername" with your Adafruit IO username and "youradafruitiokey" with your Adafruit IO key which is present under My Key in Adafruit IO.

Here we are connecting to Adafruit IO over MQTT and subscribing to the assistant-lamp feed. When we receive a 1 on this feed, the LED connected to pin 7 switches on and when we receive a 0, the LED switches off.

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.

We have successfully completed this project and have a working voice-controlled lamp ready.

Last updated