Skip to content

Commit 2b6d43c

Browse files
Added examples: https, bluetooth, I2C, threading and added start command to aws demo
1 parent 6a00857 commit 2b6d43c

File tree

13 files changed

+208
-54
lines changed

13 files changed

+208
-54
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.e6t
2+
*.e4q
3+
*.e4p
4+
*.1

examples/aws/demo.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import time
2+
from mqttclient import MQTTClient
3+
4+
DISCONNECTED = 0
5+
CONNECTING = 1
6+
CONNECTED = 2
7+
DEVICE_ID = "12345"
8+
HOST = "random_id.us-west-2.amazonaws.com"
9+
TOPIC_DOWNLOAD = "Download"
10+
TOPIC_UPLOAD = "Upload"
11+
12+
13+
state = DISCONNECTED
14+
connection = None
15+
16+
def _recv_msg_callback(topic, msg):
17+
print("Received: {} from Topic: {}".format(msg, topic))
18+
19+
def _send_msg(msg):
20+
global connection
21+
connection.publish(TOPIC_UPLOAD, msg)
22+
23+
def run():
24+
global state
25+
global connection
26+
27+
while True:
28+
# Wait for connection
29+
while state != CONNECTED:
30+
try:
31+
state = CONNECTING
32+
connection = MQTTClient(DEVICE_ID, server=HOST, port=8883)
33+
connection.connect(ssl=True, certfile='/flash/cert/certificate.crt', keyfile='/flash/cert/privateKey.key', ca_certs='/flash/cert/root-CA.cer')
34+
state = CONNECTED
35+
except:
36+
print('Error connecting to the server')
37+
time.sleep(0.5)
38+
continue
39+
40+
print('Connected!')
41+
42+
# Subscribe for messages
43+
connection.set_callback(_recv_msg_callback)
44+
connection.subscribe(TOPIC_DOWNLOAD)
45+
46+
while state == CONNECTED:
47+
connection.check_msg()
48+
msg = '{"Name":"Pycom", "Data":"Test"}'
49+
print('Sending: ' + msg)
50+
_send_msg(msg)
51+
time.sleep(2.0)
52+
53+
54+
run()

examples/aws/main.py

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,2 @@
1-
import time
2-
from mqttclient import MQTTClient
3-
4-
DISCONNECTED = 0
5-
CONNECTING = 1
6-
CONNECTED = 2
7-
DEVICE_ID = "12345"
8-
HOST = "random_id.us-west-2.amazonaws.com"
9-
TOPIC_DOWNLOAD = "Download"
10-
TOPIC_UPLOAD = "Upload"
11-
12-
13-
state = DISCONNECTED
14-
connection = None
15-
16-
def _recv_msg_callback(topic, msg):
17-
print("Received: {} from Topic: {}".format(msg, topic))
18-
19-
def _send_msg(msg):
20-
global connection
21-
connection.publish(TOPIC_UPLOAD, msg)
22-
23-
def run():
24-
global state
25-
global connection
26-
27-
while True:
28-
# Wait for connection
29-
while state != CONNECTED:
30-
try:
31-
state = CONNECTING
32-
connection = MQTTClient(DEVICE_ID, server=HOST, port=8883)
33-
connection.connect(ssl=True, certfile='/flash/cert/certificate.crt', keyfile='/flash/cert/privateKey.key', ca_certs='/flash/cert/root-CA.cer')
34-
state = CONNECTED
35-
except:
36-
print('Error connecting to the server')
37-
time.sleep(0.5)
38-
continue
39-
40-
print('Connected!')
41-
42-
# Subscribe for messages
43-
connection.set_callback(_recv_msg_callback)
44-
connection.subscribe(TOPIC_DOWNLOAD)
45-
46-
while state == CONNECTED:
47-
connection.check_msg()
48-
msg = '{"Name":"Pycom", "Data":"Test"}'
49-
print('Sending: ' + msg)
50-
_send_msg(msg)
51-
time.sleep(2.0)
52-
53-
54-
run()
1+
import demo
2+
demo.run()

examples/bluetooth/boot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from machine import UART
2+
import machine
3+
import os
4+
5+
uart = UART(0, baudrate=115200)
6+
os.dupterm(uart)
7+
8+
machine.main('main.py')

examples/bluetooth/main.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from network import Bluetooth
2+
import time
3+
bt = Bluetooth()
4+
bt.start_scan(-1)
5+
6+
while True:
7+
adv = bt.get_adv()
8+
if adv
9+
# try to get the complete name
10+
print(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL))
11+
12+
# try to get the manufacturer data (Apple's iBeacon data is sent here)
13+
print(binascii.hexlify(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_MANUFACTURER_DATA)))
14+
15+
16+
if bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'Heart Rate':
17+
conn = bt.connect(adv.mac)
18+
services = conn.services()
19+
for service in services:
20+
time.sleep(0.050)
21+
if type(service.uuid()) == bytes:
22+
print('Reading chars from service = {}'.format(service.uuid()))
23+
else:
24+
print('Reading chars from service = %x' % service.uuid())
25+
chars = service.characteristics()
26+
for char in chars:
27+
if (char.properties() & Bluetooth.PROP_READ):
28+
print('char {} value = {}'.format(char.uuid(), char.read()))
29+
conn.disconnect()
30+
break
31+
else:
32+
time.sleep(0.050)

examples/https/boot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from machine import UART
2+
import machine
3+
import os
4+
5+
uart = UART(0, baudrate=115200)
6+
os.dupterm(uart)
7+
8+
machine.main('main.py')

examples/https/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import socket
2+
import ssl
3+
4+
# Connect without a certificate
5+
s = socket()
6+
ss = ssl.wrap_socket(s)
7+
ss.connect(socket.getaddrinfo('www.google.com', 443)[0][-1])
8+
9+
# Connect with a certificate
10+
s = socket.socket()
11+
ss = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ca_certs='/flash/cert/ca.pem')
12+
ss.connect(socket.getaddrinfo('cloud.blynk.cc', 8441)[0][-1])

examples/i2c/bh1750fvi.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Simple driver for the BH1750FVI digital light sensor
2+
3+
class BH1750FVI:
4+
MEASUREMENT_TIME = const(120)
5+
6+
def __init__(self, i2c, addr=0x23, period=150):
7+
self.i2c = i2c
8+
self.period = period
9+
self.addr = addr
10+
self.time = 0
11+
self.value = 0
12+
self.i2c.writeto(addr, bytes([0x10])) # start continuos 1 Lux readings every 120ms
13+
14+
def read(self):
15+
self.time += self.period
16+
if self.time >= MEASUREMENT_TIME:
17+
self.time = 0
18+
data = self.i2c.readfrom(self.addr, 2)
19+
self.value = (((data[0] << 8) + data[1]) * 1200) // 1000
20+
return self.value

examples/i2c/boot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from machine import UART
2+
import machine
3+
import os
4+
5+
uart = UART(0, baudrate=115200)
6+
os.dupterm(uart)
7+
8+
machine.main('main.py')

examples/i2c/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import socket
2+
import time
3+
import pycom
4+
import struct
5+
from network import LoRa
6+
from machine import I2C
7+
import bh1750fvi
8+
9+
LORA_PKG_FORMAT = "!BH"
10+
LORA_CONFIRM_FORMAT = "!BB"
11+
12+
DEVICE_ID = 1
13+
14+
pycom.heartbeat(False)
15+
16+
lora = LoRa(mode=LoRa.LORA, tx_iq=True, frequency = 863000000)
17+
lora_sock = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
18+
lora_sock.setblocking(False)
19+
20+
i2c = I2C(0, I2C.MASTER, baudrate=100000)
21+
light_sensor = bh1750fvi.BH1750FVI(i2c, addr=i2c.scan()[0])
22+
23+
while(True):
24+
msg = struct.pack(LORA_PKG_FORMAT, DEVICE_ID, light_sensor.read())
25+
lora_sock.send(msg)
26+
27+
pycom.rgbled(0x150000)
28+
29+
wait = 5
30+
while (wait > 0):
31+
wait = wait - 0.1
32+
time.sleep(0.1)
33+
recv_data = lora_sock.recv(64)
34+
35+
if (len (recv_data) >= 2):
36+
status, device_id = struct.unpack(LORA_CONFIRM_FORMAT, recv_data)
37+
38+
if (device_id == DEVICE_ID and status == 200):
39+
pycom.rgbled(0x001500)
40+
wait = 0
41+
42+
time.sleep(1)

0 commit comments

Comments
 (0)