Monday, June 23, 2014

Morse code for IoT

Configuration of wireless network is always a headache. Texas instruments has solved the problem by introducing Morse code...

Recently TI released a new single-chip IoT solution CC3200 with integrated WiFi and Cortex-M4 application processor. The chip is intended for all kinds of Internet-of-Things applications, including home appliances, smart metering, security systems, and more. Many of the target products do not provide any kind of user interface for entering configuration parameters of WiFi network access.

CC3200 LaunchPad development board.
For simplicity of setting up network connectivity, the network processor of CC3200 supports WPS and SmartConfig methods. WPS is considered unsecure, due to the mandatory PIN access mode, considered vulnerable to brute force attacks. Many prefer to disable WPS from Wifi router.

SmartConfig is a method introduced by TI for initial configuration of network access. The idea is simply to use a specific app in mobile or PC to transmit configuration parameters to the embedded device over wireless network. But how to do that if no initial network configuration exists?

When no configuration exists, the embedded device can not join any network, secured or unsecured, but it can listen to all traffic in a silent monitor mode. In case of secured network the content of individual packages is unknown, the only thing that the device can detect is MAC address and the length of individual packets.

The length of a packet is the coding key, a modern day Morse code. An application transmits dummy UDP packets with certain length. Device listens to the traffic and recognizes certain fixed pattern of packet lengths. After the pre-ample the actual network configuration including SSID and WPA key is transmitted as coded into packet length of following messages. Once network configuration is received, the device can start operating normally using the available network infrastructure.

Sunday, June 22, 2014

IoT coop now in live

In the past chickens only clucked, nowadays they also do twitter.

If a chicken is interrupted during egg laying process, they get upset and make neighbour irritating noise for quite some time. That's the initial problem; how to know when it is safe to go and collect eggs? And of course the problem shall be solved at most technical and complex way.


Instrumentation for nest occupancy detection wasn't as easy as I initially expected. Here is the story.

I had some spare ultrasonic distance sensors, so decided to go with that one. But ultrasonic pings do not echo from the coat of hens. Obviously plumes do absorb or diffract the sonic wave in a way that no echo is detected by the tranceiver. In addition to not being able to detect presence of chickens, there was another problem with the ultrasonic. Operation of the sensor made hens nervous. Either they do hear the 40 kHz sound, or some other way do recognize it.

Ultrasonic sensor.
Next I decided to go with infrared gate sensor. I had some IR receivers model TSOP1740. The angle of the transmitting IR LED was limited with a shader, as well as on the receiver side. However, the gate sensor didn't recognizes the chicken passing the gate , even if it was sitting right in the middle of line of sight between transmitter and receiver. Obviously plumes do reflect the IR light in a way that it reflects from the walls of the nest box end eventually reaches the sensor.

Infrared sensor.

The TSOP-family of IR-receivers is designed to ensure signal reception even in poor conditions. With internal gain control and noise canceller, it is proven to perform well in remote control applications. One can experiment that at home by pointing TV remote controller towards the back wall and being able to control the telly that way.

Finally I decided to go with laser. Problems with absorption, diffraction, and reflection are now eliminated. Only a fully transparent chicken which does bend the laser light at all would not be detected. If such a chicken is made of any known solid transparent material like glass or plastic, it would most probably bend the beam and interrupt the signal.

Laser sensor
Once again the sensor is build from spare parts readily available in the self. Semiconductor laser transmitter is a no-name component from DealExtreme. As it has red color, infrared photo detectors I have do not work with it. Thus the only option available was a plain old photoresistor. LDRs have slow response to illumation changes, but according to my measurements over 90% of the resistance change occurs within 10 milliseconds. Blinking the laser with 10% duty-cycle with 100ms interval gives good tradeoff between detection accuracy and power consumption. 

Finding a proper sensor to detect chicken in a nest was the most complex part of solving the given problem. Rest of the technology is explained in previous postings. You can now follow the chickens in Twitter @iotcoop.

 Edit June 23th:
According to live data, hens do many short visits to nests in between laying an egg. When laying an egg, the visit takes half an hour or so. Now the algorithm has been changed in a way that visit to nest is only reported if it took more than 5 minutes. Then there most probably exists a new egg.

Edit Oct. 2nd:
Due to winter season, the IotCoop will be inactive untill Spring 2015.

Tuesday, June 17, 2014

IoT instrumentation with LabVIEW

Sensor and actuator instrumentation made easy in Internet of Things applications with LabVIEW.

In my Chicken house project, I gained the understanding that in an IoT project nowadays, the IT part is the easy one, and the embedded instrumentation with microcontroller programming and stuff is the most effort consuming one. This may sound obvious to many, but as I have my roots in embedded systems development, I have considered embedded easy, and IT more challenging.

This makes me think that if IT is so easy with modern technology, there must be a more advanced solutions for the instrumentation part as well. Implementing pure C with direct register access is something programmers did already many decades ago, and it haven't changed a bite.

LabVIEW from National Instruments is widely used in measurement and test applications in different industries. LV provides powerful signal processing functions, together with easy and intuitive graphical programming language. LV is a great utility if you need to do something quickly.

Architecture of IoT instrumentation with LabVIEW..

LV was first introduced in 1986, at the time when every single thing was not yet networked, thus the basic architecture of the tool is not primarily intended for distributed computation. However, LV does very good job in parallel computing environments utilizing multi-core CPUs efficiently.

Measurement products of RIO family from NI can run LV code in stand-alone mode without a separate PC connected. That makes RIO and LV good for measurement instrumentation in different conditions. LV can be easily made exchange data with a cloud service or other backend servers, as demonstrated here.

Minimal example of MQTT client implemented with LabVIEW.
MQTT is a M2M protocol well suited for IoT applications with limited bandwidth. A third-party contributed MQTT library is available via NI web pages. The library makes it rather easy to connect the LV program to a MQTT broker running in a cloud or other server, or in Raspberry Pi for example as in my case. RabbitMQ is an example of broker supporting many protocols, contributed by the open source community.

Once the data is delivered to a suitable broker, it is very easy to access it from cloud software, mobile app, web page, or other backend software. As I have told earlier, Node-RED and Bluemix makes it ridiculous easy to develop and deploy cloud software. Node-RED can in fact run in any hardware capable of running node.js, not in cloud only. I'm running Node-RED in a Raspberry Pi for example.

Minimal Node-RED example to receive data from MQTT broker.
With the measurement and signal processing power of LabVIEW and RIO products, combined with integration to cloud and backend services, we really have a quick start to implement IoT solutions with real world physical instrumentation. As the RIO architecture has CPU and FPGA integrated, fast signal processing and even real-time machine vision is possible on the field. Then only the processed higher value data is delivered to further storage and analysis in other places.

NI products are perhaps not most cost efficient if you need a mine field of 5 bucks WSNs. But if there is a complex process to monitor, and you need to do it quickly, then this combo is really worth of considering.

Tuesday, June 10, 2014

Connecting things to Internet

IoT is nothing without the physical things connected. Here is a simple example how to use Node-RED to connect things to Internet.

Node-RED makes it easy to connect the physical world into the IoT. Wireless Sensors are one example of things, and applied in this exercise.

Raspberry Pi with JeeLink attached, and JeeNode USB at back.
JeeNode is a family of inexpensive AVR MCU boards with integrated RFM12 radio module. RFM12 is an inexpensive Sub-1GHz RF transceiver for unlicensed ISM bands (315,433,868,915MHz). JeeNodes are compatible with Arduino development environment and there is RFM12 library available, which makes it very easy to start experimentation.

System diagram
Raspberry Pi acts as a gateway receiving messages send by wireless nodes, processing the data and forwarding it to MQTT broker, Twitter, etc. All software development in RPi has been done in Node-RED.

Wireless sensor transmits following JSON messages:
{
  "topic": "egg",
  "payload": "jeenode"
}
The format is compatible with the internal representation of flow data elements in Node-RED. These messages are received by the serial port receiver node.

Serialport handler in Node-RED
Handling serial port in Node-RED is very easy. After installing serialport package (npminstall serialport) and restarting the Node-RED service, there appears one new node in the Node-RED editor: serial. Here the serial port receiver has name "RFM12".

Node-RED serial port configuration window.
Messages received from serial port appear in msg.payload. If the message is JSON formatted, as is the case in this example, it is very easy to parse it to create a new msg with corresponding attributes. This function node called here as "JSON" is the only place in this example where Javascript/Node.js code is written manually.

Custom function to parse JSON messages received from serial port.
There may appear messages with different topics, thus a switch node is used to select proper processing flow for each topic. In this example, only messages with topic 'egg' are taken into further processing.

Configuration window for switch rules in Node-RED.
Simple as that! Now where are ready to do whatever whit the data received, to send it to MQTT broker or Twitter for example.

IoT made easy

Wiring the Internet of Things has never been so easy. Here is a simple example how the classical chicken or the egg problem is solved.

Node-RED is a browser-based flow editor for graphical wiring together of hardware devices, APIs and online services in new and interesting ways. Node-RED is an open source project initiated by IBM. It can run in local hardware or in a cloud service. In this example I'm using Raspberry Pi as local the hardware and Bluemix as the cloud.

Adafruit has kindly provided detailed and easy to follow instructions how to install Node-RED in Raspberry Pi. There is also some more explanation what Node-RED is in general. I followed the instructions and quickly had my Node-RED up and running in the embedded target.

Program running in Raspberry Pi
Program running in RPi receives messages via serial port 'Coop'. As at the time of the development there is no external hardware in place, there is debug button 'TestEgg' for test purposes. The program does some tweaking to content of the message 'Egg', and then transmits it to Twitter 'Tweet' and to external 'MQTT' broker dev.mosquitto.org for "twitter of things".

Tweets send by RPi when the test button is pressed.
Bluemix provides ready made application templates for different purposes to speed up development. IoT is one of the boilerplates containing Node-RED, IoT and time series database readily installed.
Bluemix templates
Node-RED program running in the cloud receives subscribed 'MQTT' messages from the broker, and stores messages into a database 'StoreEgg'.  There is http-server 'HttpRequest' waiting for requests, and once such occurs, the content of the DB 'FetchEggs' is dumped to the browser 'HttpResponse'.

Node-RED program running in Bluemix.
As this exercise is not about how to make cool HTML5 web pages, the simple database dump is good enough for proof of concept here.

Database dump in browser.
Simple as that! By adding few boxes, wiring them together, and configuring them, I have now in place IoT connectivity, cloud integration, social media engagement, database access and web user interface. There is only a few lines of node.js code implemented in this exercise, in order to add time stamp to messages in the function 'Egg'.

var d= new Date();
msg.payload = "Chicken called "+msg.payload+" laid an egg at "+d.toString();
return msg;
var d= new Date();
msg.payload = "Chicken called "+msg.payload+" laid an egg at "+d.toString();
return msg;
var d = new Date();
msg.payload = "Chicken called "+msg.payload+" laid an egg at "+d.toString();
return msg;

That's all folks!