-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArduinoPortal.py
161 lines (133 loc) · 7.58 KB
/
ArduinoPortal.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json
import requests
import urllib.request
from MyHTMLParser import *
from WeatherApiForecast import *
from WeatherApiCurrent import *
import os.path
class ArduinoPortal:
requestCurrent = requests.get('http://api.openweathermap.org/data/2.5/weather?id=3987342&APPID=d2669a720677f99ab5ac96c0dca9a399')
requestForecast = requests.get('http://api.openweathermap.org/data/2.5/forecast?id=3987342&APPID=d2669a720677f99ab5ac96c0dca9a399')
apiDataForecast = json.loads(requestForecast.text)
apiDataCurrent = json.loads(requestCurrent.text)
# Generic Information
name_of_city = "City -> " + apiDataForecast['city']['name']
# Current Weather Data
currentWeatherData = WeatherApiCurrent(apiDataCurrent).weatherDataPrintCurrent()
# Index values for Midnight: 0,8,16,24,32
# Index values for Noon: 4,12,20,28,36
firstDayMidnight = WeatherApiForecast(0, 4, apiDataForecast).weatherDataPrintMidnight()
firstDayNoon = WeatherApiForecast(0, 4, apiDataForecast).weatherDataPrintNoon()
secondDayMidnight = WeatherApiForecast(8, 12, apiDataForecast).weatherDataPrintMidnight()
secondDayNoon = WeatherApiForecast(8, 12, apiDataForecast).weatherDataPrintNoon()
thirdDayMidnight = WeatherApiForecast(16, 20, apiDataForecast).weatherDataPrintMidnight()
thirdDayNoon = WeatherApiForecast(16, 20, apiDataForecast).weatherDataPrintNoon()
fourthDayMidnight = WeatherApiForecast(24, 28, apiDataForecast).weatherDataPrintMidnight()
fourthDayNoon = WeatherApiForecast(24, 28, apiDataForecast).weatherDataPrintNoon()
fifthDayMidnight = WeatherApiForecast(32, 36, apiDataForecast).weatherDataPrintMidnight()
fifthDayNoon = WeatherApiForecast(32, 36, apiDataForecast).weatherDataPrintNoon()
weatherData = name_of_city + "\n" + currentWeatherData + "\n" + firstDayMidnight + "\n" + firstDayNoon + "\n" + secondDayMidnight + "\n" + secondDayNoon + "\n" + thirdDayMidnight + "\n" + thirdDayNoon + "\n" + fourthDayMidnight + "\n" + fourthDayNoon + "\n" + fifthDayMidnight + "\n" + fifthDayNoon
fileName = 'WeatherData.txt'
filePath = 'Upload/'
completename = os.path.join(filePath, fileName)
file = open(completename, 'w+')
file.write(weatherData)
file.close()
contents = urllib.request.urlopen("http://172.20.10.3").read() # Getting data from arduino
print(contents)
print("Pre Parser: ", contents)
parser = MyHTMLParser()
parser.feed(str(contents))
humiditySensor = dataList[3]
temperatureSensor = dataList[4]
moistureSensor = dataList[5]
humiditySensorList = humiditySensor.split()
humiditySensorValue = humiditySensorList[1]
temperatureSensorList = temperatureSensor.split()
temperatureSensorValue = temperatureSensorList[1]
moistureSensorList = moistureSensor.split()
moistureSensorValue = moistureSensorList[2]
print(humiditySensorValue)
print(temperatureSensorValue)
print(moistureSensorValue)
#
#
# All Watering will occur at 12 pm
# First check: Current Soil Moisture
# moisture threshold :
# Min Typ Max Condition
# # 0 0 0 sensor in open air
# # 0 20 300 sensor in dry soil
# # 300 580 700 sensor in humid soil
# # 700 940 950 sensor in water
# Writing max values can change upon testing
moistureDiscrepencyBoolLow = False
moistureDiscrepencyBoolHigh = False
if int(moistureSensorValue) < 300:
print("Soil Moisture Value is too low: Water plants")
mositureSensorDecision1 = "Soil Moisture Value is too low: Water plants"
moistureSensorDiscrepancyBoolLow = True
elif int(moistureSensorValue) > 580:
print("Soil Moisture Value is too high: Don't Water Plants")
mositureSensorDecision2 = "Soil Moisture Value is too high: Don't Water Plants"
moistureDiscrepencyBoolHigh = True
# Second check: Current Sensor value vs API value
temperatureDiscrepencyBoolHigh = False
temperatureDiscrepencyBoolLow = False
humidityDiscrepencyBoolHigh = False
humidityDiscrepencyBoolLow = False
# curData = WeatherApiCurrent(apiDataCurrent)
# if abs(temperatureSensorValue - curData.getCurrentTemperature()) > 5:
# print("Current Temperature Discrepency")
# temperatureDiscrepencyBool = True
# if abs(humiditySensorValue - curData.getCurrentHumidity()) > 10:
# print("Current Humidity Discrepency")
# humidityDiscrepencyBool = True
# Third Check: current temperature determines if too hot or cold to water RIGHT NOW
# temperature Threshold = 10C (less than this is too cold dont water) > 30C Water its too hot;
currentData = WeatherApiCurrent(apiDataCurrent)
forecastData1 = WeatherApiForecast(0, 4, apiDataForecast)
forecastData2 = WeatherApiForecast(8, 12, apiDataForecast)
forecastData3 = WeatherApiForecast(16, 20, apiDataForecast)
forecastData4 = WeatherApiForecast(24, 28, apiDataForecast)
forecastData5 = WeatherApiForecast(32, 36, apiDataForecast)
# 4 more instantiation here
if float(temperatureSensorValue) > 30.00 or forecastData1.getForecastTemperatureMidnight() > 30 or forecastData1.getForecastTemperatureNoon() > 30 or forecastData2.getForecastTemperatureMidnight() > 30 or forecastData2.getForecastTemperatureNoon() > 30 or forecastData3.getForecastTemperatureMidnight() > 30 or forecastData3.getForecastTemperatureNoon() > 30:
print("Water Plants Today")
temperatureSensorDecision1 = "Water Plants"
temperatureDiscrepencyBoolHigh = True
elif temperatureSensorValue < 10 or forecastData1.getForecastTemperatureMidnight() < 10 or forecastData1.getForecastTemperatureNoon() < 10 or forecastData2.getForecastTemperatureMidnight() < 10 or forecastData2.getForecastTemperatureNoon() < 10 or forecastData3.getForecastTemperatureMidnight() < 10 or forecastData3.getForecastTemperatureNoon() < 10:
print("Too Cold Don't Water Plants")
temperatureSensorDecision2 = "Too Cold Don't Water Plants"
temperatureDiscrepencyBoolLow = True
# Fourth Check: Humidity
# humidity threshold = >70% dont water or <20% DO water ;
if float(humiditySensorValue) > 70.00 or forecastData1.getForecastHumidityMidnight() > 70 or forecastData1.getForecastHumidityNoon() > 70:
print("Too Huimid, Don't Water Plants")
humiditySensorDecision1 = "Too Humid, Don't Water Plants"
humidityDiscrepencyBoolHigh = True
elif humiditySensorValue < 20 or forecastData1.getForecastHumidityMidnight() < 20 or forecastData1.getForecastHumidityNoon() < 20:
print("Too Dry, Water Plants Today")
humiditySensorDecision2 = "Too Dry, Water Plants Today"
humidityDiscrepencyBoolLow = True
# today's temperature is used to check and make sure our previous predictions held true
# future temperature should determine when to water plants,
# but dont actually water plants until current temperature verafies
if moistureDiscrepencyBoolHigh == True:
decision = mositureSensorDecision2
elif moistureDiscrepencyBoolLow == True:
decision = mositureSensorDecision1
elif temperatureDiscrepencyBoolHigh == True:
decision = temperatureSensorDecision1
elif temperatureDiscrepencyBoolLow == True:
decision = temperatureSensorDecision2
elif humidityDiscrepencyBoolHigh == True:
decision = humiditySensorDecision1
elif humidityDiscrepencyBoolLow == True:
decision = humiditySensorDecision2
fileName = 'API_Decision.txt'
filePath = 'Upload/'
completename = os.path.join(filePath, fileName)
file = open(completename, 'w+')
file.write(decision)
file.close()