Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
################################################################################
# MQTT at mosquitto.org
#
# Created: 2015-10-15 21:37:25.886276
#
################################################################################
import streams
from mqtt import mqtt
from wireless import wifi
from espressif.esp32net import esp32wifi as wifi_driver
import ssl
# the wifi module needs a networking driver to be loaded
# in order to control the device hardware.
# FOR THIS EXAMPLE TO WORK, A NETWORK DRIVER MUST BE SELECTED BELOW
# uncomment one of the following lines depending on used board
# (e.g. Particle Photon, esp8266 based board, esp32 based board)
# from broadcom.bcm43362 import bcm43362 as wifi_driver
# from espressif.esp32net import esp32wifi as wifi_driver
# from espressif.esp8266wifi import esp8266wifi as wifi_driver
# init the wifi driver!
# The driver automatically registers itself to the wifi interface
# with the correct configuration for the selected device
wifi_driver.auto_init()
streams.serial()
cacert = '-----BEGIN CERTIFICATE-----\n\
MIICITCCAYoCCQCdNIhDS4EYbTANBgkqhkiG9w0BAQsFADBVMQswCQYDVQQGEwJC\n\
RzEKMAgGA1UECAwBZDEKMAgGA1UEBwwBZDEKMAgGA1UECgwBZDEKMAgGA1UECwwB\n\
ZDEWMBQGA1UEAwwNMTkyLjE2OC4wLjEwMjAeFw0xOTEwMTAxNDA0NDZaFw0xOTEx\n\
MDkxNDA0NDZaMFUxCzAJBgNVBAYTAkJHMQowCAYDVQQIDAFkMQowCAYDVQQHDAFk\n\
MQowCAYDVQQKDAFkMQowCAYDVQQLDAFkMRYwFAYDVQQDDA0xOTIuMTY4LjAuMTAy\n\
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7NuQY+rg/7alGZX5P2cTmSdxE\n\
hT//pwgA65mMDG6sL/Sele0mGwfY+YkfBSHlpGDbDA2+XOeWczXIfTQtSL3VvjJr\n\
y1qjgsmPdNKFxIomwJRKHdSMX4KQoMnlRfwFW0n0K0FUwzo9JMKl4BNBv/gDIjgL\n\
Dxl/Hyr3mD8SuXJ/0wIDAQABMA0GCSqGSIb3DQEBCwUAA4GBAD9DadtvvqQxbRMh\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ADVEbMb8CIzbtpZcNu4EfbR5dpzIOotIk5EP+dxAoGBANDz\n\
L4lVJhnfLqdmxquLfndBvmYd73JviJRMQ8rBW8sJE13EH3fgMZ5XFgIckFFBes+9\n\
Tp2EyPAyfcrZ06gERB5H2qJ/9atriXpv4MmXz4x0IkXMvhSaBBegn4m+RjprQJtn\n\
xXlf5X5CXAJxUMkcr+EX0vrmE0fluM/ch48wVqzLAoGALO+DTuBJH9bypO6oXn7M\n\
bguXbqI5jP4JQ8XXBOZe/dk8b8k6brTCYXYBKSPysA2rC+92a/UwRrB2vsdWNWmf\n\
j3oMla57vyJBBzL6+bunop+SbtrJi8L57EJPdoblU2v4CQtkd5hgmzgI0VWQs3Sf\n\
yM7BStIzwKt/LMTYJa65nVECgYA9SphvGGMMw8LTp/ou58Q5BzfqjG1oi/7sQVLB\n\
buaWMBtWLeltKH5li/YQuIA81ZBIz6b33bNzgdwUXMERT+8LUth3QkG1/t1c5ORz\n\
PezLrmU/REedk3AP3jXuGbpP+JYC//WVHOQcnSqQLN0doSSWSn7Ayixtspz7nBzH\n\
i85g8QKBgGIsWt6ZfeK6vRXHIlALt2mYdxLQqbmAl2B0VKZovIAtWcYKnwu8kVo4\n\
5ldzkTzCGSt9MavHd5nXd9W8Zi8xLKlD6OzuXuFFi0tTGkEVYwnDHSiUlRa9v0Cl\n\
0fKjAJcddswG23QYNRLJAiw4Yqm5iFvi+QEidNdc6Wk4U6g3BI/r\n\
-----END RSA PRIVATE KEY-----\n\0'
# use the wifi interface to link to the Access Point
# change network name, security and password as needed
print("Establishing Link...")
try:
# FOR THIS EXAMPLE TO WORK, "Network-Name" AND "Wifi-Password" MUST BE SET
# TO MATCH YOUR ACTUAL NETWORK CONFIGURATION
wifi.link(NETWORK_SSID,wifi.WIFI_WPA2,NETWORK_PW)
except Exception as e:
print("ooops, something wrong while linking :(", e)
while True:
sleep(1000)
# define MQTT callbacks
def is_sample(data):
if ('message' in data):
return (data['message'].qos == 1 and data['message'].topic == "desktop/samples")
return False
def print_sample(client,data):
message = data['message']
print("sample received: ", message.payload)
def print_other(client,data):
message = data['message']
print("topic: ", message.topic)
print("payload received: ", message.payload)
def send_sample(obj):
print("publishing: ", obj)
client.publish("temp/random", str(obj))
def publish_to_self():
client.publish("desktop/samples","hello! "+str(random(0,10)))
try:
# set the mqtt id to "zerynth-mqtt"
client = mqtt.Client('test_node', True)
ctx = ssl.create_ssl_context(cacert = cacert, clicert = clicert, pkey = clikey, options = ssl.CERT_NONE|ssl.SERVER_AUTH)
for retry in range(10):
try:
client.connect(MQTT_BROKER_IP, 60, 8883, ssl_ctx = ctx)
sleep(1000)
break
except Exception as e:
print("connecting...")
print("connected.")
# subscribe to channels
client.subscribe([["hello",1]])
# configure callbacks for "PUBLISH" message
client.on(mqtt.PUBLISH,print_sample,is_sample)
# start the mqtt loop
client.loop(print_other)
# Every 3 seconds, send a random number to "temp/random"
# you can check temp/random changing here: http://test.mosquitto.org/gauge/
while True:
sleep(3000)
x = random(0,50)
send_sample(x)
# when x ends with 0, publish a message to desktop/samples
# it is echoed back
if x%10==0:
publish_to_self()
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.