Created
September 25, 2014 22:22
-
-
Save bachwehbi/b81a700757631a19bf44 to your computer and use it in GitHub Desktop.
Connecting Intel Galileo to Beebotte to report sensor data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding: utf8 | |
############################################################ | |
# This code uses the Beebotte API, you must have an account. | |
# You can register here: http://beebotte.com/register | |
############################################################ | |
import sys | |
import os | |
import time | |
import math | |
from array import * | |
from beebotte import * | |
import conf | |
bbt = BBT(conf._accesskey, conf._secretkey, hostname = conf._hostname) | |
#If you don,t have already a device, create it here http://beebotte.com/device/create | |
#Create Resource objects for the resources to report, | |
#chage the device, resource names as suits you | |
noise_resource = Resource(bbt, 'sozen', 'noise') | |
temp_resource = Resource(bbt, 'sozen', 'temperature') | |
humidity_resource = Resource(bbt, 'sozen', 'humidity') | |
luminosity_resource = Resource(bbt, 'sozen', 'luminosity') | |
particules_resource = Resource(bbt, 'sozen', 'fine_particules') | |
debug = False | |
B = 3975 | |
noise1 = array('f', []) | |
noise2 = 0 | |
sampleNb = 0 | |
sampleThreshold = 100 | |
periodNb = 0 | |
periodThreshold = 5 | |
def pin_export(pin): | |
try: | |
pin1export = open("/sys/class/gpio/export","w") | |
pin1export.write(pin) | |
pin1export.close() | |
except IOError: | |
print "INFO: GPIO " + pin + " already exists, skipping export" | |
fp1 = open( "/sys/class/gpio/gpio" + pin + "/direction", "w" ) | |
fp1.write( "out" ) | |
fp1.close() | |
def get_light( pin ): | |
fp2 = open( "/sys/bus/iio/devices/iio:device0/in_voltage" + pin + "_raw", "r" ) | |
value = float( fp2.read( ) ) | |
fp2.close() | |
try: | |
luminosity_resource.write(value) | |
except: | |
print "Error Writing" | |
print value | |
def get_temp( pin ): | |
fp2 = open( "/sys/bus/iio/devices/iio:device0/in_voltage" + pin + "_raw", "r" ) | |
value = float( fp2.read( ) ) | |
fp2.close() | |
resistance = (4095 - value) * 10000/ value #get the resistance of the sensor | |
temp = 1/(math.log(resistance/10000)/B + 1/298.15) -276.15 #convert to temperature via datasheet | |
try: | |
temp_resource.write(temp) | |
except: | |
print "Error Writing" | |
print temp | |
def get_noise( pin ): | |
global sampleNb | |
global periodNb | |
global noise1 | |
global noise2 | |
fp2 = open( "/sys/bus/iio/devices/iio:device0/in_voltage" + pin + "_raw", "r" ) | |
value = float( fp2.read( ) ) | |
fp2.close() | |
noise1.append(value); | |
sampleNb += 1 | |
if sampleNb >= sampleThreshold: | |
sum = 0 | |
for n in noise1: | |
sum += n | |
avg = sum/sampleThreshold | |
if avg > noise2: | |
noise2 = avg | |
sampleNb = 0 | |
noise1 = [] | |
periodNb += 1 | |
if periodNb >= periodThreshold: | |
get_temp( "2" ) | |
get_light( "3" ) | |
print 'Noise is ' + str(noise2) | |
try: | |
noise_resource.write(noise2) | |
except: | |
print "Error Writing" | |
noise2 = 0 | |
periodNb = 0 | |
return value | |
def run(): | |
pin_export("37") | |
pin_export("36") | |
pin_export("23") | |
pin_export("22") | |
while True: | |
get_noise( "0" ) | |
time.sleep( 0.06 ) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment