Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions LoraOledESP32/DoESDash.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[34mNo possible ports found\u001b[0m\u001b[34mConnecting to --port=/dev/ttyUSB0 --baud=115200 \u001b[0mcould not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'\n",
"\u001b[34m\n",
"Are you sure your ESP-device is plugged in?\u001b[0m"
]
}
],
"source": [
"%serialconnect"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sent 116 lines (2791 bytes) to main.py.\n"
]
}
],
"source": [
"%sendtofile main.py\n",
"\n",
"import time\n",
"import utime\n",
"from machine import Pin, I2C\n",
"import machine\n",
"import ssd1306\n",
"import framebuf\n",
"import network\n",
"import ubinascii\n",
"\n",
"# Definitions\n",
"ssid = 'DoESLiverpool'\n",
"password = '****'\n",
"\n",
"# Why did we wake up?\n",
"print(machine.wake_description())\n",
"\n",
"# Setup OLED\n",
"rst = Pin(16, Pin.OUT)\n",
"rst.value(1)\n",
"scl = Pin(15, Pin.OUT, Pin.PULL_UP)\n",
"sda = Pin(4, Pin.OUT, Pin.PULL_UP)\n",
"\n",
"i2c = I2C(scl=scl, sda=sda, freq=450000)\n",
"\n",
"oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)\n",
" \n",
"# Gimme a welcome screen!\n",
"oled.fill(0)\n",
"oled.text('DoES Dash', 0, 0)\n",
"oled.show()\n",
"time.sleep(0.25)\n",
"\n",
"# Set up networking\n",
"sta_if = network.WLAN(network.STA_IF)\n",
" \n",
"oled.text('Wifi Connecting', 0, 10)\n",
"#oled.text(ssid, 0, 20)\n",
"oled.show()\n",
" \n",
"if not sta_if.isconnected():\n",
" print(\"Connecting to SSID\", ssid)\n",
" sta_if.active(True)\n",
" sta_if.connect(ssid, password)\n",
" while not sta_if.isconnected():\n",
" pass\n",
"print(\"Connected! IP = \", sta_if.ifconfig()[0])\n",
"\n",
"#oled.text(\"IP: \" + sta_if.ifconfig()[0], 0, 30)\n",
"#oled.show()\n",
"#time.sleep(0.25)\n",
"\n",
"rtc = machine.RTC()\n",
"\n",
"# Sync RTC over NTP\n",
"if not rtc.synced():\n",
" rtc.ntp_sync(server=\"hr.pool.ntp.org\", tz=\"CET-1CEST\")\n",
" while not rtc.synced():\n",
" pass\n",
" print(utime.gmtime())\n",
" print(utime.localtime())\n",
"\n",
"# Send MQtt message\n",
"def conncb(task):\n",
" print(\"[{}] Connected\".format(task))\n",
"\n",
"def disconncb(task):\n",
" print(\"[{}] Disconnected\".format(task))\n",
"\n",
"def subscb(task):\n",
" print(\"[{}] Subscribed\".format(task))\n",
"\n",
"def pubcb(pub):\n",
" print(\"[{}] Published: {}\".format(pub[0], pub[1]))\n",
"\n",
"def datacb(msg):\n",
" print(\"[{}] Data arrived from topic: {}, Message:\\n\".format(msg[0], msg[1]), msg[2])\n",
"\n",
"mqtt = network.mqtt(\"does\", \"mqtt://10.0.29.187\", user=\"user\", password=\"pass\", cleansession=True, connected_cb=conncb, disconnected_cb=disconncb, subscribed_cb=subscb, published_cb=pubcb, data_cb=datacb)\n",
"\n",
"# secure connection requires more memory and may not work\n",
"#mqtts = network.mqtt(\"eclipse\", \"mqtts://iot.eclipse.org\", cleansession=True, data_cb=datacb)\n",
"\n",
"# mqtt over Websocket can also be used\n",
"# mqttws = network.mqtt(\"eclipse\", \"ws://iot.eclipse.org:80\", cleansession=True, data_cb=datacb)\n",
"# mqttwss = network.mqtt(\"eclipse\", \"wss://iot.eclipse.org:80\", cleansession=True, data_cb=datacb)\n",
"\n",
"# Start the mqtt\n",
"print(\"Connecting to MQtt\")\n",
"oled.text(\"Publishing Msg\", 0, 20)\n",
"oled.show()\n",
"\n",
"mqtt.start()\n",
"\n",
"# Wait until status is: (1, 'Connected')\n",
"while mqtt.status()[0] < 2:\n",
" print(mqtt.status())\n",
" time.sleep(1)\n",
"\n",
"#mqtt.subscribe('test')\n",
"mqtt.publish('button/' + ubinascii.hexlify(machine.unique_id()).decode('ascii'), 'woke up')\n",
"\n",
"oled.text(\"Done. Sleeping\", 0, 30)\n",
"oled.show()\n",
"\n",
"i2c.deinit()\n",
"\n",
"# Debug\n",
"time.sleep(1)\n",
"\n",
"# Setup Sleep (wake on button press)\n",
"pin=machine.Pin(0)\n",
"pin.init(mode=pin.IN, pull=pin.PULL_UP)\n",
"rtc.wake_on_ext0(pin, 0)\n",
"\n",
"print(\"Sleeping...\")\n",
"machine.deepsleep()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "MicroPython - USB",
"language": "micropython",
"name": "micropython"
},
"language_info": {
"codemirror_mode": "python",
"file_extension": ".py",
"mimetype": "text/python",
"name": "micropython"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading