-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHelperCode.py
More file actions
147 lines (122 loc) · 3.5 KB
/
MyHelperCode.py
File metadata and controls
147 lines (122 loc) · 3.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import network
import usocket
import socket
from time import time, sleep
import network
import time
import machine
from picozero import pico_temp_sensor
from machine import Pin
import micropython
import ustruct as struct
import json
Self_Name = 'PW_1'
SSID = ''
PASSWORD = ''
ssidRouter = '' #Enter the router name
passwordRouter = '' #Enter the router password
ssidAP = 'Pico W' #Enter the AP name
passwordAP = '12345678' #Enter the AP password
local_IP = '192.168.4.150'
gateway = '192.168.4.1'
subnet = '255.255.255.0'
dns = '8.8.8.8'
adcpin = 4
sensor = machine.ADC(adcpin)
sock = usocket
# SERVER_HOSTNAME = "e5d2174059b64286bd5f243dd055355a.s1.eu.hivemq.cloud:8884/mqtt"
USER = "Cuthbert"
# PASSWORD = 'Cbaines123!'
wlan = network.WLAN(network.STA_IF)
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
def read_json_file(file_path):
"""
Function for reading json file
"""
try:
with open(file_path, "r") as file:
data = json.load(file)
except:
return None
else:
return data
def write_json_file(file_path, data):
"""
Function for writing json file
"""
try:
with open(file_path, "w") as file:
json.dump(data, file)
except:
return None
else:
return data
def GetTemperature():
adc_value = sensor.read_u16()
volt = (3.3/65535) * adc_value
temperature = 27 - (volt - 0.706)/0.001721
temp = str(round(temperature, 1))
# timestamp = datetime.now().isoformat()
print(temp)
return temp
def connect_to_wifi():
SSID = 'BB'
PASSWORD = '6KH1jk1mn0s'
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Connecting to Wi-Fi...")
while not wlan.isconnected():
time.sleep(1)
print("Waiting for connection...")
else:
print("Wi-Fi connected!")
print("IP address:", wlan.ifconfig())
def to_json(temperature):
# Create a dictionary to hold the sensor data
sensor_data = {
"Self Name": Self_Name,
"temperature": temperature,
}
# Convert the dictionary to a JSON string
json_data = json.dumps(sensor_data)
return json_data
def WifiServer(ssidAP,passwordAP):
local_IP = '192.168.1.1'
gateway = '192.168.1.10'
subnet = '255.255.255.0'
dns = '8.8.8.8'
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
ap_if.ifconfig([local_IP,gateway,subnet,dns])
print("Setting soft-AP ... ")
ap_if.config(essid=ssidAP, password=passwordAP)
ap_if.active(True)
print('Success, IP address:', ap_if.ifconfig())
open_socket()
print("Setup End\n")
def open_socket():
# Open a socket
ip = '192.168.1.10'
address = (ip, 8080)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
return connection
def STA_Setup(ssidRouter,passwordRouter):
print("Setting soft-STA ... ")
if not sta_if.isconnected():
print('connecting to',ssidRouter)
sta_if.active(True)
sta_if.connect(ssidRouter,passwordRouter)
while not sta_if.isconnected():
pass
print('Connected, IP address:', sta_if.ifconfig())
print("Setup End")
def AP_Setup(ssidAP,passwordAP):
ap_if.ifconfig([local_IP,gateway,subnet,dns])
print("Setting soft-AP ... ")
ap_if.config(essid=ssidAP, password=passwordAP)
ap_if.active(True)
print('Success, IP address:', ap_if.ifconfig())
print("Setup End\n")