Pico 1: button
Pico 2: led and buzzer
Pico 2 code:
Pico 1 code:
import machine
import network
import urequests
import time
# Configure the GPIO pin for the push button
push_button_pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
led = machine.Pin("LED", machine.Pin.OUT)
# Configure the Wi-Fi connection
ssid = "Eshaiker_Net"
password = "c320af3c5a"
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
print("Wi-Fi connected", sta_if.ifconfig())
# Function to send a signal to the other Raspberry Pi Pico
def send_signal():
# Send an HTTP request to the other Raspberry Pi Pico
url = "http://192.168.86.45/led_buzzer" # Replace with the IP address of the other Pico board
response = urequests.get(url)
if response.status_code == 200:
print("Signal sent!")
else:
print("Failed to send signal.")
# Main loop
while True:
if push_button_pin.value() == 0:
led.value(1)
time.sleep(1)
led.value(0)
send_signal()
MQTT
Pico W:
# Import required libraries
import machine
import network
import time
import ubinascii
from umqtt.simple import MQTTClient
# Replace the following with your Wi-Fi credentials
WIFI_SSID = 'Eshaiker_Net'
WIFI_PASS = 'c320af3c5a'
# Replace the following with your MQTT broker information
MQTT_BROKER = '192.168.86.40'
MQTT_PORT = 1883
MQTT_USER = '$6$QrQTu7GO9kgk558s$Wc1ynvTfCODF7SqymI7T44FWgIa51wGg8Umj7m3b2Y4jIm3AGL+Ec6mQ1q6lTwGlh5ORK6sGoknN6Y8G7GPHEA=='
MQTT_PASSWORD = 'jupiter19'
MQTT_TOPIC = b'pico/led'
# Create a unique client ID for MQTT
client_id = ubinascii.hexlify(machine.unique_id())
# Connect to Wi-Fi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASS)
while not sta_if.isconnected():
pass
print('Connected to Wi-Fi:', sta_if.ifconfig())
# Create MQTT client
mqtt_client = MQTTClient(client_id, MQTT_BROKER, port=MQTT_PORT, user=MQTT_USER, password=MQTT_PASSWORD)
# Function to control the LED
def control_led(led, state):
led.value(not state) # LED is usually active-low, so invert the state
# Function to handle incoming MQTT messages
def on_message(topic, msg):
if topic == MQTT_TOPIC:
if msg == b'1':
control_led(led, True)
elif msg == b'0':
control_led(led, False)
# Setup LED pin
led = machine.Pin(1, machine.Pin.OUT)
# Setup button pin
button = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
# Subscribe to the MQTT topic
mqtt_client.set_callback(on_message)
mqtt_client.connect()
mqtt_client.subscribe(MQTT_TOPIC)
try:
while True:
mqtt_client.check_msg() # Check for incoming MQTT messages
if not button.value(): # Button is active-low, so check if it's pressed
mqtt_client.publish(MQTT_TOPIC, b'1') # Publish button press
time.sleep(0.5) # Debounce delay to avoid multiple presses
finally:
mqtt_client.disconnect()
Pi4:
Sudo apt install mosquitto
Sudo apt install mosquitto-client
Sudo systemctl start mosquitto
Sudo systemctl enable mosquitto
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
# Replace the following with your LED pin number
LED_PIN = 17
# Replace the following with your MQTT broker information
MQTT_BROKER = 'your_pi4_ip'
MQTT_PORT = 1883
MQTT_USER = 'your_mqtt_user'
MQTT_PASSWORD = 'your_mqtt_password'
MQTT_TOPIC = 'pico/led'
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
# Function to handle incoming MQTT messages
def on_message(client, userdata, msg):
if msg.topic == MQTT_TOPIC:
if msg.payload == b'1':
GPIO.output(LED_PIN, GPIO.HIGH)
elif msg.payload == b'0':
GPIO.output(LED_PIN, GPIO.LOW)
# Create MQTT client
client = mqtt.Client()
client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
client.on_message = on_message
# Connect to MQTT broker and subscribe to the topic
client.connect(MQTT_BROKER, MQTT_PORT)
client.subscribe(MQTT_TOPIC)
# Start the MQTT client loop to listen for messages
client.loop_forever()
To test:
mosquitto_sub -h your_pi4_ip -u your_mqtt_user -P your_mqtt_password -t pico/led