From b00dd1601e12132ab2d8616c5725137ea9ddc661 Mon Sep 17 00:00:00 2001 From: Jauhar Arifin <jauhararifin10@gmail.com> Date: Sun, 26 Nov 2017 17:37:19 +0700 Subject: [PATCH] update earthquake sender python --- earthquake/earthquake.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/earthquake/earthquake.py b/earthquake/earthquake.py index 4028cc3..bb2a4ba 100644 --- a/earthquake/earthquake.py +++ b/earthquake/earthquake.py @@ -3,6 +3,8 @@ import serial import requests import time +import sys +import re if len(sys.argv) < 2: exit("Usage: earthquake.py [device]") @@ -11,17 +13,20 @@ ser = serial.Serial(sys.argv[1], 115600) last = -1000000 while True: - line = ser.readline() - if line.startswith("gempa"): - strength = float(line.split(' ')[1]) - if time.time() - last >= 36000: + line = ser.readline().decode("utf-8")[:-2] + print(line) + if re.match("[0-9\\.]+", line): + strength = float(line) + if strength > 1.3 and strength < 10 and time.time() - last >= 36000: data = { "lat": -6.892004, "long": 107.599828, "strength": strength, - "date": "2017-12-06 PM 22:03:36.000Z" + "date": time.strftime("%Y-%m-%d %p %H:%M:%S.000Z") } - request.post('jauhararifin.cf:8888/api/earchquake', data) - last = time.time() \ No newline at end of file + print("send requests ", data) + resp = requests.post('http://jauhararifin.cf:8888/api/earthquakes', data) + print(resp) + last = time.time() -- GitLab