Article Index

Page 257

import network
from machine import Pin, Timer
from time import sleep_ms
def setup(country, ssid, key):
    network.country(country)
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    wifi.disconnect()
    LED = Pin(2, Pin.OUT)
    LED.on()
    timeout = 20000
    wifi.connect(ssid,key)
    timer = Timer(1)
    timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))    
    s = network.STAT_IDLE
    while timeout>0:
        print("connecting",s)
        s = wifi.status()
        if s != network.STAT_CONNECTING:
            break
        sleep_ms(100)
        timeout = timeout-100    
    if(s==network.STAT_GOT_IP):
         timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))        
    else:
        timer.deinit()
        LED.on()
    return wifi

Page 261

from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO


class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def sendResponse(self, cmd):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b'This is a '+bytes(cmd, 'utf-8')+ b' request. ')
        response.write(b'Received: ')
        response.write(body)
        self.wfile.write(response.getvalue())

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b'Hello, world!')


    def do_HEAD(self):
        self.send_response(200)
        self.end_headers()

   
    def do_POST(self):
        self.sendResponse("POST")

    def do_PUT(self):
        self.sendResponse("PUT")

    def do_DELETE(self):
        self.sendResponse("DELETE")

    def do_PATCH(self):
        self.sendResponse("PATCH")


httpd = HTTPServer(('', 8080), SimpleHTTPRequestHandler)
httpd.serve_forever()

Page 264

import network
from machine import Pin, Timer
from time import sleep_ms
import urequests

def setup(country, ssid, key):
    network.country(country)
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    wifi.disconnect()
    LED = Pin(2, Pin.OUT)
    LED.on()
    timeout = 20000
    wifi.connect(ssid,key)
    timer = Timer(1)
    timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))    
    s = network.STAT_IDLE
    while timeout>0:
        print("connecting",s)
        s = wifi.status()
        if s != network.STAT_CONNECTING:
            break
        sleep_ms(100)
        timeout = timeout-100    
    if(s==network.STAT_GOT_IP):
         timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))        
    else:
        timer.deinit()
        LED.on()
    return wifi
   
wifi=setup(country, ssid, key)
print("Connected")
r = urequests.get("https://www.example.com")
print(r.content)
r.close()

Page 265

   
import network
from machine import Pin, Timer
from time import sleep_ms
import urequests

def setup(country, ssid, key):
    network.country(country)
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    wifi.disconnect()
    LED = Pin(2, Pin.OUT)
    LED.on()
    timeout = 20000
    wifi.connect(ssid,key)
    timer = Timer(1)
    timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))    
    s = network.STAT_IDLE
    while timeout>0:
        print("connecting",s)
        s = wifi.status()
        if s != network.STAT_CONNECTING:
            break
        sleep_ms(100)
        timeout = timeout-100    
    if(s==network.STAT_GOT_IP):
         timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))        
    else:
        timer.deinit()
        LED.on()
    return wifi


wifi=setup(country, ssid, key)
print("Connected")

url="http://192.168.253.72:8080"
r = urequests.get(url)
print(r.content)
r.close()

buf= b'Hello World'
r=urequests.post(url,data=buf)
print(r.content)
r.close()

r=urequests.put(url,data=buf)
print(r.content)
r.close()

r=urequests.patch(url,data=buf)
print(r.content)
r.close()

r=urequests.head(url)
print(r.content)
print(r.headers)
r.close()

r=urequests.delete(url,data=buf)
print(r.content)
r.close()

Page 266

import network
from machine import Pin, Timer
from time import sleep_ms
import urequests
import onewire
import ds18x20

def setup(country, ssid, key):
    network.country(country)
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    wifi.disconnect()
    LED = Pin(2, Pin.OUT)
    LED.on()
    timeout = 20000
    wifi.connect(ssid,key)
    timer = Timer(1)
    timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))    
    s = network.STAT_IDLE
    while timeout>0:
        print("connecting",s)
        s = wifi.status()
        if s != network.STAT_CONNECTING:
            break
        sleep_ms(100)
        timeout = timeout-100    
    if(s==network.STAT_GOT_IP):
         timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))        
    else:
        timer.deinit()
        LED.on()
    return wifi

wifi=setup(country, ssid, key)
print("Connected")
url = "http://192.168.253.72:8080"
ow = onewire.OneWire(Pin(22))
presence = ow.reset()
if presence:
    print("Device present")
else:
    print("No device")

DS = ds18x20.DS18X20(ow)
roms = DS.scan()

while True:
    DS.convert_temp()
    temp = DS.read_temp(roms[0])
    buf = str(temp).encode("utf-8")
    try:
        r = urequests.put(url, data=buf)
        r.close()
    except:
        print("Server Not Online")
    sleep_ms(500)

Page 267

from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
     
    def log_message(self,*args, **kwargs):
        pass

    def do_PUT(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        bodyString= body.decode(encoding="utf-8")
        temp=float(bodyString)
        print(temp)
        self.send_response(200)
        self.end_headers()

httpd = HTTPServer(('', 8080), SimpleHTTPRequestHandler)
httpd.serve_forever()