Last active
March 13, 2019 14:15
-
-
Save boring-km/1e685c859c7a0e9ccc909ff69a3c7e67 to your computer and use it in GitHub Desktop.
upload_dust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import requests | |
import json | |
import socket | |
import time | |
from requests.adapters import HTTPAdapter | |
from requests.packages.urllib3.util.retry import Retry | |
def execute(): | |
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1.0) | |
ser.close() | |
ser.open() | |
file = open('register.txt', 'r') | |
d = file.readline() | |
serial_num = int(d) | |
file.close() | |
url='https://fountaingradle.run.goorm.io/sense' | |
csv = open("normal2.csv", 'w') | |
csv.write("id,pm1.0,pm2.5,pm10\n") | |
pm1, pm25, pm10 = 0, 0, 0 | |
id = 0 | |
raw = '' | |
try: | |
session = requests.Session() | |
retry = Retry(connect=20, backoff_factor=0.5) | |
adapter = HTTPAdapter(max_retries=retry) | |
session.mount('http://', adapter) | |
session.mount('https://', adapter) | |
while 1: | |
if ser: | |
data = ser.readline() | |
count = len(data) | |
if count > 17: | |
if int(data[11]) != 0 and int(data[10]) != 83 and int(data[12]) != 83 and int(data[14]) != 83: | |
raw = '{0},{1},{2}'.format(int(data[11]) | |
,int(data[13]) | |
,int(data[15])) | |
print(raw) | |
pm1 = int(data[11]) | |
pm25 = int(data[13]) | |
pm10 = int(data[15]) | |
# json object | |
datas = {'pm1':pm1, 'pm25':pm25, 'pm10':pm10, 'serial':serial_num} | |
# json header | |
headers = {'content-type':'application/json'} | |
# http response | |
session.post(url=url,data=json.dumps(datas),headers=headers) | |
#print(response.json()) | |
inputdata = '{0},{1}\n'.format(id,raw) | |
csv.write(inputdata) | |
id += 1 | |
else: | |
# json object | |
datas = {'pm1':pm1, 'pm25':pm25, 'pm10':pm10, 'serial':serial_num} | |
# json header | |
headers = {'content-type':'application/json'} | |
# http response | |
if pm1 != 0: | |
session.post(url=url,data=json.dumps(datas),headers=headers) | |
inputdata = '{0},{1}\n'.format(id,raw) | |
print("null: ", datas) | |
csv.write(inputdata) | |
id += 1 | |
time.sleep(1) | |
except KeyboardInterrupt: | |
ser.close() | |
csv.close() | |
execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment