User Tools

Site Tools


connectornot-lisbon-py

get-values.py : interfacing incoming values from the app with the physical system

needs servoclass.py and ledclass.py in the same folder

#!/usr/bin/env python

"""The main interaction computation file"""

import urllib2, time, servoclass, ledclass, sys

raya, gaya, vlaya, zlaya = servoclass.Servo(1), servoclass.Servo(2), servoclass.Servo(3), servoclass.Servo(4)
servoList = [raya, gaya, vlaya, zlaya]

### DATA
### [0] - phoneID ; [1] - timestamp ; [2] - bytes ; [3] - conversation ; [4] - sms ; [5] - signalstrength ; [6] - estimote


# reactions to different events

def reaction(led, motor1, motor2, motor3, motor4, speed_one, speed_two, duration):
    # reaction to data
    ledclass.fadeInTurq(led)
    motor1.move(speed_one)
    motor2.stop()
    motor3.stop()
    motor4.stop()
    time.sleep(duration)
    ledclass.fadeOutTurq(led)
    motor1.stop()
    # reaction to conversation
    ledclass.fadeInPurple(led)
    motor1.move(speed_two)
    motor2.stop()
    motor3.stop()
    motor4.stop()
    time.sleep(duration)
    ledclass.fadeOutPurple(led)
    motor1.stop()

def reaction_sms(number):
    for i in range(number):
        for j in range(4):
            ledclass.fadeInYellow(j)
            servoList[j].move(95)
        time.sleep(1)
        for j in range(4):
            ledclass.fadeOutYellow(j)
            servoList[j].stop()

while True: 
    try:
        #datas = codecs.open("values-test.txt", "r", encoding="utf-8")
        datas = urllib2.urlopen("http://url-to-php-file-on-remote-server")
        line = datas.read()
        #print line

        ### calculate the movement:
        bajts = abs(int(line.split(';')[2]))
        print "bajts", bajts
        if 0 <= bajts <= 150:
            speedBajt = 85 #servo up
            sleeping_time = 1
        elif 150 < bajts <= 900:
            speedBajt = 98 # servo down_slow
            sleeping_time = 2
        elif 900 < bajts <= 1600:
            speedBajt = 107 # servo down
            sleeping_time = 1
        elif 1600 < bajts <= 30000:
            speedBajt = 115 # servo down_fast
            sleeping_time = 1
        elif 30000 < bajts:
            speedBajt = 122 # servo down_very_fast
            sleeping_time = 1
        else: 
            print "none of the above byte values"
            speedBajt = 90 # servo down_very_fast

        conv = int(line.split(';')[3])
        print "conv", conv
        if 0 <= conv <= 2:
            speedConv = 88 #servo up
            sleeping_time = 0.5
        elif 2 < conv <= 30:
            speedConv = 95 # servo down
            sleeping_time = 1
        elif 30 < conv <= 120:
            speedConv = 100 # servo down_fast
            sleeping_time = 1
        elif 120 < conv:
            speedConv = 107 # servo down_very_fast
            sleeping_time = 1
        else: 
            print "none of the above byte values"
        sms = int(line.split(';')[4])
        print "sms", sms
        if 4 >= sms >= 1 :
            reaction_sms(sms)
        elif sms > 4:
            reaction_sms(4)
        else: 
            reaction_sms(0)
        signals = int(line.split(';')[5])
        estimote = line.split(';')[6].strip()
        ### locate in space
        ### and react depending on the closest estimote
        if estimote == "0":
            print "you are not here"
            for i in range(4):
                ledclass.fadeInRed(i)
            time.sleep(0.5)
            for i in range(4):
                ledclass.fadeOutRed(i)
            time.sleep(0.5)
        elif estimote == "62100": #blueberry pie
            print "blueberry"
            print speedBajt
            reaction(0, raya, gaya, vlaya, zlaya, speedBajt, speedConv, sleeping_time)
        elif estimote == "56336": #icy marshmallow
            print "icy"
            print speedBajt
            new_speedBajt = int(90-speedBajt)/4 + speedBajt
            new_speedConv = int(90-speedConv)/4 + speedConv
            reaction(1, gaya, raya, vlaya, zlaya, speedBajt, speedConv, sleeping_time) # main is gaya
            reaction(0, raya, gaya, vlaya, zlaya, new_speedBajt, new_speedConv, sleeping_time) # raya is secondary
            # reaction_sec(vlaya, vlayaLED)
        elif estimote == "55745": #mint coctail
            print "mint"
            print speedBajt
            new_speedBajt = int(90-speedBajt)/2 + speedBajt
            new_speedConv = int(90-speedConv)/2 + speedConv
            reaction(2, vlaya, raya, gaya, zlaya, speedBajt, speedConv, sleeping_time) # main is vlaya
            reaction(3, zlaya, raya, gaya, vlaya, new_speedBajt, new_speedConv, sleeping_time) # zlaya is secondary
        else:
            print "estimote weird"                       

        time.sleep(2)
    except KeyboardInterrupt:
        for i in range(len(servoList)):
            servoList[i].move(90)
        sys.exit()
'''
### WORK FLOW:

### read in the db

### parse values; what has changed since last time
####### use timestamp?

### compute the reaction
### lights to represent bytes and conversation

### move things, lights
'''
connectornot-lisbon-py.txt · Last modified: 2015/04/30 08:30 by zoza