This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnespresso2mqtt.py
executable file
·303 lines (265 loc) · 10.6 KB
/
nespresso2mqtt.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
from __future__ import print_function
import binascii
import ctypes
import struct
import time
from uuid import UUID
import paho.mqtt.client as paho
import pygatt
c_uint8 = ctypes.c_uint8
global device
#device = None
oldvalue = None
device= None
# Nespresso bluetooth MAC address
YOUR_DEVICE_ADDRESS = "XX:XX:XX:XX:XX:XX"
# Nespresso Apps secret code (you nedd to sniff it to find it)
# Replace XX by value (it's in hex format)
#AUTH_CODE = [0xXX, 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX]
YOUR_DEVICE_ADDRESS = "d2:12:f1:7b:cd:6d"
AUTH_CODE = [0x82, 0x87,0xee,0x82,0x59,0x3d,0x3c,0x4e]
# MQTT Broker IP or Name
broker="mosquito"
# MQTT Broker port
port=1883
STATUSTOPIC = "/nespresso/status"
class Flags_bits( ctypes.LittleEndianStructure ):
_fields_ = [
("bit0", c_uint8, 1 ), # asByte & 1
("bit1", c_uint8, 1 ), # asByte & 2
("bit2", c_uint8, 1 ), # asByte & 4
("bit3", c_uint8, 1 ), # asByte & 8
("bit4", c_uint8, 1 ), # asByte & 16
("bit5", c_uint8, 1 ), # asByte & 32
("bit6", c_uint8, 1 ), # asByte & 64
("bit7", c_uint8, 1 ), # asByte & 128
]
class Flags( ctypes.Union ):
_anonymous_ = ("bit",)
_fields_ = [
("bit", Flags_bits ),
("asByte", c_uint8 )
]
def on_publish(client,userdata,result): #create function for callback
print("data published \n")
pass
def on_disconnect(client, userdata, rc):
print("Client Got Disconnected")
if rc != 0:
print("Unexpected MQTT disconnection. Will auto-reconnect")
else:
print("rc value:" + str(rc))
try:
print("Trying to Reconnect")
client.connect(broker, port)
except:
print("Error in Retrying to Connect with Broker")
def on_message(mosq, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_connect(client, userdata, flags, return_code):
print("on_connect return_code: " + str(return_code))
if return_code == 0:
print("Connected to [%s]:[%s]" % (broker, port))
client1.publish(STATUSTOPIC, "CONNECTED", retain=True)
elif return_code == 1:
print("Connection refused - unacceptable protocol version")
elif return_code == 2:
print("Connection refused - identifier rejected")
elif return_code == 3:
print("Connection refused - server unavailable")
print("Retrying in 10 seconds")
time.sleep(10)
elif return_code == 4:
print("Connection refused - bad user name or password")
elif return_code == 5:
print("Connection refused - not authorised")
else:
print("Something went wrong. RC:" + str(return_code))
# Define callbacks
client1= paho.Client("nespresso2mqtt") #create client object
client1.on_publish = on_publish #assign function to callback
client1.on_connect = on_connect
client1.on_message = on_message
client1.on_disconnect = on_disconnect
client1.subscribe("/Nespresso/#", 0)
client1.connect(broker,port) #establish connection
client1.loop_start()
def printIndication(handle, value):
print('Indication received {} : {}'.format(hex(handle), binascii.hexlify(str(value))))
def handle_data(handle, value):
"""
handle -- integer, characteristic read handle the data was received on
value -- bytearray, the data returned in the notification
"""
print("Received data %s" % (binascii.hexlify(value)))
def discover_service(device):
for service in device.discover_characteristics():
try:
print("[%s] Characteristic [%s]" % (YOUR_DEVICE_ADDRESS, service))
serv= device.char_read(service)
print("Read UUID %s: %s" % (service, binascii.hexlify(serv)))
print(serv)
if service=="06aa3a22-f22a-11e3-9daa-0002a5d5c51b":
print("Slider status")
if service == "06aa3a12-f22a-11e3-9daa-0002a5d5c51b":
print("status")
except:
print("Read error for UUID [%s]" % (service))
def connectble():
try:
return adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE)
except Exception as error:
print (error)
time.sleep(15) # Wait 15s before reconnect
connectble()
def new_cofee(device):
#Make me a cofee
characteristic = "06aa3a42-f22a-11e3-9daa-0002a5d5c51b"
time.sleep(2)
try:
#device.char_write(characteristic, bytearray([0x03,0x05,0X07,0x04,0x00,0x00,0x00,0x00,0x00,0x02]))
device.char_write(characteristic, bytearray([0x03,0x05,0X07,0x04,0x00,0x00,0x00,0x00,0x00,0x02]), wait_for_response=True)
except Exception as error:
print (error)
# time.sleep(5) # Wait 5s before resendcommand
# new_cofee(device)
def connectnespresso(device,tries=0):
try:
characteristic = "06aa3a41-f22a-11e3-9daa-0002a5d5c51b"
device.char_write(characteristic, bytearray(AUTH_CODE), wait_for_response=True) #your secret code
#Subscribe to state change
# device.subscribe("06aa3a12-f22a-11e3-9daa-0002a5d5c51b", callback=handle_data)
except Exception as error:
print("connect error")
time.sleep(5) # wait 5s
if tries < 3:
print ("<3 write error")
connectnespresso(device, tries+1) #resend write auth
elif tries < 5:
print ("<5 write error")
connectble() #reconnect to machine
connectnespresso(device, tries+1) #resend write auth
else:
print (">5 write error")
raise error
ADDRESS_TYPE = pygatt.BLEAddressType.random
#adapter = pygatt.GATTToolBackend()
adapter = pygatt.backends.GATTToolBackend()
adapter.start()
device = connectble()
connectnespresso(device)
BYTE = Flags()
#discover_service(device)
#new_cofee(device)
while True:
try:
# try:
# value = device.
# print("RSSI %s" % value)
# except:
# print ("Get RSSI error")
value = device.char_read("06aa3a12-f22a-11e3-9daa-0002a5d5c51b")
if oldvalue != value:
print("Etat %s" % (binascii.hexlify(value)))
print (value)
print (bin(int.from_bytes(value,byteorder='big')).strip('0b'))
print (bin(value[0]))
BYTE.asByte = value[0]
print( "water_is_empty: %i" % BYTE.bit0)
try:
client1.publish("/nespresso/state/water_empty", BYTE.bit0)
except:
print ("error publishing this value")
print( "Descaled needed: %i" % BYTE.bit2)
try:
client1.publish("/nespresso/state/descaled_needed", BYTE.bit2)
except:
print ("error publishing this value")
print( "capsule_mechanism_jammed: %i" % BYTE.bit4)
try:
client1.publish("/nespresso/state/capsule_mechanism_jammed", BYTE.bit4)
except:
print ("error publishing this value")
print( "always_1 : %i" % BYTE.bit6)
try:
client1.publish("/nespresso/state/always_1", BYTE.bit6)
except:
print ("error publishing this value")
print (bin(value[1]))
BYTE.asByte = value[1]
print("water temp_low: %i" % BYTE.bit0)
try:
client1.publish("/nespresso/state/water_temp_low", BYTE.bit0)
except:
print ("error publishing this value")
print("awake: %i" % BYTE.bit1)
try:
client1.publish("/nespresso/state/awake", BYTE.bit1)
except:
print ("error publishing this value")
print("water_engaged: %i" % BYTE.bit2)
try:
client1.publish("/nespresso/state/water_engadged", BYTE.bit2)
except:
print ("error publishing this value")
print("sleeping %i" % BYTE.bit3)
try:
client1.publish("/nespresso/state/sleeping", BYTE.bit3)
except:
print ("error publishing this value")
print("tray_sensor_tripped_during_brewing: %i" % BYTE.bit4)
try:
client1.publish("/nespresso/state/tray_sensor_during_brewing", BYTE.bit4)
except:
print ("error publishing this value")
print("tray_open_tray_sensor_full: %i" % BYTE.bit6)
try:
client1.publish("/nespresso/state/tray_open_tray_sensor_full", BYTE.bit6)
except:
print ("error publishing this value")
print("capsule_engaged: %i" % BYTE.bit7)
try:
client1.publish("/nespresso/state/capsule_engaged", BYTE.bit7)
except:
print ("error publishing this value")
print (bin(value[2]))
print (bin(value[3]))
BYTE.asByte = value[3]
print("Fault: %i" % BYTE.bit5)
print (bin(value[4]))
print (bin(value[5]))
print (bin(value[6]))
print (bin(value[7]))
try:
client1.publish("/nespresso/descaling_counter", int.from_bytes(value[6:9],byteorder='big'))
except:
print ("error publishing descale timer")
trappe = device.char_read("06aa3a22-f22a-11e3-9daa-0002a5d5c51b")
print("Read Slider %s" % (binascii.hexlify(trappe)))
BYTE.asByte = trappe[0]
print("Slider closed?: %i" % BYTE.bit1)
try:
if (binascii.hexlify(trappe)) == b'00':
client1.publish("/nespresso/slider","Open" )
else :
client1.publish("/nespresso/slider","Closed" )
except:
print ("error publishing Slider")
nb_capsule = device.char_read("06aa3a15-f22a-11e3-9daa-0002a5d5c51b")
print("Nb capsule %s" % int.from_bytes(nb_capsule, byteorder='big'))
try:
client1.publish("/nespresso/caps_count", int.from_bytes(nb_capsule,byteorder='big'))
except:
print ("error publishing this value")
answer = device.char_read("06aa3a52-f22a-11e3-9daa-0002a5d5c51b")
print("answer %s" % (binascii.hexlify(answer)))
try:
client1.publish("/nespresso/answer", (binascii.hexlify(answer)))
except:
print ("error publishing this value answer")
oldvalue = value
time.sleep(2)
except Exception as e:
print ("error")
print (e)