Dome panels
The PSI lights are connected to a Raspberry Pi Pico WH and the SG90 servos which move the dome panels are connected to a Waveshare Servo Driver Module for Raspberry Pi Pico.
Metal Dome
Servo Control:
The script uses PWM to control servos connected to GPIO pins.
Functions like OpenServo, CloseServo, OpenServos345, and CloseServos345 are defined for specific servo movements.
Neopixel LED Control:
The script utilizes the neopixel module to control WS2812 LEDs.
LED color functions (loop, black, wheel, rainbowloop, bluefadeloop, blueredtransition, redfadeloop, greenfadeloop) are defined for different lighting effects.
MQTT Communication:
The script connects to an MQTT broker (mqtt_server) and subscribes to a specific topic (topic_pub) to receive commands.
The new_message_callback function handles incoming MQTT messages and triggers different actions based on the received commands.
Main Loop:
The main loop continuously executes the current loop function (current_loop_function) and checks for incoming MQTT messages.
It periodically sends "Keep alive" messages to the MQTT broker to maintain the connection.
Loop Function Change:
The script periodically changes the loop function to create different effects, switching between bluefadeloop and blueredtransition every 5 minutes.
Please note that this script relies on specific hardware connections, such as servos connected to GPIO pins and Neopixel LEDs connected to pin 28. Additionally, it expects the existence of a module (python script) named do_connect for network connection setup.
Code :
You will need to install umqtt.simple
Then with the pico plugged into you PC import mip
# mip.install("umqtt.simple")
# mip.install('umqtt.robust')
sudo apt-get install python-pip
sudo pip install RPi.GPIO
sudo apt-get install python-smbus
# For more information on the servo contoller see URL https://www.waveshare.com/wiki/Servo_Driver_HAT
1. Import Statements and Setup:
This section imports required libraries for networking, MQTT communication, GPIO control, PWM, time, and Neopixel LEDs.# Import necessary libraries and modules
import network
import time
from umqtt.simple import MQTTClient
import machine
from machine import Pin, PWM
from time import sleep
import array
import neopixel
2. Servo and PWM Initialization:
This section initializes and configures PWM (Pulse Width Modulation) for controlling servos connected to GPIO pins.# Define the number of servos
num_servos = 16
# Create a list to store PWM objects for each servo
servo_pwm_list = [PWM(Pin(i)) for i in range(num_servos)]
# Set the PWM frequency for all servos
for servo_pwm in servo_pwm_list:
servo_pwm.freq(50) # 20ms
servo_pwm.duty_u16(0)
3. Neopixel LED Configuration:
Here, Neopixel LED configuration is set up with the pin number, number of LEDs, and brightness.ws_pin = 28
led_num = 8
brightness = 0.2
neoRing = neopixel.NeoPixel(Pin(ws_pin), led_num)
4. Network Connection and MQTT Configuration:
This section sets up network and MQTT parameters such as the broker address, client ID, credentials, and topics.mqtt_server = '192.168.8.184'
client_id = 'DomeServo1'
user_t = 'pico'
password_t = 'picopassword'
topic_pub = 'DomeServo1'
# MQTT message interval and keep-alive time
last_message = 0
message_interval = 5
keep_alive = 30
5. Neopixel LED Functions:
def set_brightness(color):
# Function to adjust LED color brightness
...
def loop():
# Function to display different colors sequentially
...
def black():
# Function to turn off LEDs (display black color)
...
# Other Neopixel LED functions for various effects (rainbow, fade, transition, etc.)
...
6. Servo Control Functions:
# Functions for opening and closing servos
def OpenServo(channel, sposition):
...
def CloseServo(channel, sposition):
...
# Functions for opening and closing multiple servos simultaneously
def OpenServos345(sposition):
...
def CloseServos345(sposition):
...
7. MQTT Connection and Message Handling:
This section attempts to establish an MQTT connection and subscribe to a topic. It also sets up a callback function for handling incoming messages.try:
client = mqtt_connect()
client.set_callback(new_message_callback)
client.subscribe(topic_pub.encode('utf-8'))
except OSError as e:
reconnect()
8. Main Loop:
This section constitutes the main loop that continuously executes the current loop function, checks for incoming MQTT messages, and sends "Keep alive" messages.while True:
try:
current_loop_function() # Call the current loop function
client.check_msg() # Check for incoming MQTT messages
time.sleep(0.001)
# Check if it's time to change the loop function
if time.time() > next_loop_change_time:
# Change the loop function and set the next change time
...
# Send "Keep alive" messages periodically
if (time.time() - last_message) > keep_alive:
client.publish(topic_pub, "Keep alive message")
last_message = time.time()
except OSError as e:
reconnect() # Reconnect in case of an error
pass
9.MQTT Message Callback Function:
This function processes incoming MQTT messages and triggers different actions based on the received commands.def new_message_callback(topic, msg):
# Function to handle incoming MQTT messages and trigger actions based on commands
...
Free AI Website Software