-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestSG3System.py
111 lines (88 loc) · 3.1 KB
/
testSG3System.py
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
#!/usr/bin/env python3
#
# Test SG3 System and Connectivity to a Wireless Extender
# SwitchDoc Labs
# January 2022
# set SGSEXT_IP to the IP address of your connected Wireless Extender to be tested
# Example: SGSEXT_IP = "192.168.1.52"
SGSEXT_IP = ""
import sys, traceback
import os
import time
import config
import datetime
import requests
import socket
import subprocess
def sendCommandToWireless(myIP, myCommand):
myURL = 'http://'+str(myIP)+'/'+myCommand
print ("sending REST URL = ", myURL)
try:
req = requests.get(myURL,timeout=30)
returnJSON = req.json()
except Exception:
traceback.print_exc()
return {}
return returnJSON
def turnOnTimedValve(singleValve):
myIP = singleValve["ipaddress"]
myCommand = "setSingleValve?params=admin,"+str(singleValve["ValveNumber"])+",1,"+str(singleValve["OnTimeInSeconds"])
return sendCommandToWireless(myIP, myCommand)
def turnOnAndReadMoistureSensors():
myJSON = sendCommandToWireless(SGSEXT_IP, "testHydroponicsSensors?params=admin")
return myJSON
def get_ip_address():
ip_address = '';
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
ip_address = s.getsockname()[0]
s.close()
return ip_address
def setMQTT_IP():
myMQTTIP = get_ip_address()
myMQTTPort = 1883
myURL = 'checkForID?params='+myMQTTIP+','+str(myMQTTPort)
myJSON = sendCommandToWireless(SGSEXT_IP, myURL)
# Main Program
if __name__ == '__main__':
print("###########################")
print("SmartGarden3 System Test")
print("###########################")
print('%s' % datetime.datetime.now())
print()
if (SGSEXT_IP == ""):
print("###########################")
print("Error: MUST SET SGSEXT_IP to run Wireless Extender test")
print("###########################")
exit()
print("Wireless Extender Address Test Requested = ", SGSEXT_IP)
print("###########################")
print("Starting Wireless Extender Test")
print("###########################")
# test Connection to Wireless Extender
setMQTT_IP()
singleValve = {}
singleValve["id"] = "TEST"
singleValve["ipaddress"] = SGSEXT_IP
singleValve["OnTimeInSeconds"] = "20"
singleValve["ValveNumber"] = "1"
myJSON = turnOnTimedValve(singleValve)
if (len(myJSON) == 0):
print("###########################")
print("Wireless Extender ", SGSEXT_IP)
print("NOT RESPONDING")
print("###########################")
else:
print("###########################")
print("Turning On USB1 Valve for 10 seconds on Extender: ", SGSEXT_IP)
print("###########################")
print("Wireless Extender: ", SGSEXT_IP)
print("Successfuly Responding")
print("###########################")
print(myJSON)
print("###########################")
time.sleep(5)
print("ADC Sensors:")
myJSON = turnOnAndReadMoistureSensors()
print(myJSON)
print("###########################")