Skip to content
Snippets Groups Projects
Commit ec2c9e44 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

wip

parent 22e20341
No related branches found
No related tags found
No related merge requests found
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="mqtt.min.js"></script>
<script>
//
// hello.MQTT-blink.C3.html
// Seeed XIAO ESP32C3 MQTT blink hello-world
//
// uses https://github.com/mqttjs
//
// Neil Gershenfeld 11/17/23
//
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose, but must
// acknowledge this project. Copyright is retained and
// must be preserved. The work is provided as is; no
// warranty is provided, and users accept all liability.
//
var client
//
function message(msg) {
document.getElementById("message").innerHTML = msg
}
//
function connect() {
const address = document.getElementById("address").value
const port = document.getElementById("port").value
const host = "ws://"+address+":"+port+"/mqtt"
const options = {clientId:"hello.MQTT-blink.C3.html"}
message('connecting client')
client = mqtt.connect(host,options)
client.on('connect',() => {
message('connected to '+host)
client.subscribe('button')
})
client.on('message',(topic,msg,packet) => {
message('received message: '+msg.toString()+', on topic: '+topic)
})
}
//
function disconnect() {
client.end()
message('client disconnected')
}
//
function LEDon() {
client.publish('LED','1')
message('transit message: 1, on topic: LED')
}
//
function LEDoff() {
client.publish('LED','0')
message('transit message: 0, on topic: LED')
}
</script>
</head>
<body style="margin-left:2%">
<br>
MQTT broker WebSocket address: <input type="text" id="address" value="192.168.1.146" onchange="myFunction()"> &nbsp;port: <input type="text" id="port" value="9001" onchange="myFunction()">
<br><br>
<button onclick="connect()" style="font-size:125%">Connect</button>&nbsp;
<button onclick="disconnect()" style="font-size:125%">Disconnect</button>&nbsp;
<button onclick="LEDon()" style="font-size:125%">turn LED on</button>&nbsp;
<button onclick="LEDoff()" style="font-size:125%">turn LED off</button>&nbsp;
<br><br>
<div id='message'></div>
</body>
</html>
#
# hello.MQTT-blink.C3.py
# Seeed XIAO ESP32C3 MQTT blink hello-world
#
# Neil Gershenfeld 11/17/23
#
# This work may be reproduced, modified, distributed,
# performed, and displayed for any purpose, but must
# acknowledge this project. Copyright is retained and
# must be preserved. The work is provided as is; no
# warranty is provided, and users accept all liability.
#
# load MicroPython:
# https://micropython.org/download/ESP32_GENERIC_C3/
# esptool.py --chip esp32c3 --port /dev/ttyACM0 erase_flash
# esptool.py --chip esp32c3 --port /dev/ttyACM0 --baud 460800 write_flash -z 0x0 ESP32_GENERIC_C3-20231005-v1.21.0.bin
#
import network
from umqtt.simple import MQTTClient
from machine import Pin
#
# MQTT server address and port
#
# server = your MQTT server IP address
# port = your MQTT server IP port
server = "192.168.1.146"
port = 1883
#
# set up pins
#
led_pin = 20
button_pin = 21
led = Pin(led_pin,Pin.OUT)
led.value(0)
button = Pin(button_pin,Pin.IN,Pin.PULL_UP)
button_up = True
#
# connect to Wifi access point
#
print("connect to access point ...")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
while (wifi.active() == False):
pass
# wifi.connect("ssid","key")
wifi.connect("squeaky","10101010")
while (wifi.isconnected() == False):
pass
print("connected to access point at ",wifi.ifconfig())
#
# MQTT message handler
#
def handler(topic,message):
print(f"received message {message} on topic {topic}")
if (message == b"1"):
led.value(1)
else:
led.value(0)
#
# connect to MQTT broker
#
print("connect to MQTT broker ...")
client = MQTTClient("hello.MQTT-blink.C3.py",server,port)
client.set_callback(handler)
client.connect()
print(f"connected to MQTT broker at {server}:{port}")
#
# subscribe to the LED topic
#
client.subscribe("LED")
#
# start main loop
#
while True:
#
# check for a message
#
client.check_msg()
#
# publish button changes to the button topic
#
if ((button.value() == 0) and button_up):
led.value(1)
print("publish button down")
client.publish("button","button down")
button_up = False
elif ((button.value() == 1) and (not button_up)):
led.value(0)
print("publish button up")
client.publish("button","button up")
button_up = True
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment