This repository was archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
126 lines (110 loc) · 4.44 KB
/
Copy pathclient.py
File metadata and controls
126 lines (110 loc) · 4.44 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
###################################################################
# #
#############################ATTENTION#############################
###################################################################
# #
# change http://xxx.xxx.xxx.xxx:port to your ip address and port #
# #
###################################################################
import RPi.GPIO as GPIO
import time
import requests
import board
import busio
import adafruit_scd30
import Adafruit_GPIO.SPI as SPI
SCD30_device = 19
sleep_interval = 6
warm_up = 5
flag = 0
JSON_MESSAGE_ID = 1
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(SCD30_device,GPIO.OUT)
GPIO.output(SCD30_device,GPIO.HIGH)
return()
def deviceON():
print("the device is on")
GPIO.output(SCD30_device,GPIO.HIGH)
return()
def deviceOFF():
print("the device is off")
GPIO.output(SCD30_device,GPIO.LOW)
return()
def getRespberrySerial():
respberry_serial = "0000000000000000"
try:
f = open('/proc/cpuinfo','r')
for line in f:
if line[0:6] == 'Serial':
respberry_serial = line[10:26]
f.close()
except:
respberry_serial = 'ffffffffffffffff'
return respberry_serial
def sensorInitialization():
response = requests.post('http://xxx.xxx.xxx.xxx:port//climate/sensor/save', json={
"id": 1, # change this to your sensor id
"isopen": 0, # 0 means the sensor switch is off, 1 means the sensor switch is on
"name": "sensor_1" # change this to your sensor name
})
if response.json()['msg'] == 'success':
print("The sensor has been initialized")
else:
print("The sensor has been initialized before.")
def getSensorSwitchStatus():
response =requests.get("http://xxx.xxx.xxx.xxx:port//climate/sensor/info/1")
switchStatus = (response.json()['sensor'])['isopen']
return switchStatus
def sendData(ppm, temperature, humidity):
response = requests.post('http://xxx.xxx.xxx.xxx:port//sendSensorDataMessage',json ={
"humidity":humidity,
"ppm":ppm,
"processorCode":"0003",
"latitude":25.2042,
"longitude":113.2030,
"status":"200",
"sensorName":str(getRespberrySerial()),
"temperature":temperature,
"jsonMessageId":JSON_MESSAGE_ID
})
print("State code:", response.status_code)
print(response.json())
# this function has not been used.
# def sendTestData(ppm, temperature, humidity):
# # change http://xxx.xxx.xxx.xxx:port to your ip address and port
# response = requests.post('http://xxx.xxx.xxx.xxx:port//sendSensorDataTestMessage',json ={
# "humidity":humidity,
# "ppm":ppm,
# "processorCode":"0003",
# "temperature":temperature,
# })
# print("State code:", response.status_code)
# print(response.json())
def print_scd_data():
if scd.data_available:
print("Data Available!")
print("CO2: %d PPM" % scd.CO2)
print("Temperature: %0.2f degrees C" % scd.temperature)
print("Humidity: %0.2f %% rH" % scd.relative_humidity)
sendData(str("%0.2f"% scd.CO2), str("%0.2f"% scd.temperature), str("%0.2f"% scd.relative_humidity))
sendTestData(str("%0.2f"% scd.CO2), str("%0.2f"% scd.temperature), str("%0.2f"% scd.relative_humidity))
return()
setup()
sensorInitialization()
i2c = busio.I2C(board.SCL, board.SDA, frequency=50000)
time.sleep(warm_up)
scd = adafruit_scd30.SCD30(i2c)
while True:
switchStatus = getSensorSwitchStatus()
if switchStatus == "1" and flag == "0":
deviceON()
time.sleep(warm_up)
flag = "1"
elif switchStatus == "1" and flag =="1":
print_scd_data()
JSON_MESSAGE_ID += 1
elif switchStatus == "0":
deviceOFF()
flag = "0"
time.sleep(sleep_interval)