Skip to content
Snippets Groups Projects
Unverified Commit c3712e92 authored by Flora Wang's avatar Flora Wang Committed by GitHub
Browse files

Add files via upload

parent d0c343b9
No related branches found
No related tags found
No related merge requests found
<!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 type="text/javascript" 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; font-weight: bolder;'>&#8679;</span>
<br>
<span id="leftArrow" style='font-size:50px; color:grey; font-weight: bolder;'>&#8678;</span>
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span id="stop" style='font-size:50px; color:grey; font-weight: bolder;'>&#9633;</span>
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span id="rightArrow" style='font-size:50px; color:grey; font-weight: bolder;'>&#8680;</span>
<br>
<span>&nbsp;&nbsp;</span>
<span id="downArrow" style='font-size:50px; color:grey; font-weight: bolder;'>&#8681;</span>
</div>
<div class="jumbotron text-left col-md-6">
<button class="btn btn-success" onclick="update_data()">Start Updating Sensor Data</button>
<p>
<span id="ultrasonics_dot" style="color:green">&bull;</span> Ultrasonic Reading: <span id="ultrasonics">0.0</span>
<br>
<span id="battery_dot" style="color:green">&bull;</span> Battery Life: <span id="battery">0.0</span>
<br>
<span id="temperature_dot" style="color:green">&bull;</span> Pi CPU Temperature: <span id="temperature">0.0</span>
</p>
</div>
</div>
</div>
</body>
</html>
index.js 0 → 100644
var nodeConsole = require("console");
var myConsole = new nodeConsole.Console(process.stdout, process.stderr);
document.onkeydown = updateKey;
document.onkeyup = resetKey;
var server_port = 65432;
var server_addr = "192.168.1.87"; // the IP address of your Raspberry PI
function client(input) {
const net = require("net");
const client = net.createConnection(
{ port: server_port, host: server_addr },
() => {
// 'connect' listener.
myConsole.log("connected to server!");
// send the message
client.write(input);
}
);
// get the data from the server
client.on("data", (data) => {
const sensor_data = JSON.parse(data.toString());
myConsole.log(sensor_data);
document.getElementById("ultrasonics").innerText = sensor_data["ultra"];
document.getElementById("battery").innerText = sensor_data["battery"];
document.getElementById("temperature").innerText = sensor_data["temp"];
client.end();
client.destroy();
});
client.on("end", () => {
myConsole.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_data("up");
} else if (e.keyCode == "83") {
// down (s)
document.getElementById("downArrow").style.color = "green";
send_data("down");
} else if (e.keyCode == "65") {
// left (a)
document.getElementById("leftArrow").style.color = "green";
send_data("left");
} else if (e.keyCode == "68") {
// right (d)
document.getElementById("rightArrow").style.color = "green";
send_data("right");
} else if (e.keyCode == "81") {
// quit (q)
document.getElementById("stop").style.color = "green";
send_data("stop");
}
}
// 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";
document.getElementById("stop").style.color = "grey";
}
function update_data() {
setInterval(function () {
client("sensor");
}, 500);
}
function send_data(text) {
client(text);
}
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.
media/default_background.png

218 KiB

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.4.4"
}
}
// 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
import json
import picar_4wd as fc
from picar_4wd.utils import power_read, cpu_temperature
HOST = "192.168.1.87" # IP address of your Raspberry PI
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
POWER = 10
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
try:
while 1:
client, clientInfo = s.accept()
data = client.recv(1024)
if data == b"sensor":
m = {"ultra": fc.get_distance_at(0),
"battery": power_read(),
"temp": cpu_temperature()}
print("dumping into json: ")
response = json.dumps(m)
print("printing response: ")
print(response)
client.sendall(bytes(response,encoding="utf-8"))
elif data == b"up":
fc.forward(POWER)
elif data==b"left":
fc.turn_left(POWER)
elif data==b"down":
fc.backward(POWER)
elif data==b"right":
fc.turn_right(POWER)
elif data==b"stop":
fc.stop()
except:
print("Closing socket")
client.close()
s.close()
\ 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