From 700cf1cfdc345c88df4a37249565c0f535be8aac Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:22:57 +0000 Subject: [PATCH 01/13] Add pusher lib --- Dockerfile.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.template b/Dockerfile.template index 12f2978..66fddbb 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -18,7 +18,7 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian nano RUN sudo easy_install3 pip -RUN pip install pyserial +RUN pip install pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git # Set our working directory From f5518991521c1318aa7e105d694c9ebf4b369ada Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:32:20 +0000 Subject: [PATCH 02/13] Remove SQLite --- main.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/main.py b/main.py index f8a1468..8481670 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ import json #To parse response import sqlite3 #Database + os.environ['TZ'] = 'Europe/London' #SetTimezone def log(message): print(message) @@ -27,8 +28,6 @@ def reboot(): time.sleep(60) # Just in case that api call fails AGAIN as it sometimes does sys.exit() #This forces a container restart anyway -sqliteconn = sqlite3.connect("/data/weatherdatabase.sqlite3") - serialport = os.environ.get('serialPort', '/dev/ttyUSB0') baudrate = os.environ.get('baudRate', 19200) #Set the Baudrate to 19200 which is a nice default for the davis logger try: @@ -116,12 +115,6 @@ def looprequest(): errorcount = errorcount + 1 previousworkingresponse = thisresponse return data -def storefailedrequest(data): #Cache all the requests that didn't work - global sqliteconn - cursor = sqliteconn.cursor() - cursor.execute('INSERT INTO `weatherData`(`id`,`timestamp`,`windSpeedMPH`,`windSpeed10MinAverageMPH`,`windDirection`,`temperatureC`,`humidity`,`barometer`) VALUES (NULL,' + str(data["timestamp"]) + ',' + str(data["windSpeed"]) + ',' + str(data["wind10MinAverage"]) + ',' + str(data["windDirection"]) + ',' + str(data["temperatureC"]) + ',' + str(data["humidity"]) + ',' + str(data["barometer"]) + ');') - cursor.commit() - cursor.close() while True: data = looprequest() @@ -134,10 +127,8 @@ def storefailedrequest(data): #Cache all the requests that didn't work if requestParsedResponse["success"] != True: print(response) log("[ERROR] Couldn't upload the data online - server rejected with " + str(requestParsedResponse["message"])) - #storefailedrequest(data) except Exception as e: log("[ERROR] Couldn't upload data online " + str(e)) - #storefailedrequest(data) if errorcount > 5: #If it's hit an error more than 5 times just reboot it reboot() From ebad354aea44d1432e13b177f34d1fd3b4ea09aa Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:40:19 +0000 Subject: [PATCH 03/13] Remove IDE files --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7c215cd..bd1a807 100644 --- a/.gitignore +++ b/.gitignore @@ -46,4 +46,6 @@ atlassian-ide-plugin.xml com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties -fabric.properties \ No newline at end of file +fabric.properties + +.idea \ No newline at end of file From b6925612863a224ace283183675b3a000a7bc94a Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:40:30 +0000 Subject: [PATCH 04/13] Add first basic support for Pusher --- main.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 8481670..3a15084 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ import urllib.request #For internet import json #To parse response import sqlite3 #Database - +import pusher #Pusher.com os.environ['TZ'] = 'Europe/London' #SetTimezone def log(message): @@ -60,6 +60,17 @@ def reboot(): log("[INFO] Quitting") ser.readline() #Read the /r character that follows but ignore it +log("[INFO] Connecting to Pusher") +pusher_client = pusher.Pusher( + app_id=os.environ.get('PUSHERappid'), + key=os.environ.get('PUSHERkey'), + secret=os.environ.get('PUSHERsecret'), + cluster='eu', + ssl=True +) +log("[INFO] Connected to Pusher") + + log("[INFO] Ready to start getting data") previousworkingresponse = "" #Global var errorcount = 0 @@ -115,23 +126,31 @@ def looprequest(): errorcount = errorcount + 1 previousworkingresponse = thisresponse return data +lastSentToServerTime = time.time() while True: data = looprequest() if data: + if time.time()-lastSentToServerTime > os.environ.get('serverSendFrequency', 60): #Send the server a reading every minute + try: + requestPayload = urllib.parse.urlencode(data).encode("utf-8") + requestResponse = urllib.request.urlopen(os.environ.get('uploadUrl', ''), requestPayload) + response = requestResponse.read().decode('utf-8') + requestParsedResponse = json.loads(response) + if requestParsedResponse["success"] != True: + print(response) + log("[ERROR] Couldn't upload the data online - server rejected with " + str(requestParsedResponse["message"])) + except Exception as e: + log("[ERROR] Couldn't upload data online " + str(e)) try: - requestPayload = urllib.parse.urlencode(data).encode("utf-8") - requestResponse = urllib.request.urlopen(os.environ.get('uploadUrl', ''), requestPayload) - response = requestResponse.read().decode('utf-8') - requestParsedResponse = json.loads(response) - if requestParsedResponse["success"] != True: - print(response) - log("[ERROR] Couldn't upload the data online - server rejected with " + str(requestParsedResponse["message"])) + pusher_client.trigger('PSCWeatherDataLive', 'PSCWeatherDataLiveNEWReading', {'message': {'reading': data}}) + log("[SUCCESS] Sent Data to Pusher.com") except Exception as e: - log("[ERROR] Couldn't upload data online " + str(e)) + log("[ERROR] Couldn't upload data to Pusher " + str(e)) + if errorcount > 5: #If it's hit an error more than 5 times just reboot it reboot() - time.sleep(2) + time.sleep(1) #Only take a reading every second log("[INFO] End of Program") \ No newline at end of file From 8c379b9f3ad2b227b9bf773608aaa7ef990b8f7f Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:43:10 +0000 Subject: [PATCH 05/13] Aim to resolve RuntimeError: cryptography requires setuptools 18.5 or newer, please upgrade to a newer version of setuptools error --- Dockerfile.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.template b/Dockerfile.template index 66fddbb..d2c440d 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -18,7 +18,7 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian nano RUN sudo easy_install3 pip -RUN pip install pyserial pusher +RUN pip install --upgrade pip setuptools pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git # Set our working directory From 2c9de45ebce4113fb082d8c8dd6c01f68e6d9f02 Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:45:46 +0000 Subject: [PATCH 06/13] Try alternative pip install --- Dockerfile.template | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile.template b/Dockerfile.template index d2c440d..ef3fc83 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -11,14 +11,13 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian python3-dev \ python3-pip \ python3-tk \ - python3-setuptools \ + python3-pip \ libfreetype6-dev \ rpl \ git \ nano -RUN sudo easy_install3 pip -RUN pip install --upgrade pip setuptools pyserial pusher +RUN pip install setuptools pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git # Set our working directory From e97540544544136da4a990b1a922e799873bb6f2 Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:48:32 +0000 Subject: [PATCH 07/13] Use Pip3 --- Dockerfile.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.template b/Dockerfile.template index ef3fc83..a79ec8e 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -17,7 +17,7 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian git \ nano -RUN pip install setuptools pyserial pusher +RUN pip3 install setuptools pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git # Set our working directory From 722f2a892616138d99381e8576bc4bd2b8cdf8be Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:49:20 +0000 Subject: [PATCH 08/13] Remove SQlite references --- Dockerfile.template | 2 +- start.sh | 4 ---- weatherdatabase.sqlite3 | Bin 5120 -> 0 bytes 3 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 start.sh delete mode 100644 weatherdatabase.sqlite3 diff --git a/Dockerfile.template b/Dockerfile.template index a79ec8e..b24b4c2 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -34,4 +34,4 @@ COPY . ./ RUN apt-get clean && rm -rf /var/lib/apt/lists/* # main.py will run when container starts up on the device -CMD ["bash","/usr/src/app/start.sh"] +CMD ["python3","/usr/src/app/main.py"] diff --git a/start.sh b/start.sh deleted file mode 100644 index cacd5a6..0000000 --- a/start.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -cp -n weatherdatabase.sqlite3 /data/weatherdatabase.sqlite3 #Copy a blank db if it doesn't exist - -python3 /usr/src/app/main.py \ No newline at end of file diff --git a/weatherdatabase.sqlite3 b/weatherdatabase.sqlite3 deleted file mode 100644 index e91c3c8a9f96e6ff1253539c7fe5793889483877..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5120 zcmeH}&riZI6vw+CppbCp+Jp-POa#N-35zbmfUzAoL4l_i@f?LdGlyT z1jb^FN0ap>+urNz_htRGFKNHmq73kzAGn0!3#1}RLYEjL#9f3^{Mnhpop5Yt($4wi ztD>`yx192cR9Qi?qr5Ax+!6Ml2z1qSzFd|DjEv{7TF&`-7_MNs25%A%Qw>YkY#rO$ zRZGXQxkZn-P~pnbb4@zQ7Yfo#d0TS>gv}sm5=J7fd{k=`$&7F@n;FsRC}Wtmeyv;B z?AX|JS}okQjJ9SC@U1?;nqzkilUHf$ri~rb=s9|Y3mJ7`#fZB&=nu&a^``v=z_i`H z+2=#mK2beyQ%`$ Date: Sat, 24 Mar 2018 20:55:53 +0000 Subject: [PATCH 09/13] Another attempt --- Dockerfile.template | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile.template b/Dockerfile.template index b24b4c2..2cfb65a 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -17,7 +17,8 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian git \ nano -RUN pip3 install setuptools pyserial pusher +RUN pip3 install -U setuptools +RUN pip3 install pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git # Set our working directory From 50b0dcfea4753347fd93db39101bc718b2c63cd7 Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 20:58:21 +0000 Subject: [PATCH 10/13] Another attempt with setuptools --- Dockerfile.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.template b/Dockerfile.template index 2cfb65a..41b8c51 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -17,7 +17,7 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian git \ nano -RUN pip3 install -U setuptools +RUN pip3 install 'setuptools>=39.0.1' --force-reinstall RUN pip3 install pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git From c99bfec14b623140fbe46755a6f12955dfe8255b Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 21:00:13 +0000 Subject: [PATCH 11/13] Add cffi dependency --- Dockerfile.template | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile.template b/Dockerfile.template index 41b8c51..24dacd5 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -12,12 +12,13 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian python3-pip \ python3-tk \ python3-pip \ + python3-cffi \ libfreetype6-dev \ rpl \ git \ nano -RUN pip3 install 'setuptools>=39.0.1' --force-reinstall +RUN pip3 install -U setuptools RUN pip3 install pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git From cf42b357794c5c45852a4bb7b24abea1b8025810 Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 21:03:33 +0000 Subject: [PATCH 12/13] Install some other dependencies in the hope they work --- Dockerfile.template | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile.template b/Dockerfile.template index 24dacd5..e6242a6 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -19,6 +19,8 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian nano RUN pip3 install -U setuptools +RUN pip3 install cffi +RUN pip3 install cairocffi RUN pip3 install pyserial pusher #pip also install - git+https://github.com/resin-io/resin-sdk-python.git From 1e08b1678726059a7d20a40a2b719ab8d79415c0 Mon Sep 17 00:00:00 2001 From: James Bithell Date: Sat, 24 Mar 2018 21:04:21 +0000 Subject: [PATCH 13/13] Add more potential dependencies --- Dockerfile.template | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile.template b/Dockerfile.template index e6242a6..05f6d96 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -13,6 +13,8 @@ FROM resin/%%RESIN_MACHINE_NAME%%-debian python3-tk \ python3-pip \ python3-cffi \ + libssl-dev \ + libffi-dev \ libfreetype6-dev \ rpl \ git \