-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotecomm.py
101 lines (79 loc) · 2.93 KB
/
notecomm.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
"""Use a cellular/wifi Notecard to send/receive images to/from Notehub.io
init_notecard(...) needs to be run once on startup. Afterward send_to_notehub()
and get_from_notehub() can be called, one at a time, as desired.
"""
import io
import base64
import notecard # pip3 install note-python
from PIL import Image # pip3 install Pillow
from periphery import I2C # pip install python-periphery
CARD = 0 # notecard
def init_notecard(notehub_productuid, i2c_device_file_path):
"""Run once at startup before send_to_notehub() or get_from_notehub()"""
global CARD
port = I2C(i2c_device_file_path)
CARD = notecard.OpenI2C(port, 0, 0)
req = {'req': 'hub.set'}
req['product'] = notehub_productuid
req['mode'] = 'continuous'
req['sync'] = True
res = CARD.Transaction(req)
print(res)
res = CARD.Transaction({
"req": "file.delete",
"files": ["image.qi"]
})
print(res)
res = CARD.Transaction({
"req": "note.template",
"file": "image.qo",
"body": {
'image': "string",
'destDeviceUID': "string"
}
}
)
print(res)
def send_to_notehub(imageName, destDeviceUID):
"""Compresses and encodes an image. Then sends the encoding to the Notecard
which will forward it to Notehub as soon as cellular signal is found."""
# load image from file
imageData = Image.open(imageName)
imageData = imageData.resize((240, 200))
imageData = imageData.convert('RGB')
# Qual 1, 2 have a ~3kb filesize, q3 bumps to 4k with no notable change
imageData.save('temp.webp', 'webp', optimize=True, quality=2)
# Image.close(imageName)
# convert b64
with open("temp.webp", "rb") as image_file:
b64Data = base64.b64encode(image_file.read()).decode('UTF-8')
# send a note.add with the image as the body
request = {'req': 'note.add',
'file': 'image.qo',
'sync': True,
'body': {
'image': b64Data,
'destDeviceUID': destDeviceUID
}}
result = CARD.Transaction(request)
print(result)
# check flash storage usage
request = {'req': 'card.status'}
result = CARD.Transaction(request)
print(result)
print(f"Flash storage is {result.storage}. Please don't exceed 80%.")
def get_from_notehub(imageName):
"""If an image is the inbound queue, image.qi, decode and save the image."""
req = {'req': 'note.get'}
req['file'] = 'image.qi'
req['sync'] = True
req['delete'] = True
res = CARD.Transaction(req)
if not ('body' in res and 'image' in res['body']):
print("can't get image from notehub right now")
print(res)
return
b64Data = res['body']['image']
imageData = Image.open(io.BytesIO(base64.b64decode(b64Data)))
imageData = imageData.convert('RGB')
imageData.save(imageName, 'jpeg')