-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
83 lines (49 loc) · 1.61 KB
/
Copy pathserver.py
File metadata and controls
83 lines (49 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# BUTM Intro Project 2024
# Team 1
# Written by Jack Justus
# This is main file to run on the raspberry pi
# It will setup a TCP server using the netcode library
# It will poll sensor data using the _________ library
#
# Server IP: 192.168.1.2/24
import resources.netcode as net
import resources.sensor as sensor
import time
import random
import constants
ultrasonic_sensor = sensor.sensor_data_collector(constants.trigger_pin,constants.ultrasonic_read_pin, constants.ir_read_pin, time.time())
# ---------------SENSOR GET FUNCTIONS------------------
def get_temperature():
return random.randint(50,80)
def get_ultrasonic():
dis, clos, time = ultrasonic_sensor.collect_data()
return dis
def get_ir():
dis, clos, time = ultrasonic_sensor.collect_data()
return clos
def sensor_two():
return 1
# ---------------RESPONSE TABLE------------------
# Robust way of keeping track of responses
response_table = {
'ir': get_ir,
'ultrasonic': get_ultrasonic,
}
# ---------------SERVER SETUP------------------
# Start the server
piServer = net.TCPServer()
piServer.set_server_address(constants.pi_IP_ADDRESS, constants.router_to_PI_PORT)
piServer.start_server()
def run_server():
# Check for requests
request = piServer.run()
# Look up the function in the table and return it
if request in response_table:
response = response_table[request]() # Call the function
else:
response = "Invalid request"
# Send the response back to the client
piServer.send_response(response)
# ---------------EVENT LOOP------------------
while True:
run_server()