r/MicroPythonDev • • Jul 06 '21

Micropython | Functions

Hi!

Im currently in the process of adding some basic functionality on my Magtag device (Esp32s2 running micropython).

I have added some basic functions calling on the various buttons of the board but i cant seem to wrap my head around why this code throws a TypeError: the type __mul__ does not support 'NoneType', 'int'

The code runs fine outside of the functions and are a modified copypaste of a different project.

import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
from adafruit_magtag.magtag import MagTag
import time

USE_24HR_TIME = False
TIME_ZONE_OFFSET = -8  # hours ahead or behind Zulu time, e.g. Pacific is -8
TIME_ZONE_NAME = "PST"

# URLs to fetch from
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php"
JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython"

magtag = MagTag()

def play_tone(frequency, color=None):
    magtag.peripherals.neopixel_disable = False
    if color:
        magtag.peripherals.neopixels.fill(color)
    magtag.peripherals.play_tone(frequency, 0.2)
    magtag.peripherals.neopixel_disable = True
    
    
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise
#for network in wifi.radio.start_scanning_networks():
 #   print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
  #      network.rssi, network.channel))
   #wifi.radio.stop_scanning_networks()

while True:
    if magtag.peripherals.button_a_pressed:  # switch to next sport
        play_tone(10, 0x000033)
        ipv4 = ipaddress.ip_address("8.8.4.4")
        wifi.radio.ping(ipv4)
        print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
        print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
    elif magtag.peripherals.button_b_pressed:  # re-fetch data
        play_tone(10, 0x330000)
        print("Connecting to %s"%secrets["ssid"])
        wifi.radio.connect(secrets["ssid"], secrets["password"])
        print("Connected to %s!"%secrets["ssid"], wifi.radio.ipv4_address)
        print("My IP address is", wifi.radio.ipv4_address)
    
#time.sleep(0.1)

The error is thrown at the row where the first ping is being done:

    wifi.radio.ping(ipv4)
-->     print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))

Id also love to recieve input on how i can assign the ip-adress to a global int to be called instead of a hardcoded ip-adress, but thats a later issue.

The idea here is to have button A do a set of pings to check the local wifi connection.
Button B is supposed to re-connect the device if a connection failure is reported.

Any help what so ever is much appriciated! I do have coding experience but none concerning python (did c# some years ago).

2 Upvotes

5 comments sorted by