Try   HackMD
tags: TA Stuff 2021 Pycom ESP32

Tutorial 3: Connect to Pybytes

It's time to start sending your sensor data to the cloud. And the easiest place to get started doing that is probably Pybytes.

Connect to Pybytes

This is done in 2 steps, first you'll get the device connected to Pybytes over Wi-Fi and then you'll get to send data and organise the Dashboard.

At the Pycom website there is a helpful tutorial with pictures to follow, and you can find it here: https://docs.pycom.io/pybytes/gettingstarted/

If something is unclear, take a look below where some parts are explained further.

  • First of all you'll need an account at Pybytes.

  • Before setting up your device with Pybytes it is an good idea to configure your network (see picture below) and enter your Wi-Fi's SSID (the name of your Wi-Fi) and credentials (and save!).

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Then, in your device menu, press Configuration then Networks, and select your added Wi-Fi network before doing the Firmware update.

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • For step 2.5 the layout have changed a bit. If you're connecting using the Firmware updater you will find the token asked for in step 3 here under offline Firmware updater:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • Firmware updater can be found here. Remember to select "Force update pybytes registration" in the updater before continuing.

  • Remember to close any VSCode or Atom before running the firmware updater.

  • If for some reason you cannot connect to your Wi-Fi after running the updater and starting your IDE again, the easiest way to check your SSID and password is by pressing "download" in your IDE and accept all new files. Then a new file pybytes_config.json will appear (if you've done the Pybytes firmware update). There you can check the SSID and password you entered in Pybytes. Update your SSID/password if needed, save and upload the project again.

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Tip: right-click in that config file then press "format document" (in VSCode, in Atom you need to install a package such as atom-beautify) to format it to something easier to read.

Using Pybytes

Now it is time for sending some data to Pybytes and organising the Dashboard. Again, there is a good tutorial on this provided by Pycom and you may find it here: https://docs.pycom.io/pybytes/dashboard/

Send Data

When you want to send a signal to pybytes you can use the function pybytes.send_signal(signal_number, value). The signal_number defines which "channel" you will use for sending your message. The value represents what you want to send, this is your message. It can be a number, but also a string of text.

For example you can run this in main.py:

import time while True: #The value can be a sensor reading being done here value = 5 #Sending to pybytes in channel 1 pybytes.send_signal(1, value) print("sending: {}".format(value)) #Send every 5 seconds time.sleep(5)

Connect to Wi-Fi

If you use Pybytes, this is done via the provisioning alone when you do your pybytes firmware update and enter you Activation token (after you've entered your Wi-Fi credentials on pybytes). No need to do this below if you're only using Pybytes.

On your Pycom device there is a built-in antenna for connecting to Wi-Fi. When connecting to online platforms, other than Pybytes, or if you want to connect manually to your Wi-Fi you may use the code below. Note, this code will only try connecting to the Wi-Fi specified by ssid. Don't forget to change ssid and password to the corresponding for your Wi-Fi.

In boot.py:

# boot.py -- run on boot-up
from network import WLAN
import machine
wlan = WLAN(mode=WLAN.STA)

nets = wlan.scan()
for net in nets:
    if net.ssid == "your_wifi_ssid":
        print('Network found!')
        wlan.connect(net.ssid, auth=(net.sec, "your_wifi_password"), timeout=5000)
        while not wlan.isconnected():
            machine.idle() # save power while waiting
        print('WLAN connection succeeded!')
        print(wlan.ifconfig())
        break
  • It is also possible to add a timeout field to wlan.connect(ssid='ssid', auth=(WLAN.WPA2, 'password'), timeout='time_in_ms'), after the time has run out the device will stop trying to connect to your network.
  • You may read more about ways to connect to networks, like scanning for networks, here. More information about the WLAN class can be found here.

Hiding your keys

When sharing or showing code you've written, then you may not want to expose your private keys and, later, tokens. As we encourage you to help each other and share your knowledge, we can work around this problem by storing this information in another file.

Create a new file in your project and call it keys.py, here you can store your keys and tokens.

wifi_ssid = "your_wifi_ssid"
wifi_password = "your_wifi_password"

Then your code in boot.py becomes somehing like this instead.

# boot.py -- run on boot-up
from network import WLAN
import machine
import keys
wlan = WLAN(mode=WLAN.STA)

nets = wlan.scan()
for net in nets:
    if net.ssid == keys.wifi_ssid:
        print('Network found!')
        wlan.connect(net.ssid, auth=(net.sec, keys.wifi_password), timeout=5000)
        while not wlan.isconnected():
            machine.idle() # save power while waiting
        print('WLAN connection succeeded!')
        print(wlan.ifconfig())
        break

Credit
Written by Linnea Allander, Erik Karlsson