Building a Simple Arduino IoT Device

Now we know how to interact with our board via Serial, and it can interact with the world around it using the digital and analog functionality, we can take another look at what comprises some IoT Devices.

 

Example: Wifi Power Socket

There are WiFi Power sockets and LED Light bulbs which are simplistic IoT devices (as often only a couple of IO in use).  Often these are actually ICs found on popular Arduino boards, such as the ESP8266/8285 IC are used in some of the ITEAD SonOff power switches.

Some Sonoff module leave the program headers exposed, and with a little soldering you can even re-flash it with your own firmware (at your own risk of course), to add functionality, or integrate it with your own custom system.

 

Building an IoT Device with an Arduino Board

So to build our own custom IoT device, all we need is a board, with some IO to control/read, and a way of communicating with our PC, and eventually external devices such as a mobile phone or tablet PC.

We are assuming our board will be connected to our PC, so we can stay with Serial Comms in this first project to keep things simple.

We will also just control an LED on our board, and track an analog reading from the board, as this is easy to modify to most needs quickly.

 

Example Sketch

/*
    Name:       SimpleIoTDevice.ino
    Author:     Visual Micro
    Purpose:    Simple IoT Sketch, to be used in conjunction with the Serial Debugger
*/

const int LED_OUTPUT_PIN = 13;
const int TEMP_SENSOR_PIN = A0;

void setup()
{
    Serial.begin(115200);                   // Ensure we have comms to the PC via Serial
    pinMode(LED_OUTPUT_PIN, OUTPUT);        // Setup our LED
                                            // No Need to Setup Analog Pin here
}

void loop()
{
    int currentTempReading = analogRead(TEMP_SENSOR_PIN);   // Reading an external Temp Sensor
    currentTempReading = ((ADCW - 324.31) / 1.22) - 20;     // DEMO: Internal Temp Sensor on ATMega328p, with 20deg offset from observation 
    // The Serial Debugger will give us control and Monitoring over these pins with the breakpoint below...

    /*  Click in Margin to Add the break
        Right click the break, change the Actions to:
            a) Ensure Continue Execution is checked
            b) add the message {@ReportDigital}{@Plot.Temperature.Reading currentTempReading}
            c) Close and Upload the sketch to your board...
     */
    delay(1000); // Ensure we dont overload the Serial.... and have time to receive a response
}

 

Debugging Options

To save us having to build our own interface, we can use the Serial Debugger to provide one on our PC quickly.

We will need to add in tracepoints to track our analog variables, and enable the digital pin reports so we can affect the digital pins while the program is running from the debugger.

Tracepoint Example for simple IoT Solution

Once we have uploaded this with the new settings, the debugger will launch automatically, showing us the analog readings, and states of the digital pins.

To update the state of a digital pin simply click on it to alternate its state.

 

 

Web Interface

If your board supports WiFi, then you can embed a Web Interface onto the board, allowing the whole project to become as portable as the board you have chosen. 

This adds complexity of having to manage the WiFi, HTML/CSS/JS for the Web Interface and all code to transmit data, but a good starting example can be seen on our Power Socket Instructable, which uses WiFi and an embedded Web UI to control an RF Power Socket.

This guide will be updated shortly with a Web Version of the vMicro Serial Debugger, so you can add a Web UI hosted on your PC in a few clicks!