Jarvis Cayenne

About This Project

How about you get a personal voice assistant like Jarvis from iron man movie, to control actuator or get the sensor data from the cayenne dashboard. this project does it and is quite simple.
First, get familiar with cayenne API Python code to access basic cayenne API

What’s Connected

Arduino, raspberry pi or any device connected to cayenne.
A device like raspberry pi with a mic and speaker to get receive the audio and playback. (i have used my laptop)

Dashboard Screenshots

Video

Code:

import os
import time
import speech_recognition as sr
import pyttsx3
import requests
import base64

import configparser

config = configparser.ConfigParser()

mqtt_username = ""
mqtt_password = ""
client_id = ""
client_secret = ""
username = ""
password = ""
device_id = ""
sensor_id_temp = ""
channel_light = 5

Wake = "hey jarvis"


def speak(text):
    engine = pyttsx3.init()
    engine.setProperty('voice', 'com.apple.speech.synthesis.voice.daniel')
    engine.say(text)
    engine.runAndWait()


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))
    return said.lower()


def get_mqtt_token(mqtt_username, mqtt_password):
    string = mqtt_username + ':' + mqtt_password
    print(string)
    data = base64.b64encode(string.encode())
    mqtt_token = data.decode("utf-8")
    #config['DEFAULT']['mqtt_token'] = mqtt_token
    # with open('credentials.ini', 'w') as configfile:
    #        config.write(configfile)
    return (mqtt_token)


def get_access_token(client_id, client_secret, username, password):
    response = requests.post('https://accounts.mydevices.com/auth/realms/cayenne/protocol/openid-connect/token',
                             data={'grant_type': 'password',
                                   'client_id': client_id,
                                   'client_secret': client_secret,
                                   'username': username,
                                   'password': password},
                             headers={'content-type': 'application/x-www-form-urlencoded'})
    payload = response.json()
    access_token = payload['access_token']
    print('Access_Token = ' + access_token)
    #config['DEFAULT']['access_token'] = access_token
    # with open('credentials.ini', 'w') as configfile:
    #        config.write(configfile)
    return (access_token)


def get_latest_history(access_token, device_id, sensor_id):
    bearer = 'Bearer ' + access_token
    URL = 'https://platform.mydevices.com/v1.1/telemetry/' + \
        device_id+'/sensors/'+sensor_id+'/summaries?type=latest'
    response = requests.get(URL,
                            headers={'authorization': bearer},)
    payload = response.json()
    data = payload[0]['v']
    print('Data history = %d' % data)
    return data


def publish_actuator_data(access_token, device_id, channel, value):
    #            URL = 'https://api.mydevices.com/things/'+device_id+'/data'
    #URL = 'https://platform.mydevices.com/v1.1/telemetry/' +device_id+'/cmd'
    URL = 'https://platform.mydevices.com/v1.1/things/'+device_id+'/cmd'
    basic = 'Bearer ' + access_token
    headers = {'authorization': basic,
               'content-type': 'application/json'}
    data = {'value': value,
            'channel': channel
            }
    response = requests.post(URL, json=data, headers=headers)
    print(response)

#speak("welcome to cayenne smart home")


access_token = get_access_token(client_id, client_secret, username, password)
mqtt_token = get_mqtt_token(mqtt_username, mqtt_password)

print(access_token)
speak("hello")

while True:
    print("listing")
    text = get_audio()
    if text.count(Wake) > 0:
        print("what")
        speak("Hi shramik. what do you want me to do")
        text = get_audio()

        if "what is the temperature in the living room" in text:
            data = get_latest_history(access_token, device_id, sensor_id_temp)
            #data = "the current temperature " + data
            data = 'the living room temperature is %d celsious' % data
            print(data)
            speak(data)

        elif "turn on the living room light" in text:
            publish_actuator_data(access_token, device_id, channel_light, 1)
            speak("the living room light is turned on")

        elif "turn off the living room light" in text:
            publish_actuator_data(access_token, device_id, channel_light, 0)
            speak("the living room is light turned off")
1 Like