-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_main.py
99 lines (74 loc) · 2.29 KB
/
ch_main.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
from machine import Pin, I2C
import network
import time
import uumail
from umqtt.robust import MQTTClient
import paho.mqtt.client as mqtt
#import paho.mqtt as mq
try:
import urequests as requests
except:
import requests
import ADXL3445
import esp
esp.osdebug(None)
import ujson as json
import gc
gc.collect()
def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
wifi.connect('esp','password')
if not wifi.isconnected():
print('connecting..')
timeout = 0
while (not wifi.isconnected() and timeout < 5):
print(5 - timeout)
timeout = timeout + 1
time.sleep(1)
if(wifi.isconnected()):
print('connected')
else:
print('not connected')
def senser():
url = "172.20.43.39"
port = 1883
topic = "test_topic"
client = mqtt.Client()
client.connect(url, port)
city = 'Allahabad'
country_code = 'IN'
i2c = I2C(scl=Pin(22),sda=Pin(21), freq=10000)
adx = ADXL3445.ADXL345(i2c)
open_weather_map_api_key = '6d6ca359b7e46f1c18c19122b905afbd'
while True:
x=adx.xValue
y=adx.yValue
z=adx.zValue
print('The acceleration info of x, y, z are:%d,%d,%d'%(x,y,z))
open_weather_map_url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + ',' + country_code + '&APPID=' + open_weather_map_api_key
if(x>=65 or y>=65 or z>=260):
#connect_wifi()
weather_data = requests.get(open_weather_map_url)
wind = 'Wind: ' + str(weather_data.json().get('wind').get('speed')) #+ 'mps ' #+ str(weather_data.json().get('wind').get('deg')) + '*'
print(wind)
time.sleep(2)
if(wind>='5'):
client.publish(topic, payload=1, qos=2, retain=False)
time.sleep(1)
else:
client.publish(topic, payload=2, qos=2, retain=False)
time.sleep(1)
broker_url = "192.268.43.253"
broker_port = 8080
x = message.payload.decode()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_url, broker_port)
client.subscribe("test_topic", qos=1)
#client.loop_forever()
while True:
connect_wifi()
senser()