Skip to content
Snippets Groups Projects
Commit 0da7c728 authored by nwp2's avatar nwp2
Browse files

Update 8 files

- /index.js
- /main.js
- /package.json
- /package-lock.json
- /pi_server.py
- /preload.js
- /renderer.js
- /index.html
parents
No related branches found
No related tags found
No related merge requests found
Pipeline #202117 failed
<!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">0.0</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="ultra_dot" style="color:green">&bull;</span> Ultrasonci Distance: <span id="ultrasonic">0.0</span>
<br>
Car return value: <span id="bluetooth"> </span>
</p>
</div>
</div>
</div>
</body>
</html>
index.js 0 → 100644
document.onkeydown = updateKey;
document.onkeyup = resetKey;
var server_port = 65432;
var server_addr = "192.168.4.127"; // the IP address of your Raspberry PI
var pi_client = null;
function connect_pi() {
const net = require('net');
const client = net.createConnection({ port: server_port, host: server_addr }, () => {
// 'connect' listener.
console.log('connected to server!');
// send the message
pi_client = client;
setInterval(()=>{
pi_client.write(' ');
}, 100)
});
client.on("data", (data) => {
data = new TextDecoder().decode(data)
// [traveled, speed, direction, dist]
split = data.split(',')
document.getElementById("bluetooth").innerHTML = data;
document.getElementById("direction").innerHTML = split[2];
document.getElementById("speed").innerHTML = split[1]
document.getElementById("distance").innerHTML = split[0]
document.getElementById("ultrasonic").innerHTML = split[3];
});
}
function send(str){
if(pi_client) pi_client.write(str)
}
// 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;
// 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) {
e = e || window.event;
if (e.keyCode == '87') {
// up (w)
document.getElementById("upArrow").style.color = "green";
send("w");
}
else if (e.keyCode == '83') {
// down (s)
document.getElementById("downArrow").style.color = "green";
send("s");
}
else if (e.keyCode == '65') {
// left (a)
document.getElementById("leftArrow").style.color = "green";
send("a");
}
else if (e.keyCode == '68') {
// right (d)
document.getElementById("rightArrow").style.color = "green";
send("d");
}
}
// 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";
}
connect_pi();
\ No newline at end of file
main.js 0 → 100644
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1000,
height: 1000,
webPreferences: {
nodeIntegration: true,
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.
This diff is collapsed.
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^9.1.2"
}
}
import socket
from picarx import Picarx
from time import sleep
px = Picarx()
traveled = 0
direction = 0
def forward():
px.set_dir_servo_angle(0)
px.forward(1)
sleep(1)
global traveled
traveled += 10
px.forward(0)
def backward():
px.set_dir_servo_angle(0)
px.backward(1)
sleep(1)
global traveled
traveled -= 10
px.forward(0)
def left():
px.set_dir_servo_angle(-40)
px.forward(1)
sleep(1)
global traveled
global direction
direction -= 18.5
traveled += 10
px.forward(0)
def right():
px.set_dir_servo_angle(40)
px.forward(1)
sleep(1)
global traveled
global direction
direction += 18.5
traveled += 10
px.forward(0)
HOST = "192.168.4.127" # IP address of your Raspberry PI
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
try:
while 1:
client, clientInfo = s.accept()
print("server recv from: ", clientInfo)
while True:
data = client.recv(1024) # receive 1024 Bytes of message in binary format
if data != b"":
speed = 0
if(data == b'w'):
forward()
speed = 10
if(data == b's'):
backward()
speed = 10
if(data == b'a'):
left()
speed = 10
if(data == b'd'):
right()
speed = 10
dist = px.get_distance()
client.sendall(f"{traveled},{speed},{direction},{dist}".encode()) # Echo back to client
except:
print("Closing socket")
client.close()
s.close()
\ No newline at end of file
// 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
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