Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
although there is some instability on the MicroPython side of things and I'm seeing non-reproducible crashes when using websockets.
- Loading branch information
Showing
8 changed files
with
434 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# These should be edited to reflect your actual SSID and password | ||
|
||
SSID = '' | ||
PASSWORD = '' | ||
|
||
if SSID == '': | ||
print('Please edit config.py and set the SSID and password') | ||
raise ValueError('SSID not set') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import machine | ||
import network | ||
import time | ||
import config | ||
|
||
def start_ftp(): | ||
print('Starting FTP...') | ||
network.ftp.start() | ||
|
||
station = network.WLAN(network.STA_IF) | ||
if not station.active(): | ||
station.active(True) | ||
if not station.isconnected(): | ||
print('Connecting....') | ||
station.connect('SSID', 'password') | ||
while not station.isconnected(): | ||
time.sleep(1) | ||
print('.', end='') | ||
print('ifconfig =', station.ifconfig()) | ||
|
||
print('Syncing to NTP...') | ||
rtc = machine.RTC() | ||
rtc.ntp_sync(server='pool.ntp.org') | ||
|
||
print('Starting mDNS...') | ||
mdns = network.mDNS() | ||
_ = mdns.start("esp32-upy","MicroPython with mDNS") | ||
_ = mdns.addService('_ftp', '_tcp', 21, "MicroPython", {"board": "ESP32", "service": "mPy FTP File transfer", "passive": "True"}) | ||
_ = mdns.addService('_telnet', '_tcp', 23, "MicroPython", {"board": "ESP32", "service": "mPy Telnet REPL"}) | ||
_ = mdns.addService('_http', '_tcp', 80, "MicroPython", {"board": "ESP32", "service": "mPy Web server"}) | ||
def start_ntp(): | ||
print('Syncing to NTP...') | ||
rtc = machine.RTC() | ||
rtc.ntp_sync(server='pool.ntp.org') | ||
|
||
print('Starting FTP...') | ||
network.ftp.start() | ||
|
||
if not rtc.synced(): | ||
print(' waiting for time sync...', end='') | ||
time.sleep(0.5) | ||
while not rtc.synced(): | ||
print('.', end='') | ||
if not rtc.synced(): | ||
print(' waiting for time sync...', end='') | ||
time.sleep(0.5) | ||
print('') | ||
while not rtc.synced(): | ||
print('.', end='') | ||
time.sleep(0.5) | ||
print('') | ||
print('Time:', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) | ||
|
||
print('Time:', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) | ||
def connect_to_ap(): | ||
station = network.WLAN(network.STA_IF) | ||
if not station.active(): | ||
station.active(True) | ||
if not station.isconnected(): | ||
print('Connecting....') | ||
station.connect(config.SSID, config.PASSWORD) | ||
while not station.isconnected(): | ||
time.sleep(1) | ||
print('.', end='') | ||
print('') | ||
print('ifconfig =', station.ifconfig()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from property import Property | ||
from thing import Thing | ||
from value import Value | ||
from server import MultipleThings, WebThingServer | ||
import logging | ||
import time | ||
import machine | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
class Led(Thing): | ||
|
||
def __init__(self, ledPin): | ||
Thing.__init__(self, | ||
'Blue LED', | ||
'dimmableLight', | ||
'Blue LED on SparkFun ESP32 Thing') | ||
self.pinLed = machine.Pin(ledPin, machine.Pin.OUT) | ||
self.pwmLed = machine.PWM(self.pinLed) | ||
self.ledLevel = 50 | ||
self.on = False | ||
self.updateLed() | ||
|
||
self.add_property( | ||
Property(self, | ||
'on', | ||
Value(self.on, self.setOnOff), | ||
metadata={ | ||
'type': 'boolean', | ||
'description': 'Whether the LED is turned on', | ||
})) | ||
self.add_property( | ||
Property(self, | ||
'level', | ||
Value(self.ledLevel, self.setLevel), | ||
metadata={ | ||
'type': 'number', | ||
'description': 'The brightness of the LED', | ||
})) | ||
|
||
def setOnOff(self, onOff): | ||
log.info('setOnOff: onOff = ' + str(onOff)) | ||
self.on = onOff | ||
self.updateLed() | ||
|
||
def setLevel(self, level): | ||
log.info('setLevel: level = ' + str(level)) | ||
self.ledLevel = level | ||
self.updateLed() | ||
|
||
def updateLed(self): | ||
log.debug('updateLed: on = ' + str(self.on) + | ||
' level = ' + str(self.ledLevel)) | ||
if self.on: | ||
self.pwmLed.duty(self.ledLevel) | ||
else: | ||
self.pwmLed.duty(0) | ||
|
||
class Button(Thing): | ||
|
||
def __init__(self, pin): | ||
Thing.__init__(self, | ||
'Button 0', | ||
'binarySensor', | ||
'Button 0 on SparkFun ESP32 Thing') | ||
self.pin = machine.Pin(pin, machine.Pin.IN) | ||
|
||
self.button = Value(False) | ||
self.add_property( | ||
Property(self, | ||
'on', | ||
self.button, | ||
metadata={ | ||
'type': 'boolean', | ||
'description': 'Button 0 pressed' | ||
})) | ||
self.prev_pressed = self.is_pressed() | ||
|
||
def is_pressed(self): | ||
return self.pin.value() == 0 | ||
|
||
def process(self): | ||
pressed = self.is_pressed() | ||
if pressed != self.prev_pressed: | ||
self.prev_pressed = pressed | ||
log.debug('pressed = ' + str(pressed)) | ||
self.button.notify_of_external_update(pressed) | ||
|
||
def run_server(): | ||
log.info('run_server') | ||
|
||
led = Led(5) | ||
button = Button(0) | ||
|
||
# If adding more than one thing here, be sure to set the `name` | ||
# parameter to some string, which will be broadcast via mDNS. | ||
# In the single thing case, the thing's name will be broadcast. | ||
server = WebThingServer(MultipleThings([led, button], | ||
'SparkFun-ESP32-Thing'), | ||
port=80) | ||
try: | ||
log.info('starting the server') | ||
server.start() | ||
except KeyboardInterrupt: | ||
log.info('stopping the server') | ||
server.stop() | ||
log.info('done') | ||
|
||
while True: | ||
time.sleep(0.1) | ||
button.process() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.