Many of you are familiar with the announcement of the Electric Imp, which was not so long ago on Habré , moreover , first impressions of its use are beginning to appear . Since this device seemed to me promising and quite interesting, when I first went on sale the developer edition I ordered myself to play a bit and appreciate the possibilities.





#!/usr/bin/env python import webapp2 import json import logging import utils import time import os import datetime from google.appengine.ext.webapp import template from google.appengine.ext import db class Sensor(db.Model): temperature = db.FloatProperty(required = True) battery = db.FloatProperty(required = True) added = db.DateTimeProperty(auto_now_add = True, indexed=True) class SensorRequestHandler(webapp2.RequestHandler): def post(self): data = json.loads(self.request.body) params = json.loads(data['value']) temp = params['temp'] battery = params['battery'] sensor = Sensor(temperature = temp, battery = battery) sensor.put() self.response.out.write('OK') def get(self): sensors_data = Sensor.all().order('added').fetch(None) temperature_data = [] battery_data = [] for item in sensors_data: temperature_data.append([int(time.mktime(item.added.timetuple()))*1000 ,round(item.temperature, 1)]) battery_data.append([int(time.mktime(item.added.timetuple()))*1000, round(item.battery, 2)]) path = os.path.join(os.path.dirname(__file__), 'templates/charts.html') self.response.out.write(template.render(path, { 'temperature_data' : utils.GqlEncoder().encode(temperature_data), 'battery_data' : utils.GqlEncoder().encode(battery_data) })) class LastRequestHandler(webapp2.RequestHandler): def get(self): ordered_list = db.GqlQuery('select * from Sensor order by added desc limit 1') last = ordered_list.get() self.response.headers['Content-Type'] = 'application/json' self.response.out.write(utils.GqlEncoder().encode(last)) class CleanRequestHandler(webapp2.RequestHandler): def get(self, bulk = 'old'): logging.debug("bulk: %s", bulk) try: while True: q = Sensor.all() if bulk != 'all': q.filter('added <', datetime.date.today() - datetime.timedelta(days=60)) assert q.count() db.delete(q.fetch(200)) time.sleep(0.5) except Exception, e: self.response.out.write(repr(e)+'\n') pass app = webapp2.WSGIApplication([ ('/sensor', SensorRequestHandler), ('/sensor/last', LastRequestHandler), ('/sensor/clean/?(all)?', CleanRequestHandler) ], debug=True) class Termistor { pin_num = null; constructor(pin){ pin_num = pin hardware["pin" + pin_num].configure(ANALOG_IN); } function read(){ return hardware["pin" + pin_num].read(); } function getTemperature(){ local temp = math.log(((655350000/read()) - 10000)); temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp ))* temp ); temp = temp - 273.15; return temp; } } local sensor = Termistor(9); local output = OutputPort("Temperature", "string"); imp.configure("Termistor 10K", [], [output]); function capture(){ imp.wakeup(600.0, capture); local temp = sensor.getTemperature(); local jsonOut = "{\"temp\":"+temp+", \"battery\":"+hardware.voltage()+"}"; output.set(jsonOut); server.show(format("%1.1fºC", temp)); } capture(); imp.sleep(2.0) server.sleepfor(600.0) 

Source: https://habr.com/ru/post/164105/
All Articles