Votecontroller

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
rubends
Date:
Mon Oct 15 14:54:25 2018 +0000
Revision:
4:bb892177ce23
Parent:
3:793425387c91
Child:
5:592f4272c549
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jensdehoog 0:fd29cd85e75e 1 #define APP_VERSION 0.6f
jensdehoog 0:fd29cd85e75e 2 #define MQTT_VERSION 3
jensdehoog 0:fd29cd85e75e 3 #define BROKER_NAME "broker.hivemq.com"
jensdehoog 0:fd29cd85e75e 4 #define BROKER_PORT 1883
jensdehoog 0:fd29cd85e75e 5
jensdehoog 0:fd29cd85e75e 6 #include "debounce_button.h"
jensdehoog 0:fd29cd85e75e 7 #include "EthernetInterface.h"
jensdehoog 0:fd29cd85e75e 8 #include "MQTTNetwork.h"
jensdehoog 0:fd29cd85e75e 9 #include "MQTTmbed.h"
jensdehoog 0:fd29cd85e75e 10 #include "MQTTClient.h"
jensdehoog 0:fd29cd85e75e 11
rubends 3:793425387c91 12 char* topic = "embedded/controller";
rubends 3:793425387c91 13 bool connected = false;
rubends 3:793425387c91 14
rubends 3:793425387c91 15 DigitalOut led2(LED2); //led if connected
rubends 3:793425387c91 16 InterruptIn button(USER_BUTTON);
rubends 3:793425387c91 17 EthernetInterface eth;
rubends 3:793425387c91 18 MQTTNetwork network(&eth);
rubends 3:793425387c91 19 MQTT::Client<MQTTNetwork, Countdown> client(network);
jensdehoog 0:fd29cd85e75e 20
jensdehoog 0:fd29cd85e75e 21 /**
jensdehoog 0:fd29cd85e75e 22 TODO
jensdehoog 0:fd29cd85e75e 23 ----
jensdehoog 0:fd29cd85e75e 24 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
jensdehoog 0:fd29cd85e75e 25 - Make an MQTT-service which:
jensdehoog 0:fd29cd85e75e 26 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
jensdehoog 0:fd29cd85e75e 27 - makes a client and connects it to the broker using a client ID and credentials (username & password).
jensdehoog 0:fd29cd85e75e 28 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
jensdehoog 0:fd29cd85e75e 29 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
jensdehoog 0:fd29cd85e75e 30 can be called each time we want to send a certain message.
jensdehoog 0:fd29cd85e75e 31 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
jensdehoog 0:fd29cd85e75e 32 the program disconnects from the broker and terminates.
jensdehoog 0:fd29cd85e75e 33
jensdehoog 0:fd29cd85e75e 34 Extra
jensdehoog 0:fd29cd85e75e 35 -----
jensdehoog 0:fd29cd85e75e 36 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
jensdehoog 0:fd29cd85e75e 37 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
jensdehoog 0:fd29cd85e75e 38
jensdehoog 0:fd29cd85e75e 39 Tips & tricks
jensdehoog 0:fd29cd85e75e 40 -------------
jensdehoog 0:fd29cd85e75e 41 - To generate an interrupt on the press of a button, use:
jensdehoog 0:fd29cd85e75e 42 InterruptIn button(USER_BUTTON);
jensdehoog 0:fd29cd85e75e 43 ...
jensdehoog 0:fd29cd85e75e 44 button.fall(callback(someFunction));
jensdehoog 0:fd29cd85e75e 45 - Before implementing MQTT, test the multiclick feature first.
jensdehoog 0:fd29cd85e75e 46 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
jensdehoog 0:fd29cd85e75e 47 - To have a uniform message sending procedure, use the following function usage:
jensdehoog 0:fd29cd85e75e 48 sendMessage(&client, topic, buf, qos, retained, duplicate)
jensdehoog 0:fd29cd85e75e 49 */
jensdehoog 0:fd29cd85e75e 50
rubends 3:793425387c91 51 void MQQTConnect(){
rubends 3:793425387c91 52 eth.connect();
rubends 3:793425387c91 53 while(eth.get_ip_address()== NULL){
rubends 3:793425387c91 54 }
rubends 3:793425387c91 55
rubends 3:793425387c91 56 network.connect(BROKER_NAME, BROKER_PORT);
rubends 3:793425387c91 57 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
rubends 3:793425387c91 58 data.MQTTVersion = MQTT_VERSION;
rubends 3:793425387c91 59 data.clientID.cstring = "client-ruben";
rubends 3:793425387c91 60 data.username.cstring = "user";
rubends 3:793425387c91 61 data.password.cstring = "pass";
rubends 3:793425387c91 62 client.connect(data);
rubends 3:793425387c91 63 connected = true;
rubends 3:793425387c91 64 led2 = 1;
rubends 3:793425387c91 65 }
rubends 3:793425387c91 66
rubends 3:793425387c91 67 void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* topic, char* buf, int qos, bool retained, bool duplicate){
rubends 3:793425387c91 68 MQTT::Message message;
rubends 4:bb892177ce23 69 if(qos == 0){
rubends 4:bb892177ce23 70 message.qos = MQTT::QOS0;
rubends 4:bb892177ce23 71 }
rubends 3:793425387c91 72 message.retained = retained;
rubends 3:793425387c91 73 message.dup = duplicate;
rubends 3:793425387c91 74 message.payload = (void*)buf;
rubends 3:793425387c91 75 message.payloadlen = strlen(buf) + 1;
rubends 3:793425387c91 76
rubends 3:793425387c91 77 client->publish(topic, message);
rubends 3:793425387c91 78 }
rubends 3:793425387c91 79
jensdehoog 0:fd29cd85e75e 80 int main(int argc, char* argv[])
jensdehoog 0:fd29cd85e75e 81 {
rubends 4:bb892177ce23 82 printf("boot\n");
rubends 3:793425387c91 83 button.fall(callback(&button1_onpressed_cb));
rubends 4:bb892177ce23 84 printf("here\n");
rubends 3:793425387c91 85 MQQTConnect();
rubends 4:bb892177ce23 86 printf("connected\n");
rubends 3:793425387c91 87 while(connected){
rubends 3:793425387c91 88 if(!button1_busy && multiclick_state != 0){
rubends 3:793425387c91 89 char* buf = "";
rubends 3:793425387c91 90 switch(multiclick_state)
rubends 3:793425387c91 91 {
rubends 3:793425387c91 92 case 1:
rubends 3:793425387c91 93 buf = "upvote";
rubends 3:793425387c91 94 sendMessage(&client, topic, buf, 0, false, false);
rubends 3:793425387c91 95 break;
rubends 3:793425387c91 96 case 2:
rubends 3:793425387c91 97 buf = "downvote";
rubends 3:793425387c91 98 sendMessage(&client, topic, buf, 0, false, false);
rubends 3:793425387c91 99 break;
rubends 3:793425387c91 100 case 4:
rubends 3:793425387c91 101 client.disconnect();
rubends 3:793425387c91 102 network.disconnect();
rubends 3:793425387c91 103 connected = false;
rubends 3:793425387c91 104 led2 = 0;
rubends 3:793425387c91 105 }
rubends 4:bb892177ce23 106 multiclick_state = 0;
rubends 3:793425387c91 107 printf("\n click %d: %s", multiclick_state, buf);
rubends 3:793425387c91 108 }
rubends 3:793425387c91 109 }
jensdehoog 0:fd29cd85e75e 110 return 0;
jensdehoog 0:fd29cd85e75e 111 }