예제 #1
0
def main(config=None, wifi=None, console=None):
    # Ensure messages from Boot.py are visible for at least 1 sec
    time.sleep(5)

    if config is None:
        config = ujson.loads('{"SECRET_SALT": "", "AUTH_SERVER_IP": ""}')

    ip = wifi.ip

    if not ip == config['AUTH_SERVER_IP']:
        console.error(['Invalid', 'Wifi or IP', ip, config['AUTH_SERVER_IP']])
        return

    # Socket listen to my current IP on port 80
    addr = socket.getaddrinfo(ip, 80)[0][-1]
    s = socket.socket()
    s.bind(addr)
    s.listen(1)
    console.log(['Init Auth Server', addr])
    # Ensure messages is displayed for at least 2 sec
    time.sleep(2)

    while True:
        cl, addr = s.accept()

        current_time = Clock.get_time()
        client_ip = addr[0]

        console.log([current_time, 'Request from', 'Client IP', client_ip])
        # TODO to be continued
        if client_ip == config['MAIN_SERVER_IP']:
            # Compare password to the list return True if authorize other wise return False
            cl.send('HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n')
            cl.send('True')
            cl.close()
        else:
            # Add password_hash to the list use the client_ip_hash to map
            cl.send('HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n')
            cl.send('Password added to the list')
            cl.close()
예제 #2
0
led = Pin(2, Pin.OUT)
led.off()

i2c = I2C(-1, Pin(5), Pin(4))
console = Console(i2c)

# Clear Serial Monitor
console.log(['', '', ''])
console.log('Hello from boot!')

console.log(' Init Wifi', console.y)
wifi = Wifi()
wifi.connect(config['SSID'], config['PASSWORD'])
console.log('IP: ' + wifi.ip, console.y)

IS_MAIN_SERVER = config['MAIN_SERVER_IP'] == wifi.ip
console.log('Is main: ' + str(IS_MAIN_SERVER), console.y)

console.log(' Init Clock', console.y)
Clock.fetch_time()
Clock.set_time()
current_time = Clock.get_time()
console.log(current_time, console.y)

bme = BME(i2c)

led.on()

gc.collect()
def main(config=None, wifi=None, console=None, bme=None):
    # Ensure messages from Boot.py are visible for at least 1 sec
    time.sleep(5)

    if config is None:
        config = ujson.loads('{"SECRET_SALT": "", "MAIN_SERVER_IP": ""}')

    ip = wifi.ip

    if not ip == config['MAIN_SERVER_IP']:
        console.error(['Invalid', 'Wifi or IP', ip, config['MAIN_SERVER_IP']])
        return

    # Socket listen to my current IP on port 80
    addr = socket.getaddrinfo(ip, 80)[0][-1]
    s = socket.socket()
    s.bind(addr)
    s.listen(1)
    console.log(['Init Main Server', addr])
    # Ensure messages is displayed for at least 2 sec
    time.sleep(2)

    while True:
        cl, addr = s.accept()

        data = cl.recv(4096)
        data = data.decode('utf-8')
        data = data.split('\n')
        data = data[0]
        password = data[0].split('/')

        # Validate password
        try:
            client_ip = addr[0]
            # client_ip_hash = Hash.encrypt(client_ip)
            # password_hash = Hash.encrypt(password)
            urequests.get('http://' + config['AUTH_SERVER_IP'] + '/' +
                          client_ip)

            # if not response.text == 'True':
            #   cl.send('HTTP/1.0 401 OK\r\nContent-Type: text/plain\r\n\r\n')
            #   cl.send('401 Unauthorized')
            #   cl.close()
            #   continue

        except:
            cl.send('HTTP/1.0 401 OK\r\nContent-Type: text/plain\r\n\r\n')
            cl.send('401 Unauthorized')
            cl.close()
            continue

        current_time = Clock.get_time()
        temperature = bme.temperature
        pressure = bme.pressure
        humidity = bme.humidity
        console.log([
            current_time, 'Temperature', temperature, 'Pressure', pressure,
            'Humidity: ' + humidity
        ])

        response = ujson.dumps({
            'client_ip': client_ip,
            'password': password,
            'current_time': current_time,
            'temperature': temperature,
            'pressure': pressure,
            'humidity': humidity,
        })

        cl.send('HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n')
        cl.send(response)
        cl.close()