Skip to content

Commit

Permalink
Merge pull request #4 from 425629/master
Browse files Browse the repository at this point in the history
1st pull request
  • Loading branch information
artem-smotrakov authored May 13, 2020
2 parents 1dad986 + 6c4d89b commit 841722f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/google/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Spreadsheet:
def __init__(self):
self._id = ''
self._range = ''
self._url_params = 'insertDataOption=INSERT_ROWS&valueInputOption=RAW'
self._url_params = 'insertDataOption=INSERT_ROWS&valueInputOption=USER_ENTERED'
self._url_template = 'https://sheets.googleapis.com/v4/spreadsheets/%s/values/%s:append?%s'
self._sa = None

Expand Down Expand Up @@ -46,3 +46,4 @@ def append_values(self, values):

print('spreadsheet: response:')
print(response.text)

10 changes: 7 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class WeatherHandler:
def __init__(self, spreadsheet):
self.spreadsheet = spreadsheet

def handle(self, t, h):
print('temperature = %.2f' % t)
def handle(self, c, f, h, now):
f = int(f)
print('centigrade = %.2f' % c)
print('farenheit = %.2f' % f)
print('humidity = %.2f' % h)
spreadsheet.append_values([t, h])
print('now = %s' % now)
spreadsheet.append_values([c, f, h, now])

# required imports
from weather import Weather
Expand Down Expand Up @@ -92,3 +95,4 @@ def handle(self, t, h):
print('achtung! something wrong happened! ignoring ...')

time.sleep(1) # in seconds

20 changes: 17 additions & 3 deletions src/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import util

# the class controls a DHT22 sensor which measures temperature and humidity

class Weather:

# initilizes an instance of Weather with the following parameters
Expand All @@ -14,20 +15,33 @@ def __init__(self, pin, interval, handler = None):
self.last_measurement = time.ticks_ms()
self.dht22 = dht.DHT22(machine.Pin(pin))
self.interval = util.string_to_millis(interval)
self.iterations = 0
self.handler = handler

# mesures temperature and humidity
def measure(self):
self.dht22.measure()
t = self.dht22.temperature()
c = self.dht22.temperature()
h = self.dht22.humidity()
f = (c * 1.8) + 32
now = "=TIMESTAMP_TO_DATE(INDIRECT(\"A\" & ROW()))"
if self.handler != None:
self.handler.handle(t, h)
self.handler.handle(c, f, h, now)

# checks if it's time to measure temperature and humidity
def check(self):
current_time = time.ticks_ms()
deadline = time.ticks_add(self.last_measurement, self.interval)
if time.ticks_diff(deadline, current_time) <= 0:
if ((time.ticks_diff(deadline, current_time) <= 0) or (self.iterations == 0)):
self.measure()
self.iterations += 1
self.last_measurement = current_time

# the following function, when added to the google sheet (Tools > Script editor) allows the
# formula uploaded in the "now" variable (see "measure(self)") to calculate a local timestamp
# from the epoch value loaded in column A of the inserted row
#
#function TIMESTAMP_TO_DATE(value) {
# return new Date(value * 1000);
#}
# see the sheets.py file to set the ValueInputOption to USER_INPUT to avoid now string value being prefixed with a '

0 comments on commit 841722f

Please sign in to comment.