No more coffee alone on your desk !!
Coffee brake MUST be a convivial moment shared between colleagues at any company. However, synchronization in main issue. you cannot walk around all the offices and ask if the folks want a coffee.
This solution id for all the lost souls in search of a convivial moment on a hard working day. Just push the Make Coffee button and the machine will make the coffee for you and tweet it to let all your colleagues know that someone just made a coffee and they can join it.
The ArchitectureThe Smart Coffee Brake system involves Structure platform, Twitter, and a Particle Photon that control the coffee machine. As illustrated in Figure 1, the workflow in composed by seven interactions:
- (1) A user asks for a coffee using his mobile phone to Structure.
- (2) The coffee machine start making coffee once notified by Structure.
- (3) the coffee machine send an acknowledgment to Structure
- (4) The acknowledgment is displayed on the user
- (5)The coffee machine send a "ready" message to Structure once the coffee is ready.
- (6) Structure notify the user and a message is displayed on his mobile device.
- (7) Structure tweet a message on twitter
- (8) The colleagues are notified via their twitter app.
As illustrated in the interaction diagram Figure 2. The User communicates withe Structure using MQTT over websocket. it a single page web application built using Paho Javascript implementation. The code bellow illustrates how it works:
...
<script type="text/javascript">
var mqtt;
var reconnectTimeout = 2000;
var host = "broker.getstructure.io";
var port = 443;
var clientID = "YOUR-DEVICE-ID";
var username = "YOUR-ACCESS-KEY";
var password = "YOUR-ACCESS-SECRET";
var commandtopic = "structure/"+clientID+"/command";
var statetopic = "structure/"+clientID+"/state";
var useTLS = true;
var cleansessionmqtt = true;
function MQTTconnect() {
mqtt = new Paho.MQTT.Client(
host,
port,
clientID);
var options = {
timeout: 5,
useSSL: useTLS,
cleanSession: cleansessionmqtt,
onSuccess: onConnect,
onFailure: function (message) {
$('#status').val("Connection failed: " + JSON.stringify(message) + "Retrying");
console.log("Connection failed: " + JSON.stringify(message) + "Retrying");
setTimeout(MQTTconnect, reconnectTimeout);
}
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
if (username != null) {
options.userName = username;
options.password = password;
}
...
mqtt.connect(options);
}
function onConnect() {
...
mqtt.subscribe(commandtopic, {qos: 0});
}
...
function onMessageArrived(message) {
var topic = message.destinationName;
var payload = message.payloadString;
console.log('Received ' + payload);
...
};
...
$("#coffee").click(function (e) {
var payload = { data : {coffee: "make"}};
var str= JSON.stringify(payload);
var message = new Paho.MQTT.Message(str);
message.destinationName = statetopic;
message.qos = 0;
mqtt.send(message);
console.log("Message sent " + str + ' to ' + statetopic);
});
});
</script>
...
The Figure 3 illustrates how the mobile web page looks like :
The Structure platform provides support for devices using MQTT and MQTT over websocket. Moreover, Structure provides an easy way to communicate with twitter. To enable communication with Particle photon, we use the Structure-MQTT-device library. Twitter apps are available in all the known devices. You can use the mobile website using HTTP if you do not have a client.
Structure ApplicationStructure provides a platform to build the IoT solution. in this project, we used the workflow and the broker features.
You need first to register on Structure. then you have to create two Devices flowing this steps :
- CoffeeMachine: Represents your photon board.
- MobilePhone: Represents your mobile device
You need next to create your workflow. The Figure 4 illustrate the workflow design.
Use the following expression for the conditional on the left:
{{data.coffee}} === 'make'
Use the following expression for the conditional on the right:
{{data.make}} === false
Choose the CoffeeMachine as device and use the command "make" for Start Machine with the following payload
{ "make": true}
Choose the CoffeeMachine as device and use the command "stop" for Stop Machine with the following payload
{ "make": false}
Choose the MobilePhone as device and use the command "update" for Mobile phone KO with the following payload
{"msg":"error in process"}
Choose the MobilePhone as device and use the command "update" for Coffee in progress with the following payload
{"msg":"coffee in progress"}
Choose the MobilePhone as device and use the command "update" for Coffee Ready with the following payload
{"msg":"coffee ready"}
For twitter, follow this documentation.
Particle Photon ApplicationOn the particle builder, create a new application and add a new application. You have to add the SparkJson library and Structure-MQTT-device library as illustrated in the Figure 5.
Copy paste the following code and flash it to your device:
// This #include statement was automatically added by the Particle IDE.
#include "Structure-MQTT-device/Structure-MQTT-device.h"
// The Photon's onboard LED.
int LED = D7;
int MAKE_COFFEE = 90;
int STOP_COFFEE = 0;
Servo coffee;
// The initialization of your device
GetStructure::Device photon("YOUR-DEVICE-ID", "YOUR-ACCESS-KEY", "YOUR-ACCESS-SECRET");
void publishStatus(bool status){
// Build the json payload: { "make" : ON/OFF, "time" : yyy}
StaticJsonBuffer<100> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["make"] = status;
root["time"] = millis();
//Publish directly your JsonObject !!!
photon.publish(root);
}
// Callback signature for GetStructure.io subscriptions.
void myCallback(JsonObject& command);
// Callback for GetStructure.io subscriptions.
// Receive Your JsonObject from GetStructure.io
void myCallback(JsonObject& command) {
Serial.print("Received : ");
Serial.println(String(command["name"].asString()));
if(String(command["name"].asString()).equals(String("make"))) {
Serial.println("Making Coffee");
digitalWrite(LED, HIGH);
coffee.write(MAKE_COFFEE);
publishStatus(true);
delay(1000);
digitalWrite(LED, LOW);
coffee.write(STOP_COFFEE);
publishStatus(false);
Serial.println("Coffee is ready");
}
else{
digitalWrite(LED, LOW);
coffee.write(STOP_COFFEE);
publishStatus(false);
Serial.println("Emergency Stop");
}
}
void setup() {
Serial.begin(9600);
Serial.println("Starting the Setup");
pinMode(LED, OUTPUT);
coffee.attach(D2);
//Connect the photon to GetStructure.io broker
photon.connect(myCallback);
Serial.println("Setup complete");
}
void loop() {
photon.loop();
}
Hacking the Coffee MachineThere is many ways to hack the coffee machines to control them using devices. We prefer to use a servo motor to control the switch rather than turn using a relay to control the power supply. The Figure 6 contains multiple pictures that illustrates how to get rid of the manual witch and replace it with a servo motor.
Please refer to the schematics and Figure 7 to see how to connect the servo to the particle photon.
Conclusion
Yes we can !!
Yes makers can transform everyday dumb tools to funny and social connected IoT device. Thanks to those amazing platforms like Structure, Particle and Hackster.
Yes we can introduce a little joy on a "hostile" working environment.
Comments