Base project
This commit is contained in:
111
main.py
Normal file
111
main.py
Normal file
@@ -0,0 +1,111 @@
|
||||
import time
|
||||
import network
|
||||
import socket
|
||||
import ubinascii
|
||||
from machine import Pin, I2C
|
||||
|
||||
import micropython_mcp9808.mcp9808 as mcp9808
|
||||
from secrets import secrets
|
||||
|
||||
GET_PATH_START = 4
|
||||
POST_PATH_START = 5
|
||||
|
||||
led = Pin('LED', Pin.OUT)
|
||||
def blink_onboard_led(num_blinks):
|
||||
for i in range(num_blinks):
|
||||
led.value(1)
|
||||
time.sleep_ms(200)
|
||||
led.value(0)
|
||||
time.sleep_ms(200)
|
||||
blink_onboard_led(1)
|
||||
|
||||
sdaPIN=Pin(20)
|
||||
sclPIN=Pin(21)
|
||||
i2c=I2C(0,sda=sdaPIN, scl=sclPIN, freq=200000)
|
||||
devices = i2c.scan()
|
||||
if len(devices) != 0:
|
||||
print('Number of I2C devices found=',len(devices))
|
||||
for device in devices:
|
||||
print("Device Hexadecimel Address=",hex(device))
|
||||
else:
|
||||
print("No device found")
|
||||
|
||||
mcp9808 = mcp9808.MCP9808(i2c)
|
||||
|
||||
|
||||
ssid = secrets['ssid']
|
||||
password = secrets['pw']
|
||||
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
wlan.connect(ssid, password)
|
||||
|
||||
max_wait = 10
|
||||
while max_wait > 0:
|
||||
if wlan.status() < 0 or wlan.status() >= 3:
|
||||
break
|
||||
max_wait -= 1
|
||||
print('waiting for connection...')
|
||||
time.sleep(1)
|
||||
|
||||
ip = "???"
|
||||
if wlan.status() != 3:
|
||||
blink_onboard_led(3)
|
||||
raise RuntimeError('network connection failed')
|
||||
else:
|
||||
print('connected')
|
||||
status = wlan.ifconfig()
|
||||
ip = status[0]
|
||||
print( 'ip = ' + ip )
|
||||
|
||||
wlan_mac = wlan.config('mac')
|
||||
mac_readable = ubinascii.hexlify(wlan_mac).decode()
|
||||
|
||||
blink_onboard_led(2)
|
||||
|
||||
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
|
||||
|
||||
s = socket.socket()
|
||||
s.bind(addr)
|
||||
s.listen(1)
|
||||
|
||||
print('listening on', addr)
|
||||
|
||||
def result_ok(cl, response = None, content_type = "text/plain"):
|
||||
cl.send('HTTP/1.0 200 OK\r\nContent-type: ' + content_type + '\r\n\r\n')
|
||||
cl.send(response if response is not None else "Ok")
|
||||
cl.close()
|
||||
|
||||
def result_notfound(cl, response = None):
|
||||
cl.send('HTTP/1.0 404 NotFound\r\nContent-type: text/plain\r\n\r\n')
|
||||
cl.send(response if response is not None else "Not Found")
|
||||
cl.close()
|
||||
|
||||
while True:
|
||||
|
||||
cl, addr = s.accept()
|
||||
try:
|
||||
print('client connected from', addr)
|
||||
request = cl.recv(1024)
|
||||
|
||||
request = str(request)
|
||||
request = request[2:-1] # remove b' and ' from string
|
||||
print(request)
|
||||
|
||||
if request.find('/ ') == GET_PATH_START:
|
||||
temp = str(mcp9808.temperature)
|
||||
result_ok(cl, "{ \"temperature\": \"" + temp + "\" }", 'application/json')
|
||||
|
||||
elif request.find('/prometheus') == GET_PATH_START:
|
||||
temp = str(mcp9808.temperature)
|
||||
content = """# HELP temperature Temperature in Celsius
|
||||
# TYPE temperature gauge
|
||||
temperature{mac=\"""" + mac_readable + "\",ip=\""+ ip +"\"} " + temp
|
||||
result_ok(cl, content)
|
||||
else:
|
||||
result_notfound(cl)
|
||||
|
||||
|
||||
except OSError as e:
|
||||
cl.close()
|
||||
print('connection closed')
|
||||
Reference in New Issue
Block a user