3 import urllib # for url unquote
13 pi = pigpio.pi() # connect to local Pi
15 consumer_key = credentials.API_key
16 consumer_secret_key = credentials.API_secret_key
17 access_token = credentials.access_token
18 access_token_secret = credentials.access_token_secret
20 auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
21 auth.set_access_token(access_token, access_token_secret)
22 api = tweepy.API(auth)
25 api.verify_credentials()
26 print("Authentication Successful")
28 print("Authentication Error")
30 pi.set_mode(2, pigpio.OUTPUT)
32 gpio8period = 20 # period of 1 bit to achieve 45bps was 20
34 ColumnCurrentPosition = 1
37 # first we map ascii to the possible ascii chars
38 ascii_to_baudot_char = {
118 # then we map limted set to baudot
119 # see http://rabbit.eng.miami.edu/info/baudot.html
204 initialize teletype i/o
206 txbaudot("01000") # cr
208 txbaudot("00010") # lf
215 # establish timer for TTY Motor
217 def motor_start(time_secs=0):
221 global MotorTimerCtr, MotorTimerVal
223 if (not MotorTimerCtr) :
225 gpio.output(PWR_RLY,gpio.LOW)
229 MotorTimerCtr = MotorTimerVal
231 #print "motor_start(): non-standard timeout value: %d" % time_secs
232 MotorTimerCtr = time_secs
236 turn off motor, turn off data relay
239 gpio.output(PWR_RLY,gpio.HIGH)
241 gpio.output(DATA_RLY,gpio.HIGH)
250 test mapping tables by attempt to print out all possible codes
253 for i in range(0,256):
254 if (ascii_to_baudot_char.has_key(chr(i))): # if first reduce mapping table has key
255 a = ascii_to_baudot_char[chr(i)]
256 #print 'ascii_to_baudot_char(%d): %s' % (i,a)
257 if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
258 b = ascii_to_binstr[a]
260 #print 'test(%s)' % (a)
265 transmit one character to the teletype
276 micros = gpio8period*1000
278 wf.append(pigpio.pulse(0, 1<<gpio, 20000))
280 for b in reversed(c):
282 wf.append(pigpio.pulse(1<<gpio, 0, micros))
284 wf.append(pigpio.pulse(0, 1<<gpio, micros))
286 wf.append(pigpio.pulse(1<<gpio, 0, micros))
287 pi.wave_add_generic(wf)
289 wid = pi.wave_create()
292 pi.wave_send_once(wid)
294 while pi.wave_tx_busy():
304 #print 'tx_keycode(%s)' % (s)
306 if (ascii_to_baudot_char.has_key(chr(k))):
307 a = ascii_to_baudot_char[chr(k)]
308 #print 'ascii_to_baudot_char(%d): %s' % (i,a)
309 if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
310 b = ascii_to_binstr[a]
311 #print 'tx_keycode() %d (%s) -> (%s)' % (k,chr(k),b)
313 #print 'tx_keycode(%s)' % (b)
318 def update_column_position():
320 keep track of column position so we can insert cr lf when necessary
322 global ColumnCurrentPosition, ColumnMax
323 ColumnCurrentPosition = ColumnCurrentPosition + 1
324 if ColumnCurrentPosition > ColumnMax:
325 #print "update_column_position(): col 0"
328 ColumnCurrentPosition = 0;
329 #print "column reset to 0"
342 Shift down to letters
352 send an ascii character
354 if (ascii_to_baudot_char.has_key(c)):
355 a = ascii_to_baudot_char[c]
356 #print 'ascii_to_baudot_char(%d): %s' % (i,a)
357 if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
358 b = ascii_to_binstr[a]
360 #print 'tx_ascii_chr(%s)' % (b)
361 if (a in needs_shift_up):
366 update_column_position()
372 send an ascii character
378 transmit an ascii string
380 de_uried_str = urllib.unquote(s)
381 for i in range(len(de_uried_str)):
382 #print '[%s]' % de_uried_str[i]
383 tx_ascii_chr(de_uried_str[i])
388 transmit a control code 'lf' = line feed, 'cr' = carriage return, etc.
390 global ColumnCurrentPosition
391 #print "tx_ctl(%s)" % c
394 ColumnCurrentPosition = 0
402 txbaudot('11011') # shift up
404 txbaudot('11111') # shift down
405 txbaudot('01000') # cr
406 txbaudot('00100') # space
414 txbaudot('01000') # cr
415 ColumnCurrentPosition = 0
420 update_column_position()
424 FILE_NAME = 'last_seen_id.txt'
426 def retrieve_last_seen_id(file_name):
427 f_read = open(file_name, 'r')
428 last_seen_id = int(f_read.read().strip())
432 def store_last_seen_id(last_seen_id, file_name):
433 f_write = open(file_name, 'w')
434 f_write.write(str(last_seen_id))
438 def reply_to_tweets():
439 #print('retrieving and replying to tweets...', flush=True)
440 # DEV NOTE: use 1060651988453654528 for testing.
441 last_seen_id = retrieve_last_seen_id(FILE_NAME)
442 # NOTE: We need to use tweet_mode='extended' below to show
443 # all full tweets (with full_text). Without it, long tweets
445 mentions = api.mentions_timeline(
447 tweet_mode='extended')
448 for mention in reversed(mentions):
449 #print(str(mention.id) + ' - ' + mention.full_text, flush=True)
451 tx_str(mention.full_text)
453 last_seen_id = mention.id
454 store_last_seen_id(last_seen_id, FILE_NAME)
455 if '#tweetateletype' in mention.full_text.lower():
456 #print('found the magic word!', flush=True)
457 #print('responding back...', flush=True)
458 api.update_status('@' + mention.user.screen_name +
459 '#Howdy do back to you!', mention.id)