Showing posts with label chat-bot. Show all posts
Showing posts with label chat-bot. Show all posts

Thursday, January 11, 2018

Access Your Raspberry Pi Remotely via XMPP Chat

How to remote-access a Raspberry Pi over the internet using an XMPP-based chat-bot — no port forwarding or dynamic DNS required. The chat-bot is written in C++ using the gloox XMPP client library, and communicates through a public Jabber server.

Setup diagram — Raspberry Pi chat-bot communicating with smartphone via XMPP

Typical setup — Raspberry Pi behind a firewall communicates with a smartphone via a public XMPP/Jabber server.

Building the chat-bot on Raspberry Pi

Tested on Raspbian Stretch Lite (2017-11-29). These steps work on any Debian-based Linux distro.

Step 1 — Log in to the Raspberry Pi shell (keyboard or SSH) and install dependencies:

sudo apt-get update
sudo apt-get install cmake git libjson-c-dev libgloox-dev openssl

Step 2 — Clone and build:

mkdir /home/pi/xmproxy
cd /home/pi/xmproxy
git clone https://github.com/hackboxguy/brbox
cd /home/pi/xmproxy/brbox/sources
cmake -H. -BOutput -DCMAKE_INSTALL_PREFIX=/home/pi/xmproxy/buildir -DAUTO_SVN_VERSION=OFF
cmake --build Output -- install    # add -j5 on quad-core Pi for faster build

Step 3 — Configure XMPP credentials and start the chat-bot:

cd /home/pi/xmproxy/buildir
echo "user: my-raspi-username@gmail.com" > xmpp-login.txt
echo "pw: super-secret-pw" >> xmpp-login.txt
export LD_LIBRARY_PATH=/home/pi/xmproxy/buildir/lib
/home/pi/xmproxy/buildir/bin/xmproxysrv --loginfile=/home/pi/xmproxy/buildir/xmpp-login.txt

Testing

Check on your Android phone's Hangouts (or any XMPP client) if my-raspi-username is online. Send help and the chat-bot will reply with the list of available commands.

Notes

For Google accounts, ensure Allow less secure apps is enabled on the Raspberry Pi's account.

The two XMPP accounts don't need to be on Google — any two Jabber accounts will work. See this guide for preparing Jabber accounts.

These steps work on any Debian-based Linux distro, not just Raspbian.

SOURCE CODE

github.com/hackboxguy/brbox — XMPP chat-bot sources and build instructions

Sunday, December 31, 2017

Remote Home Access with a 5-Euro Pocket Router

How to turn a ~5€ A5-V11 pocket router into an XMPP chat-bot that lets you remotely access and control your home network from a smartphone — no port forwarding or dynamic DNS required.

Why the A5-V11?

In the quest for the cheapest off-the-shelf Linux hardware, the A5-V11 pocket router stands out — it consumes just ~0.3W, making it ideal for always-on IoT applications.

CPU360 MHz MIPS (Ralink RT5350)
RAM32 MB SDRAM
Flash4 MB
Ethernet100 Mbit
Wi-FiIEEE 802.11b/g/n
USB1 port for peripherals
LEDsRed and blue status indicators
A5-V11 pocket router overview

The A5-V11 pocket router — a ~5€ Linux device consuming just 0.3W.

How it works

The custom a5v11-xmpp firmware strips down most router/Wi-Fi functionality to fit an XMPP chat client daemon (based on lib-gloox) that acts as a chat-bot. XMPP makes it easy to access your home network without tweaking your home router settings — no port forwarding or NAT configuration needed.

Keep this device connected to your home internet and "chat" with it from anywhere using the Xabber app on your smartphone.

Setup instructions

Step 1 — Create two XMPP/Jabber accounts (master and slave) on your favorite Jabber server as explained here. Add both accounts as friends so the XMPP server can pass messages between them.

Step 2 — Connect a CAT-5/6 cable between your PC and the A5-V11 router, then power it on.

Step 3 — Flash the stock firmware with the a5v11-xmpp image. Follow the step-by-step guide in this blog post (also see the OpenWrt wiki for general instructions).

Step 4 — Connect the A5-V11 router to your home router's network (internet).

Step 5 — Log in via SSH to host my-a5v11 (user: root, default password: brb0x).

Step 6 — Configure the XMPP login details:

echo "user: test-slave-account@xabber.de" >/etc/xmproxy/xmpp-login.txt
echo "pw: chatbotpw" >>/etc/xmproxy/xmpp-login.txt
reboot

Important: Replace the test account details with your own credentials. Also use passwd to change the default root password.

Step 7 — Install Xabber on your smartphone and log in with the master account.

Step 8 — You should see the A5-V11 router appear online in Xabber. Send help to get a list of available commands.

Use cases

1. Get your public IP — Send publicip and the chat-bot responds with your home router's public IP. No dynamic DNS needed.

2. I2C peripheral control — Connect an i2c-tiny-usb adapter to the USB port for controlling sensors, GPIO expanders, and other I2C devices:

shellcmd i2cdetect -r -y 0       # scan for I2C devices
shellcmdresp                      # read the scan result
shellcmd i2cset -f -y 1 0x3c 0x00 0xff b   # set PCF8574 pins high
A5-V11 with i2c-tiny-usb adapter connected

A5-V11 with i2c-tiny-usb adapter for peripheral control.

3. Sonoff relay control — Control Sonoff Wi-Fi relays running Tasmota firmware on your local network:

sonoff 192.168.1.10 on       # switch ON
sonoff 192.168.1.10 off      # switch OFF
sonoff 192.168.1.10 toggle   # toggle
sonoff 192.168.1.10          # read current state

4. Remote SSH commands — Trigger commands on other Linux machines via sshpass:

shellcmd sshpass -praspberry ssh -y pi@raspi-ip sudo reboot
shellcmd sshpass -praspberry ssh -y pi@raspi-ip sudo poweroff
shellcmd sshpass -praspberry ssh -y pi@raspi-ip omxplayer /tmp/video.mp4

5. Wake-on-LAN — Power on a PC that supports WoL:

shellcmd etherwake 00:00:DE:AD:BE:EF

SOURCE CODE

github.com/hackboxguy/lede-a5v11 — build instructions and sources

a5v11-xmpp.uimg — pre-built firmware image