-
Notifications
You must be signed in to change notification settings - Fork 14
/
SkyCamRemote.py
212 lines (168 loc) · 6.38 KB
/
SkyCamRemote.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# wireless sensor routines
import config
import json
import random
import sys
from subprocess import PIPE, Popen, STDOUT, check_output
from threading import Thread
import datetime
import MySQLdb as mdb
import traceback
import state
import os
import ProcessPicture
import util
GoodMessage = 0
Resends = 0
MQTTUPDATEPARAM = 10
MQTTCYCLECHANGE = 11
MQTTSTARTDELAY = 12
MQTTTURNOFFBLINK = 13
MQTTBLINKXTIMES = 14
MQTTSETTODEFAULTS = 15
import paho.mqtt.client as mqtt
from paho.mqtt import publish
def startMQTT():
broker_address= config.MQTT_Server_URL
print("creating new MQTT instance")
client = mqtt.Client("SkyCam") #create new instance
client.on_message=MTon_message #attach function to callback
client.on_connect=MTon_connect
print("connecting to broker")
client.connect(broker_address) #connect to broker
client.on_log=MTon_log
client.loop_start()
def MTon_log(client, userdata, level, buf):
#print("log: ",buf)
pass
# The callback for when the client receives a CONNACK response from the server.
def MTon_connect(client, userdata, flags, rc):
global GoodMessage, Resends
GoodMessage = 0
Resends = 0
#print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("SKYCAM/+/PICTURECHUNKS")
client.subscribe("SKYCAM/+/INFO")
# The callback for when a PUBLISH message is received from the server.
def MTon_message(client, userdata, msg):
global GoodMessage, Resends
#print("message found:",msg.topic)
# get msg topic
SplitTopic = msg.topic
SplitTopic = SplitTopic.split("/")
if (SplitTopic[2] == "INFO"):
if (config.SWDEBUG):
print("INFO msg Received: %s" % msg.payload)
processINFOMessage(msg)
return
if (SplitTopic[2] == "PICTURECHUNKS"):
myChunk = json.loads(msg.payload)
#print("Picture ID %s Chunk %s of %s Received. Resends: %s Size=%d" % (myChunk["messageid"],int(myChunk["chunknumber"])+1, myChunk["totalchunknumbers"], myChunk["totalchunkresends"], len(myChunk["chunk"])))
if (int(myChunk["chunknumber"])+1 == int(myChunk["totalchunknumbers"])):
GoodMessage = GoodMessage + 1
Resends = Resends + int(myChunk["totalchunkresends"])
if (config.SWDEBUG):
print("Picture ID %s Good Message %s Resends: %d" % (myChunk["messageid"], GoodMessage, Resends))
# deal with Chunks
ProcessPicture.saveChunk(myChunk)
def sendCommand(client, msg, messageType, payload):
# not complete - do not use
# Commands:
# send Blynk X Times
# get msg topic
SplitTopic = msg.topic
SplitTopic = SplitTopic.split("/")
#print (SplitTopic)
MyTopic = SplitTopic[0]+"/"+SplitTopic[1]+"/"+"COMMANDS"
# set up JSON
myIP = check_output(['hostname', '-I'])
myIP = myIP.decode()
myIP = myIP.replace("\n","")
myIP = myIP.replace(" ","")
# send blink message
if (messageType == MQTTBLINKXTIMES):
myMessage = {
"messagetype": MQTTBLINKXTIMES,
"myip": myIP,
"length": 300,
"count": 3
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
# send time to sleep message
if (messageType == MQTTCYCLECHANGE):
myMessage = {
"messagetype": MQTTCYCLECHANGE,
"myip": myIP,
"timetosleep": 50
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
# send time to set contrast delay on startup
if (messageType == MQTTUPDATEPARAM):
myMessage = {
"messagetype": MQTTUPDATEPARAM,
"myip": myIP,
"sensorparams": payload
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
def processINFOMessage(msg):
myJSON = json.loads(msg.payload)
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
myTEST = ""
myTESTDescription = ""
print("in INFO with SQL")
con = mdb.connect(
"localhost",
"root",
config.MySQL_Password,
"WeatherSenseWireless"
)
cur = con.cursor()
batteryPower = float(myJSON["batterycurrent"])* float(myJSON["batteryvoltage"])
loadPower = float(myJSON["loadcurrent"])* float(myJSON["loadvoltage"])
solarPower = float(myJSON["solarpanelcurrent"])* float(myJSON["solarpanelvoltage"])
batteryCharge = util.returnPercentLeftInBattery(float(myJSON["batteryvoltage"]), 4.2)
#print("batteryChange=", batteryCharge)
fields = "cameraid, messageid, softwareversion, messagetype, rssi, internaltemperature, internalhumidity, batteryvoltage, batterycurrent, loadvoltage, loadcurrent, solarvoltage, solarcurrent, batterypower, loadpower, solarpower, gndrreboots, batterycharge"
values = "'%s', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d " % (
myJSON["id"],
myJSON["messageid"],
myJSON["softwareversion"],
myJSON["messagetype"],
myJSON["currentrssi"],
myJSON["internaltemperature"],
myJSON["internalhumidity"],
myJSON["batteryvoltage"],
myJSON["batterycurrent"],
myJSON["loadvoltage"],
myJSON["loadcurrent"],
myJSON["solarpanelvoltage"],
myJSON["solarpanelcurrent"],
batteryPower,
loadPower,
solarPower,
myJSON["gndrreboots"],
batteryCharge)
query = "INSERT INTO SkyCamSensors (%s) VALUES(%s )" % (fields, values)
#print(query)
cur.execute(query)
con.commit()
except:
traceback.print_exc()
con.rollback()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con