From 067055d78a5adf1020f7f389af0193f5ebd97091 Mon Sep 17 00:00:00 2001 From: 0x1abin <270995079@qq.com> Date: Mon, 5 Feb 2018 11:44:01 +0800 Subject: [PATCH] added examples --- examples/MQTT/mqtt_example.py | 39 +++++++++++++++++++++++++++++++++ examples/Timer/timer_example.py | 22 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 examples/MQTT/mqtt_example.py create mode 100644 examples/Timer/timer_example.py diff --git a/examples/MQTT/mqtt_example.py b/examples/MQTT/mqtt_example.py new file mode 100644 index 0000000..2335937 --- /dev/null +++ b/examples/MQTT/mqtt_example.py @@ -0,0 +1,39 @@ +# https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/mqtt + +import network + +def conncb(task): + print("[{}] Connected".format(task)) + + +def disconncb(task): + print("[{}] Disconnected".format(task)) + + +def subscb(task): + print("[{}] Subscribed".format(task)) + + +def pubcb(pub): + print("[{}] Published: {}".format(pub[0], pub[1])) + + +def datacb(msg): + print("[{}] Data arrived from topic: {}, Message:\n".format( + msg[0], msg[1]), msg[2]) + +# mqtt = network.mqtt(name, server [, user, password, port, autoreconnect, clientid, cleansession, keepalive, qos, retain, secure, connected_cb, disconnected_cb, subscribed_cb, unsubscribed_cb, published_cb, data_cb]) +mqtt = network.mqtt("loboris", "mqtt.m5stack.com", connected_cb=conncb, disconnected_cb=disconncb, subscribed_cb=subscb, published_cb=pubcb, data_cb=datacb) + +# secure connection requires more memory and may not work +# mqtts = network.mqtt("eclipse", "iot.eclipse.org", secure=True, cleansession=True, connected_cb=conncb, disconnected_cb=disconncb, subscribed_cb=subscb, published_cb=pubcb, data_cb=datacb) + + +''' + +# Wait until status is: (1, 'Connected') + +mqtt.subscribe('test') +mqtt.publish('test', 'Hi from Micropython') + +''' diff --git a/examples/Timer/timer_example.py b/examples/Timer/timer_example.py new file mode 100644 index 0000000..0f2a464 --- /dev/null +++ b/examples/Timer/timer_example.py @@ -0,0 +1,22 @@ +# https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/timer + +import machine + +tcounter = 0 + +p1 = machine.Pin(27) +p1.init(p1.OUT) +p1.value(1) + +def tcb(timer): + global tcounter + if tcounter & 1: + p1.value(0) + else: + p1.value(1) + tcounter += 1 + if (tcounter % 10000) == 0: + print("[tcb] timer: {} counter: {}".format(timer.timernum(), tcounter)) + +t1 = machine.Timer(2) +t1.init(period=20, mode=t1.PERIODIC, callback=tcb) \ No newline at end of file