Skip to content
Snippets Groups Projects
Commit 54186b0f authored by Sidharth Jain's avatar Sidharth Jain
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
.DS_Store 0 → 100644
File added
File added
File added
import bluetooth
import json
target_name = "raspberrypi"
sock = None
# TODO: Implement server for p2p comms
def start_client():
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
print(bluetooth.lookup_name( bdaddr ))
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print ("found target bluetooth device with address ", target_address)
else:
print ("could not find target bluetooth device nearby")
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((target_address, port))
def set_target(target):
target_name = target
def send_data(data):
sock.send(json.dumps(data))
def terminate():
sock.close()
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.container{
display: flex;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="./index.js"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h2>CS498: IoT -- Lab 2</h2>
<!-- <video class="center" width="600" height="400" controls>
<source src="samplevid.mp4" type="video/mp4">
Your browser does not support the video tag.
</video> -->
<img src="./media/default_background.png" id='pics'>
</div>
</div>
<div class="row">
<div class="container">
<div class="jumbotron text-center col-md-6">
<span>&nbsp;&nbsp;</span>
<span id="upArrow" style='font-size:50px; color:grey;'>&#8679;</span>
<br>
<span id="leftArrow" style='font-size:45px; color:grey;'>&#8678;</span>
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span id="rightArrow" style='font-size:45px; color:grey;'>&#8680;</span>
<br>
<span>&nbsp;&nbsp;</span>
<span id="downArrow" style='font-size:50px; color:grey;'>&#8681;</span>
</div>
<div class="jumbotron text-left col-md-6">
<input id="message" type="text" placeholder="message to Pi"/>
<button class="btn btn-success" onclick="update_data()">Sumbit</button>
<p>
<span id="direction_dot" style="color:Green">&bull;</span> Car Direction: <span id="direction"> </span>
<br>
<span id="speed_dot" style="color:green">&bull;</span> Speed: <span id="speed">0.0</span>
<br>
<span id="distance_dot" style="color:green">&bull;</span> Distance Traveled: <span id="distance">0.0</span>
<br>
<span id="temprature_dot" style="color:green">&bull;</span> Temperature of the Pi: <span id="temperature">0.0</span>
<br>
Bluetooth return value: <span id="bluetooth"> </span>
</p>
</div>
</div>
</div>
</body>
</html>
document.onkeydown = updateKey;
document.onkeyup = resetKey;
var server_port = 1026;
var server_addr = "192.168.3.2"; // the IP address of your Raspberry PI
document.addEventListener("DOMContentLoaded", function() {
update_data();
});
// var return_data = function()
// {
// // var text = "hi";
// const fs = require('fs')
// fs.readFile('/home/dabdoue/iot-labs/iot-lab-2/electron/message.txt', 'utf8', (err, data) => {
// blue_message = data.toString();
// if (err) {
// // return data;
// console.error(err)
// }
// })
// };
function client(){
const net = require('net');
var input = document.getElementById("message").value;
const client = net.createConnection({ port: server_port, host: server_addr }, () => {
// 'connect' listener.
console.log('connected to server!');
// send the message
client.write(`${input}\r\n`);
});
// get the data from the server
client.on('data', (data) => {
// document.getElementById("bluetooth").innerHTML = data;
str_data = data.toString();
all_data = str_data.split(",");
document.getElementById("direction").innerHTML = all_data[0];
document.getElementById("speed").innerHTML = all_data[1];
document.getElementById("distance").innerHTML = all_data[2];
document.getElementById("temperature").innerHTML = all_data[3];
// return_data();
console.log(data.toString());
client.end();
client.destroy();
});
client.on('end', () => {
console.log('disconnected from server');
});
}
// for detecting which key is been pressed w,a,s,d
function updateKey(e) {
const net = require('net');
e = e || window.event;
dir = "";
if (e.keyCode == '87')
{
// up (w)
document.getElementById("upArrow").style.color = "green";
dir = "up"
}
else if (e.keyCode == '83')
{
// down (s)
document.getElementById("downArrow").style.color = "green";
dir = "down"
}
else if (e.keyCode == '65')
{
// left (a)
document.getElementById("leftArrow").style.color = "green";
dir = "left"
}
else if (e.keyCode == '32')
{
// left (a)
dir = "stop"
}
else if (e.keyCode == '68')
{
// right (d)
document.getElementById("rightArrow").style.color = "green";
dir = "right"
}
const client = net.createConnection({ port: server_port, host: server_addr }, () =>
{
// 'connect' listener.
console.log('connected to server! in updateKey function');
// send the message
if (dir != "")
{
client.write(`${dir}`);
}
});
}
function printer(){
console.log("upkey");
}
// reset the key to the start state
function resetKey(e) {
e = e || window.event;
document.getElementById("upArrow").style.color = "grey";
document.getElementById("downArrow").style.color = "grey";
document.getElementById("leftArrow").style.color = "grey";
document.getElementById("rightArrow").style.color = "grey";
}
// update data for every 50ms
function update_data()
{
setInterval(function ()
{
// get image from python server
client();
// updateKey(e);
}, 50);
}
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// const net = require('net');
try {
require('electron-reloader')(module);
} catch {}
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1000,
height: 1000,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js')
// nodeIntegration: false, // Disable Node.js integration
// contextIsolation: true, // Enable context isolation
// preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
// if (process.platform !== 'darwin') app.quit()
app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
\ No newline at end of file
File added
electron/media/default_background.png

218 KiB

File added
{
"name": "electron",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "electron",
"version": "1.0.0",
"license": "ISC"
}
}
}
{
"name": "electron",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^9.1.2"
},
"dependencies": {
"electron-reloader": "^1.2.1"
}
}
// const { contextBridge, ipcRenderer } = require('electron');
// contextBridge.exposeInMainWorld('ipcRenderer', {
// send: (channel, data) => ipcRenderer.send(channel, data),
// on: (channel, func) =>
// ipcRenderer.on(channel, (event, ...args) => func(...args)),
// });
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
\ No newline at end of file
import socket
HOST = "100.70.253.115" # IP address of your Raspberry PI
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while 1:
text = input("Enter your message: ") # Note change to the old (Python 2) raw_input
if text == "quit":
break
s.send(text.encode()) # send the encoded message (send in binary format)
data = s.recv(1024)
print("from server: ", data)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment