How to create an office or studio status indicator and doorbell with NodeMCU Part #2

Author:

Anders Kitson

This is the second part of our series on how to setup a status indicator and doorbell. Here we are going to be setting up our three different states, busy (red), recording(blinking red) & free (green)

We will connect our first device to wifi and aws iot.

Let's get into it.

Here's the video walk through as well

We are going to setup aws first. The first step is to setup the aws cli, you can install it here for your operating system.

Next you want to go to aws console, navigate to iam user add a new user and attach the following policies AWSIoTConfigAccess AWSIoTDataAccess

Add user policies

Then you want to create the user and refer to the Access key id and secret access key id throughout this series, remember to store your secret access key id because you can only view it once

Access Key and Secret access key

Next you would open up your terminal and type aws configure then enter the credentials it asks for from your iam user. Once that's done your are configured and ready to connect your board to aws.

aws configure

Next you want to open up mongoose os so you can run mos from your terminal which will open up a browser tab running mongoose os.

So first we want to connect to your wifi network via mos. You can run the following command. If your wifi network has a space in it, put it in quotes.

mos wifi wifiname password

Next we want to connect to aws, in order to do so we need to run the following command, replacing us-east-1 with your region

mos aws-iot-setup --aws-region us-east-1

you'll see that the device comes online, you will notice if you hit the flash button on the device you will get a publishing in the serial console.

State check

Next we want to start editing out code, and we want to do a check. So we will modify and Shadow handler to look as follows. We are checking if the desired state is recording so we can change the device from aws.

Shadow.addHandler(function (event, obj) {
  if (event === "UPDATE_DELTA") {
    print("GOT DELTA:", JSON.stringify(obj));
    for (let key in obj) {
      // Iterate over all keys in delta
      if (key === "on") {
        // We know about the 'on' key. Handle it!
        state.on = obj.on; // Synchronise the state
        setLED(state.on); // according to the delta
        //ADD THIS SECTION
        if (state.on === "recording") {
          GPIO.blink(14, 100, 200);
        }
      } else if (key === "reboot") {
        state.reboot = obj.reboot; // Reboot button clicked: that
        Timer.set(
          750,
          0,
          function () {
            // incremented 'reboot' counter
            Sys.reboot(500); // Sync and schedule a reboot
          },
          null
        );
      }
    }
    reportState(); // Report our new state, hopefully clearing delta
  }
});

Then we want to run the following commands to build, flash, connect to wifi and connect to aws. Your red led will turn off which is just fine.

mos build

mos flash

mos wifi wifiname password

mos aws-iot-setup --aws-region us-east-1

Next we are going to want to visit aws console -> iotCore -> things -> the board you are working on -> Shadows -> classic shadow -> edit

we want to add a desired state so changed the shadow state to look like the following.

leave everything in the ellipses the same

Shadow manual state change

Once you hit save on the state, the red led will start blinking. You can update this state through a post request via the interact url in iot board we are working on

{
  "desired": {
    "on": "recording"
  },
  "reported": {
    ...
  }
}

state example

Next we are going to be setting up the button so that we can send a message up to aws, and eventually connect to our ec2 express server. We hook up one corner of the button to pin d3 which is the flash button, and the other corner to the ground

Board button setup

Next we are going to visit aws iot, go to test and subscribe to the topic devices/# this will display an event when we press the button.

aws IoT test

In order to create a free status we are going to setup the green led. We are connecting one end of the green led to the positive strip on the bread board via a 20 ohm resistor and a jumper cable from the positive strip to the 3v3 on the board, and then from the other end of the led to the pin d0 which is GPIO16.

green led board setup part 1

green led board setup part 2

Writing to the green led

Next we are going to write to the green led. You can open up your code editor with init.js file.

//...

GPIO.set_mode(16, GPIO.MODE_OUTPUT);
GPIO.write(16, 0);

//...

Then we will run

mos build

mos flash

and the green led should turn on

Writing final state

Now that we have confirmed that the green led is working we are going to go back into the code and write some state to control the led's in different situations. So we are setting up a busy, free and recording state, that cancel each other out when necessary.

Shadow.addHandler(function (event, obj) {
  if (event === "UPDATE_DELTA") {
    print("GOT DELTA:", JSON.stringify(obj));
    for (let key in obj) {
      // Iterate over all keys in delta
      if (key === "on") {
        // We know about the 'on' key. Handle it!
        state.on = obj.on; // Synchronise the state
        setLED(state.on); // according to the delta
        if (state.on === "recording") {
          GPIO.blink(14, 100, 200);
          GPIO.write(16, 1);
        }
        if (state.on === "busy") {
          GPIO.write(14, 0);
          GPIO.blink(14, 0, 0);
          GPIO.write(16, 1);
        }
        if (state.on === "free") {
          GPIO.write(16, 0);
          GPIO.blink(14, 0, 0);
          GPIO.write(14, 1);
        }
      } else if (key === "reboot") {
        state.reboot = obj.reboot; // Reboot button clicked: that
        Timer.set(
          750,
          0,
          function () {
            // incremented 'reboot' counter
            Sys.reboot(500); // Sync and schedule a reboot
          },
          null
        );
      }
    }
    reportState(); // Report our new state, hopefully clearing delta
  }
});

Then we will build, flash, connect to wifi, connect to aws

mos build

mos flash

mos wifi wifiname password

mos aws-iot-setup --aws-region us-east-1

Once it's connected we should be getting our recording state and the led should be flashing. Next we will try and change the state from insomnia.

Next open up insomnia and create a post request to the interact url from your device

https://interacturl/things/boardname/shadow

and the state will look like the following

state request object

{
	"state" :{
		"desired":{
			"on": "recording"
		}
	}
}

Next we will configure aws security hit send and you can change the state of the led from recording, to busy to free. Congrats you made it. In the next part of the series we are going to be building our secondary device and then in the 4th part creating a ec2 instance that runs a express server api where the devices can talk to each other.

AWS credentials iam user 4

Next part coming soon