Anyone who's started learning about microcontrollers and code could tell you that a good place to start is by looking at the most basic example sketches provided. As often times their simplicity can teach you a lot more than you think. With the Arduino, one of my favorite examples is the blink sketch, which is deceptively short yet teaches you so much about how the Arduino IDE and Arduino boards work. So without further delay, let's take a look at an IoT version of the classic blink sketch.
Step 1: How Does Blink Work?
Before we can jump into the IOT side of things lets just take a look at how different boards are able to blink an LED. All microcontrollers such as the Arduino, NodeMCU or Sparkfun ESP8266 Thing have pins that we can control through code and one of the most basic things we can do to these pins in make them HIGH or LOW, turn them on or off. So for example when we set a certain pin to HIGH that pin will output power (anywhere from 1 to 3 volts depending on the board) however when we then set that pin to LOW it stops outputting power almost like a digital on-off switch. Then all we have to do is add a delay between the pin going HIGH and LOW and we have a blinking LED!
Step 2: Setting Up Our Board
So there are a lot of IOT boards out there, some made by Arduino, some made by Sparkfun, some made by Adafruit and many from other places. It would be quite difficult and long to talk about each device so we've compiled some of the most popular boards however this will work on all microcontroller IOT boards.
So for this project, we will be demoing on the following boards:
- Sparkfun ESP8266 Thing (Can be found Here)
- NodeMCU (Can be found Here)
- Adafruit Feather (Can be found Here)
- Arduino (Can be found Here)
Any one of these boards will work great with this example
Step 3: Code and Libraries
To upload our code we are going to be using the very popular Arduino IDE which can be downloaded Here . Now because we are going to be using the website AppShed to control and edit the way the board works we don't need to focus on the actual code going into the board, the code we are uploading is the AppShed master sketch which allows the website to control all the pins on the board.
Now to be able to upload code to our board through the Arduino IDE we need to install its library which allows to IDE to talk to our specific board. This is done as follows:
- Launch the Arduino IDE
- Navigate to File and click Preferences
- Towards the bottom, you should see "additional boards manager URLs" followed by a blank space
- Copy and paste this into the blank space http://arduino.esp8266.com/stable/package_esp8266c...
Now we need to install the boards under board manager.
- Navigate to Tools, then Board and then Click on Board Manager
- Now in the search bar search for ESP8266
- Click on the first option and click Install
Now our board is able to communicate with the Arduino IDE
Step 4: Uploading the Code
So at this point, we have downloaded the libraries that are needed to help the Arduino IDE communicate with our IoT board and we've downloaded the libraries that allow the AppShed master sketch to run, now we just need to upload the master sketch to the board.
To do this we need the following:
- Plug your IoT board into your computer
- Download and open the Appshed master sketch (which can be found here)
- Navigate to tools and click on board
- Scroll down until you see your board, then click on it (I'm using the NodeMCU so I'm going to click on NodeMCU)
- Now navigate back to tools and click on port, from here you should see your board (should look like this "com 9")
- Click the side facing arrow to upload and wait while it does so.
If you get a message after about 2 - 3 minutes saying done uploading then everything worked perfectly! To double-check that our board is working we can also go our WiFi setting and look for the name that we gave the board earlier if it's there it's working.
Step 5: Making the App to Control It
Now that we have the board up and running we want to be able to control the digital pins which allow us to turn the LED on and off and to do that we are going to be using the IoT side of AppShed.
The first thing we need to do is head over to appshed.com and register a free account so we can save our work. Once that's done we need to click on IoT Builder which will allow us to define the pins we want to control and then click on new IoT project.
Now I know that the built-in LED on my board is on pin 5 so I'm going to connect an LED to pin 5 in the IoT builder by clicking on the LED icon and then clicking on pin 5, however, if you have a different board you'll need to find out which pin your built-in LED is on which as easy as googling your boards name followed by built-in LED pin. Now we can just give it a name like LED and save it and we are ready to make the actual app!
So to make the app we can head back to that menu from earlier and click on appbuilder this time instead IoT builder. From here you should be presented with a simulated phone which is what we will be building our app on! The first thing to do is click the plus button at the bottom of the phone, which will take us into a new app and give us all the tools we need.
The first tool we will take advantage of is connecting the IoT board that we made to this app and we do that by clicking on boards and then we click on the board we made earlier. Now the board is linked to our app. Now we add our first button which will turn on the LED. To do this we click on the button icon under forms which will then give us to option to give it a name and actions. We start by naming it LED On then we click on the actions tab to give it an action. The action we want to give it is control of the LED pin from our IoT builder board so we click on change action and then scroll all the way to the bottom where we should find the LED action, once we've clicked on it we are then given the option to make this button set the pin as on or off, since we named the button LED on we are going to make it turn on the LED. And lastly, we just need to click save. Now we can do the same prosses for the off button and we should have a functioning barebones app that will allow us to turn on and off and LED from our phone!
Step 6: Publishing and Testing the App
At this point we have our app and IoT board ready to go, the last thing we are going to do is get our app onto our phone or anything other smart device and to do that we need to click on publish which will render the app allowing us to use it.
Let's just talk for a second about what the app is, unlike other apps you won't need to download it from the IOS or Play store and it is compatible with every single device that has a web browser this is because it is a web app that runs on your phone's browser. Which means once your app is finished publishing you can send the link to your phone by clicking share and scanning the QR code or any of the other options and the app will show up on your phone instantly.
Now if you tried clicking the on and off buttons nothing would happen because the IoT board isn't connected to your phone yet. To connect it we just need to go to your phones WiFi setting and connect to the name we gave it earlier. Now if you go back into the app you should be able to control the LEDs from your app! (if for some reason it isn't working check out the troubleshooting step)
Step 7: Taking It Further
When looking at this app and this project it does look pretty barebones but keep in mind that this can lead to much cooler projects, for example, you could connect a motor to your IoT board and control your blinds through your phone, connect a servo to the IoT board and make a phone controlled door lock or if your really skilled electronics and understand how painful and deadly it is to touch 110/220 volts you could even use a relay and an IoT board and control the lights in your room all from your phone.
Much more can be done with the app too! I spent about an hour or two trying to get my app looking really nice so I could use it in future projects and this is what I was able to come up with My App (if you're using the same IoT board as me my app should be able to control your IoT device)
So don't let the simplicity of this project hold you back!
Step 8: Troubleshooting
Of course, things don't always go as planned and that's exactly why this step is here to help sort out any issues you might have.
- Reset you IoT device, most of the time there will be a little button on your device that resets it, hold that button down for 2 seconds and then reconnect your phone to the device.
- Make sure your LED is on the right pin if you're using a different board to me there's a great chance that your LED isn't on pin 5 like mine is. The best thing to do is google the name of your board and do a little research to find out which pin the built-in pin is linked to
- Make sure that your AppShed app tells you that it's downloaded and ready to be taken offline before you swap to the IoT devices WiFi