diff --git a/src/google/sheet.py b/src/google/sheet.py index 0beaab5..b0c871d 100644 --- a/src/google/sheet.py +++ b/src/google/sheet.py @@ -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 @@ -46,3 +46,4 @@ def append_values(self, values): print('spreadsheet: response:') print(response.text) + diff --git a/src/main.py b/src/main.py index 3d54414..230abcd 100644 --- a/src/main.py +++ b/src/main.py @@ -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 @@ -92,3 +95,4 @@ def handle(self, t, h): print('achtung! something wrong happened! ignoring ...') time.sleep(1) # in seconds + diff --git a/src/weather.py b/src/weather.py index cb596ef..ed2ec22 100644 --- a/src/weather.py +++ b/src/weather.py @@ -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 @@ -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 '