Installing tinyos 2.1.2 on MacOSX Yosemite

So this is my third post about installing tinyos and I am quite used to writing about installation instructions.
So this is for installing the latest branch of tinyos(as of 3rd August 2015) on MacOSX 10.10.4
First you need to have macports installed. It’s very simple. Just open the following link: https://www.macports.org/install.php. You don’t need to install Xcode as is mentioned in the page, just the command line tools should work.

Once you have the Xcode command line tools and macports installed you need to install the Java jdk.
After this the installation instructions are very simple.
So I won’t give detailed description of what happens.
Just listing down the commands. Run them one at a time.

sudo port install msp430-gcc msp430-libc automake autoconf python35 gcc47 gcc_select
sudo port select gcc mp-gcc47
git clone https://github.com/tinyos/nesc.git
cd nesc/
./Bootstrap
./configure
make
sudo make install
cd ../
git clone https://github.com/tinyos/tinyos-main.git
cd tinyos-main/tools/
./Bootstrap
./configure
make
sudo make install
cd ../apps/Blink/
make telosb

And you should have the compiled file.
The problem is I haven’t had a chance to test the motes yet.
So please leave some comments and let me know if it compiles and gets installed.

Installing Mosquitto 1.4 on RaspberryPi and using Eclipse Paho Python and Javascript

MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.
Mosquitto is an open soure message broker that implements the MQ Telemetry Transport protocol.

So if you are on this page you have some idea about what MQTT is and/or what the terms broker,subscribe and publish mean. If not then I will just briefly describe what these terms mean:
Broker – This is the server where clients subscribe and publish to topics
Subscribe – A client subscribes to a topic and he can receive all the messages published to a particular topic
Publish – A client can publish messages to a particular topic

So lets start with the installation of first mosquitto 1.4 which acts as the broker.

Firstly update your Pi and make sure your repositories are up-to date. I am using 2014-06-20-wheezy-raspbian for this software.

sudo apt-get update && sudo apt-get upgrade

Then we install all dependencies needed for mosquitto
sudo apt-get install mercurial libssl-dev cmake uuid-dev python2.7-dev xsltproc docbook-xsl python-pip

Now that we have all the dependencies.

Let’s start by creating a new directory called mosquitto

mkdir mosquitto
cd mosquitto

Let’s first fetch websockets and compile it for the Pi
wget http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.3-chrome37-firefox30.tar.gz
tar -zxf libwebsockets-1.3-chrome37-firefox30.tar.gz
cd libwebsockets-1.3-chrome37-firefox30/
mkdir build
cd build/
cmake .. -DOPENSSL_ROOT_DIR=/usr/bin/openssl
make
sudo make install

The above commands fetches libwebsockets and installs it.
Next, We will be compiling mosquitto from source obviously and it can be fetched from a bit-bucket repository with the following command:

hg clone ssh://hg@bitbucket.org/oojah/mosquitto

If you get a error like this:

remote: Permission denied (publickey).
abort: no suitable response from remote hg!

Then run the following commands
ssh-keygen -t rsa -C pi@raspberrypi
cat ~/.ssh/id_rsa.pub

And then add it to the your bit-bucket account.
And then run hg clone ssh://hg@bitbucket.org/oojah/mosquitto again.

If you don’t get the above error then proceed with the following commands:
cd mosquitto/
hg pull && hg update 1.4

After that edit the config.mk file and change the option to yes from no
WITH_WEBSOCKETS:=yes

Finally make and install mosquitto
make
sudo make install
sudo ldconfig

After this just run the following commands
mosquitto &
mosquitto_sub -d -t hello/world

Open a new terminal on your pi and run the following command from there
mosquitto_pub -d -t hello/world -m "Mosquitto Works"

Once you get a message on the terminal from which you had subscribed you can be sure that you have mosquitto setup on your pi.

Next we setup Eclipse Paho for Python on the Pi

Install eclipse paho with
sudo pip install paho-mqtt

And a basic python file for paho looks like this

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("hello/world")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

#Non Blocking call interface
#It starts a thread and is a more preferred method for use
client.loop_start()

The example above is self explanatory and can be understood with the comments in the code.

Next we will write the code for using websockets with mqtt.
This is rather simple.
First we will copy the /etc/mosquitto/mosquitto.conf.example to /etc/mosquitto/mosquitto.conf and add the following lines in the section labeled “Extra Listeners”

listener 9001
protocol websockets

And start mosquitto with the following command in the background

mosquitto -c /etc/mosquitto/mosquitto.conf -d

To run websockets I used the following example code and cloned in from git with
git clone https://github.com/jpmens/simple-mqtt-websocket-example.git

Then you can move all the files to /var/www/mosquitto and navigate to {{ip of raspberrypi}}/mosquitto/index.html
If you do not get a page means you do not have apache installed and you can install it with
sudo apt-get install apache2 -y

So this post helps you install mosquitto and eclipse paho for python and gives you examples of running mqtt clients from python and websockets with javascript.

Cheers!

Google Drive Terminal

So I started using github and realized I had to pay some money to make my code private and I did not want to pay money for that. So I started looking around for alternatives and came across bitbucket but I wanted to use my google drive. So I started writing a python script to control my google drive folder from my Ubuntu 14.04.

This app has one file called as drive.py and you can find the file at https://github.com/shady33/google-drive-terminal . Just download the file or clone it. This file is the main module that is responsible for all the commands. There is also another file that we need.If you want it you can drop me an e-mail and I will mail you the file. That file is basically a credentials.txt file in the .drive folder in the same directory where your project is. This file contains the client key and client secret and can also be obtained with the following method. Firstly, login to https://code.google.com/apis/console/ . Here you need to create a new project and enable the Drive API and Drive SDK in the API’s tab. You will then receive a Client ID for native application.  Copy the Client Id  and Client Secret to a credentials.txt file in the .drive folder on the first and second lines respectively. With this you will have a working drive for your terminal and you can call it by <code>python drive.py [command] [args]</code>

Now for the commands:

I have added the following 6 commands as of now and if and when I come up with a new command will include it in the source.

The first command is init. It basically creates the .drive folder for the application. Also when you do a init you will get a url which you need to open and copy paste the code in your terminal.

Second command is the origin where we add the remote url if needed for a particular folder and you dont need to run it if you wish to use the root directory.

Third command is add. It works similar to the git add where we add the files to be commited on later.

Next is the push. This command pushes the added files to your google drive.

Next is pull [args]. The valid args is all or nothing. If you input all as the argument then it will pull all files in that folder. If you do not use any arguments then all the files that you had pushed earlier would be pulled.

The last is upload. It is basically used to upload the file directly without a add operation. With this you won’t be able to pull the file after somebody has updated it.

I hope this helps you in some way. You can find the source at https://github.com/shady33/google-drive-terminal .

Cheers!

 

New TinyOS Make System

So I was working with my installation of tinyos and decided to pull the new updates from github. As soon as i did a git pull my make system stopped working. After trying to locate the error I came across this page https://github.com/tinyos/tinyos-main/blob/master/support/make/README.md . Turns out they had modified the tinyos make system and made it more simpler by removing the unnecessary ncc and changed the environment variables and moved the platform files in a more organized manner. The new version of the Make System is the version 3 and it was released in the summer of 2014

So to use the new make system you can follow the instructions at https://lakshbhatia.wordpress.com/2014/02/11/installation-of-tinyos-on-ubuntu/

Also for a better way and more efficient way to install TinyOS use Installing tinyos on Raspberry Pi

And instead of using the environment variables in the previous link use the following variables:

<code>

# Here we setup the environment
# variables needed by the tinyos
# make system

export TINYOS_ROOT_DIR=”~/tinyos-main/”
export TOS_OS_DIR=”$TINYOS_ROOT_DIR/tos”
export TINYOS_MAKE_DIR=”$TINYOS_ROOT_DIR/support/make”
export TINYOS_MAKERULES=”$TINYOS_MAKE_DIR/Makerules”
export TINYOS_MAKELOCAL=”$TINYOS_MAKE_DIR/Makelocal”
export TINYOS_MAKEDEFAULTS=”$TINYOS_MAKE_DIR/Makedefaults”
echo “setting up TinyOS on source path $TINYOS_ROOT_DIR”

</code>

Rest all you can follow from the other link and do a make telosb to check if your new make system works.

UASN – Underwater Acoustic Sensor Networks

This post is about the underwater acoustic sensor networks project that I worked on with one of my professors and two of my friends in the last semster.  Underwater acoustic sensor networks(UASN) is a very niche field. Not a lot of work is going on for developing applications for underwater networks. So working in this field was not only a challenge but something that I found very interesting because of the lack of proper documentation and transfer speeds of 8 bits/second. This post will tell you about how to setup a small testbed with 7 telosb motes and a Simple Acoustic modem that gave us 8 bits/second and worked only on RS232 standard. So to use telosb motes we used a max232 converter to convert data from UART to RS232.

The 10 pin expansion header on the telosb has the following pinout:

telosb_connector

 

We connected pin 2 and pin 4 of the expansion header to the max232 connector which was connected to the Acoustic modem. The circuit for the board was

olpc-xo-ttl-rs232-circuit

 

Now that we had the hardware ready, we started working on the software part.

We used the normal TinyOS repository from Github. You can find the instructions for doing this at https://lakshbhatia.wordpress.com/2014/02/11/installation-of-tinyos-on-ubuntu/ .

When you have a working tinyos repository.. navigate to tos/platforms/telosa and edit the PlatformSerialC.nc file and change the line

components new Msp430Uart1C() as UartC to components new Msp430Uart0C() as UartC .

Then modify the file PlatformHdlcUartC.nc and change the line components new Msp430Usart1C() as UsartC to components new Msp430Usart0C() as UsartC .

We also modified the TelosSerialP.nc file to match the baudrate that the modem could support. We modified the

const msp430_uart_union_config_t msp430_uart_telos_config = { {

ubr: UBR_1MHZ_115200,

umctl: UMCTL_1MHZ_115200,

ssel: 0x02, pena: 0, pev: 0, spb: 0, clen: 1, listen: 0, mm: 0,

ckpl: 0, urxse: 0, urxeie: 1, urxwie: 0, utxe : 1, urxe : 1

} };

to

const msp430_uart_union_config_t msp430_uart_telos_config = { {

ubr : UBR_1MIHZ_4800,

umctl: UMCTL_1MIHZ_4800,

ssel: 0x02, pena: 0, pev: 0, spb: 0, clen: 1, listen: 0, mm: 0,

ckpl: 0, urxse: 0, urxeie: 1, urxwie: 0, utxe : 1, urxe : 1

} }; 

Also to use any standard radio code we need to wire SerialActiveMessageC instead of ActiveMessageC in your code.

This completed the software and hardware part. Now a simple make telosb install would compile the code that would help in running this modem.

You can also clone the source for the same at https://github.com/shady33/tinyos-main/tree/underwater .

We tested the entire system with 7 modems arranged as follows:

setup_uasn

 

Here the box is node 1 and the other nodes have been numbered.

Node 1 acts as the link between the underwater communication and the wireless network. It is responsible for collecting the data via UART and send it wirelessly to the Basestation. Nodes 2 and 5 are cluster heads and nodes 3,4,6 and 7 are all children nodes.

In the first phase the entire network is time-synchronized starting from node 1. Node 1 syncs its time with 2 which in turn syncs it with 5 followed by time-synchronization of the children nodes by their respective cluster heads.

In the second phase the children nodes transfer data from 6->5 and 7->5. Then 5 transmits its data to 2 followed by data from 3 and 4. After 2 has all the data it transmits it to 1 and then it sends it to the base-station over the 2.4GHz channel.

This was the entire experiment that we conducted with underwater acoustic modems and telosb motes.

Edit: The Acoustic modem that we were using was called SAM-1

 

sam1

Leapcast – Chromecast for Raspberrypi

Leapcast is a software implementation of the Chromecast. It works on the same protocol that Chromecast does and can be easily used as a Chromecast device. What is interesting is that the cost of a Chromecast device and a Raspberry Pi is the same($35).

To use your Raspberry Pi as a Chromecast device follow the following steps:

1. Upgrade your Raspberry Pi

sudo apt-get update
sudo apt-get upgrade

2. Install Chromium  browser

sudo apt-get install chromium

3. Now we will install dependencies for leapcast and clone the git repository

git clone https://github.com/dz0ny/leapcast.git
cd ./leapcast
sudo apt-get install virtualenvwrapper python-pip python-twisted-web python2.7-dev

Now reset your ssh connection by typing exit and then again ssh into your Pi.

4.Now you are ready to run leapcast. Make sure your HDMI cable is connected.After that type

cd leapcast/
startx &
mkvirtualenv leapcast
pip install .
DISPLAY=:0 leapcast --chrome /usr/bin/chromium

If you get the error “failed to load nss libraries” then type

sudo ln -s /usr/lib/arm-linux-gnueabihf/nss/ /usr/lib/nss

With this i could stream youtube videos to my Pi but I have not tried the new screen cast feature yet. If you try it and it works please leave a comment.

 

 

Installing tinyos on Raspberry Pi

The Raspberry Pi is a credit-card-sized single-board computer developed in the UK by the Raspberry Pi Foundation. It makes programming the motes easy because it can be connected directly to the motes and the motes can be programmed without having to install tinyos or nesc and other tools for programming. This would help a lot of people to use the same tinyos installation without screwing with their operating system.

For this tutorial I am using a RASPBIAN-Debian Wheezy and you can download it from here.

After you burn your image and boot your raspberrypi for the first time don’t forget to run a sudo raspi-config and expand your root partition to fill the SD Card.

First we will install all the essentials required to compile nesC. To do this run the following command :

1. sudo apt-get update

2. sudo apt-get install emacs gperf bison flex git

After the above files are installed reboot your raspberrypi with sudo reboot -h now

Next we will clone the git repository from this link.  Type

git clone https://github.com/tinyos/nesc.git

cd nesc/

./Bootstrap

./configure

make

sudo make install

Next we will install the essentials for tinyos.

sudo apt-get install build-essential openjdk-6-jdk openjdk-6-jre python2.7 python2.7-dev automake avarice avr-libc msp430-libc avrdude binutils-avr binutils-msp430 gcc-avr gcc-msp430 gdb-avr subversion graphviz python-docutils checkinstall

After this is done go back to your home folder and type git clone https://github.com/tinyos/tinyos-main.git 

Now type sudo nano .bashrc and add the following lines to the end of the file

export TOSROOT=”/home/pi/tinyos-main-master”
export TOSDIR=”$TOSROOT/tos”
export CLASSPATH=$CLASSPATH:$TOSROOT/support/sdk/java
export MAKERULES=”$TOSROOT/support/make/Makerules”
export PYTHONPATH=$PYTHONPATH:$TOSROOT/support/sdk/python

echo “setting up TinyOS on source path $TOSROOT”

Almost done.

Now type source .bashrc

Now navigate to ~/tinyos-main-master/tools and execute the following commands

./Bootstrap

./configure

make

sudo make install

You’re done! 😀

Check your TinyOS install by:

cd ~/tinyos-main-master/apps/Blink

make telosb

The code should compile successfully and at the end of the compilation, the result would be similar to that of writing a tos-image.

Cheers!

Snapget – decryptor for Snapchat

Snapchat is a photo messaging application available for iOS and Android devices. This app helps in exchanging images and videos between contacts. What is special about this app is it deletes the image after the viewing time is over. Say if you send a image to a friend with a time limit of 10 seconds,he can view the image for a maximum of 10 seconds.

I started looking around for how snapchat stores its images till they are opened and found that they store the image in the app’s internal data folder which can be easily accessed if you have a rooted device. So i copied to *.jpg.nomedia file onto my computer and tried to open it but they had used a AES-128 encryption. I used the app’s apk file to see if i can find the key for encryption and it was hardcoded in the app in the crpyto folder under util in the com.snapchat.android package. I then decrypted the image using that key .

So now that i had all the essentials i made a small android  widget for saving the images . You need a rooted phone to use this app.  Download the source files from my github directory and compile it using eclipse. Install it on your phone and add the widget to your homescreen. Everytime you receive a snap open the app let the image complete loading .. go to your homescreen and press the button. The image will be at “/sdcard/Snapchat/” and it should also be there in your gallery.

Github directory : https://github.com/shady33/snapget-decrpyt

Getting Started with TinyOS-The Basics

The first thing an outsider needs to get familiar with is the structure of the program while coding in TinyOS. The official TinyOS tutorial does a great job of making all these basics clear, but I just thought we’d include this post as a token post in order to maintain some sort of order 😛 Feel free to check out the official post and all of their tutorials:

http://www.tinyos.net/nest/doc/tutorial/lesson1.html

Every-time you start a new project “xyz”, you’ll need at least 4 files.

  1. Makefile
  2. xyzC.nc file
  3. xyzAppC.nc file
  4. xyz.h file

Laksh has hosted all of our projects on github and I’ll keep referring to the relevant folders so that you could try out the codes as well. For instance we’ve called these skeletal files “null files” and you could have a look at them to understand the basic structure for any TOS project.

https://github.com/shady33/tinyos-bits/tree/master/null/src

A Makefile tells the compiler which flags to set and tells it where your main configuration file is present (xyzAppC.nc). We mainly use the Makefile to also declare the buffer size for the printf commands we use in our projects. One can also set the power level for communication for static range scenarios here.

xyzC.nc file contains your main code where you also declare all the interfaces of the mote (in our case, UC-Berkeley or TelosB or MicaZ) that you’ll be using. xyzAppC.nc is your main configuration file and interfaces your main code with the hardware modules like LEDs, timers, radio module, sensors etc.

The header file is used to declare which channel you’ll be using for communication and also in most of our applications the structure of the message packet that each mote shall be sending. By default, the maximum packet size that one can send is 28 bytes but it can be modified to upto 127 bytes by changing the header file.

That’s it for this post, we’ll keep updating this space with more posts which’ll sort of act as a timeline of the experiments/projects we did till now in this field in the hope that it might be of use to anybody trying to work on similar projects using TinyOS.

Cheers!

Setting up Cooja: A simulator for TinyOS

It might not be possible to test out all the work you do in TinyOS on a physical test bench at all times. Or maybe sometimes you want to save yourself the trouble of flashing a large number of motes only to realize that you’d made trivial errors in your code. Enter: Cooja. Contiki is another widely used platform in the field of Wireless Sensor Networks. We’ll now be describing how to setup Cooja: the simulator tool offered by Contiki. The normal installation is by following the instructions on this site: http://www.contiki-os.org/start.html However, for somebody who just wants to use Cooja as a simulation platform in conjunction with TinyOS, the normal setup is bulky at a little over 2 GB to download and the instructions are something that even we struggled with. In order to save you the trouble of this procedure, we’ve uploaded the relevant part of Contiki to make Cooja. Steps: You can either download the zip file which contains an older version of cooja or clone it from git with the following command
git clone https://github.com/contiki-os/contiki.git
and you can then skip steps 1 and 2.
1. Download the .zip file from https://docs.google.com/file/d/0Byj9l1T-DECtRGd2a1Vka1RUakk/edit 2. Extract the files to any convenient location. 3. Make sure you have java development kit (7 or greater) and ant installed on your system. If not, then :                     sudo apt-get install ant (on Linux)  or https://ant.apache.org/manual/install.html (for Windows) http://www.oracle.com/technetwork/java/javase/downloads/index.html (for Windows) or sudo apt-get install openjdk-7-jdk (for Linux) 4.Set the path variables for java and ant (only required if not automatically set or in windows) 5.Go to the location where you’ve extracted the folder. 6.Navigate to contiki/tools/cooja 7.Write ant run (in terminal or cmd) This should launch the simulator and you can now explore Cooja. The example we’re going to show here is simple radio communication between two motes and from our basic Send Receive code hosted at: https://github.com/shady33/tinyos-bits/tree/master/basic_send_receive General Steps for simulating code based on TelosB motes: 1. File->New Simulation. Create. 2.Motes->Add Motes->Create a new mote type->Sky Mote 3.In the Contiki Process/Firmware field browse to the location on disk where you’ve compiled your code using make telosb 4.Go to build/telosb/ and choose main.exe 5.Add motes 6.Press Start in the simulation control panel

That’s about it. Some of the other common features we use are: 1. The on board LEDs which you can add from View->LEDs in the Network Window. 2. Radio Messages from Tools->Radio Messages 3.Speed Limit control in the Simulation Control window 4.The output of Printf statements that show up in the Mote Output Window (Something to watch out for while using Printf statements, its better to use the SerialPrintfC component in the configuration file XYZAppC.nc file while simulating in Cooja. On the other hand, PrintfC works better in real-world simulations using the java.net.tinyos.tools.listen tool for tinyos or something else like Cutecom.) Cheers, Laksh Bhatia, Mayank Joneja,Sachin