Sunday, August 18, 2013

Conditional Statement in Bash on OS X

Ok,
I had a bit of trouble getting a conditional statement to evaluate on my bash script in OS X.
I was trying to perform an action if I was online or not.

A good way to test if you are online - ping google of course!

For testing purposes I just got the computer to use text to speech to let me know the result.
Here is my working example, spaces are important!

#!/bin/bash

FLAG=0

ping -q -c 1 google.com && FLAG=1
if [ $FLAG = "1" ] ; then
say $FLAG
else 
say $FLAG
fi

exit

If you can ping google,the computer will speak "one"

If you you want to test, change google.com to something you cant resolve such as googlekdfsjbg.com and run it again.

You shouldn't be able to ping googlekdfsjbg.com so the computer will speak "zero"

Of course this is a simple example and you need to tailor to your situation, just because you can't ping google doesn't mean you are not online. For example pings could be blocked on your network, however this suits my situation.

enjoy!

Thursday, August 1, 2013

Raspberry Pi Temperature Sensing.

Thanks to Adafruits Raspberry Pi tutorials on temperature sensing,
and this blog on Raspberry Pi temperature sensing to Sen.se.

I decided to do my own.
I also wanted to store the results locally, so I also saved it to a file.

I used a DS18B20 1-wire temperature sensor
Adafruit T-Cobbler
Breadboard and wires.

I wired it up just like they did in their diagrams.



Add one wire support.
In /boot/config.txt add
dtoverlay=w1-gpio
I subscribed to Sen.se and in your profile you can find your API key.

After checking out their tutorials I then wrote a bash script to grab the the temperature, then use curl and JSON to upload it.

Here is the script replace SERIALNUMBERHERE with the serial number of your sensor.
API KEY HERE with your API key, and SENSEFEEDID with your Sen.se feed id.

#!/bin/bash

sudo modprobe w1-gpio

sudo modprobe w1-therm

cd /sys/bus/w1/devices/SERIALNUMBERHERE/


URL="http://api.sen.se/events/?sense_key=API KEY HERE"


DATE=$(date)

NOW=$(date +"%d_%m_%Y")


DATA=$(cat w1_slave)


DATA1=${DATA:69:2}

DATA2=${DATA:71}

DATA3=$DATA1.$DATA2


OUTPUT="$DATE , $DATA1.$DATA2"


echo $OUTPUT >> /root/output_$NOW.csv


curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"feed_id": SENSEFEEDID,"value": '$DATA3'}' $URL



I then use Cron to schedule a reading every few minutes.

Then back in Sen.se you can graph it or manipulate it like you want.