Exemple #1
0
    def start(self):
        """ Start server """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self._host, self._port))
        sock.listen(1)
        t = 0
        while True:

            # Connect to wifi
            if time.time() > t + 30:
                Connect.check_and_reconnect()
                t = time.time()
                print(t)

            try:
                self._connect, address = sock.accept()
                request = self._get_request()
                if len(request) == 0:
                    self._connect.close()
                    continue
                if self._on_request_handler:
                    if not self._on_request_handler(request, address):
                        continue
                route = self.find_route(request)
                if route:
                    route["handler"](request)
                else:
                    self.not_found()
            except Exception as e:
                self.internal_error(e)
            finally:
                self._connect.close()
Exemple #2
0
import ubinascii
import time
import machine
import json
from connect import Connect
from led_pwm import LedPwm
from lcd import Lcd
from temperature_dht11 import TemperatureSensor as Dht
from mqtt import Mqtt
dht = Dht(4)
led = LedPwm(2)
lcd = Lcd(spi=1, cs=2, dc=15, rst=0, bl=12)
config = json.loads(open('config.json').read())
client_id = ubinascii.hexlify(machine.unique_id())
try:
    mqtt = Mqtt(client_id, config['broker_ip'])
except:
    mqtt = False
while True:
    # Connect to wifi
    Connect.check_and_reconnect()
    try:
        data = {
            'temperature': dht.read_temp_and_hum()[0],
            'humidity': dht.read_temp_and_hum()[1]
        }
        print(str(time.localtime()) + ' ' + str(data))
        if not mqtt:
            try:
                mqtt = Mqtt(client_id, config['broker_ip'])
Exemple #3
0
import ubinascii
import json
from micropyserver import MicroPyServer
from machine import unique_id, Pin, I2C
from my_lib import bme280
from connect import Connect
from led_pwm import LedPwm
led = LedPwm(2)
i2c = I2C(scl=Pin(5), sda=Pin(4))
config = json.loads(open('config.json').read())
client_id = ubinascii.hexlify(unique_id())
# Connect to wifi
Connect.check_and_reconnect()
server = MicroPyServer()
''' add request handler '''

def show_message(request):
    """ request handler """
    led.blink_duty(2, 0.1, 1010)
    bme = bme280.BME280(i2c=i2c, address=0x76)
    temp = bme.values[0]
    hum = bme.values[2]
    pres = bme.values[1]
    html = """<html><head> <title>Temp, Hum & Pres</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="data:,"> 
    <style>
    html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
    h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}