Last active
September 23, 2023 05:06
-
-
Save kakopappa/d2659a68f70bb4760292d2362a066e3e to your computer and use it in GitHub Desktop.
Sinric Pro WebRTC Alexa Google Home Pi Camera. Tutorial: https://help.sinric.pro/pages/tutorials/camera/raspberrypi-alexa-googlehome-camera.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
SinricPro, startSinricPro, raiseEvent, eventNames, | |
} = require('sinricpro'); | |
const fetch = require('node-fetch'); | |
const APPKEY = ""; | |
const APPSECRET = ""; | |
const cameraId = ""; | |
const deviceIds = [cameraId]; | |
async function mediamtx(offer) { | |
/* | |
Get the answer from mediamtx `http://<hostname>:8889/<name>/whep`. eg: http://pi3:8889/cam/whep | |
*/ | |
const url = "http://<mediamtx-hostname>:8889/cam/whep"; // TODO: Change <mediamtx-hostname>. | |
const response = await fetch(url, { | |
headers: { | |
"content-type": "application/sdp", | |
}, | |
body: offer, | |
method: "POST", | |
}); | |
// eslint-disable-next-line no-return-await | |
return await response.text(); | |
} | |
const getWebRTCAnswer = async (deviceid, base64Offer) => { | |
// Alexa Eco needs camera stream answer | |
const offer = Buffer.from(base64Offer, 'base64').toString(); | |
try { | |
const answer = await mediamtx(offer); | |
const answerInBase64 = Buffer.from(answer).toString('base64'); | |
return { success: true, answer: answerInBase64 }; | |
} catch (error) { | |
console.error(error); | |
return { success: false }; | |
} | |
}; | |
const setPowerState = async (deviceid, data) => { | |
console.log(deviceid, data); | |
return true; | |
}; | |
const sinricpro = new SinricPro(APPKEY, deviceIds, APPSECRET, false); | |
const callbacks = { setPowerState, getWebRTCAnswer }; | |
startSinricPro(sinricpro, callbacks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment