-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino code to calibrate the voltage sensor and measure voltage.txt
89 lines (71 loc) · 2.22 KB
/
arduino code to calibrate the voltage sensor and measure voltage.txt
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
83
84
85
86
87
88
89
# Import necessary libraries
import network
import socket
from machine import UART
import machine
import esp
import gc
import time
import os
# Perform initial setup
gc.collect()
esp.osdebug(None)
# Set up Wi-Fi connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
w = "Galaxy A73 5G 5354" # WiFi name
p = "oqry3557" # WiFi password
# Create a socket for communication
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Try to connect to the WiFi network
try:
if wlan.isconnected() == False:
wlan.connect(w, p)
except:
print('Not connected, please check if the WiFi network is active')
# Wait until the device is connected to the WiFi network
while wlan.isconnected() == False:
pass
# Print a message indicating successful connection
print(f'Connected successfully to {w}')
# Set up host and port for socket communication
HOST = wlan.ifconfig()[0]
PORT = 9999
# Bind the socket to the host and port
s.bind((HOST, PORT))
s.listen(5)
# Set up UART communication
uart = UART(1, baudrate=9600, tx=17, rx=16)
# Set public key values for RSA encryption
n = 6910782171213996658397253121710630123795235324197483219005527369298065059694620267925472189305202037262152652525687783466320955431741311010423968405213211
e = 65537
# Main loop
while True:
# Check if there is data available on UART
if uart.any():
# Read 8 bytes of data from UART
data = uart.read(8)
if data:
pass
else:
# If no data, set default value
data = b'Voltage : 0'
else:
# If no data, set default value
data = b'Voltage : 0'
# Convert bytes to integer
num = int.from_bytes(data, "big")
# Perform RSA encryption
x = pow(num, e, n)
# Accept incoming socket connection
conn, addr = s.accept()
# Print information about the connection
print('Got a connection from %s' % str(addr))
# Convert the result to a string and send it through the socket
html = str(x)
html = html.replace('\r\n', '\n')
conn.sendall(html)
# Wait for a short duration
time.sleep(0.1)
# Close the socket connection
conn.close()