# wlan.connect(wlan_id, wlan_pass) print("wlan.ifconfig():", wlan.ifconfig()) print("wlan.isconnected():", wlan.isconnected()) pin = Pin(PIN_NUMBER, Pin.OUT, value=0) def show_index_page(request): server.send("THIS IS INDEX PAGE") def show_info_page(request): server.send("THIS IS INFO PAGE") def show_pin_page(request): if request.startswith("GET /on HTTP"): pin.value(1) elif request.startswith("GET /off HTTP"): pin.value(0) server.send("PIN IS " + ("ON" if pin.value() == 1 else "OFF")) server = MicroPyServer() server.add_route("/info", show_info_page) server.add_route("/", show_index_page) server.add_route("/on", show_pin_page) server.add_route("/off", show_pin_page) server.start()
pin.value(1) server.send(" LED ON!") def do_off2(request): pin.value(0) server.send("LED OFF!") pin = Pin(2, Pin.OUT) def show_message(request): ''' request handler ''' server.send("HELLO WORLD!") def show_message2(request): ''' request handler ''' server.send("HELLO S***N!") server = MicroPyServer() ''' add request handler ''' server.add_route("/", show_message) server.add_route("/s***n", show_message2) server.add_route("/pin2on", do_on2) server.add_route("/pin2off", do_off2) server.add_route("/data", show_data) ''' start server ''' server.start()
Example 2 Needed ESP8266 or ESP32 board @see https://github.com/troublegum/micropyserver """ import esp import network from micropyserver import MicroPyServer wlan_id = "your wi-fi" wlan_pass = "******" wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): wlan.connect(wlan_id, wlan_pass) def show_index_page(request): html_file = open("index.html") html = html_file.read() html_file.close() server.send(html, content_type="Content-Type: text/html") server = MicroPyServer() server.add_route("/", show_index_page) server.start()
def temp(request): server.send(bme.temperature) def show_message(request): ''' request handler ''' server.send("HELLO WORLD!") def show_message2(request): ''' request handler ''' server.send("HELLO S***N!") def show_message3(request): ''' request handler ''' server.send( "HELLO YOULIA! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!!" ) server = MicroPyServer() ''' add request handler ''' server.add_route("/", show_message) server.add_route("/s***n", show_message2) server.add_route("/yla", show_message3) server.add_route("/pin2on", do_on2) server.add_route("/pin2off", do_off2) server.add_route("/temp", temp) ''' start server ''' server.start()
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;}
if wlan.isconnected() == False: wlan.connect(wlan_id, wlan_pass) while wlan.isconnected() == False: sleep(1) print('Device IP:', wlan.ifconfig()[0]) def show_message(request): ''' request handler ''' server.send("HELLO WORLD!") def show_message2(request): ''' request handler ''' server.send("HELLO S***N!") def show_message3(request): ''' request handler ''' server.send( "HELLO YOULIA! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!! I LOVE YOU!!!" ) server = MicroPyServer() ''' add request handler ''' server.add_route("/", show_message) server.add_route("/s***n", show_message2) server.add_route("/yla", show_message3) ''' start server ''' server.start()
# it's not pressed. This is because the pull-up resistor keeps the pin at # a high level when it's not connected to ground through the button. # When the button is pressed then the input pin connects to ground # and reads a low level (value returns 0). if not button.value(): # Remember that the internal led turn on # when the pin is LOW led.value(0) else: led.value(1) result['count'] += 1 data = json.dumps(result) server.send(data, content_type="Content-Type: application/json") print(result) sleep(3) button = Pin(12, Pin.IN, Pin.PULL_UP) led = Pin(13, Pin.OUT) d = dht.DHT22(Pin(14)) """ init server """ server = MicroPyServer() server.add_route("/", show_index_page) server.add_route("/data", show_data) server.add_route("/on", do_on) server.add_route("/off", do_off) server.add_route("/c", counter) """ start server """ server.start()
from micropyserver import MicroPyServer srv = MicroPyServer() from machine import Pin, PWM def write_set(sets): f = open('rgb', 'w') f.write(' '.join(sets)) f.close() def read_set(): f = open('rgb') sb = f.read() f.close() return sb.split(' ') def set_rgb(sets): write_set(sets) pwm_red.duty(int(sets[0])) pwm_green.duty(int(sets[1])) pwm_blue.duty(int(sets[2])) def show_index(request): html_file = open("index.html") html = html_file.read() html_file.close() srv.send(html, content_type="Content-Type: text/html")