-
Notifications
You must be signed in to change notification settings - Fork 2
/
control.py
executable file
·71 lines (64 loc) · 1.95 KB
/
control.py
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
70
71
#!/usr/bin/python
import serial
import re
import time
import sys
import json
import popen2
class Monitor:
def __init__(self):
self.serialPort = None
def open(self,portName,baud=9600):
self.serialPort = serial.Serial(portName, baud, timeout=10)
try:
self.serialPort.open()
except serial.SerialException, e:
sys.stderr.write("Could not open serial port %s: %s\n" % (self.serialPort.portstr, e))
time.sleep(1.1);
self.serialPort.write("+++");
time.sleep(1.1);
self.serialPort.write("ATPL2\r");
time.sleep(0.03);
self.serialPort.write("ATMY2\r");
time.sleep(0.03);
self.serialPort.write("ATDL1\r");
time.sleep(0.03);
self.serialPort.write("ATSM0\r");
time.sleep(0.03);
self.serialPort.write("ATCN\r");
time.sleep(0.03);
data = self.serialPort.read(1) # read one, blocking
n = self.serialPort.inWaiting() # look if there is more
if n:
data = data + self.serialPort.read(n)
print "DATA: ", data
mon = Monitor()
mon.open("/dev/ttyUSB0")
last_id = "foo"
req_str=r"wget --quiet -O - 'http://search.twitter.com/search.json?q=%23nycr&rpp=1%page=1'"
def twitter():
global last_id
while 1:
try:
(so,si) = popen2.popen2(req_str)
d = so.read()
o = json.loads(d)
r = o['results'][0]
t_id = r['id']
t_text = r['text']
if (t_id != last_id):
last_id = t_id
print "Writing",t_id,t_text
m = t_text
while (len(m)+len(t_text)+5) < 70:
m = m + " "+t_text
mon.serialPort.write(m+"\n")
except Exception as e:
print "except", e
pass
time.sleep(2)
if len(sys.argv) > 1:
s = " ".join(sys.argv[1:])
mon.serialPort.write(s+"\n")
else:
twitter()