revdeluxe / SPAS

Smart Public Address System
GNU General Public License v3.0
0 stars 0 forks source link

Create a communication link between ESP32(node) and Raspberry Pi(server) #4

Open revdeluxe opened 4 months ago

revdeluxe commented 4 months ago

exploration of using OPENSIPS as the program's SIP/RTC manager. https://opensips.org/Documentation/Manuals

Spasv3 commented 4 months ago

const express = require('express'); const http = require('http'); const JsSIP = require('jssip');

const app = express(); const server = http.createServer(app);

const kamailioConfig = { host: '192.168.0.23', port: 5060, username: 'SPASv3', password: 'spasv3' };

const socket = new JsSIP.WebSocketInterface(ws://${kamailioConfig.host}:5066);

const userAgentConfig = { uri: sip:${kamailioConfig.username}@${kamailioConfig.host}, authorization_user: kamailioConfig.username, password: kamailioConfig.password, sockets: [socket] };

const userAgent = new JsSIP.UA(userAgentConfig);

// Event handlers for JsSIP userAgent.on('connected', () => { console.log('Connected to SIP server'); });

userAgent.on('disconnected', () => { console.log('Disconnected from SIP server'); });

userAgent.on('failed', (e) => { console.error('Connection failed:', e); });

// Start JsSIP User Agent userAgent.start();

// Express route for making calls app.get('/call/:destination', (req, res) => { const destination = req.params.destination;

const callOptions = { mediaConstraints: { audio: true, video: false }, pcConfig: { iceServers: [] } };

const session = userAgent.invite(sip:${destination}@${kamailioConfig.host}, callOptions);

session.on('accepted', () => { console.log('Call accepted'); res.send('Call accepted'); });

session.on('failed', () => { console.log('Call failed'); res.status(500).send('Call failed'); }); });

// Express route for receiving calls app.post('/incoming-call', (req, res) => { // Handle incoming call here res.send('Incoming call'); });

// Start the server server.listen(3000, () => { console.log('Server is running on port 3000'); });

Spasv3 commented 4 months ago

const express = require('express'); const http = require('http'); const WebSocket = require('ws'); const JsSIP = require('jssip');

const app = express(); const server = http.createServer(app);

// WebSocket server for receiving audio data const wss = new WebSocket.Server({ server, path: '/audio' });

let currentSession = null;

wss.on('connection', (ws) => { console.log('WebSocket connection established');

ws.on('message', (message) => { console.log('Received audio data:', message); if (currentSession) { // Here you would forward the audio data to the SIP session // This part requires implementing media handling in JsSIP } });

ws.on('close', () => { console.log('WebSocket connection closed'); }); });

const kamailioConfig = { host: '192.168.0.23', port: 5060, username: 'SPASv3', password: 'spasv3' };

const socket = new JsSIP.WebSocketInterface(ws://${kamailioConfig.host}:5066);

const userAgentConfig = { uri: sip:${kamailioConfig.username}@${kamailioConfig.host}, authorization_user: kamailioConfig.username, password: kamailioConfig.password, sockets: [socket] };

const userAgent = new JsSIP.UA(userAgentConfig);

// Event handlers for JsSIP userAgent.on('connected', () => { console.log('Connected to SIP server'); });

userAgent.on('disconnected', () => { console.log('Disconnected from SIP server'); });

userAgent.on('failed', (e) => { console.error('Connection failed:', e); });

// Start JsSIP User Agent userAgent.start();

// Express route for the root URL app.get('/', (req, res) => { res.send('Welcome to the SIP server'); });

// Express route for making calls app.get('/call/:destination', (req, res) => { const destination = req.params.destination;

const callOptions = { mediaConstraints: { audio: true, video: false }, pcConfig: { iceServers: [] } };

currentSession = userAgent.invite(sip:${destination}@${kamailioConfig.host}, callOptions);

currentSession.on('accepted', () => { console.log('Call accepted'); res.send('Call accepted'); });

currentSession.on('failed', () => { console.log('Call failed'); res.status(500).send('Call failed'); }); });

// Express route for receiving calls app.post('/incoming-call', (req, res) => { // Handle incoming call here res.send('Incoming call'); });

// Start the server on port 3002 server.listen(3002, () => { console.log('Server is running on port 3002'); });

Spasv3 commented 4 months ago

include

include

include <driver/i2s.h>

const char ssid = "converge29"; const char password = "byanang_29";

const char* serverIP = "192.168.0.23"; const uint16_t serverPort = 3003; // Update this to match the new port

WebSocketsClient webSocket;

const i2s_port_t I2S_PORT = I2S_NUM_0; const i2s_config_t i2s_config = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = 44100, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, .communication_format = I2S_COMM_FORMAT_I2S_MSB, .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, .dma_buf_count = 8, .dma_buf_len = 64, .use_apll = false, .tx_desc_auto_clear = true, .fixed_mclk = 0 };

void setup() { Serial.begin(115200);

// Connect to WiFi Serial.print("Connecting to WiFi"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected.");

// Initialize I2S i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL); i2s_set_pin(I2S_PORT, NULL); i2s_zero_dma_buffer(I2S_PORT);

// Connect to WebSocket server Serial.println("Connecting to WebSocket server"); webSocket.begin(serverIP, serverPort, "/audio"); webSocket.onEvent(webSocketEvent); }

void loop() { // Check WebSocket connection webSocket.loop();

// Read audio data from I2S int16_t samples[512]; size_t bytesRead; i2s_read(I2S_PORT, (void*)samples, sizeof(samples), &bytesRead, portMAX_DELAY);

// Send audio data over WebSocket if (bytesRead > 0) { if (webSocket.isConnected()) { webSocket.sendBIN((const uint8_t*)samples, bytesRead); } else { Serial.println("WebSocket not connected, cannot send data"); } } }

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch (type) { case WStype_DISCONNECTED: Serial.println("WebSocket Disconnected"); break; case WStype_CONNECTED: Serial.println("WebSocket Connected"); break; case WStype_BIN: // Handle incoming binary data here if needed break; } }

Spasv3 commented 4 months ago

const express = require('express'); const http = require('http'); const WebSocket = require('ws'); const JsSIP = require('jssip');

const app = express(); const server = http.createServer(app);

// WebSocket server for receiving audio data const wss = new WebSocket.Server({ server, path: '/audio' });

let currentSession = null;

wss.on('connection', (ws) => { console.log('WebSocket connection established');

ws.on('message', (message) => { console.log('Received audio data:', message); if (currentSession) { // Forward the audio data to the SIP session const pc = currentSession.connection; const sender = pc.getSenders().find(s => s.track.kind === 'audio'); if (sender) { // Create a new AudioFrame and push the data into the RTP stream const audioFrame = new AudioFrame({ samples: new Int16Array(message), sampleRate: 44100, channels: 2 }); sender.track.processor.push(audioFrame); } } });

ws.on('close', () => { console.log('WebSocket connection closed'); }); });

const kamailioConfig = { host: '192.168.0.23', port: 5060, username: 'SPASv3', password: 'spasv3' };

const socket = new JsSIP.WebSocketInterface(ws://${kamailioConfig.host}:5066);

const userAgentConfig = { uri: sip:${kamailioConfig.username}@${kamailioConfig.host}, authorization_user: kamailioConfig.username, password: kamailioConfig.password, sockets: [socket] };

const userAgent = new JsSIP.UA(userAgentConfig);

// Event handlers for JsSIP userAgent.on('connected', () => { console.log('Connected to SIP server'); });

userAgent.on('disconnected', () => { console.log('Disconnected from SIP server'); });

userAgent.on('failed', (e) => { console.error('Connection failed:', e); });

// Start JsSIP User Agent userAgent.start();

// Express route for the root URL app.get('/', (req, res) => { res.send('Welcome to the SIP server'); });

// Express route for making calls app.get('/call/:destination', (req, res) => { const destination = req.params.destination;

const callOptions = { mediaConstraints: { audio: true, video: false }, pcConfig: { iceServers: [] } };

currentSession = userAgent.invite(sip:${destination}@${kamailioConfig.host}, callOptions);

currentSession.on('accepted', () => { console.log('Call accepted'); res.send('Call accepted'); });

currentSession.on('failed', () => { console.log('Call failed'); res.status(500).send('Call failed'); }); });

// Express route for receiving calls app.post('/incoming-call', (req, res) => { // Handle incoming call here res.send('Incoming call'); });

// Start the server on port 3003 server.listen(3003, () => { console.log('Server is running on port 3003'); });