Thursday, January 11, 2018

Remote accessing the Raspberry pi over internet

In this guide, an xmpp based client-daemon is used as a chat-bot, this chat-bot is written in C++ and it is based on gloox-xmpp-client library. Xmpp provides an easy way of communication between two clients which are typically behind a firewall. A jabber server with public-ip over internet helps to achieve this secure communication between two xmpp clients.
Following steps explain how to compile and run the xmpp-client-daemon(chat-bot) on raspberry pi. Here is a diagram showing typical setup of remote communication between raspi and a smart phone.

How to prepare raspberry-pi to act as a chat-bot?
(following steps are tested on a raspi running 2017-11-29-raspbian-stretch-lite.img)
  1.  Login to the linux-shell-terminal of your raspi using directly connected keyboard or ssh.
  2.  sudo apt-get update
  3. sudo apt-get install cmake git libjson-c-dev libgloox-dev openssl
  4. mkdir /home/pi/xmproxy
  5. cd /home/pi/xmproxy
  6. git clone
  7. cd /home/pi/xmproxy/brbox/sources
  8. cmake -H. -BOutput -DCMAKE_INSTALL_PREFIX=/home/pi/xmproxy/buildir -DAUTO_SVN_VERSION=OFF
  9. cmake --build Output -- install (suffix -j5 incase of quad-core-raspi for faster build)
  10. cd /home/pi/xmproxy/buildir
  11. echo "user:" >/home/pi/xmproxy/buildir/xmpp-login.txt;echo "pw: super-secret-pw" >>/home/pi/xmproxy/buildir/xmpp-login.txt
  12. export LD_LIBRARY_PATH=/home/pi/xmproxy/buildir/lib
  13. /home/pi/xmproxy/buildir/bin/xmproxysrv --loginfile=/home/pi/xmproxy/buildir/xmpp-login.txt

Check on your android phone's hangout if "my-raspi-usrname" is online, if so, then send "help" message and the reply would show the list of available commands.

enjoy chatting with your raspi-chat-bot!!

Note-1: For proper operation of xmpp with google account, ensure that your Raspi's account has this allow-less-secur-apps setting enabled. 

Note-2: Two xmpp accounts need not be on google, any two jabber accounts would work. Here is an explanation on preparing the jabber accounts.

Note-3: steps explained above would work on any debian based linux distro machine.
source code is open, suggestions and contributions are welcome.

Sunday, January 07, 2018

Overwrite stock A5-V11 Chinese firmware with openwrt-based a5v11-xmpp-image

This is a step-by-step guide for overwriting stock OEM chinese-image of a5-v11 with openwrt based a5v11-xmpp image.

what is a5-v11?: This is a low cost(5Euro) wifi router capable of running openwrt linux.

what is a5v11-xmpp-image?: this is a customized openwrt image which includes a c++ based xmpp-client-daemon for IoT application - look here for details.

Setup: As shown in the picture below, item-1,2 and 3 are needed and item-4,5 and 6 are optional - they are needed only when you are unable to figure out the ip address of a5-v11 using its hostname. There are otherways to know the ip of your a5-v11, ex: looking at the dhcp client list on your home-router's webpage. For any reason if you are unable to figure out the ip, you can use a usb-to-i2c converter and ssd-1306-i2c-display. Upon linux boot, a5-v11 prints its ip-addr on this i2c display.

Step-1: Download a5v11-xmpp-factory.uimg to your pc's hard-disk.

Step-2: Connect a cat-5 or cat-6 cable between a5v11 and your PC.

Step-3: Power ON a5-v11 with 5VDC and wait for ~1minute till your pc gets the ip from a5-v11(your pc shall get an ip in the range of

Step-4: Enter "" in your browser's URL and you shall see the following page.
If you dont see this page - stop here!!!!, your a5-v11 might have a different firmware. I have a separate blog explaining the step-by-step guide for qualcomm-based a5v-11 firmware.

Step-5: Click on language dropdown list to switch to english
Step-6: Click on Admin->Firmware-Upgrade->Choose File xmpp-a5v11-factory.uimg(which was downloaded in step-1).  And then click "upgrade" button.

Step-7: You will have to confirm by clicking "OK"
Step-8: A second confirmation is needed, click "OK"
Step-9: Firmware upgrade starts, you will see the following pages

Step-10: Wait for about 40seconds, remove the power to a5-v11, also remove cat-5/cat-6 cable which was connected between a5-v11 and PC.

 Step-11: Using cat-5/cat-6 network cable, connect a5-v11 to your home-router and ensure that your PC is also connected on your home-router's network.

Step-12: Power ON a5-v11 and wait till the red-LEDon a5-v11 stops blinking.

Step-13: Using ssh or putty.exe, login to a5-v11's shell terminal using hostname "my-a5v11" - login username/password is root/brb0x. For any reason, if you are unable to figure out the ip of a5-v11, as discussed earlier, you can connect an i2c display on usb port of this router,  and reboot to see its ip as shown here in this picture.

Step-14: after login to ssh terminal of a5-v11, change the default password to your own using "passwd" command

Step-15: Set the xmpp(jabber) server's login credentials with this command(replace example user and password with your own) - echo "user:" >/etc/xmproxy/xmpp-login.txt; echo "pw: chatbotpw">>/etc/xmproxy/xmpp-login.txt ; reboot

Step-16: Reboot a5-v11, you shall see that your device coming online on your smart phone's xabber chat client.

Next Steps: Follow my other blog to see how you can use a5v11-xmpp-chat-bot as an IoT gateway for home automation.

Saturday, January 06, 2018

Low cost - Uninterruptible power supply for broadband wifi router

There are places and situations where people are struggling with frequent power outages causing interruption in the internet access, this leads to dropped calls, dropped download sessions etc.

Typically, these power outages are of short duration as backup generator takes over the load within few minutes. A short disconnection of power causes reboot of broadband wifi routers and re-connection of internet for all the devices on the network.

To tackle this problem of short power outage, i found this interesting cable on aliexpress sold for 1Euro including shipping.
This 5v to 12v booster cable along with battery bank makes it a perfect low cost uninterruptible power supply for your broadband wifi router. Please note:  power output capacity of this bootster cable is in the range of 8Watts which should be enough for a standard broadband wifi router. (if you happen to have a router with higher than 8watt consumption, then look for highter capacity booster cable).

The black plastic housing towards the usb end contains a booster pcb(MT3068 converter) as shown here

As shown in the following picture, you can connect 5v Dc adapter to the battery bank's charging port and 5v output of batterybank powers the broadband wifi router through this booster cable.

Backup time of such a solution depends on capacity of the battery bank. In your situation if the power outage is of short duration, then you can manage with lower capacity battery bank, else you can go for a higher capacity one.

Sunday, December 31, 2017

Remote Accessing your home network using smart phone and 5€ Linux H/W

In my quest for finding a cheapest off-the-shelf linux h/w, i came across this 5Euro device capable of running open-source linux OS(openwrt). I was delighted to see this tiny little device(A5-V11-router) consuming just around 0.3Watts - this is a perfect piece of h/w for IoT application.

Here are some interesting facts about this little device(A5-V11-router).
1)100Mbit Ethernet port
2)IEEE802.1b/g/n wifi interface
2)An usb port for attaching peripheral device
4)360Mhz Mips CPU(Ralink RT5350)
5)32Mb SDRAM
6)4Mb flash.
7)red and blue LED for showing different status.

It was intended to be used as an internet-hotspot  in combination with usb-3g-dongle and simcard. But, for my use, i have overwritten OEM fmw with my own customized openwrt(a5v11-xmpp) image which can be downloaded from here.

Q: what is the purpose of customized a5v11-xmpp image?
Ans: in this image, I have stripped down most of the router/wifi functionality to accomodate an xmpp-chat-client-daemon(lib-gloox based) to act as a chat-bot. Xmpp makes it easy to access your home network without tweaking your home router settings(e.g:port-opening/NAT etc). You dont need to expose any ports of your home-broadband-router to the internet.

Overwrite OEM firmware of a5-v11-router with a5v11-xmpp image and keep this little piece connected to your home internet so that you can "chat" with your h/w even when you are outside of home network.

Setup Instructions:

Step-1: Xmpp master/slave account creation.
As explained here, you need to prepare two xmpp/jabber accounts on your favorite jabber-server. and let these two accounts be "friends" so that xmpp-server can pass the messages between these two accounts.

Step-2: connect CAT-5/6 cable between your pc and a5-v11-router and power ON the router.

Step -3: Overwrite Stock Firmware with openwrt based a5v11-xmpp-image
On openwrt wiki there are instructions about how to overwrite stock firmware with openwrt-firmware. For overwriting the stock firmware with a5v11-xmpp-image, you can follow a step-by-step guide as detailed here in my other blog.

Step-5: Connect a5-v11-router to your home router's network(internet).

Step-6: login  to host "my-a5v11" using ssh/putty (user: root | pw: brb0x)

Step-7: prepare login details (This step is redundant as it is already explained in step-3)
echo "user:" >/etc/xmproxy/xmpp-login.txt; echo "pw: chatbotpw">>/etc/xmproxy/xmpp-login.txt ; reboot
Note: in above command, replace test-slave-account details with your own user/pw. (also use passwd command to change the default root password(brb0x) to your own)

Step-8: prepare your smart phone with xabber app and login using  test-master-account username and password.

Step-9: you will see that your a5-v11-router is online on your smart-phone's xabber app.

Step-10: type "help" and send the message to a5-v11-router, you will get the reply with list of available commands.

Q: Ok, what now? i have this tiny little device sitting next to my home router consuming 0.3w, what next? what can i do with it? can you give some examples?

Ans: here are some commands that makes this device useful.
1)From your smart-phone(no matter where u r in the word, as long as your phone is on internet) send "publicip" chat message and you get the response from chat-bot showing public-ip of your home router, this helps you to be independent of dynamic-dns setup, you can always see public-ip using xabber app.

2)connect i2c-tiny-usb (as shown in the picture below) to generate i2c from USB port of a5-v11-router, with i2c you can control/read many pheripheral chips ex: temp/humidity sensor, power sensor, gpio relay-control(use your imagination for home automation with i2c).
send  "shellcmd i2cdetect -r -y 0" and wait for "return=Success" message, and then read the output of last triggered shell command using "shellcmdresp" to see what i2c devices were detected.
ex: use "shellcmd i2cset -f -y 1 0x3c 0x00 0xff b" to make all pins of PCF8574 high.

3)Control sonoff wifi relays having tasmota firmware. ex: In your home network, if there is a sonoff relay at ip, send the following commands to chat-bot for controlling the relay(chat-bot uses http GET commands to control sonoff relays).
                a)"sonoff on"        - switches ON the relay.
                b)"sonoff off"       - switches OFF the relay.
                c)"sonoff toggle"  - toggles the relay.
                d)"sonoff"             - reads the current state of relay.
Note: Incase if  sonoff relay has a hostname, replace the ip with hostname in the example above.

4)on your network if there are linux pc's, you can trigger remote commands via sshpass of the chat-bot. Here is an example..
                a)"shellcmd sshpass -praspberry ssh -y pi@raspi-ip  sudo reboot" would reboot raspi.
                b)"shellcmd sshpass -praspberry ssh -y pi@raspi-ip  sudo poweroff" would switch off raspi.
                c)shellcmd sshpass -praspberry ssh -y pi@raspi-ip  omxplay /tmp/video.mp4" would start playing mp4 video.

5)send etherwake command to turn on a PC which was already in WoL mode, ex:
                 a)"shellcmd etherwake 00:00:DE:AD:BE:EF" (replace the MAC id with actual one)

watch-out this blog for for more use cases...

Q: i dont know what are u cooking in this image, i need to know the details, where are the sources? how can i create the image myself?
Ans: Here is the link to sources and build instruction

Friday, September 16, 2016

RBox - Raspberry pi Zero + USB 3G internet + XMPP = awesome IoT device

RBox - its a lightweight linux distribution built with buildroot. Current update binary size of RBox is around 25MB.

Here is my compact IoT setup based on Pi Zero + 3G usb Modem +prepaid SIM card+ 5200mAH battery pack.

What does this setup do?
SDCard in this Pi-Zero boots RBox linux image, during linux bootup following activities happen.

  1. usb_modeswitch switches the usb 3G modem to act as modem instead of default memory stick behaviour.
  2. umts-keeper along with sakis3g script would establish internet connection and keeps the connection alive.
  3. gloox, c++ library based daemon starts and logs in to a jabber server using preset xmpp login username and password. this xmpp daemon acts as a chat-bot.
  4. using android xabber app, communicate and control your pi-zero-IoT setup using chat messages.

What is so special about this setup?

  1. very low cost setup < 50Euro(PI_zero + sdcard + 3g_stick+sim_card+battery_pack)
  2. linux image is mounted as read-only, power cut doesnt harm the sdcard data.
  3. OTA linux image upgrade - full linux image upgrade over-the-air can be triggered by sending xmpp chat message.
  4. No need to have special cloud based service or opening-up-the port on internet with special dyndns registration. you just need two xmpp(jabber) or gmail accounts. one for pi-zero, second one for your mobile phone.
  5. possible to dial ussd codes via xmpp chat message for knowing the balance amount of prepaid sim card.
  6. possible to send/receive sms from your 3g-stick using xmpp chat messages.
  7. acts as a sms-gateway with xmpp based remote access for sms sending and receiving.
  8. possible to control pi-zero gpio's by sending get/set-gpio chat messages.
  9. possible to execute shell commands on pi-zero by sending  chat messages.

What are the typical application's of this setup?
  1. sms gateway - deploy this setup in your home country having local prepaid sim card, access your sms from anywhere in the world by sending jabber chat messages.
  2. weather station - deploy this setup within the 3g coverage area of your sim provider, along with solar panel and weather sensors, monitor the parameters by sending xmpp/jabber chat messages.
  3. fleet management - place this setup in your taxi/truck along with gps sensor, keep track of all your vehicles from a centralized control PC.
  4. wildlife monitoring - place this setup(along with camera) in 3G/Gsm covered forest area, monitor the activity of animals and take a picture and upload it on a cloud server.
  5. Remote asset management(e.g digital signage) - deploy this setup along with digital-signage display, control the content of your signage display by sending xmpp chat messages.
  6. Anti theft device - for cars, boats, mobile homes and caravans. let this device send you gps co-ordinates as chat messages when the pi-zero starts moving.
  7. Home automation - keep track of your holiday home, monitor temperature/humidity and control the relay's via gpio for switching on/off various devices.
  8. many more..

1) how much 3g data is consumed with this setup?
Ans: if there is no chat-message traffic to this setup, there is just xmpp-heart-beat packet sent to the jabber server(in my case typical usage per day is around few hundred kilo bytes).

2)how long does the battery backup last?
Ans: i have 5200mAH battery, fully charged battery kept my setup alive for about 8 to 10hours(you can have higher capacity battery for increased backup time).

3)Can i deploy this setup in a off the grid remote location?
Ans: ensure that you have higher capacity battery along with large solar panel(especially during winter with very low sun light  condition, you might need extra juice to keep setup running). Also Ensure that your setup is in 3G coverage area.

4)Is this project open source? where can i find the sources? how can i build the binary image myself?
Ans: yes, source code is hosted at this github location. Instructions for creating the binary image is given in the github location.

5)If i do, Over-The-Air linux image update, will this consume lot of my 3g-Data? 
Ans: yes, compared to normal xmpp traffic, linux image Over-The-Air update consume about 25MB data per upgrade, but then, how frequently would you be upgrading linux image? if you keep on adding new features, you might want to do frequent upgrades.

6)What happens if battery or power to pi-zero goes off during Over-The-Air linux image upgrade? will this brick my remotely deployed pi zero?
Ans:Rbox upgrade mechanism reduced the chances of bricking. During the upgrade process, non-active partition is programmed with new image. After the image is written to non-active partition, boot flag is flipped and pi reboots with updated partition.

7)I see that you have stacked up pi-zero/3g-stick/battery, why did u do that? wouldn't this be simpler to use pi-zero-included mini-usb-to-usb-converter?
Ans: yes, initially i did this, this is how my old and new setup looks, i like the compact setup instead of dangling wires, moreover rectangular brick shaped setup is easy for designing a 3D printed case.

8) I see that you are distributing ready to use bootable-binary-disk-image, but what if i want to build the image by myself from sources?
Ans: Here are details for generating binary from sources.

9) what about two or more xmpp accounts controlling this pi-zero-IoT setup? will this work?
Ans: Yes, Raspi0 acts as an xmpp-chat-bot, like typical xmmp-chat-scenario between humans, raspi can have multiple "friends" authorized to send/receive messages. e.g: you and your wife having separate xmpp/jabber accounts can trigger house-door-opening after kids arrival from school. Similarly, raspi can send asynchronous chat message to multiple subscribers on an I/O event(e.g when kids ring the door bell).

10)How much does this setup cost? what are the running costs?
Ans: Total cost including all components and prepaid simcard shall not be more than 50Euros.   Having a prepaid sim with around 100MB/30days data plan would be sufficient to keep the setup alive with basic I/O control and command traffic. Here in Germany, congstar charges 2Euros for a data plan of 100MB/30days. You need to decide for yourself based on your data-usage and provider's data plan.

11) I want to re-create the exact setup of your's, what parts do i need and what are the steps for creating sd card?
Ans: Items needed

  1. Raspberry pi zero v1.3
  2. Huawei E173 usb-3g-modem
  3. 8gb sdcard
  4. battery-pack
  5. four pogo pins
  6. general purpose pcb
  7. pcb mountable usb male and female connectors
SDCard Image creation:

  1. First and foremost is to have two xmpp login accounts on publicly available jabber servers(So far RBox has been tested with and Follow my previous Post on how to create master and slave xmpp accounts(ensure that master/slave accounts have authorized each other to send/receive messages).
  2. Download the initial SDCard Image from here.
  3. Unzip the downloaded xz image using 7zip or your favorite decompression tool.
  4. Write the unzipped binary image to Sdcard using Win32DiskImager
  5. After writing the image to SDCard, remove and re-insert the card on your PC. Your PC will detect a new drive as "boot" where raspi's kernel and boot images are listed.
  6. In this "boot" drive, create a new file called xmpp-login.txt and copy this text. After copy, edit this file and replace sample user and password with actual username and password of your slave-device which you created in Step-1(please note that your providers APN details may differ).
  7. Remove the SDCard from PC, and insert it into pi zero, wait for about 2-3 minutes for 3G Modem's LED to glow solid blue.
  8. On your android phone, open xabber app and login using master's username and password which you created in Step-1.
  9. If everything goes well, raspi-zero will show up as online on your xabber client.
  10. Send a "Help" to your raspi-zero, it will reply with list of available commands.
  11. Send "Sysupdate;reboot" and wait for raspi to update to latest RBox image, after update, it will reboot and be back online again with latest RBox version. Here is the detailed information on all available commands.
  12. for gracious shutdown, send "poweroff" chat message and wait for 3g Modem's blue led to go off.

Monday, January 04, 2016

RBox - Remotely Deployed GSM SIM Management

As I discussed earlier in my previous post, one of the use cases of RBox is for management of remotely deployed GSM-sim card for sending/receiving sms. You might wonder why would someone deploy their Sim card in a remote location rather than inserting it in a phone and keeping it close to your body. Well, there are many reasons why remotely deployed sim card would make sense, so far I could think of following reasons(do let me know in comments section if there are other use cases).

  1. When on international travel, your sim-card may not have roaming feature enabled.
  2. You might be located outside gsm coverage area, but you are in need of sms send/receive functionality.
  3. You may have multiple sim cards, but want to access them from centralized location.
  4. You might want to setup your own sms-gateway for sending/receiving sms over internet.
  5. You might have remotely deployed sensors sending the data via sms, and you want to collect all sms data from a central gateway.

There might be many other reasons why someone would deploy their SIM card remotely, but whatever you decide to do with this kind of setup, ensure that your SIM card is deployed at a safe and secure location to avoid possible misuse.

In simple words, this solution allows you to keep your SIM card connected to the internet and you can access it from your xmpp-client(or google-hangout) using your handheld mobile device.

You need following items to prepare this setup:

  1. SDCard image(download it from here, RBox is an open source project hosted on github
  2. Raspberry pi B or B+
  3. Micro or full size SDCard
  4. Huawei E173 USB 3G stick
  5. Sim card(ensure that SIM is not locked with pin)
  6. 5V/2A power supply.
  7. Two google(or jabber) accounts(one for you, second for your raspi)

Solution shown above, uses Huawei e173 USB 3G Modem connected to your Raspberry pi B+ and further on Raspi is connected to internet via home router. Here you can find the details about preparing the SDCard. If everything goes well, you shall see raspi being online on your android xabber app(or on your google-hangout). Send "Help" chat message and the raspi will reply will list of supported commands.

You will notice that when RBox is booted without USB-3G-stick, sms/sim related commands from the "Help" list will be missing because of missing USB-3G-stick hardware.

Before switching on the setup, ensure that password-unlocked SIM card is inserted into USB-3G-stick and a 2Amp capable 5V power supply shall be used.

Note: Ensure that both the google(or jabber) accounts are known to each other by authorizing the contact, you have to do this separately on your PC before switching ON the raspi.

How to read SMS?

  1. Send "Smsupdate"
  2. Immediate reply "InProgress : taskID=1" will be received
  3. nothing happens for 3-to-5seconds(dont panic, just wait)
  4. If everything goes well, reply "Success : taskID=1" will be received, else "Fail : taskID=1".
  5. Send "Smstotal"
  6. reply will look like: "Success : 2" this means total 2 messages are read from sim to cache
  7. send "Smsget 0" to read first message, or "Smsget 1" to read second message.

How to delete all SMS?

  1. Send "Smsdeleteall"
  2. Immediate reply "InProgress : taskID=2" will be received
  3. nothing happens for 3-to-5seconds(dont panic, just wait)
  4. If everything goes well, reply "Success : taskID=2" will be received, else "Fail : taskID=2"
  5. To know if all messages are delete, follow the steps above in section "How to read SMS"
  6. If you notice there are still undeleted messages, Repeat the step 1 to 4(hopefully in future version of RBox this problem will be fixed).

How to send SMS?

  1. Send "Smssend 009198xxxxxxxx hello this is a test message"
  2. Immediate reply "InProgress : taskID=3" will be received
  3. nothing happens for 3-to-5seconds(dont panic, just wait)
  4. If everything goes well, reply "Success : taskID=3" will be received, else "Fail : taskID=3"
  5. If you notice sms was not sent, just skip "0091" international+country code and try again.

How to do voice dialling?

  1. Send "Dialvoice 009198xxxxxxxx"
  2. Immediate reply "InProgress : taskID=4" will be received
  3. nothing happens for 20 to 25seconds(dont panic, just wait, dialling might be in progress)
  4. Call ringing will happen for 20 to 25seconds, if caller picks the call, then he will hear nothing
  5. After 25seconds call will be disconnected automatically and reply "Success : taskID=4" will be received.
  6. As of now, there is no specific use of this blank call, but future RBox release would include auto playback of recorded message to notify certain events for automation use case.

How to Dial USSD codes like *101#(useful for knowing balance amount of prepaid SIM)?

  1. Send "Dialussd *101#" (this number may vary depending on the provider)
  2. Immediate reply "InProgress : taskID=5" will be received
  3. nothing happens for 3-to-5seconds(dont panic, just wait)
  4. If everything goes well, reply "Success : taskID=5" will be received, else "Fail : taskID=5"
  5. Send "Readussd" to know the return text of last ussd-dial.

RBox - Home Automation

As I discussed earlier in my previous post, one of use cases of RBox is for home-automation to controll the devices of your home network from any remote location. As long as your phone is connected to internet via 3G-Data or wifi, you can send/receive commands to Home-RBox  from xmpp client of your handheld device.

The advantages of this setup is, you dont need tweaking of any settings of home router, you can secure your home network by blocking all the incoming ports of the firewall, and there is no need to depend on any paid cloud services, you can find tons of free Xmpp/Jabber servers hosted on the public internet, this way you can achieve secure and easy connection between two devices on the internet.

As shown above, Raspi h/w at home is running RBox linux distribution, and your handheld device on the public internet is running xabber android app. Both raspi and android-xabber have logged in to with their own username and password, and these two devices have already authorized each other(as per the instructions given here) to send/receive chat messages .

In order to control the GPIO2 pin of the raspberry, send following command on your android-xabber, "Gpio 2 0" or "Gpio 2 1" for toggling the GPIO2 of Raspberry Pi.

For details on the preparation of this setup, refer to my previous blog where all the details of RBox installation on Raspberry Pi is given.