Merge branch 'publish' into network-examples

This commit is contained in:
gijsio
2020-09-18 09:48:40 +02:00
131 changed files with 1743 additions and 1880 deletions

View File

@@ -42,7 +42,7 @@ print("this will be printed after 10 seconds: " str(time.ticks_ms()))
#### Deep sleep
Deepsleep disables, next to the lightsleep, the main CPU and RAM. This leaves only a low power coprocessor and RTC timer running. After waking up, the board will start again at `boot.py`, just like with pressing the reset button. The CPU counter (`time.ticks()`) will continue to count however!
You can also leave the brackets empty to sleep indefinetely, until the reset button is pressed, the power is removed, or an external wake up signal (interrupt) is provided. Be aware that the LTE modem. ***
You can also leave the brackets empty to sleep indefinetely, until the reset button is pressed, the power is removed, or an external wake up signal (interrupt) is provided. Be aware that the LTE modem will remain switched on unless you actively switch off its power, or use its own power saving modes.
```python
@@ -82,4 +82,4 @@ print("This will never be printed")
>Note: Using `deepsleep()` will also stop the USB connection. Be wary of that when trying to upload new code to the device!
For the Pysense, Pytrack and Pyscan expansionboards, an additional sleep function is available. You can find out more about that [here](../expansionboards/sleep/)
For the Pysense, Pytrack and Pyscan expansionboards, an additional sleep function is available. You can find out more about that [here](/tutorials/expansionboards/sleep/)

View File

@@ -8,7 +8,7 @@ While the Pygate shield has the radio chips required to act as a LoRaWAN gateway
A USB connection is recommended for the initial firmware update of the Pycom development module (WiPy 3, GPy, LoPy4) and to upload the configuration & start-up script. The module can be updated over the air via WiFi / LTE-M (depending on network capabilities) or via Ethernet connection which allows installation of the gateway in remote locations.
The Pygate board can have the PyEthernet adapter connected which allows an Ethernet connection. The PyEthernet also supports PoE. Please check the separate [page and warning for PoE-NI!](/tutorials/all/poe)
The Pygate board can have the PyEthernet adapter connected which allows an Ethernet connection. The PyEthernet also supports PoE. Please check the separate [page and warning for PoE-NI!](/tutorials/networkprotocols/poe/)
### Quickstart

View File

@@ -14,7 +14,7 @@ This section contains tutorials and examples for use with Pycom modules and Expa
General Pycom tutorials contains tutorials that may be run on any Pycom device, such as connecting to a WiFi network, Bluetooth, controlling I/O pins etc. Later sections are specific to the LoPy and SiPy devices such as setting up a LoRa node or connecting to the Sigfox network. The final sections are related to examples using the Pytrack and Pysense.
Before starting, ensure that any Pycom devices are running the latest firmware; for instructions see [Firmware Updates](/gettingstarted/installation/firmwaretool).
Before starting, ensure that any Pycom devices are running the latest firmware; for instructions see [Firmware Updates](/updatefirmware/device/).
The source code for these tutorials, along with the required libraries can be found in in the [pycom-libraries](https://github.com/pycom/pycom-libraries) repository.

View File

@@ -23,72 +23,32 @@ Incorrect usages of Power over Ethernet can lead to unrecoverable damage of not
A battery can be connected to the PyGate without issues. The battery can be charged either via USB-C or PoE power.
<h3>Setup Options</h3>
## Setup Options
In the following diagram, we clarify what is, and what is not allowed:
### Power over Ethernet
<h4> Power over Ethernet</h4>
![](/gitbook/assets/poe-ni-warn1.png)
<div class="poe-table">
<table class="poe">
<tbody>
<tr>
<td> USB-C cable connected to a PC <b>with</b> mains ground protection</td>
<td class="poe-red">NOT OK</td>
</tr>
<tr>
<td> USB-C cable connected to a Notebook <b>without</b> mains ground protection</td>
<td class="poe-green"> OK</td>
</tr>
<tr>
<td> USB-C charger <b>with</b> mains ground protection</td>
<td class="poe-red"> NOT OK</td>
</tr>
<tr>
<td> USB-C charger <b>without</b> mains ground protection</td>
<td class="poe-green"> OK</td>
</tr>
<tr>
<td> Oscilloscope / Logic Analyser / Other equipment <b>with</b> mains ground protection</td>
<td class="poe-red"> NOT OK</td>
</tr>
<tr>
<td> Battery</td>
<td class="poe-green"> OK</td>
</tr>
</tbody>
</table>
</div>
| Connection type | OK?|
|--- |--- |
|USB-C cable connected to a PC with mains ground protection|NOT OK|
|USB-C cable connected to a Notebook without mains ground protection|OK|
|USB-C charger with mains ground protection|NOT OK|
|USB-C charger without mains ground protection|OK|
|Oscilloscope / Logic Analyser / Other equipment with mains ground protection|NOT OK|
|Battery|OK|
### Power over USB-C / External
<br><br>
<h4> Power over USB-C or external supply</h4>
![](/gitbook/assets/poe-ni-warn2.png)
<div class="poe-table">
<table class="poe">
<tbody>
<tr>
<td> USB-C cable connected to a PC <b>with</b> mains ground protection</td>
<td class="poe-green">OK</td>
</tr>
<tr>
<td> USB-C cable connected to a Notebook <b>without</b> mains ground protection</td>
<td class="poe-green"> OK </td>
</tr>
<tr>
<td> USB-C charger <b>with</b> mains ground protection</td>
<td class="poe-green"> OK </td>
</tr>
<tr>
<td> USB-C charger <b>without</b> mains ground protection</td>
<td class="poe-green"> OK </td>
</tr>
<tr>
<td> Oscilloscope / Logic Analyser / Other equipment <b>with</b> mains ground protection</td>
<td class="poe-green"> OK </td>
</tr>
<tr>
<td> Battery</td>
<td class="poe-green"> OK </td>
</tr>
</tbody>
</table>
</div>
| Connection type | OK?|
|--- |--- |
|USB-C cable connected to a PC with mains ground protection| OK|
|USB-C cable connected to a Notebook without mains ground protection|OK|
|USB-C charger with mains ground protection| OK|
|USB-C charger without mains ground protection|OK|
|Oscilloscope / Logic Analyser / Other equipment with mains ground protection| OK|
|Battery|OK|

View File

@@ -0,0 +1,253 @@
---
title: "Pymesh BLE Examples"
aliases:
- tutorials/all/ble_mesh.html
- tutorials/all/ble_mesh.md
- chapter/tutorials/all/ble_mesh
---
Pymesh BLE module enables many-to-many device connections, based on the Bluetooth module.
For the API, please check the section [Pymesh BLE API](/firmwareapi/pycom/network/bluetooth/ble_mesh/).
## Generic OnOff Example
Generic OnOff model is one of the simplest model in BLE Mesh.
This model illustrates the light-switches (OnOff Client) and light-bulbs (OnOff Server). In other words, the Client can send on/off commands to one/all nodes, and the Server is records state changes based on these commands.
### OnOff Server
OnOff Server has one boolean State. Server can Get, Set or send Status about this State to Client(s).
In the example below, during Provisioning, `Output OOB` can be selected. LED is yellow in case of Not-provisioned, and green in case of Provisioned state.
Changing the State of Server, LED's light is green or red.
```python
from network import Bluetooth
import pycom
import time
BLE_Name = "OnOff Server 18"
def blink_led(n):
for x in range(n):
pycom.rgbled(0xffff00) # yellow on
time.sleep(0.3)
pycom.rgbled(0x000000) # off
time.sleep(0.3)
def server_cb(new_state, event, recv_op):
print("SERVER | State: ", new_state)
# Turn on LED on board based on State
if new_state == True:
pycom.rgbled(0x007f00) # green
else:
pycom.rgbled(0x7f0000) # red
def prov_callback(event, oob_pass):
if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT):
# Yellow if not Provision yet or Reseted
pycom.rgbled(0x555500)
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
# Green if Provisioned
pycom.rgbled(0x007f00)
if(event == BLE_Mesh.PROV_OUTPUT_OOB_REQ_EVT):
print("Privisioning blink LED num:", oob_pass)
blink_led(oob_pass)
# BLE Mesh module
BLE_Mesh = Bluetooth.BLE_Mesh
# Turn off the heartbeat behavior of the LED
pycom.heartbeat(False)
# Need to turn ON Bluetooth before using BLE Mesh
bluetooth = Bluetooth()
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
element = BLE_Mesh.create_element(primary=True, feature=BLE_Mesh.GATT_PROXY)
model_server = element.add_model(BLE_Mesh.GEN_ONOFF, BLE_Mesh.SERVER, callback=server_cb)
# Initialize BLE_Mesh
BLE_Mesh.init(BLE_Name, auth=BLE_Mesh.OOB_OUTPUT, callback=prov_callback)
# Turn on Provisioning Advertisement
BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT)
print("\nBLE Mesh started")
print(BLE_Name, "waits to be provisioned\n")
"""
# After this node was provisioned
# Current state can be read using
model_server.get_state()
"""
```
### OnOff Client
Client can Get or Set State of Server. In case of Get, or Server Status, Client Gets the Status through the Model's callback.
```python
from network import Bluetooth
import pycom
import time
BLE_Name = "OnOff Client 17"
def blink_led(n):
for x in range(n):
pycom.rgbled(0xffff00) # yellow on
time.sleep(0.3)
pycom.rgbled(0x000000) # off
time.sleep(0.3)
def client_cb(new_state, event, recv_op):
print("CLIENT | State: ", new_state)
def prov_callback(event, oob_pass):
if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT):
# Yellow if not Provision yet or Reseted
pycom.rgbled(0x555500)
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
# Green if Provisioned
pycom.rgbled(0x007f00)
if(event == BLE_Mesh.PROV_OUTPUT_OOB_REQ_EVT):
print("Privisioning blink LED num:", oob_pass)
blink_led(oob_pass)
# BLE Mesh module
BLE_Mesh = Bluetooth.BLE_Mesh
# Turn off the heartbeat behavior of the LED
pycom.heartbeat(False)
# Need to turn ON Bluetooth before using BLE Mesh
bluetooth = Bluetooth()
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
element = BLE_Mesh.create_element(primary=True, feature=BLE_Mesh.GATT_PROXY)
model_client = element.add_model(BLE_Mesh.GEN_ONOFF, BLE_Mesh.CLIENT, callback=client_cb)
# Initialize BLE_Mesh
BLE_Mesh.init(BLE_Name, auth=BLE_Mesh.OOB_OUTPUT, callback=prov_callback)
# Turn on Provisioning Advertisement
BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT)
print("\nBLE Mesh started")
print(BLE_Name, "waits to be provisioned\n")
"""
# After this node was provisioned
# transmit the change of state broadcasting in the Mesh
model_client.set_state(False, 0xFFFF)
model_client.set_state(True, 0xFFFF)
# or to a unique server
model_client.set_state(False, 3)
model_client.set_state(True, 5)
"""
```
## Sensor Example
In case of Sensor Models, State of Server can be modified only by Server itself, Client can only Get the State by calling Client's Get, or by Servers Status call, but cannot modify the Server's State.
### Sensor Server
In this example Server takes a time measurement every 1 seconds, and send a Status message every 5 seconds, after it was provisioned.
```python
from network import Bluetooth
import pycom
import time
from machine import Timer
def read_sensor(alarm):
# In this example sensor reads local seconds
if(device_provisioned):
model_server.set_state(time.localtime()[5])
print("SENSOR | State: ", model_server.get_state())
def status_sensor(alarm):
if (device_provisioned):
model_server.status_state()
def prov_callback(event, oob_pass):
global device_provisioned
if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT):
# Yellow if not Provision yet or Reseted
pycom.rgbled(0x555500)
device_provisioned = False
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
# Green if Provisioned
pycom.rgbled(0x007f00)
device_provisioned = True
# BLE Mesh module
BLE_Mesh = Bluetooth.BLE_Mesh
# Turn off the heartbeat behavior of the LED
pycom.heartbeat(False)
# Need to turn ON Bluetooth before using BLE Mesh
bluetooth = Bluetooth()
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
element = BLE_Mesh.create_element(primary=True, feature=BLE_Mesh.GATT_PROXY)
model_server = element.add_model(BLE_Mesh.SENSOR, BLE_Mesh.SERVER, sen_min = 0, sen_max = 59, sen_res = 1)
# Initialize BLE_Mesh
BLE_Mesh.init("Pycom Sensor Server", callback=prov_callback)
# Turn on Provisioning Advertisement
BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT)
# Sensor takes measurement every 1 second
Timer.Alarm(read_sensor, 1, periodic=True)
# Sensor send status every 5 seconds
Timer.Alarm(status_sensor, 5, periodic=True)
```
### Sensor Client
Sensor Client is looking for measurements, as Server sends Status every 5 seconds. Between these calls, Client can Get message any time.
```python
from network import Bluetooth
import pycom
def client_cb(new_state, event, recv_op):
print("CLIENT | State: ", new_state)
def prov_callback(event, oob_pass):
if(event == BLE_Mesh.PROV_REGISTER_EVT or event == BLE_Mesh.PROV_RESET_EVT):
# Yellow if not Provision yet or Reseted
pycom.rgbled(0x555500)
if(event == BLE_Mesh.PROV_COMPLETE_EVT):
# Green if Provisioned
pycom.rgbled(0x007f00)
# BLE Mesh module
BLE_Mesh = Bluetooth.BLE_Mesh
# Turn off the heartbeat behavior of the LED
pycom.heartbeat(False)
# Need to turn ON Bluetooth before using BLE Mesh
bluetooth = Bluetooth()
# Create a Primary Element with GATT Proxy feature and add a Server model to the Element
element = BLE_Mesh.create_element(primary=True, feature=BLE_Mesh.GATT_PROXY)
model_client = element.add_model(BLE_Mesh.SENSOR, BLE_Mesh.CLIENT, callback=client_cb, sen_min = 0, sen_max = 59, sen_res = 1)
# Initialize BLE_Mesh
BLE_Mesh.init("Pycom Sensor Client", callback=prov_callback)
# Turn on Provisioning Advertisement
BLE_Mesh.set_node_prov(BLE_Mesh.PROV_ADV|BLE_Mesh.PROV_GATT)
```

View File

@@ -27,9 +27,9 @@ Using the WLAN class from network, you can change the name (SSID) and security s
```python
from network import WLAN
wlan = WLAN(mode=WLAN.AP)
wlan = WLAN(mode=WLAN.AP, ssid='hello world')
wlan.init(ssid="hello world", auth=None)
wlan.init()
#use the line below to apply a password
#wlan.init(ssid="hi", auth=(WLAN.WPA2, "eightletters"))
print(wlan.ifconfig(id=1)) #id =1 signifies the AP interface