Monday, November 14, 2016

Mounting ARM Linux (OLinuXino/Raspberry Pi) SD card .img files on Desktop PC to access its rootfs

Linux distributions for embedded ARM board like Raspberry Pi or iMX233-OLinuXino-MAXI are available as prebuilt SD card images supplied as raw .img files.
For example, .img files for ArchLinux are available for Raspberry Pi and OLinuXino.
These files can be written to SD cards using the program dd on Linux or Win32 Disk Imager on Windows.

These files contain the image of the SD card as a whole including multiple partitions - bootloader/bootlets, rootfs, swap etc.

At some point of time we might find ourselves wanting to access the files from the rootfs contained within one of the partition of these image files. Accessing the kernel headers folder for cross-compiling kernel modules for these boards can be one such situation. And one does not necessarily need to write the image to an SD card and mount it back on ones desktop to achieve this.

Major Hayden has straightforward instructions here on how to mount .img files which contain multiple partitions.

So when I found myself wanting to extract the rootfs from ArchLinux distribution for OLinuXino (ArchLinuxARM-2014.02-olinuxino-rootfs.img) onto my Ubuntu 12.04 32 bit Desktop, here are the steps I followed:


Analyse the image file using the fdisk and file commands:
There are two partitions, the first one is for the bootloader and the second one is the rootfs. We want to mount the second one

The start sector for the second partition which houses the rootfs is 67584. We need to mount that. Since block size is 512, we need to mount starting from 67584*512 byte 34603008

Make a folder for mounting the rootfs. I named mine "rootfs"

And issue the command to mount:
sudo mount -o ro,loop,offset=34603008 ArchLinuxARM-2014.02-olinuxino-rootfs.img rootfs

The rootfs from the .img file is successfully mounted
You can even use nautilus to browse the mounted folder!!


Compiling Linux Kernel modules on OLinuXino (ArchLinux ARM)

So if you have an OLinuXino and have installed the development tools on the board and successfully compiled userspace programs on it (refer this blog post), and are feeling adventurous, you can try compiling kernel modules as well. (Make sure you have gcc setup – refer to the blog post linked to above)

So at the OLinuXino console (Serial or SSH) logged in as root:
  1. Install kernel headers:
    pacman -S linux-armv5-headers
  2. In your home directory, create the main.c source file:
    nano main.c

    and type the following and save the file:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    #include <linux/module.h> //Needed by all modules
    #include <linux/kernel.h> //Needed for KERN_INFO
    int init_module(void) {
     printk(KERN_INFO "Hello world\n");
    
     //A non 0 return means init_module failed; module can't be loaded.
     return 0;
    }
    
    void cleanup_module(void) {
     printk(KERN_INFO "Goodbye world\n");
    }
    
    MODULE_AUTHOR("Anurag Chugh");
    MODULE_DESCRIPTION("Hello World");
    MODULE_LICENSE("GPL v2");
    
  3. While still in your home directory, create the makefile:
    nano Makefile

    and type the following into it:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    obj-m += hello.o
    
    hello-objs := main.o
    
    all: uninstall-kernel-module
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
     -sudo insmod hello.ko
     sudo dmesg -c 
    
    clean: uninstall-kernel-module
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
     sudo dmesg -c
    
    #the '-' in front of the command tells make to continue with the next command incase an error is encountered 
    install-kernel-module:
     -sudo insmod hello.ko
     
    uninstall-kernel-module:
     -sudo rmmod hello
    
  4. Issue the command to clear the kernel message buffer:
    dmesg –c
  5. Build the kernel module, the make file will automatically load the module as well and the kernel messages will report its execution:
    make

image
Successfully compiled and loaded the hellow world kernel module


Sunday, November 13, 2016

Interface USB Mouse to your Arduino using CH375B

CH375B module. It is usually supplied with a 2 way jumper mounted on TXD and RXD pin.
The TXD RXD pins are located next to the female USB receptacle. Please unmount the jumper before use.
  1. Get the following components:
    1. Arduino Nano
    2.  Male to female jumper wires  - 4 pieces
    3. USB Mouse
    4. CH375B module
  2. Download MeUsb.cpp and MeUsb.h from https://github.com/xeecos/Me-USB-Host
  3. On your Windows computer which already has arduino installed, go to your documents folder. There you will find a folder called arduino and within that another folder called libraries. Inside this libraries folder create a new folder called "MeUsb"
  4. Place MeUsb.cpp and MeUsb.h in this folder
  5. In the arduino\libraries folder, create another folder called "SoftwareSerial_fix"
  6. And in this folder download and place SoftwareSerial_fix.cpp and SoftwareSerial_fix.h from the same link as above.
  7. Assemble the circuit on the bread board as follows:
    1. +5V of Arduino Nano to +5V of CH375B
    2. GND of Arduino Nano to GND of CH375B
    3. D10 of Arduino Nano to TXD of CH375B
    4. D9 of Arduino Nano to RXD of CH375B 
      Connecting the CH375B to Arduino Nano

      Connecting the CH375B to Arduino Nano
  8. Start arduino and open a new sketch. Copy the source code into it and download it into the arduino nano.
  9. Once the code is downloaded successfully, start serial monitor on your PC with baud rate set to 115200 and observe the pattern of 4 bytes that are being received.
    Every time you move the mouse, 4 bytes are sent to the Arduino Nano.
    You will observe that there are 4 bytes being printed on the serial monitor. Here are what those 4 bytes mean:
Deciphering the 4 bytes received from CH375B when a mouse is connected to it
You can CH375B module in India from www.vishaworld.com

To interface a USB Joystick, have a look at Me-USB-Host.ino available at https://github.com/xeecos/Me-USB-Host

Source Code to interface USB Mouse.


#include <Arduino.h>
#include "SoftwareSerial_fix.h"
#include "MeUsb.h"

MeUsb usb(10,9);

void setup() 
{
   Serial.begin(115200); 
   usb.init(USB1_0);
}

void loop()
{
  if(!usb.device_online)
  {
    usb.probeDevice(); 
    delay(100);
  }
  else
  {
    int len = usb.host_recv();
    if (len == 4){
      Serial.print(usb.RECV_BUFFER[0],DEC);
      Serial.print(',');
      Serial.print(usb.RECV_BUFFER[1],DEC);
      Serial.print(',');
      Serial.print(usb.RECV_BUFFER[2],DEC);
      Serial.print(',');
      Serial.print(usb.RECV_BUFFER[3],DEC);
      Serial.print('\n');
    }
  }
}

Thursday, November 10, 2016

Automatically rebooting Vstarcam HD C7838WIP Wireless IP Camera

I have 6 IoT devices in my house. Most of them are DIY and I have documented their build processes on this blog. One of them is a commercial product.


  1. Weather Station (OpenWrt) blogpost
  2. Energy Meter (OpenWrt) blogpost
  3. Ambient Sensors (Particle Photon) blogpost
  4. ADS-B Receiver (PiAware on RPi2) instructions on piaware
  5. DIY Security Camera (motionEye/RPi) blogpost
  6. Commercial Security Camera (Vstarcam HD C7838WIP)
Out of these, the ADS-B Receiver and the Vstarcam (both running linux) kept losing connection to the cloud regularly. I have a few theories behind why this might be happening:

  1. Both these devices lie in the fringe area (near door/windows) of my house far away from the home router which is placed in the center of the house. The WiFi signal from my neighbors might be causing interference and decrease in signal quality for these devices and the WiFi driver might be trying hard to maintain connection before giving up. I know this because I looked up the system logs of the fringe devices and video streaming on my laptop hiccups when I sit next to the window. Also this is what my balcony's spectrum looks like:

    Oh soo many WiFi's!!
  2. The WiFi chipset might be buggy
  3. The WiFi drivers might be buggy
  4. Some other application running on these devices causes the freeze up
  5. We experience power failure for very short duration a couple of times in the day. I have a UPS system for my house but the next higher networking device in the hierarchy (my ISPs) reboots everytime this happens - it does not have any backup and the diesel generator to which it is connected takes 10-20 seconds to come online. This might cause the devices to somehow lose connection to the cloud.
The Raspberry Pi2 used for ADS-B receiver is quite capable and I was able to quickly write a cron job that runs every 10 minutes to check if it has a sane connection to the internet and if it doesn't, it reboots the Pi. A blog post documenting it is here.

As for the Vstarcam HD C7838WIP, well its a whole different story. The Linux distro for RPi2 is an SD card image which has many command line utilities pre-installed which makes our life easier. The Vstarcam has its linux running from a flash chip inside it and so the manufacturer had to limit the number of software packages that make it up. But I was able to figure out how to get around those restrictions and have a script running in the background doing its job of detecting internet outage and rebooting the device. Here is how you go about it:


You might have heard of Mirai botnet and there vulnerability it uses to infect security cams like these. Console access to most of these security cams can be gained by simply telnetting into it (although from within the same LAN that they are on) and using the login as "root" and password as "123456" (Read more here). The password "123456" is hard coded into the read only file system and cannot be changed.

Using PuTTY to telnet (not SSH) into the VStarcam
As you can see, the rootfs is mounted in Read Only Mode and so root password cannot be changed.
This is not a big issue since the camera cannot be accessed from outside the LAN anyway.

The df and ls command show us the layout of the memory partitions and filesystem.

The internal flash memory is 32MB
And you can see that I have a 32GB SD card inserted for saving the video recordings.
The /system and /mnt/sda0 (SD card) are the only two read-write areas of this camera. So we can write and save a hand script to chec the "online-ness" of the camera in either of these location. I chose to save it in /system (which is inside the camera itself and not on the easily removed/lost/corrupted SD card)

So I cd over to /system and use the command "vi pingtest.sh" to save the following script.
(Press i to put vi in insert mode, then past the code into your PuTTY, then press Esc and then type :wq! to save and exit)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
while true; do
        echo "Waiting for 10 minutes"
        sleep 10m
        return_value=$(ping -c 2 -W 4 8.8.8.8)
        return_value=$?

        case $return_value in
                0)echo "Ping result: ONLINE"
                  ;;
                *)echo "Ping result: OFFLINE. Rebooting..."
                  reboot
                  ;;
        esac
done

Once the script is set we need to mark it as executable. Issue the command "chmod +x pingtest.sh".

And now we need to set the script to run in the background at boot up. For that we need to add it to the init script which the manufacturers have created for their on purposes. Issue the command "vi /system/init/ipcam.sh" and add the following line to the end of that script and save it: "/system/pingtest.sh &"

Editing the ipcam.sh


(Use i to insert and Esc and type ":wq!" to save and exit)

The ampersand makes sure that the script is begins running in the background.
After you have saved the script, reboot and check if its is in fact running using the ps command:

ps command shows that the pingtest.sh script is in fact running and so is the "sleep 10"
So what this script does is to check if it is able to ping the Google's Public DNS server. If yes then it goes to sleep for 10 minutes and repeats the process. If the ping fails, it issues the reboot command.

And this will ensure that the camera always stays connected to the internet - ofcourse there will be a downtime of a few seconds when the camera is rebooting and not recording, but that is something I can live with.

Saturday, November 5, 2016

Automatically rebooting piaware and prevent outage (avoid "not feeding ADS-B to FlightAware" emails)

A few months ago, I setup a Raspberry Pi 2 at my house in Pune to monitor the ADS-B signals from aircrafts passing overhead.
I referred the build process outlined here to build my own PiAware setup.



The problem is that roughly twice a week, I used to get this mail saying that my feeder was no longer feeding data to flighaware.

Barring a few rare instances when the trouble was due to internet outage, most of the time it required me to reboot the Raspberry Pi. Eventually I got tired, like a few other users here. Apparently it is due to WiFi dongle hanging or losing connection to the WiFi Router. To get around that, I wrote a script and set it up as scheduled task that runs every 5 minutes. The script checks if the RPi is able to ping Google's Public DNS servers, if yes, then it exits. If the ping fails, it waits for 10 seconds, tries again to ping again and if it fails this time, it issues a reboot command. In both cases, it loges results to a text file (saved on SD card) before exiting. I could then analyse how many times the RPi rebooted later on.

Here is the script (Saved as /home/pi/monitor_gateway.sh):



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Send two Ping request packets and 4 seconds timeout
Logfile=/home/pi/pingtest.log
 
return_value=$(ping -c 2 -W 4 8.8.8.8)
return_value=$?
# Return code for ping are taken from
# ./OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/include/sysexits.h
 
case $return_value in
	0)  #Ping reply received
		logger "Ping result: ONLINE"
		TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
		echo " Ping result: ONLINE" >> $Logfile
		;;
	1)  #Ping sent but reply was not received
		logger "Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds"
		TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
		echo " Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds" >> $Logfile
		sleep 10
		return_value2=$(ping -c 2 -W 4 8.8.8.8)
		return_value2=$?
		case $return_value2 in
			0)  #Ping reply received
				logger "Ping result: ONLINE"
				TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
				echo " Ping result: ONLINE" >> $Logfile
				;;
			1)  #Ping sent but reply was not received
				logger "Ping result: OFFLINE (Ping reply not received), Rebooting..."
				TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
				echo " Ping result: OFFLINE (Ping reply not received), Rebooting..." >> $Logfile
				reboot
				;;
			*)  #Error
				logger "Ping result: OFFLINE (ping return code: $return_value2), Rebooting..."
				TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
				echo " Ping result: OFFLINE (ping return code: $return_value2), Rebooting..." >> $Logfile
				reboot
				;;
		esac
		;;
	*)  #Error
		logger "Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds"
		TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
		echo " Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds" >> $Logfile
		sleep 10
		return_value2=$(ping -c 2 -W 4 8.8.8.8)
		return_value2=$?
		case $return_value2 in
			0)  #Ping reply received
				logger "Ping result: ONLINE"
				TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
				echo " Ping result: ONLINE" >> $Logfile
				;;
			1)  #Ping sent but reply was not received
				logger "Ping result: OFFLINE (Ping reply not received), Rebooting..."
				TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
				echo " Ping result: OFFLINE (Ping reply not received), Rebooting..." >> $Logfile
				reboot
				;;
			*)  #Error
				logger "Ping result: OFFLINE (ping return code: $return_value2), Rebooting..."
				TZ="Asia/Kolkata" date | xargs echo -n >> $Logfile
				echo " Ping result: OFFLINE (ping return code: $return_value2), Rebooting..." >> $Logfile
				reboot
				;;
		esac
		;;
esac


Here is how my crontab looks like:

You can edit your crontab by issuing the command (on the RPi): sudo crontab -e
I also set my RPi2 to reboot once everyday at 12:00 AM just in case the internet connection
stays alive but dump1090 itself crashes. We could write a script to detect that case as well, but this
preemptive strike seems easy. 


And just for reference, here is what my log file looks like after a day of using the script:
(The Raspberry Pi did reboot a couple of times.)



pi@rpi-lithiumhead-piaware ~ $ cat pingtest.log
Fri Nov 4 10:43:13 IST 2016 Ping result: ONLINE
Fri Nov 4 10:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 11:00:03 IST 2016 Ping result: ONLINE
Fri Nov 4 11:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 11:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 11:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 11:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 11:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 12:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 12:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 12:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 12:30:03 IST 2016 Ping result: ONLINE
Fri Nov 4 12:40:03 IST 2016 Ping result: ONLINE
Fri Nov 4 12:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 13:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 13:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 13:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 13:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 13:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 13:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 14:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 14:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 14:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 14:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 14:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 14:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 15:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 15:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 15:20:03 IST 2016 Ping result: ONLINE
Fri Nov 4 15:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 15:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 15:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 16:00:03 IST 2016 Ping result: ONLINE
Fri Nov 4 16:10:04 IST 2016 Ping result: ONLINE
Fri Nov 4 16:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 16:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 16:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 16:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 17:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 17:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 17:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 17:30:03 IST 2016 Ping result: ONLINE
Fri Nov 4 17:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 17:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 18:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 18:10:03 IST 2016 Ping result: ONLINE
Fri Nov 4 18:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 18:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 18:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 18:50:03 IST 2016 Ping result: ONLINE
Fri Nov 4 19:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 19:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 19:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 19:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 19:40:03 IST 2016 Ping result: ONLINE
Fri Nov 4 19:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 20:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 20:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 20:20:03 IST 2016 Ping result: ONLINE
Fri Nov 4 20:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 20:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 20:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 21:00:03 IST 2016 Ping result: ONLINE
Fri Nov 4 21:10:06 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Fri Nov 4 21:10:17 IST 2016 Ping result: ONLINE
Fri Nov 4 21:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 21:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 21:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 21:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 22:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 22:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 22:20:03 IST 2016 Ping result: ONLINE
Fri Nov 4 22:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 22:40:02 IST 2016 Ping result: ONLINE
Fri Nov 4 22:50:02 IST 2016 Ping result: ONLINE
Fri Nov 4 23:00:02 IST 2016 Ping result: ONLINE
Fri Nov 4 23:10:02 IST 2016 Ping result: ONLINE
Fri Nov 4 23:20:02 IST 2016 Ping result: ONLINE
Fri Nov 4 23:30:02 IST 2016 Ping result: ONLINE
Fri Nov 4 23:40:03 IST 2016 Ping result: ONLINE
Fri Nov 4 23:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 00:00:02 IST 2016 Ping result: ONLINE
Sat Nov 5 00:10:03 IST 2016 Ping result: ONLINE
Sat Nov 5 00:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 00:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 00:40:02 IST 2016 Ping result: ONLINE
Sat Nov 5 00:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 01:00:02 IST 2016 Ping result: ONLINE
Sat Nov 5 01:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 01:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 01:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 01:40:02 IST 2016 Ping result: ONLINE
Sat Nov 5 01:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 02:00:03 IST 2016 Ping result: ONLINE
Sat Nov 5 02:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 02:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 02:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 02:40:03 IST 2016 Ping result: ONLINE
Sat Nov 5 02:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 03:00:06 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 03:00:17 IST 2016 Ping result: ONLINE
Sat Nov 5 03:10:03 IST 2016 Ping result: ONLINE
Sat Nov 5 03:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 03:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 03:40:02 IST 2016 Ping result: ONLINE
Sat Nov 5 03:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 04:00:02 IST 2016 Ping result: ONLINE
Sat Nov 5 04:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 04:20:03 IST 2016 Ping result: ONLINE
Sat Nov 5 04:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 04:40:02 IST 2016 Ping result: ONLINE
Sat Nov 5 04:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 05:00:03 IST 2016 Ping result: ONLINE
Sat Nov 5 05:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 05:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 05:30:03 IST 2016 Ping result: ONLINE
Sat Nov 5 05:40:03 IST 2016 Ping result: ONLINE
Sat Nov 5 05:50:20 IST 2016 Ping result: ONLINE
Sat Nov 5 06:00:20 IST 2016 Ping result: ONLINE
Sat Nov 5 06:10:27 IST 2016 Ping result: ONLINE
Sat Nov 5 06:20:03 IST 2016 Ping result: ONLINE
Sat Nov 5 06:30:27 IST 2016 Ping result: ONLINE
Sat Nov 5 06:40:02 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 06:40:13 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 06:50:02 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 06:50:13 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 07:00:02 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 07:00:13 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 07:10:02 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 07:10:13 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 07:20:03 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 07:20:14 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 07:30:03 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 07:30:14 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 07:40:02 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 07:40:13 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 07:50:03 IST 2016 Ping result: OFFLINE (Ping reply not received), Retrying after 10 seconds
Sat Nov 5 07:50:14 IST 2016 Ping result: OFFLINE (Ping reply not received), Rebooting...
Sat Nov 5 08:00:02 IST 2016 Ping result: ONLINE
Sat Nov 5 08:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 08:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 08:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 08:40:03 IST 2016 Ping result: ONLINE
Sat Nov 5 08:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 09:00:02 IST 2016 Ping result: ONLINE
Sat Nov 5 09:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 09:20:03 IST 2016 Ping result: ONLINE
Sat Nov 5 09:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 09:40:02 IST 2016 Ping result: ONLINE
Sat Nov 5 09:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 10:00:03 IST 2016 Ping result: ONLINE
Sat Nov 5 10:10:03 IST 2016 Ping result: ONLINE
Sat Nov 5 10:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 10:30:02 IST 2016 Ping result: ONLINE
Sat Nov 5 10:40:02 IST 2016 Ping result: ONLINE
Sat Nov 5 10:50:03 IST 2016 Ping result: ONLINE
Sat Nov 5 11:00:02 IST 2016 Ping result: ONLINE
Sat Nov 5 11:10:02 IST 2016 Ping result: ONLINE
Sat Nov 5 11:20:02 IST 2016 Ping result: ONLINE
Sat Nov 5 11:30:03 IST 2016 Ping result: ONLINE
Sat Nov 5 11:40:03 IST 2016 Ping result: ONLINE
Sat Nov 5 11:50:02 IST 2016 Ping result: ONLINE
Sat Nov 5 12:00:03 IST 2016 Ping result: ONLINE
pi@rpi-lithiumhead-piaware ~ $



Sunday, September 11, 2016

MT7688 Duo getting started

Now days I am putting together a basic kit for an IoT course that I am designing.
The kit consists of items purchased from Visha World.
This is what it looks like at the moment.


All of the above components - except MT7688 Duo - can be purchased online from Visha World. For MT7688 Duo, you can visit seeed studio and order it from them. Once I am done designing the course, the details will be available on our training website over at www.seekhow.in and the kit as a whole will be made available at www.vishaworld.com.

As of now, the kit consists of the following:

  1. MT7688 Duo [Buy]
  2. 4GB MicroSD Card
  3. 8 Channel Relay board controllable over USB [Buy]
  4. CH340G Based USB UART
  5. Breadboard [Buy]
  6. Breadboard patch cables 40 way Male to Female 200mm long [Buy]
  7. Breadboard patch cables 40 way Female to Female 200mm long [Buy]
  8. Breadboard patch cables 40 way Male to Male 200mm long [Buy]
  9. USB OTG Cable
  10. DHT11 Temperature Humidity Sensor [Buy]
  11. 0.96" OLED Display [Buy]
  12. microUSB cable for MT7688 Duo's Microprocessor
  13. miniUSB Cable for Relay Board


Step 1: Gaining access to the serial console.
The first thing I did when I got my MT7688 Duo was to get access to its console over UART. For that I used CH340G based USB-TTL convertor with its voltage set to 3.3V. Here is a photo and screenshot:



For wiring details, have a look at Seeed Studio's Wiki Page for MT7688 here.


Step 2: Connecting the MT7688 Duo as a client to your home's WiFi Connection
To know how to connect your MT7688 Duo to your home/office WiFi network, have a look at Seeed Studio's Wiki Page for MT7688 here.

Step 3: Mounting the microSD card
Insert the microSD card and then via the serial console, issue the commands:

cd /dev
mount -t vfat mmcblk0p1 /mnt/

Then issue the command:
df -h
and you will note the microSD card showing up in the list.


Friday, July 22, 2016

Simple chat server and client

Simple Chat IPv4 Server and Client code. Run the server on one computer and the clients on the same network. Whatever one client sents to the server, it is copied and sent to all the clients connected to it. The code is hosted on gist and embedded below.

Monday, May 30, 2016

Personal Analytics using DIY IoT devices and InitialState.com

Recently I have blogged about three DIY IoT devices that I have put up in my house. Some of these devices have been running for past 3 years and uploading data points to one of the three data brokers - Xively or ThingSpeak or Carriots. A few months ago I switch to InitialState.com which has a sleek interface and allows you to analyse your data in a much better visually intuitive way.

So with the following 3 devices collecting data using sensors from inside my house...

  1. Weather station outside - installed in my balcony (temperature, humidity, pressure, light)
  2. Ambient sensors inside the house next too the bathroom (temperature and humidity)
  3. Electrical Energy Meter - Monitoring voltages, currents etc on all 3 phases 


...here are the few insights that I gleaned on from analyzing the data:



Overview:


This is what my tiles app looks like with the various data stream being fed from the 3 IoT devices
Recently, InitialState.com made changes to their platform to allow decreasing the size of the tiles even further
This helps organize and fit in much more information on a single screen.
Lines app with all data streams activated. This gives you storage oscilloscope like functionality with cursors and zoom.


My electricity usage patterns:


This is the voltage of third electrical phase to which all my water heaters are connected.
I can check the days on which I used them (i.e. I took a bath) and
days on which I did not (i.e. I did not take a bath) and ... days
on which I used them twice or for longer duration (i.e. I had a guest stay over).
I was on vacation 7th may on wards, so no water heater usage during the period post that date

This is the second electrical phase to which my
kitchen appliances (microwave, washing machine and fridge) are connected.
The persistent square wave is my fridge's compressor turning on and off.
The spikes are the days on which I washed clothes.
The smaller spikes are when I used the microwave.

This is the voltage on the first phase of my electrical supply.
Lights and fans, TV, computer, router  are connected to these.
You can see the things been turned on and off - evident from discretization.

Zoomed out view of the total electrical energy consumed.
Obviously it would keep increasing over time.
I guess how it accumulates over time depends on the energy meter I am using.

If you zoom in, you can figure out the days (flat parts) when there was a
power failure for a few minutes

Same graph as above. Different color, more zoom.
Power failure is clearly seen

Citizen science - analyzing the weather:

 
Detecting Clouds
Ambient light being captured by the weather station installed outside.
Zoomed in to 4 days worth of data
As the Sun moves across the sky, it created a curve.
The curve is distorted at the top because there is a pillar in the balcony that
casts a shadow on the sensor. You will note that of the 4 days, the graph
on the third day is much more smoother.
This perhaps indicates that there were no clouds on that day

Daily Temperature/Humidity/Light cycles
When the sun is at its peak, the humidity is the least and temperature is maximum.

The days on which I took bath
Lines app showing readings from humidity sensor placed inside my house.
The peaks every now and then are the times at which I took bath - opening the
bathroom unleashes extremely humid air into the house.



Temperature cycling through the day
You can not the maximum and minimum temperature.
The daily temperature range is 10 degrees centigrade.
Also its getting cooler on an average.

As the sun rises (light sensor in red), temperature starts rising as well (temperature in orange)

Atmospheric Pressure cycles twice a day.
Not very clear from here, but unlike temperature, humidity and light which only cycle once a day,
atmospheric pressure goes through 2 peaks and troughs in a day.
No one taught me this at school

Outside temperature and outside humidity plotted together.
It is jst fascinating to look at. Nothing new there.

Plot of humidity levels inside and outside.
The inside humidity levels follows the ones outside.
Nothing much to see there. 
The inside temperature of an unoccupied house is the average of outside temperature.
I haven't been living inside the house for the few days that are covered in this graph.
The graph shows outside and inside temperature.
The outside temperature cycles daily.
The inside temperature on the other hand is a smooth curve which sort
of looks like the average value of the temperature outside.
The insulating effect of the house is responsible for that.



How to tell if an house is unoccupied by looking at inside temperature values
Inside temperature: On the days I was at home you can see a lot of ups and downs
But 7th May on wards, the inside temperature curves is smooth cause there
was no one to open and close the windows and doors.

Graph of inside temperature and inside humidity.
There are much more ups and downs when people are living in the house than when they are not.
Opening and closing the doors and windows causes those ups and downs.