Saturday, May 26, 2018

1. Introduction
In this post, we will learn how to build an IoT Dashboard using Node-Redand node-red-dashboard.
These set-up can be deployed on Raspberry Pi, Orange Pi, ... easily.
We will make a demo for this post, a simple smart home demo: a floor has 1 bulb and 1 temperature sensor. They will be control by a ESP32. This ESP32 send and receive data using MQTT protocol. A server with MQTT broker, Node-Red, NodeJS and a dashboard with a chart to monitor temperature and a switch to control the bulb.

2.1 Concepts
With this dashboard:
+ Layout will be considered as a grid. It is in Dashboard Tab.
+ A group element has a width - by default 6 'units'.
+ A unit is 48px wide by default with a 6px gap.
+ A widget in the group also has a default 'auto'. It means it will fill the width of the group contained it, or you can set it to a fixed number of units.2.2 Features
+ Layout: Tab, Link to other web pages.
+ Theme: Light, Dark or Custom Theme.
+ Widgets include Button, Chart, Form, Gauge, Notification, Switch ...Note: the node-red-dashboard will be deployed at: http://localhost:1880/ui (change localhost accordingly)2.3 InstallationNote: I instaled all things on my localhost
I assume that you did install NodeJS
Then install Mosquito broker following How to turn the Orange Pi/Raspberry Pi into an IoT node

Figure: ensuare MQTT broker is running

Then you need to install Node-Red following Demo 8: How to use TCP/IP with Arduino ESP32 - part 1.
Then install node-red-dashboard. It requires Node-RED version 0.14 or more recent. Open Terminal and typing commands:cd ~/.node-rednpm i node-red-dashboard
Then run Node-Red from Terminal using command:node-red
You will see:

Or you can import Node-Red model from script below:
[{"id":"bc6c4c2e.077c5","type":"mqtt in","z":"d4b07266.f397","name":"","topic":"floor2/temp","qos":"2","broker":"fc2a1412.370a68","x":284.5,"y":478,"wires":[["b9eef4aa.a11648"]]},{"id":"fc2a1412.370a68","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Wednesday, March 21, 2018

The nRF24L01 2.4 GHz wireless chip from Nordic Semiconductor. It has:
- SPI interface, hardware link layer, multiple pipelines, ...
- The chip is very cheap.
Comparing to Wifi modules such as ESP8266, ESP32:
- It has less power consumption.
- It is cheaper (but need a MCU to control).
- It has lower data rate.
If your Wifi infrastructure is good, you can consider using Wifi modules for your application.
If power consumption is not your issue, you can consider using Wifi modules for your application.
If we use nrf24l01 and need to send data to cloud via Internet, consider using ESP or Raspberry as a Gateway.
Some main characteristics: 1.1 NRF24L01 Hardware Interface
- 8 pins Vcc, GND, IRQ, CE, SPI pins (CSN (chip select not), SCK, MISO, and MOSI).
- IO pins are 5V-tolerant and operating range of 1.9 to 3.6V for Vcc.
- CSN, SCK, MISO, and MOSI for data transmission and reception.
- The CSN pin is active-low, and is normally kept high. When this pin goes low, the 24L01 begins listening on its SPI port for data and processes it accordingly.
- CE is used to control data transmission and reception when in TX and RX modes.
- IRQ is the interrupt pin, and is active-low. There are three internal interrupts that can cause this pin to go low when they are active. Each of these bits can be masked out such that when the bit’s respective interrupt becomes active, the status of the IRQ pin is not changed.1.2 Interfacing the nRF24L01 via SPI
- The SPI interface allows you to read/write registers, transmit data, receive data from and to the 24L01. For start step we use SPI at 2 Mbps. The high SPI data rates are used if you needed huge on-air data rates.1.3 SPI Instruction Set Summary
- In order to send data to or receive data from the SPI port on the nRF24L01:
+ The CSN pin on the 24L01 must be high then bring the CSN pin low to receive SPI data. Note: this pin will stay low throughout the entire transaction.
+ You will transmit the command byte of the instruction you wish to send. If you are receiving data bytes for this instruction, you must then send one byte to the 24L01 for every one byte that you wish to get out of the 24L01. If you are just sending the 24L01 data, you simply send your data bytes and generally don’t worry about what it sends back to you.
+ Once you have transmitted and/or read all of the bytes that you need, you bring CSN back high.
+ When you send any command byte, the 24L01 always returns to you the STATUS register.1.4 FIFO Info
- There are FIFOs for both TX and RX modes.
- Both of the FIFOs will hold the three newest packets that have been put into them. If you receive three packets in RX mode and you don’t read the payloads, the first (oldest) packet received is pushed out by the newest packet received. The same goes for the TX payload – if you load three packets and don’t transmit them (by executing the aforementioned CE toggle), then the fourth packet will push out the first packet you loaded.1.5 Data Packet Format
- The transceiver protocol has 2 modes:Shockburst and Enhanced Shockburst
- The message format for both Shockburst and Enhanced Shockburst modes are different.
- In both modes, the preamble is sent first (1 byte), is used to allow the receiver to know that what it is hearing is the beginning of a packet and not just on-air noise.
- The next bytes sent are the address bytes. This is set by the user, and is between three and five bytes long.
- The next bytes sent is different between the two modes. In Enhanced Shockburst only, a flag word of nine bits is sent to indicate the message status concerning re-transmissions. Only two bits are currently used (a count of resent packets), and the other seven are reserved for future use.
- The last half of the packet that is sent is the same in both modes. The first of the fields to be sent in both modes is the payload data. The length of the payload is also set by the user (1 to 32 bytes long). The final part of the packet to be sent is the CRC, which is user-settable (0, 1, or 2 byte(s)).
Let 's make 2 demos:- Demo 1: ESP32 Nano will send "hello" message to Arduino. - Demo 2: Arduino Nano will send "hello" message to ESP32.
If you have 2 ESP32 you can replace Arduino Nano with ESP32. 2. Hardware
You need 2 Arduino/ESP8266/ESP32/Raspberry boards for testing: 1 board will send/receive message and 1 board will receive/send message which was sent by first board.
I used Arduino for testing, just do wiring like below:Arduino_D8 x NRF24_CSNArduino_D7 x NRF24_CEArduino_D13 x NRF24_SCKArduino_D11 x NRF24_MOSIArduino_D12 x NRF24_MISOArduino_3.3 x NRF24_VccArduino_GND x NRF24_GND
For ESP32, I modified the library RF24 so that the software can use SPI software instead of using SPI hardware. Do wiring like below:NRF24_CE x ESP32_IO12NRF24_CSN x ESP32_IO14NRF24_SCK x ESP32_IO26NRF24_MISO x ESP32_IO25NRF24_MOSI x ESP32_IO27NRF24_Vcc x ESP32_3.3VNRF24_GND x ESP32_GND3. Software
For ESP32, I modified the library RF24 so that the software can use SPI software insted of using SPI hardware. The new API has new form:RF24(uint16_t _cepin, uint16_t _cspin, uint16_t sck, uint16_t miso, uint16_t mosi)
and to create an instance: RF24 radio(12, 14, 26, 25, 27);
For Arduino, we use SPI hardware so just use form: RF24(uint16_t _cepin, uint16_t _cspin)
and the instance is RF24 radio(7,8);- Demo 1: ESP32 Nano will send "hello" message to Arduino.ESP32 transmitter code

1. Introduction
In this demo, I will show you how to create a Facebook Messenger chat bot for monitoring and controlling home devices using Raspberry/Orange Pi and ESP32/8266. This is not a full solution so you need to improve it by yourself.

You can refer to this guideline.And choose type of Page is Business, Brand or Place. In my demo, I created a page named IoT Sharing.

2.2 Setup Facebook chat bot

In order to develop or use Facebook services for Software development you need to register a Facebook developer account here.

After created a FB developer account choose My Apps -> Add a new App -> Fill Display name

In Select a Product -> choose messenger -> Set Up

Choose Webhooks -> Setup Webhooks

Note: We do not do this step now. This step will be done after running the Raspbbery/Orange Pi software. Because after filled Callback URL, Verify Token and press Verify and Save, FB will send a GET request with "hub.challenge=value of Verify Token" (in this case Verify Token is "iotsharing.com" string)to Pi. Pi need to check the value of hub.challenge must match the value that we filled in Verify Token field (in this case Verify Token is "iotsharing.com"string). And then send this hub.challenge back to FB to finish the verification.

Pi code (chatbot.py):
We use variable history to hold the id of Facebook messenger sender while waiting for the response from ESP. This sender id is used in reply() function to send response back to Facebook messenger sender.

5. Steps to deploy
- MQTT broker run on Pi
- Open 2 Terminal on Pi:
+ Run "python chatbot.py" with Flask in it.

+ Run ngrok (5000 is the port Flask listen on): "./ngrok http 5000"

Copy the url in red box to Callback URL field that mention in step 2.2 Setup Facebook chat botNote: in case we shutdown ngrok and run it again the new url will be generated. We have to re-register this url to Facebook.
Choose Webhooks -> Edit Subscription

And then Unsubcribe and Subscribe the page again:

Now go to the Page that we created, choose About and Send Message

And type one of commands:"set floor1/room1/led1 on""set floor1/room1/led1 off""get floor1/room1/temp1"6. Result

Tuesday, January 16, 2018

1. Introduction
In previous demos, I showed you how to use MQTT/MQTTS and how to update firmware OTA (TCP/UDP and HTTP). In this demo, I will show you another interesting topic. That is how to download and multi-part upload a file via HTTP.

Figure: multipart upload and download a file via http

Based on this demo, you can build an application that can download the new firmware or the configuration file to memory card that is attached to client board for using later. Or you can upload the log file that records occurred events in run time to a server.
This is the requirement of this demo: a ESP32 will download a file from internet and after finish downloading, upload it to local apache server. The downloaded file will be stored in sdcard.
In order to make it easy, I created a library here in github. You just install and use it.Note: this library can be apply for ESP8266.2. Software
The library is simple to use. It has 2 APIs: upload() and download() with parameters. The parameters are:
- url of uploading and downloading file.
- progress displaying callback function.
- response processing callback function.
- reading and writing data to storage (sdcard, SPIFFS, ...) callback functions.
You should change them accordingly to your application.Note: The url parser of this library is quite simple and it will be updated later.

Note: The variable$_FILES['data']['name'] has fixed 'data' field name (similar to html form ""). The downloaded file will be stored in sub-folder 'uploads' which is relative to folder where the php script located. Reference this post to install local apache server.3. Hardware
For harware connection please refer here.

Thursday, December 7, 2017

1. Introduction
When developing software for ESP8266 or ESP32, you often face error(s) that caused the ESP halt. At that moment, looking the Serial Monitor window, you will see the error/exception like below:

Figure: error that caused ESP CPU halt

It is not easy to know what was happening with our software. Actually, the string that is highlighted has meaning; it is the called stack of software when the CPU halted. There is a tool that supports you to decode the ESP error/exception above. It called EspExceptionDecoder. It is tool that is attached with Arduino IDE.
In order to install it, please follow these steps:
- Install Arduino IDE with ESP8266/ESP32 core.
- Download the tool here.
- Create tools directory under Arduino directory if it is not exist.
- Unpack the downloaded tool into tools directory (the path will look like /Arduino/tools/EspExceptionDecoder/tool/EspExceptionDecoder.jar). - Restart Arduino IDE.

Here the bug is that buf is not initialized buf buf[2] is assigned to 4.
Loading the software to ESP and look the Serial Monitor:

Figure: error in software

After that choosing Tools->ESP Exception Decoder, a window will occur.

Figure: Exception Decoder window

Copy and paste the string "Backtrace: 0x400d05be:0x3ffc77c0 0x400d9b1a:0x3ffc77e0
" to it and you will see the output:

Figure: the result after decoding stacktrace

So you see the CPU halted executing the code at line 4: buf[2] = 4; as we knew.
This is just a simple application. In real world, you may face a more difficult situation than this, but at least you could know where your application is halted.