A site to help me remember how I got things to work the way they do…

Menu

So after looking around the local Anaconda on the weekend I came across a Bluetooth battery monitor the BM Pro which I thought was a great idea. Then I glanced at the price tag… $250.99 AUD and thought there’s no way I couldn’t make a similar system myself. I’ve already got a microcontroller to play with (Arduino Uno) that I haven’t used in anything. Surely I’d just need to measure the voltage – Process is somehow and send it out over Bluetooth to a connected device. Then the part I don’t know how to do – Program an android app to display that info.

From that simple seed of inspiration, I had another eureka moment. I’ve already got the van router so why would I both using Bluetooth. Let’s send information around the van from various sensors via wifi and have the router do the processing. Then all I’d need is a nice little web page that I could go to on any connected device with a web browser.

From there as with all my projects scope started becoming a problem as my brain kicks into gear and thinks “So we’ll check the voltage this way… ohh squirrel … I can do this, and I can also do that”. So let define what we’re playing with its capabilities and then what the actual monitor can do for us.

So from that, we can see 14 digital pins and 6 analogue pins. Essentially if we think of the digital pins as a way to turn circuits on and off (1 or 0) and the analogue pins as a way to read in values such as battery voltage we can monitor 6 devices which in my case (at this stage of thinking) was enough.

The caravan monitor would need to be able to do the following:

Run from 12volts

Measure the battery voltage

Measure the power use (In amps) at any given time

Give an approximate state of charge level (percentage charged) of the battery bank

Measure two water tank levels

Measure gas bottle levels

Communicate with the van router over wifi

Now that we’ve defined what it will do. How will we achieve this… simple really.

Figuring I’d start with the most basic thing on the list of wants – BATTERY VOLTAGE.

The Arduino uses an analogue to digital converter with a max input of 5V so we need a way to translate the possible max of say 15V down to 5V. We do that with the aid of a resistive voltage divider. Not going into the details i settled on some resistor values based on what i had laying around and got to work on the code.

Forgive me if the code no longer works I had to do it from memory as the project has long since progressed to something bigger.

So that gets us the voltage and prints it out to the computer via USB/serial – Now we need to send it to the router/network. After a few ideas of using a web page etc I stumbled on an article relating to MQTT from hiveMQ.

Essentially MQTT allows for very simple comms over a network and there already exists iPhone and Android apps to work with it…. YIPEE I still don’t have to learn to program for phones.

Now we have two problems – getting the Arduino onto wifi and sending MQTT data

I’d seen a lot of press about a small cheap wifi chip named the esp8266 for less than $10 you could add wifi to your projects hell you could do away with the Arduino all together and just use the esp if you wanted (I needed more than 1 analogue pin). I jumped on eBay and ordered 3 of them. Only need one for the project but come on – for $6 i can wifi enable anything i like. lol

Chip arrives – I wire it up, it springs to life – Using a USB to serial converter i can send it AT type commands and make it connect to the van router and ping things etc. OK. now how to get the Arduino to talk to my wifi network using this thing.

OK now how to get it work with the Arduino – Turns out really easily. There’s a library wifiESP that takes care of the legwork for us. We make a few config lines in our Arduino sketch to tell it what pins the esp is on and it takes care of the rest. One problem down one to go.

MQTT is just as simple – There’s a library called PubSubClient which we can utilise to publish and subscribe to topics.

So I needed a way on bootup to see if I was connected to the home wifi or not.

Figured when i’m away from home I wont be able to “see” the home network so why not just test to see if its in range on bootup, and if its not assume we’re out enjoying the great outdoors and disable the home network. This means I can have the router enable the Access Point rather than work as a bridge when i’m home

So I’ve been wrestling with a way to have the van router automagically connect to the home wifi network when ever its turned in at home.

Obviously I could simply setup the router as both a client to connect to the home network and access point to enable devices to connect to the van router. This works fine whilst I’m at home. The minute the router cant find the home network (For example when traveling) and it simply kills wifi from ever starting up meaning devices cant connect to router.

After searching the web, I’ve managed to find a very clever solution that i dont understand why I didnt think of myself. So elegant and yet so simple. Now I didn’t come up with the script. I simply plagerised it from here

On startup… Have the router try and download the google.com page, if of course this works it means we’re at home and the home network is obviously avaiable. Therefor leave it enabled.

If we try the download and it fails – Obviously the home network is not available and as such it should be disabled. Its this disabling of the home network settings that then enables the router to continue start up wifi.

now we change the file permissions.. being i’m lazy i did it this way as the router doesn’t need to be super secure. I mean come on I use it for camping.. lol

chmod 777 /usr/bin/wifi-check.sh

next we edit rc.local to enable the script to run on bootup

vi /etc/rc.local

add the following line before the exit 0 statement

wifi-check.sh &

Save the file and reboot.

You should have access to your home network. Now turn off the home wifi router and reboot your van router. You should find that after an extra 15 seconds than normal, you will have wifi available.

This is super awesome as it means we now have way to automate backup of home files to the van router when it becomes available at home. On successful uplink test we could theoretically then start an rbackup of say a movie folder. The skys the limit.

Actually I’ve just had a thought. If we start the connection at home, and then drive away do things mess up. Rather than rebooting – What if the wps button could toggle the home wifi network on and off, that might be even easier…

So, now that we’ve purchased our first caravan and progressed from caping to glamping with all the mod cons (Well the ones I can afford) it was decided between the brains trust at work, that being able to take away the entire movie library so the kids could watch anything they liked as we drove along, or if heaven forbid bad weather hits us, it will be just like being at home.

So what do we need this magical box to do

1. Provide wifi access whilst travailing in the vehicle as well as when camped

2. Provide windows file sharing of media to allow devices running xvmc or other software to access the media

3. Possibly run some sort of DLNA type server to stream media to devices that can’t access SMB shares directly

4. Possibly provide a bridge between my phones next-g connection and wifi only devices such as iPads/iPods (Not implemented yet as not required)

The size of the ext4 partitions was chosen as this allowed ample room for messing around with openwrt, whilst when considering scale, a few gb out of a tb doesn’t really lend itself to being noticed.

NTFS was chosen as the other fs type as this will mean when the HDD is plugged into a windows machine to copy moves etc to it, the ext4 parts by default will be invisible and only the ntfs partition will be visible (Protection by obfuscation)

How you partition your drive is up to you, I used an existing linux box, however gparted can be downloaded and used to do it from a usb key. Windows on its own doesn’t work as you can’t format ext4 partitions. Once you get comfortable with it, you can actually do the partitioning with openwrt, this will not be covered in this guide.

The Main Event

Connect your computer to the 703N via ethernet as I don’t trust using wifi to do a firmware upgrade

The 703N will more than likely come pre-installed with Chinese language firmware. So the first hurdle is trying to work out how to flash OpenWRT to the router in the first place.

Click the button labeled 1. and select the openwrt firmware you downloaded. Then press the button i’ve labeled 2.

After a few minutes the file will be uploaded in the router, and it will begin to reboot.

On reboot you may need to statically set the ip address of your network card. Configure them to match the settings I have listed here and your should be able to ping the router once its blue light stops flashing indicating it has booted up

Now we need to setup a password to allow us to ssh into the router.

Open your browser and connect to the router at its default address (usually 192.168.1.1). Login using username root with an empty password.

Make sure your browser accepts (session) cookies. LuCI login fails if password is set and session cookies cannot be accessed.

Then click on the left in the top bar on Administration, then go to System in the bar underneath. A page to change the password is displayed.

Write your desired password into the field Password and repeat it in the field Confirmation. Finally click on Save & Apply.

Your password is set now.

Next we ssh into the router (on mac enter ssh root@192.168.1.1 into a terminal windows)(on windows again use Putty select SSH from the connection type and enter root@192.168.1.1 as the host) you will be asked to enter the password, type it in and press enter.

Now we’ve set the admin password and logged in via ssh its time to enable wifi. (Plagirised from the openwrt wiki)

# Now to copy the required files to the usb hdd that will become our overlay

mkdir-p /mnt/sda1

mount/dev/sda1/mnt/sda1

mkdir-p /tmp/cproot

mount--bind / /tmp/cproot

tar-C /tmp/cproot-cvf - . | tar-C /mnt/sda1-xf -

umount/tmp/cproot

umount/mnt/sda1

Now we need to tell openwrt using stab how and where to mount the partition

vi /etc/config/fstab

make the following changes

#old

option target /home

option enabled 0

#new

option target /

option enabled 1

press the esc key and then press : and enter wq

now enter the following:

reboot

after rebooting the machine ssh back into the box and enter the following

df

you should see rootfs size now matches that of the partition we created

Thats it done. Our router is now using the attached USB HDD as its main storage. If we boot the router without the HDD attached it will use its own internal flash as storage. This essentially means we can have a failsafe configuration if we screw something up on the USB HDD, we remove the drive, reboot the router and BOOM we’re back in.

Now you can go ahead and install what ever you like to the drive. For us thats samba, and in an upcoming project dlna for those that don’t utilise XBMC

Configure the samba mount points as required, sit back, grab a cold one and be done iwth it.

For the next project I’m looking at recompiling OpenWrt with the required packages already built in. I’ve had success building in most if not all the required apps just need to bundle config files to make setup alot more rapid should something go wrong in the future.

############
# ddclient.conf
# namecheap
############
## update time in seconds, i believe
daemon=300
## you can open this file with any text editor to see what is being sent
cache=/tmp/ddclient.cache
pid=/var/run/ddclient.pid
## This line will get your public IP address if you're system is not directly connected
## to the internet, such as behind a firewall/router
use=web, web=http://whatismyipaddress.com/, web-skip='Your IP address is'
protocol=namecheap
server=dynamicdns.park-your-domain.com
## Your Domain name
login=example.com
password='#namecheap provides this - leave quotes'
ssl=yes
## this was the tricky part for me to figure out
## I was embarrassed with myself when I learned how easy it was
## separate each sub-domain you want to update with the IP address with a comma then space
@, forum, img, ssl, tracker, webmail, www

Thats about it.. just add ddclient to startup with your pc

sudo update-rc.d ddclient defaults
sudo update-rc.d ddclient enable

Thats all there is to it. To check if the updates have occured we can use

sudo /tmp/ddclient.cache

to see if there's been any updates and if they were successful. Hope that helps 🙂