This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitpachrad.py
182 lines (157 loc) · 6.44 KB
/
twitpachrad.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python3
# Joey Stanford
# radation board upload
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
import twitter
import serial
import time
import sys
import argparse
import configparser
def readconfigfile(configfile):
"""read in cofiguration file"""
config = configparser.ConfigParser()
try:
config.read(configfile)
except configparser.Error as err:
print(("Config File Error", err))
exit(1)
return config
def main():
"main function"
# twitter
encoding = None
consumer_key = config.get("Twitter", "consumer_key")
consumer_secret = config.get("Twitter", "consumer_secret")
access_token_key = config.get("Twitter", "access_token_key")
access_token_secret = config.get("Twitter", "access_token_secret")
twitter_api = twitter.Api(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret,
input_encoding=encoding,
)
# Arduino Serial port
arduino_serial = config.get("Arduino", "serial")
arduino_baud = config.get("Arduino", "baud")
arduino = serial.Serial(arduino_serial, arduino_baud)
# we don't want to spam twitter
twitter_counter = 0
while True:
# get the data and assemble it
message = arduino.readline().decode("utf-8").rstrip()
message = message.split(",")
if args.verbose:
print(("Readline: %s\n" % message))
# On Mac OS X, there is a python-readline bug which will often
# produce bogus data on the first pull. Until that bug is fixed
# you'll need to include some sort of fix loop like this:
# try:
# #fix readline errors for usv
# if float(message[0]) < 200:
# if float(message[1]) > 20:
# message[1] = str(float(message[1]) / 100)
# if float(message[2]) > 20:
# message[2] = str(float(message[2]) / 100)
# twitter_message = message[0] + ' CPM, ' \
# + message[1] + ' uSv/h, ' \
# + message[2] + ' AVG uSv/h, ' + message[3] \
# + ' time(s) over natural radiation'
# if args.verbose:
# print "Twitter: %s\n" % twitter_message
# except:
# print "Received bogus data"
# print "%s" % message
# continue
# This is the original twitter message with all of the data.
# People will not
# read the web page that describes how to interpret this and will start
# panicking at the "times(s) over natural radiation". Because of this
# we'll default to something less scary. The code is left here for your
# reference.
# twitter_message = message[0] + ' CPM, ' \
# + message[1] + ' uSv/h, ' \
# + message[2] + ' AVG uSv/h, ' + message[3] \
# + ' time(s) over natural radiation'
# High radiation is anything over 100 milirems, aka 1000 uSv so let's
# provide some hopefully helpful commentary on twitter.
# We can use the on-board average to help filter out anomalies.
# There is probably a better way to do this.
#
# Sometimes we get a back packet back resulting in us not having a
# message[2]. And sometimes we just get a bad readline.
if len(message) == 4:
try:
usv_reading = float(message[1])
usv_average = float(message[2])
float(message[0])
float(message[3])
except:
print(("1:Malformed readline: %s" % (message)))
else:
interpretation = ""
if usv_reading == 0:
print(("2:Malformed readline: %s" % (message)))
continue
elif usv_reading <= 1.2:
interpretation = "(normal range)"
elif usv_reading > 1.2 and usv_reading <= 250:
interpretation = "(slightly elevated)"
elif (usv_reading > 250 and usv_reading <= 499) and (usv_average > 250):
interpretation = "(Elevated reading)"
elif (usv_reading > 499 and usv_reading <= 999) and (usv_average > 499):
interpretation = "(Pre-Alarm! Significantly Elevated.)"
elif usv_reading > 999 and usv_average > 999:
interpretation = "(Radiation Alarm! [or the detector is broken])"
else:
interpretation = "(Disregard: failed quality control check)"
twitter_message = str(message[1]) + " uSv/h " + interpretation
if args.verbose:
print(("Twitter: %s\n" % twitter_message))
# send data to twitter every 10 minutes so we don't spam them
if twitter_counter >= 10 and not args.noop:
try:
__ = twitter_api.PostUpdate(twitter_message)
except:
print(
(
"Twitter error: %s, Message: %s"
% (sys.exc_info()[0], twitter_message)
)
)
else:
twitter_counter = 0
twitter_counter += 1
else: # len(message)
print(("3:Malformed readline: %s" % (message)))
# sleep for 1 minute
time.sleep(60)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Receive Radiation" " data from arduino and post to" " twitter."
)
parser.add_argument(
"-n",
"--noop",
action="store_true",
dest="noop",
help="""Do not post data online. Use with -v""",
default=False,
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="""Extra output regarding received values.""",
default=False,
)
args = parser.parse_args()
configfile = "twitpachrad.ini"
config = readconfigfile(configfile)
main()