Hi,
I'm struggling to get this working, hopefully you are help me to guide me a bit.
This is quite a brainbreaker to me, I'm working on this for months now, so please allow me to do a full explanation.
--> Intro:
I'm trying to make the following:
A teensy 4.1 with network module is connected to a computer that runs a webserver.
A button and relay are connected to the Teensy.
If someone activates the button a sequence of GET-requests is fired to the webserver which triggers light-effects.
(Note 1: This sequence exists out of a single request with at least 1 second delay till next request)
(Note 2: The server is a standard 80 port server from the light application, allowing you to control the application through webcommands.
The requests are not HTTPS as well as very basic without authentication)
The relay simply activates a standard 5mm LED to indicate visitors triggering the button is allowed.
As long as the sequence is active the LED is off.
(Note 3: all cable lengths are short, no power spikes or anything. Shielded and quality cabling is only used.)
It is important the board needs to run the script 24/7
Please know; using Postman or even Chrome to use the endpoints always worked stable.
I've made this working already first on Pi Pico W -- which was unstable due to the WiFi connection
Then on a Raspberry Pi 4B, where the board always stopped working after several days (Still not sure why, probably coding issue).
After that I've decided to go to a M5 Stack, but I'm still struggling a bit to get this working, need some more time to deep-dive into this.
I've also tried Arduino Uno + Shield but I know (for a while already) that the GET-request I'm making to the webserver (which is a default application server) is really hard to make working.
Not sure why, but all the libraries are just not working for this.
So important is that I want to script this in python. I've seen with the Pico how easy I was able to make it.
script:
So yes, quite some money, time and boards further, and with a bit of pressure from my client I've went safe for the Teensy 4.1 + ethernet module.
I've did previous projects with Teensy so absolutely think they are reliable, but not yet with ethernet requests.
Now I do face issues on getting it working.
--> Technical:
My main problem currently is even getting the network module operational.
I've test the AsyncHttpRequest-Teensy41 in the Arduino IDE which works, however; it indicates the Teensy doesn't get an IP from the DHCP.
Which is interesting, cause the teensy is directly connected to my router through a standard LAN cable. LED indication on the connector; only green is blinking.
if I connect the lan to my computer, I get an IP-address
-- returning back to Python. I'm using Thonny, and first of all I'm trying to make a single request.
I've confirmed my board is operational by running a blink script.
I've also noticed urequests is not supported, so instead after googling I need to use Adafruit Requests.
The module is installed and confirmed as an included plug-in
This results in no module with name 'adafruit_requests' while I still see it in my plug-ins.
Does anyone has a tip to help me a bit on how to make a GET-request with a teensy 4.1 with LAN-connection?
I would be very thankful.
I'm struggling to get this working, hopefully you are help me to guide me a bit.
This is quite a brainbreaker to me, I'm working on this for months now, so please allow me to do a full explanation.
--> Intro:
I'm trying to make the following:
A teensy 4.1 with network module is connected to a computer that runs a webserver.
A button and relay are connected to the Teensy.
If someone activates the button a sequence of GET-requests is fired to the webserver which triggers light-effects.
(Note 1: This sequence exists out of a single request with at least 1 second delay till next request)
(Note 2: The server is a standard 80 port server from the light application, allowing you to control the application through webcommands.
The requests are not HTTPS as well as very basic without authentication)
The relay simply activates a standard 5mm LED to indicate visitors triggering the button is allowed.
As long as the sequence is active the LED is off.
(Note 3: all cable lengths are short, no power spikes or anything. Shielded and quality cabling is only used.)
It is important the board needs to run the script 24/7
Please know; using Postman or even Chrome to use the endpoints always worked stable.
I've made this working already first on Pi Pico W -- which was unstable due to the WiFi connection
Then on a Raspberry Pi 4B, where the board always stopped working after several days (Still not sure why, probably coding issue).
After that I've decided to go to a M5 Stack, but I'm still struggling a bit to get this working, need some more time to deep-dive into this.
I've also tried Arduino Uno + Shield but I know (for a while already) that the GET-request I'm making to the webserver (which is a default application server) is really hard to make working.
Not sure why, but all the libraries are just not working for this.
So important is that I want to script this in python. I've seen with the Pico how easy I was able to make it.
script:
Python:
# Import modules
import network
import urequests
import time
import random
from machine import Pin
# Variables
led = Pin("LED", Pin.OUT)
button = Pin("GP2", Pin.IN, Pin.PULL_UP)
relay = Pin("GP3", Pin.OUT)
activationAllowed = False
connectionAttempt = 1
debug = True
pressed = False
# Important variables
effectDuration = 10
timeOutDuration = 600 #final onsite
#timeOutDuration = 30 #temp debug
# Wlan details
ssid = ""
password = ""
# Connection details
#destinationIp = "192.168.1.148" #Office debug
destinationIp = "192.168.68.105" #Final on-site
wlan = network.WLAN(network.STA_IF)
print("Booting...")
# Connect to WLAN
def connectWifi():
wlan.active(True)
wlan.connect(ssid, password)
print("Check...")
while not wlan.isconnected():
if connectionAttempt == 10:
led.off()
time.sleep(2.5)
print('Reconnecting...')
led.on()
time.sleep(2.5)
continue
time.sleep(5)
led.off()
activationAllowed = False
connectWifi()
print("attempting to (re)connect...attempt: " + str(connectionAttempt))
connectionAttempt += 1
led.on()
time.sleep(0.5)
led.off()
time.sleep(0.5)
pass
print('Connected to WLAN')
attempt = 0
led.on()
relay.on()
def triggeredAction():
global activationAllowed
if activationAllowed == True:
if debug: print("Activation started")
# Disable any new input
activationAllowed = False
# Turn LED pushbutton OFF
relay.value(0)
# Turn internal LED OFF to indicate active state
led.off()
## print(bool(relay.value())) # Debug
# Set fade speed to 5 seconds
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetFadeTime=5")
if debug: print("Statuscode request 1: " + str(r.status_code))
r.close()
# Wait 1 second
time.sleep(1)
# Turn internal LED back ON
led.on()
# Fade to dark
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetStorageDeckB=S2P1")
if debug: print("Statuscode request 2: " + str(r.status_code))
r.close()
# Wait 5 seconds (to complete fade)
time.sleep(5)
# Set fade to speed to 0 for quick change
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetFadeTime=0")
if debug: print("Statuscode request 3: " + str(r.status_code))
r.close()
# Wait 2 seconds
time.sleep(2)
# Trigger random effect (must be chosen between 2, 3 and 4)
val = random.randint(2,4)
if debug: print("Random is: " + str(val))
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetStorageDeckB=S2P" + str(val))
if debug: print("Statuscode request 4: " + str(r.status_code))
r.close()
# Keep effect active for 10 seconds
time.sleep(effectDuration)
# Change fade speed back to 4
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetFadeTime=4")
if debug: print("Statuscode request 5: " + str(r.status_code))
r.close()
# Wait for 4 seconds
time.sleep(4)
# Fade back to dark
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetStorageDeckB=S2P1")
if debug: print("Statuscode request 6: " + str(r.status_code))
r.close()
# Wait for as long as fade will take
time.sleep(4)
# Change fade speed to 1 second
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetFadeTime=1")
if debug: print("Statuscode request 7: " + str(r.status_code))
r.close()
# Wait for 1 second
time.sleep(1)
# Fade back to the normal effect
r = urequests.get("http://" + destinationIp + "/RemoteCommands/SetFadeToDeckA")
if debug: print("Statuscode request 8: " + str(r.status_code))
r.close()
if debug: print("end of activation")
# Disable activation for 10 minutes
time.sleep(timeOutDuration)
if debug: print("new activation is allowed again")
while True:
while wlan.isconnected():
activationAllowed = True
buttonState = button.value()
if not buttonState:
if not pressed:
if debug: print("Button pressed!")
triggeredAction()
pressed = True
# button not pressed (or released) (originally "else:" was here, gave an error)
relay.value(1)
pressed = False
led.off()
time.sleep(0.1)
led.on()
wlan.connect(ssid, password)
time.sleep(2)
So yes, quite some money, time and boards further, and with a bit of pressure from my client I've went safe for the Teensy 4.1 + ethernet module.
I've did previous projects with Teensy so absolutely think they are reliable, but not yet with ethernet requests.
Now I do face issues on getting it working.
--> Technical:
My main problem currently is even getting the network module operational.
I've test the AsyncHttpRequest-Teensy41 in the Arduino IDE which works, however; it indicates the Teensy doesn't get an IP from the DHCP.
Which is interesting, cause the teensy is directly connected to my router through a standard LAN cable. LED indication on the connector; only green is blinking.
if I connect the lan to my computer, I get an IP-address
-- returning back to Python. I'm using Thonny, and first of all I'm trying to make a single request.
I've confirmed my board is operational by running a blink script.
I've also noticed urequests is not supported, so instead after googling I need to use Adafruit Requests.
The module is installed and confirmed as an included plug-in
Python:
import board
import digitalio
import time
import adafruit_requests
# Static IP address of the board
board_ip = "192.168.1.100"
def trigger_get_request():
try:
response = requests.get("http://192.168.1.101/RemoteCommands/SetFadeTime=4")
response.raise_for_status()
except Exception as e:
print("Error:", e)
# Main loop
while True:
print("Ready")
trigger_get_request()
time.sleep(10)
This results in no module with name 'adafruit_requests' while I still see it in my plug-ins.
Does anyone has a tip to help me a bit on how to make a GET-request with a teensy 4.1 with LAN-connection?
I would be very thankful.