Snap4City: Arduino & ESP8266 IOT Device NGSI

Arduino & ESP8266 – user Guide and developers guide:

It is a secure device with Mutual authentication and secure encrypted connection.

      

one registered it can be used 

  • go on Dashboard Wizard to create a new Dashboard rendering the values received from your Snap4City IOT Button
  • go on IOT Applications to create a flow and application reacting to the commands you are sending by pressing the buttons. Commands can activate any activity on Snap4City IOT Applications: sending email, sending SMS, notifications, acting on Dashboards, communicating with other applications, changing messages on some virtual panels, sending the alarm to get help, etc. Have a fun and enjoy the Snap4All button.

Mounting Guide

: Arduino Uno.

: ESP8266 ESP-12E UART WIFI Shield.

: Pixnor FT232RL FTDI USB.

:

  • Mount the shield on top of Arduino.
  • Connect the button's wires to pin 4 and to ground.
  • Connect the RGB led's wires to pin 11, 12, 13 and to ground.
  • Connect the DHT22's wires to pin8 and to ground.

User Guide

  1. As a first step we suggest you to be registered on Snap4City Portal in the role of Manager or upper. As a Manager, that is the first level of users in Snap4City, you can register your IOT Device and thus also an IOT Device created on the basis of Arduino Uno as explained in this page.

    • Go on IOT Directory and Devices and click the button "Add New Device" on top of the page.
    • To register a New Device “Arduino Uno Snap4City as described in this page”, please use the model "Arduino Uno". Then provide the needed information, such as a name you would like to assign to it and the typical GPS position. Thus the system will provide you
      • Broker URI, will be requested during the setup of Arduino in the next steps
      • Both Protection approaches (you have to provide them during the registration one of them), and you can have to chose:
        • K1 and K2 and SHA, or
        • Certificate and the Private Key. Clicking on "Private Key" and "Certificate" two files will be downloaded. We suggest you to move them to a folder, where you can save the other informations by copy past in a file

          You can get this information at any time from the list of your devices (IOT Directory and Devices  My Devices) by clicking on the "+" corresponding to the device you registered. In case of large number of devices, search for the name you assigned.

          The Arduino device operates in two different modalities:
    • Setup: that permits to configure your device;
    • Operative: that permits to use your device to send information to the Snap4City platform.

      It’s always needed to Setup your device before starting the Operative modality
  1. As a second step you have to SETUP and thus please:
    • Press the botton for 3 seconds and at your release it will start to blink white, notify the begin of Setup modality.
    • After few seconds, an Access Point will be created by the device. (the first figure in the following is showing the case in which an initial configuration is performed, while the second on right is presenting the case in which a second Wi-Fi network is added in addition to the first one. It can keep up to 5 Wi-Fi networks, with autated selection on the basis of the stronger)

                     
  1. As a third step, we suggest you to use a PC (or a smartphone) to connected to the WiFi created by the device.
    • The Access Point of Arduino can be easily recognised since it is going to have "Snap4City" in the AP name, the so called SSID. The AP name will also include a ID code segment of the device. Every Arduino has an identifier written in a piece of paper attached to the device. The unique identifier is composed by 5 segments: the first one is used for identification ( e.g. In Access Point name) and the last 2 segments (dash included) form the default password.
    • Please connect to the Arduino's WiFi AP using as password the last two segments of the identifier, such as "XXXX-YYYY". When you are connected to the Arduino's AP, you will not be entitled to go on Internet, but you can get connection with Arduino by using your browser and going to .
    • The WiFi AP web page  for Setup mode will present a form to fill in:
      • WiFi-SSID: (write here the Access Point SSID of the WiFi that Arduino has to use to connect with Internet in Operative mode). You can choose it from the options proposed in the dropdown field where the nearby connections are shown or write it manually.
      • WiFi-PSW: (write here the Access Point password of the WiFi that Arduino has to use to connect with Internet in Operative mode). If your WiFi network needs to control/register the MAC address of all devices for security reason, the MAC address of the button is provided in the same page.
      • Device Type: (write here the Device Type you gave Arduino during the registration of the device on Snap4City IOT Directory). If left empty, this field will be set to default (Ambiental).
      • IOT Device ID: (write here the name you gave to your Arduino during the registration on Snap4City IOT Directory).
      • Service Broker URI: (write here the Service Broker URI that Snap4City IOT Directory has provided to you on "+" information of your device, once completed the registraton of your Arduino). The URI provided should be one using HTTPS secure protocol.
      • Broker URI Port: (write here the Broker URI Port that Snap4City IOT Directory has provided to you on "+" information of your device, once completed the registration of your Arduino).
      • SHA thumbprint: (write here the value SHA that Snap4City IOT Directory has provided to you on "+" information of your device, once completed the registraton of your Arduino).
      • Security level: select one of the two options (K1&K2 or Certificate&Key) to adjust the desired security level. Depending on your choice, a different menu will be shown.
        • K1, K2: (write here the values of K1, K2 that Snap4City IOT Directory has provided to you on "+" information of your device, once completed the registraton of your Arduino).
        • Certificate and Key: (upload here the Certificate and Key that Snap4City IOT Directory has provided to you on "+" information of your device, once completed the registraton of your Arduino).
    • Once completed all fields, click "Save" to submit the information and start using the device.
    • Clicking on cancel at any time the device will be turned off and no information will be saved.
  2. After clicking "Save" the device will switch to Operative mode: it will try to connect to the SSID specified in the Setup. During this phase the led will blink  color for about 10/20 seconds. If the connection is correctly established, the led will turn green. Otherwise, it will turn red.
  3. From this moment, every 20 seconds the device will send temperature and humidity data. If the connection to the WiFi was correctly established, the led will blink  color during the sending and green/red according to the result of the sending. If the connection attempt failed before, the device will execute step 5 again.
  4. It's possible to send data on demand just performing a short press. The sending phase will be the same of step 6, with a  color blink followed by a green/red one.
  5. At any time is possible to enter the Setup mode performing a long press on the button (3 seconds). The same Access Point described in the step 4 will be created and you will be able to access it following the same instructions. Going to  a slightly different page will be shown. On top there will be a recap of the previously inserted parameters. Under "Saved Connections" will be listed all the saved connections by the user, selecting one of them and clicking on "Delete" it will be erased from memory and the AP will be shut down. After a moment it will start to blink  color for 20/30 seconds reporting the attempt to connect to the best WiFi. Again, the fields WiFi-SSID and WiFi-PSW, described before, will allow you to add a new connection. Note that in case of multiple connections the device will choose the strongest one. Just like before, clicking on "Cancel" the device will be turned off and no information will be saved. Finally clicking on the "Reset" button all the saved informations will be erased, included the security credentials. A confirm check will precede the operation, giving you the opportunity to prevent this from happening by mistake. After a reset, a 3 seconds pressure will activate the setup mode to let you provide a new configuration.
  6. If the device is unplugged the last status will be kept. When powered again, the first message will fail and automatically it will try to connect to the saved WiFi configurations.

Developer Guide

: Arduino Uno.

: ESP8266 ESP-12E UART WIFI Shield.

: Pixnor FT232RL FTDI USB.

:

  • Mount the shield on top of Arduino.
  • Connect the button's wires to pin 4 and to ground.
  • Connect the RGB led's wires to pin 11, 12, 13 and to ground.
  • Connect the DHT22's wires to pin8 and to ground.

:

  • Download the last version of the Arduino IDE.
  • Run Arduino IDE.
  • In Preferences > Additional Board Managers URLs add the links
  • In Tools > Board > Board Manager search for esp8266 by ESP8266 Community and install the boards.
  • In Sketch > Include Library > Manage search for fastled and install it.

  • In Tools > Board select the board Generic ESP8266 Module.
  • In Tools > Board apply the following settings:
    • Upload speed: 115200.
    • Flash frequency: 40MHz.
    • Flash mode: DIO.
    • Flash size: 1M (no spiffs).
    • CPU frequency: 80MHz.
    • Crystal frequency: 26MHz.
    • Reset method: ck.
    • Debug port: disabled.
    • Debug level: No assert - NDEBUG.
    • IwIP variant: v2 lower memory.
    • Builtin led: 2.
    • Erase flash: sketch + wifi settings.
    • Programmer: AVRISP mkll.
  • If some fields are not listed, due to different versions of the IDE, leave them by default.

:

  • The serial adapter has to be connected to the shield's debug pins according to the image below.
  •  
  • The switch's buttons on top of the shield have to be set to DOWN, DOWN, UP, UP.
  • Connect the programmer, through a USB adapter, to a USB port. In Tools > Port select the correct port for the device just connected.
  • Upload your code to the device pressing on the big arrow in top left corner.
  • Unplug the USB from PC.
  • Unplug the device from FTDI programmer.

:

  • To debug the shield set the switch to DOWN, DOWN, UP, UP.
  • Connect the programmer to the shield and than to the USB port.
  • Open the Serial Monitor in Tools > Serial Monitor. Be sure that the baund in bottom right is set to 115200.
  • From the top input field commands can be sent to the shield e.g. sending "L" it will enter setup mode.

  • In Sketch > Include library search and install OneButton by Matthias, DHT sensor library by Adafruit and Adafruit Unified Sensor by Adafruit.



  • From Tools > Board select Arduino/Genuino Uno.
  • In Tools > Port select the correct port for the device just connected.

:

  • No need to remove the shield, just set all switch's buttons to DOWN.
  • Connect the Arduino to the developer PC with its USB cable (different from above); sometimes this cable is called a USB printer cable.
  • Upload your code to Arduino pressing on the big arrow in top left corner.
  • Unplug the USB from PC.
  • Unplug the device from USB.

:

  • To debug Arduino set the switch to DOWN, DOWN, DOWN, DOWN.
  • Connect the Arduino to the developer PC with its USB cable.
  • Open the Serial Monitor in Tools > Serial Monitor. Be sure that the baund in bottom right is set to 115200.
  • From the top input field commands can be sent to the shield e.g. sending "R" the led will blink red.

    Important: after flashing the board and the shield, the buttons have to be set to UP, UP, DOWN, DOWN. Otherwise, the serial communication between Arduino and the ESP8266 shield won't be active.

Arduino Code

The Arduino code is responsible for carrying out three checks:

  • if the user presses the button and what kind of pressure it is
  • if 20 seconds have elapsed since the last comunication to the server. If so, a new measurement will be done and the new information will be sent.
  • if the ESP8266 has sent a command. The possible commands are:
    • W - start white blink
    • w - end white blink
    • R - start red blink
    • r - end red blink
    • (this pattern is used for green and blue too)
    • C - configured

ESP8266 Code

  • In the setup function the Serial and EEPROM are initialized. A status check is also performed to determine if the device is to be sent in AP mode. In this case, the Arduino will be notified to blink . The reachable pages of the webserver are associated with their handler functions.
  • These functions simply define the HTML content of the page and perform the necessary logic to carry out their purpose (for example, the handleSave function writes the values to EEPROM).
  • The getter methods can be used to retrieve the user's informations from EEPROM. This consists in reading the correct part of memory.
  • In the process of saving the certificate and the key, a base64 decoding is performed.
  • The connect to wifi method guarantees that the device is always connected to the most powerful wifi. It performs a scan of nearby networks and, if some of these have been previously saved by the user, sorts the saved ones according to their signal. Then it procedes to try to establish a connection following this order.
  • The send message method deals with forwarding data on the basis of a secure protocol. At the beginning a check is made to understand the level of security. The server is then contacted and, if available, its certification is verified and the data is sent.

 Source code is accessible from https://github.com/disit/snap4city/tree/master/Snap4CityIoTDevices