MohammadReza Sharifi
Published © MIT

Integrating Google Gemini with Home Assistant and RPi Pico W

In this project, we connect Raspberry Pi Pico W, Home Assistant and Google Gemini by MQTT Protocol.

AdvancedWork in progress8 hours911
Integrating Google Gemini with Home Assistant and RPi Pico W

Things used in this project

Story

Read more

Schematics

circuit diagram

circuit diagram

Code

python script for Gemini

Python
import google.generativeai as genai
import paho.mqtt.publish as publish
import random
import paho.mqtt.client as mqtt
from time import sleep
# MQTT parameters
MQTT_SERVER = "192.168.1.232" #mqtt broker ip
MQTT_PATH = "test" #topic
port = 1883
'''
broker = '192.168.1.232'
port = 1883
topic = "test"
'''
final_msg = []

client_id = f'python-mqtt-{random.randint(0, 1000)}'

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc, properties=None):
    client.username_pw_set('mqtt', 'sharifi77mm98')

    print("Connected with result code "+str(rc))
    
    client.subscribe("question")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    #print(msg.payload.decode())
    message = msg.payload.decode()
    final_msg.append(message)
    print(message)
# Create an MQTT client and attach our routines to it.
# Specify the new callback API version
client = mqtt.Client(client_id=client_id, callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
client.on_connect = on_connect
client.on_message = on_message

client.connect(MQTT_SERVER, port, 60)

# Process network traffic and dispatch callbacks.
client.loop_start()


genai.configure(api_key="AIzaSyDSFt21TMaIhJOahjg6YxRA2DStFrG7sAY")

model = genai.GenerativeModel('gemini-1.0-pro-latest')


while True:
    if final_msg:
        nodered_message = final_msg.pop(0)
        #print(nodered_message)
        response = model.generate_content(str(nodered_message))
        print(response.text)
        publish.single("response", response.text, hostname=MQTT_SERVER, auth={'username':"mqtt", 'password':"sharifi77mm98"})
        sleep(1)
    else:
        continue
        
    

main program on RPi Pico W microcontroller

MicroPython
import network
from time import sleep
from umqtt.simple import MQTTClient
import ujson
from machine import Pin, ADC
import dht
import _thread

dht_sensor = dht.DHT11(Pin(15))
analog_value = ADC(Pin(26))
light = Pin(14,Pin.OUT)

def air_params():
    #sleep(2)
    dht_sensor.measure()
    temp = dht_sensor.temperature()
    hum = dht_sensor.humidity()
    #CO_value = analog_value.read_u16()
    
    return temp, hum

# Network credentials
WIFI_SSID = 'Mrsh77'
WIFI_PASSWORD = '1m77n2299215r77#'

# MQTT credentials
MQTT_BROKER = '192.168.1.232'
MQTT_PORT = 1883
MQTT_TOPIC = 'pico'

msg_from_node_red = []

# Connect to WiFi
def connect_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('Connecting to network...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    print('Network config:', wlan.ifconfig())

# Callback function to handle messages
def on_message(topic, msg):
    #print('Received message:', msg)
    msg_from_node_red.append(msg)
# Setup MQTT client and connect to the broker
def setup_mqtt():
    
    client = MQTTClient(client_id='pico_w_client', server=MQTT_BROKER, port=MQTT_PORT, user="mqtt", password="sharifi77mm98")
    

    client.set_callback(on_message)
    client.connect()
    client.subscribe(MQTT_TOPIC)
    print('Connected to %s, subscribed to %s topic.' % (MQTT_BROKER, MQTT_TOPIC))
    return client


connect_wifi(WIFI_SSID, WIFI_PASSWORD)

client = setup_mqtt()

def send_params():
    while True:
        temperature, humidity = air_params()
        sensor_data = {
            "temperature": temperature,
            "humidity": humidity
            }

        json_data = ujson.dumps(sensor_data)
        client.publish("pico_in",json_data)
        sleep(1)

def core1_task():
    try:
        while True:
            client.check_msg()
            if msg_from_node_red:
                
                final_msg = msg_from_node_red.pop(0)
                print(final_msg)
                if final_msg == b'light on':
                    light.value(1)
                elif final_msg == b'light off':
                    light.value(0)
                sleep(1)
    except KeyboardInterrupt:
        print('Disconnected from MQTT Broker.')
    finally:
        client.disconnect()


_thread.start_new_thread(core1_task, ())

send_params()

Node-RED Block Diagram

JSON
[{"id":"2e86cc79543dd949","type":"ui_microphone","z":"ab42f0c2b344442b","name":"controller","group":"673ac7a1e346be05","order":1,"width":0,"height":0,"maxLength":5,"maxRecogLength":5,"timeslice":0,"press":"press","mode":"recog","interimResults":false,"x":80,"y":280,"wires":[["6d2432cc9a571c09"]]},{"id":"673ac7a1e346be05","type":"ui_group","name":"controller","tab":"4c2a2d8b4284e446","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"4c2a2d8b4284e446","type":"ui_tab","name":"Egoo","icon":"dashboard","disabled":false,"hidden":false}]

source code on my github page

Credits

MohammadReza Sharifi

MohammadReza Sharifi

13 projects • 6 followers
I'm an Electrical Engineer and Maker.

Comments