Skip to content
Snippets Groups Projects
Unverified Commit f4c656dc authored by limanus2's avatar limanus2
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #199883 failed
node_modules
<html></html>
<script src="index.js"></script>
<body>
<p>Hello IoT!</p>
<button class="btn btn-primary" onclick="moveForward()">Forward</button>
<button class="btn btn-primary" onclick="moveBackward()">Backward</button>
<button class="btn btn-primary" onclick="moveLeft()">Left</button>
<button class="btn btn-primary" onclick="moveRight()">Right</button>
<br><br>
<p>Power: <span id="power">0%</span></p>
<p>Ultrasonic Sensor: <span id="ultrasonic">0 cm</span></p>
<p>Direction Angle: <span id="direction"></span></p>
</html>
\ No newline at end of file
index.js 0 → 100644
var server_port = 65432;
var server_addr = "10.165.81.107"; // the IP address of your Raspberry PI
var client = null;
function initConnection() {
const net = require("net");
client = net.createConnection(
{ port: server_port, host: server_addr },
() => {
console.log("Connected to server!");
}
);
client.on("end", () => {
console.log("disconnected from server");
});
}
function call(route, callback) {
let paddedRoute = route.padEnd(1024, " ");
client.write(paddedRoute, (err) => {
const responseHandler = (data) => {
response = data.toString();
console.log("Server response:", response);
callback(response);
};
client.on("data", responseHandler);
});
}
function moveForward() {
call("forward", (response) => {});
}
function moveBackward() {
call("backward", (response) => {});
}
function moveLeft() {
call("left", (response) => {});
}
function moveRight() {
call("right", (response) => {});
}
function getSensorData() {
call("getsensor", (response) => {
let sensorData = response.trim().split(",");
updateSensorData(sensorData[0], sensorData[1], sensorData[2]);
});
}
function updateSensorData(power, ultrasonic, directionAngle) {
document.getElementById("power").innerHTML = power;
document.getElementById("ultrasonic").innerHTML = ultrasonic + " cm";
document.getElementById("direction").innerHTML = directionAngle + "°";
}
initConnection();
// 3 sec
setInterval(getSensorData, 1000);
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,
contextIsolation: false,
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-iot",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"electron": "^32.1.2"
}
}
import socket
from picarx import Picarx
HOST = "10.165.81.107" # IP address of your Raspberry PI
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
dir_angle = 0
power = 0
def get_sensor_data(car):
global power, dir_angle
ultrasonic_distance = int(car.ultrasonic.read())
response = f"{power},{ultrasonic_distance},{dir_angle}"
return response
def forward(car):
global power, dir_angle
power += 5
if power >= 0:
px.forward(power)
else:
px.backward(int(-1 * power))
return ""
def backward(car):
global power, dir_angle
power -= 5
if power >= 0:
px.forward(power)
else:
px.backward(int(-1 * power))
return ""
def left(car):
global power, dir_angle
dir_angle -= 30
car.set_dir_servo_angle(dir_angle)
return ""
def right(car):
global power, dir_angle
dir_angle += 30
car.set_dir_servo_angle(dir_angle)
return ""
# init car
px = Picarx()
px.set_dir_servo_angle(0)
px.set_cam_pan_angle(0)
px.set_cam_tilt_angle(0)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
client, _ = s.accept()
try:
while True:
data = client.recv(1024)
if data != b"":
command = data.decode("utf-8").strip()
print("Received command: ", command)
# Route handling
if command == "forward":
response = forward(px)
elif command == "backward":
response = backward(px)
elif command == "left":
response = left(px)
elif command == "right":
response = right(px)
elif command == "getsensor":
response = get_sensor_data(px)
else:
response = ""
client.send(response.encode("utf-8"))
finally:
client.close()
s.close()
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