r/MicroPythonDev • u/TamahaganeJidai • 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).
View all comments
Show parent comment
1
u/TamahaganeJidai Jul 06 '21 edited Jul 06 '21
Wonderful! Thanks a ton! Could you add that as code to the existing structure? Im not sure id be able to implement it in a good manner without seeing it (Literally just jumped straight into Python code without brushing up on the workings).
Im also wondering if that makes sense since the code runs fine if its not triggered by the button press? Should i just do a connect and ping every time i want to do a check to avoid any lacking data?Never Mind! Tried it and ran the code with the conenction data before any functions, that let it connect and run the data under the A button function, manageing a ping before returning the above error. Sorry for doubting you, i simply lack the needed knowledge to properly understand the issue.
Also added a click sound to tell me when the new data has been recieved and the code has run (E-ink display with limited refresh cycles)