-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespressoweight.ino
204 lines (172 loc) · 5.63 KB
/
espressoweight.ino
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
#include <EEPROM.h>
#include <Wire.h>
#include "SparkFun_Qwiic_Relay.h"
#define RELAY_ADDR 0x18 // default address from docs. Alternate is 0x19
Qwiic_Relay relay(RELAY_ADDR);
#include "SparkFun_Qwiic_Twist_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_Twist
TWIST twist; //Create instance of this object
#include <SparkFun_Alphanumeric_Display.h> //Click here to get the library: http://librarymanager/All#SparkFun_Qwiic_Alphanumeric_Display by SparkFun
HT16K33 display;
#include <ArduinoBLE.h>
const String SCALE_NAME="Decent Scale";
const int GRAMS_LOCATION = 0; //just picking the first one. If you use this for something else, move it.
int grams = 0;
const int GRAMS_EPSILON = 1;
const byte TARE_COMMAND[7] = {(byte)0x03,(byte)0x0F,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x0C};//from decent docs
void printWeight(){
if (grams < 10){
display.print("0"+String(grams)+" g");
}
else{
display.print(String(grams)+" g");
}
}
void resetBLE(){
if(BLE.connected()){
BLE.disconnect();
}
if (!BLE.begin()) {
dispErr("starting Bluetooth® Low Energy module failed!");
while (1);
}
Serial.println("Scanning for Scale");
twist.setColor(0,0,255);
BLE.scan();
}
void dispErr(const String& errmsg){
Serial.println(errmsg);
if(twist.isConnected()){
twist.setColor(255,0,0);
}
if(display.isConnected(0)){
display.println("ERR");
}
}
//runs once at startup
void setup() {
Wire.begin();
Serial.begin(115200);
EEPROM.init(); //artemis is technically flash, and init does the routing
grams = EEPROM.read(GRAMS_LOCATION);
//Initially eeprom could be anything 0-255. Should be fine after setting once, though
if(grams > 99){
grams = 99;
}
//we'll do the knob first because it's our easiest error indicator
if (twist.begin() == false)
{
Serial.println("Twist does not appear to be connected. Please check wiring. Freezing...");
while (1);
}
twist.setColor(0,255,0);
if (display.begin() == false)
{
dispErr("Display device did not acknowledge! Freezing.");
while (1);
}
printWeight();
if(!relay.begin()){
dispErr("Check connections to Qwiic Relay.");
while(1);
}
relay.turnRelayOff(); //Should be a no-op
resetBLE();
}
void connectedLoop(BLEDevice scale){
if (scale.connect()) {
Serial.println("Connected to scale");
}
else {
dispErr("Failed to connect to scale");
return;//go retry
}
//This allows characteristic discovery, too
if (!scale.discoverAttributes()) {
dispErr("Attribute discovery failed!");
return;
}
//read-characteristic for finding scale data
BLECharacteristic weight = scale.characteristic("fff4");//from decent docs
if(!weight){
dispErr("Weight not found...");
return;
}
else{
weight.subscribe();
}
//write-characteristic for sending commands to the scale
BLECharacteristic command_scale = scale.characteristic("36f5");//from decent docs
if(!command_scale){
dispErr("Unable to command scale");
return;
}
Serial.println("Ready to go");
bool brewing=false;
while(scale.connected()){
if(brewing){
//this happens.... all the time. It "updates" even if it hasn't actually updated.
if(weight.valueUpdated()){
//bytes 0,1: 03 (decent) CE or CA (Equalized or Altered)
//bytes 2,3: (grams weight *10 as 2 byte signed short int)
//bytes 4+: time, xor checksum etc.
//should be 7-10 bytes.Making it 16 in case they up size again on a newer scale version
byte wVal[16];
weight.readValue(&wVal,16);
const short curWeight = (wVal[2]<<8) | (wVal[3] & 0xff);
Serial.print(curWeight/10.0);
Serial.println("g");
if (curWeight > (grams - GRAMS_EPSILON) * 10 ){
Serial.println("OK STOP");
relay.turnRelayOff();
twist.setColor(255,255,255);
brewing=false;
}
}
}
else{
if (twist.isClicked()){
//indicate something is happening
Serial.println("GO GO GO!");
twist.setColor(255,0,128);
//send a tare command to the scale
command_scale.writeValue(TARE_COMMAND,7);
//start espresso
relay.turnRelayOn();
brewing=true;
EEPROM.write(GRAMS_LOCATION,grams); //save grams
//sleep for a second.
// Behavior of the Steel is that a quick switch press will use its volumetric mode. Stopping that mode early is a second switch press.
// To avoid this, we will just hold for at least 1 second before we start paying attention to the scale. This way we can just turn the relay on until we're ready to be done.
delay(1000);
}
else if (twist.isMoved()){
grams += twist.getDiff();
if (grams > 99){
grams = 99;
}
if (grams <1){
grams = 1;
}
printWeight();
}
}
delay(1);
}
}
void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
//there is a BLE.scanForName, but it seemed to hang...
if(peripheral.localName() == SCALE_NAME){
Serial.println("Found a scale");
BLE.stopScan();
twist.setColor(255,255,255);
connectedLoop(peripheral);
resetBLE(); // scale not connected
}
else{
BLE.scan(); //wrong device. Resume scanning
}
}
}