Tuesday, June 21, 2016

Wemos power consumption (No sleep)

If you are considering to battery power your device, it is good to know how long your sensor can run. This week I checked out my wemos' power consumption. I used an arduino mega to power my wemo since I do not have an external 5v power supply. I loaded a code which simply sends a mqtt message every 5 seconds. The average current is 0.073A as seen in the photo below.

Let's calculate the duration of the life of our sensor. The batteries I know are around 2600mAh. So,
Time can last = 2600mAh / 73mA
= 35.616 hours

Note: This measurement is when the wemo is awake all the time. To optimise the battery life, you should sleep the wemo when it is not sensing data or sending data.

Sunday, June 5, 2016

How to install Mosquitto (Mqtt Broker) with websockets enabled on Raspberry Pi

(Skip to installation part at the bottom is you just want to install)

Building block of an Iot system (Network protocol)

There are many many protocols used in Iot systems but one of the most popular and free protocol is Iot protocol for communications is Mqtt. This is the protocol which I used in my projects. 

In this post I will share: 
  • What is Mqtt?
  • What is Mosquitto (Mqtt Broker)?
  • How to install Mosquitto with websockets enabled on raspberry pi.

What is Mqtt?


MQTT (formerly MQ Telemetry Transport) is a publish-subscribe based "light weight" messaging protocol for use on top of the TCP/IP protocol. The publish-subscribe messaging pattern requires a message broker. In this post, the Mqtt broker I describe is Mosquitto.

What is Mosquitto?


(Taken from Mosquitto website) Eclipse Mosquitto™ is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for "Internet of Things" messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino.

Basically, it is a broker for Mqtt protocol. Mqtt requires a broker as it uses a server client architecture unlike data distribution service(DDS) which does not require a broker. 

Mqtt Server client architecture

In the figure below you can see how an Iot system works using Mqtt protocol. Each client can be a sensor, computer, user interface, etc. In addition there can be multiple publishers and subscribers for each topic.

Mqtt server client architecture

How to install Mosquitto on Raspberry Pi/Raspberry Pi 2

1. Download and unzip source tar file from http://mosquitto.org/download/. We need to build from source codes because we have to enable websockets ourselves.
  • Right click mosquitto-1.4.9.tar.gz on the page and click copy link
  • In raspberry pi console type:
    • cd
    • mkdir mosquitto
    • cd mosquitto
    • wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
    • tar -xvzf mosquitto-1.4.9.tar.gz
2. Install some mosquitto dependencies before building mosquitto
  • In raspberry pi console type:
    • sudo apt-get install libssl-dev
    • sudo apt-get install libc-ares-dev
    • sudo apt-get install cmake
    • sudo apt-get install uuid-dev
    • sudo apt-get install daemon
    • sudo apt-get install libwebsockets-dev
3. Enable websocket
  • In raspberry pi console type:
    • cd
    • cd mosquitto
    • cd mosquitto-1.4.9 (whichever folder you unzipped to in (1))
  • edit config.mk changing the line:
    • WITH_WEBSOCKETS:=no to WITH_WEBSOCKETS:=yes
  • save
4. Build and install mosquitto
  • In raspberry pi console type:
    • cd
    • cd mosquitto
    • cd mosquitto-1.4.9 (whichever folder you unzipped to in (1))
    • sudo make clean
    • sudo make
    • sudo make install
  • Note: If you want to uninstall type sudo make uninstall
5. Now you have installed mosquitto but have not configured or started it. To configure:
  • In raspberry pi console type:
    • cd /etc/mosquitto
    • cp mosquitto.conf.example mosquitto.conf
  • Edit mosquitto.conf to include the lines
    • listener 1883
    • listener 9001 127.0.0.1
    • protocol websockets
  • Here we set port 1883 to use tcp protocol and port 9001 to use websockets
  • save
6. Add mosquitto user to rpi
  • In raspberry pi console type:
    • adduser mosquitto
  • Follow the instructions to add a user
7. Now you are done installing and configuring. We want to create make mosquitto as a service so that it starts on boot up and is controllable through systemctl.
  • Modify the lines in /etc/mosquitto/mosquitto.conf
    • #pid_file to pid_file /var/run/mosquitto.pid
    • #user mosquitto to user mosquitto
  • Create a .service file /etc/systemd/system/mosquitto.service with this content
[Unit]
Description=Mosquitto MQTT Broker daemon
ConditionPathExists=/etc/mosquitto/mosquitto.conf
Requires=network.target

[Service]
Type=simple
ExecStartPre=/bin/rm -f /var/run/mosquitto.pid
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/run/mosquitto.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target


  • To start mosquitto on boot up
    • sudo systemctl enable mosquitto or mosquitto.service
  • To disable auto startup
    • sudo systemctl disable mosquitto or mosquitto.service
  • Now you can do
    • sudo systemctl start mosquitto
    • sudo systemctl status mosquitto
    • sudo systemctl stop mosquitto
8. To check whether mosquitto is running
  • type  ps -ef | grep mosq && netstat -tln | grep 1883

Tuesday, May 31, 2016

DIY Home automation vs Samsung SmartThings (Cost)

This is my first content post. So I guess to kick start some interesting facts I will do a small literature review. Hopefully it will make readers interested in DIY home automation.

In this post I will do a brief comparison of the cost of Samsung SmartThings suite vs a DIY home automation  suite. I will not compare all products but only those which can be easily DIY.

Note that the DIY ones does not fully replace the original ones. Certain features will not have, but the function will be similar.

I took the price of Samsung SmartThings from Amazon. The prices are in USD so converting to SGD would be more expensive. For this post I used google currency converter to convert USD to SGD.

Home control Hub. 

For the control hub, a raspberry pi would be suffice costing about SGD53. Buy Link
Micro Usb charger SGD3.34 Buy Link

109.05 vs 56.34

Multipurpose Sensor (To sense door open or close)
Microcontroller SGD5.52 Buy Link
Micro Usb charger SGD3.34 Buy Link
Magnetic Sensor SGD1.09 Buy Link

SGD44.16 vs SGD9.95

Power Outlet
Microcontroller SGD5.52 Buy Link
Micro Usb charger SGD3.34 Buy Link
Relay SGD2.90 Buy Link

SGD60.72 vs SGD11.76

Motion Sensor
Microcontroller SGD5.52 Buy Link
Micro Usb charger SGD3.34 Buy Link
PIR sensor SGD1.50 Buy Link

SGD44.16 vs SGD10.36

Moisture Sensor
Microcontroller SGD5.52 Buy Link
Micro Usb charger SGD3.34 Buy Link
Humidity sensor SGD6.76 Buy Link

SGD44.16 vs SGD15.62

Total cost for Samsung SmartThings: SGD302.25
Total cost for DIY: SGD104.03
Total savings: SGD198.22

The above calculations is only for 1 set. Most of the time we buy more than 1 power socket. And note that each power socket saves almost SGD50!

In summary, DIY your home control system not only cost cheaper but it allows greater customization. But of course, DIY requires time and effort. There are good points when buying reliable brands products as well, it depends on what you want. So in time to come, I will look into the details of how to implement the system and upload some guides as well. Stay tuned.