PGO6_VoteController (Astrid Vanneste)

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
AstridVanneste
Date:
Thu Oct 11 09:51:37 2018 +0000
Revision:
6:60b968c8f35c
Parent:
5:ba94770ce1c7
MQTT service

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
AstridVanneste 6:60b968c8f35c 12 char* topic = "test/embedded\0";
AstridVanneste 6:60b968c8f35c 13
AstridVanneste 6:60b968c8f35c 14 void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* topic, char* payload, int qos, bool retained, bool duplicate);
jensdehoog 0:fd29cd85e75e 15
jensdehoog 0:fd29cd85e75e 16 /**
jensdehoog 0:fd29cd85e75e 17 TODO
jensdehoog 0:fd29cd85e75e 18 ----
jensdehoog 0:fd29cd85e75e 19 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
jensdehoog 0:fd29cd85e75e 20 - Make an MQTT-service which:
jensdehoog 0:fd29cd85e75e 21 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
jensdehoog 0:fd29cd85e75e 22 - makes a client and connects it to the broker using a client ID and credentials (username & password).
jensdehoog 0:fd29cd85e75e 23 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
jensdehoog 0:fd29cd85e75e 24 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
jensdehoog 0:fd29cd85e75e 25 can be called each time we want to send a certain message.
jensdehoog 0:fd29cd85e75e 26 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
jensdehoog 0:fd29cd85e75e 27 the program disconnects from the broker and terminates.
jensdehoog 0:fd29cd85e75e 28
jensdehoog 0:fd29cd85e75e 29 Extra
jensdehoog 0:fd29cd85e75e 30 -----
jensdehoog 0:fd29cd85e75e 31 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
jensdehoog 0:fd29cd85e75e 32 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
jensdehoog 0:fd29cd85e75e 33
jensdehoog 0:fd29cd85e75e 34 Tips & tricks
jensdehoog 0:fd29cd85e75e 35 -------------
jensdehoog 0:fd29cd85e75e 36 - To generate an interrupt on the press of a button, use:
jensdehoog 0:fd29cd85e75e 37 InterruptIn button(USER_BUTTON);
jensdehoog 0:fd29cd85e75e 38 ...
jensdehoog 0:fd29cd85e75e 39 button.fall(callback(someFunction));
jensdehoog 0:fd29cd85e75e 40 - Before implementing MQTT, test the multiclick feature first.
jensdehoog 0:fd29cd85e75e 41 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
jensdehoog 0:fd29cd85e75e 42 - To have a uniform message sending procedure, use the following function usage:
jensdehoog 0:fd29cd85e75e 43 sendMessage(&client, topic, buf, qos, retained, duplicate)
jensdehoog 0:fd29cd85e75e 44 */
jensdehoog 0:fd29cd85e75e 45
jensdehoog 0:fd29cd85e75e 46 int main(int argc, char* argv[])
jensdehoog 0:fd29cd85e75e 47 {
AstridVanneste 5:ba94770ce1c7 48 printf("STARTED!\n");
AstridVanneste 3:376ac6744373 49 InterruptIn button(USER_BUTTON);
AstridVanneste 5:ba94770ce1c7 50 button.fall(callback(&button1_onpressed_cb));
AstridVanneste 3:376ac6744373 51 init_debouncer();
AstridVanneste 4:08da93eb6014 52
AstridVanneste 6:60b968c8f35c 53 printf("Trying to connect to ethernet network\n");
AstridVanneste 5:ba94770ce1c7 54 EthernetInterface network;
AstridVanneste 5:ba94770ce1c7 55 int error = network.connect();
AstridVanneste 5:ba94770ce1c7 56 if(error != 0)
AstridVanneste 5:ba94770ce1c7 57 {
AstridVanneste 5:ba94770ce1c7 58 printf("ERROR: ethernet.connect() = %d\n", error);
AstridVanneste 5:ba94770ce1c7 59 }
AstridVanneste 5:ba94770ce1c7 60 const char* ip_add = network.get_ip_address();
AstridVanneste 5:ba94770ce1c7 61
AstridVanneste 6:60b968c8f35c 62 printf("Success!\n");
AstridVanneste 6:60b968c8f35c 63 printf("Trying to connect to MQTT network\n");
AstridVanneste 6:60b968c8f35c 64
AstridVanneste 5:ba94770ce1c7 65 MQTTNetwork mqttNetwork(&network);
AstridVanneste 5:ba94770ce1c7 66
AstridVanneste 5:ba94770ce1c7 67 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
AstridVanneste 5:ba94770ce1c7 68
AstridVanneste 5:ba94770ce1c7 69 error = mqttNetwork.connect(BROKER_NAME, BROKER_PORT);
AstridVanneste 5:ba94770ce1c7 70 if(error != 0)
AstridVanneste 5:ba94770ce1c7 71 {
AstridVanneste 5:ba94770ce1c7 72 printf("ERROR: mqtt.connect() = %d\n", error);
AstridVanneste 5:ba94770ce1c7 73 }
AstridVanneste 6:60b968c8f35c 74 printf("Success\n");
AstridVanneste 5:ba94770ce1c7 75
AstridVanneste 5:ba94770ce1c7 76 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
AstridVanneste 5:ba94770ce1c7 77 data.MQTTVersion = MQTT_VERSION;
AstridVanneste 6:60b968c8f35c 78 data.clientID.cstring = "test";
AstridVanneste 6:60b968c8f35c 79 data.username.cstring = "username";
AstridVanneste 6:60b968c8f35c 80 data.password.cstring = "password";
AstridVanneste 6:60b968c8f35c 81
AstridVanneste 6:60b968c8f35c 82 error = client.connect(data);
AstridVanneste 6:60b968c8f35c 83 if(error != 0)
AstridVanneste 6:60b968c8f35c 84 {
AstridVanneste 6:60b968c8f35c 85 printf("ERROR: client.connect() = %d\n", error);
AstridVanneste 6:60b968c8f35c 86 }
AstridVanneste 5:ba94770ce1c7 87
AstridVanneste 5:ba94770ce1c7 88 printf("INITIALIZED\n");
AstridVanneste 4:08da93eb6014 89
AstridVanneste 6:60b968c8f35c 90 bool connected = true;
AstridVanneste 6:60b968c8f35c 91
AstridVanneste 6:60b968c8f35c 92 while(connected)
AstridVanneste 4:08da93eb6014 93 {
AstridVanneste 5:ba94770ce1c7 94 if(button1_pressed)
AstridVanneste 5:ba94770ce1c7 95 {
AstridVanneste 6:60b968c8f35c 96 char* buf = "";
AstridVanneste 5:ba94770ce1c7 97 printf("Clicks: %d\n", multiclick_state);
AstridVanneste 5:ba94770ce1c7 98 button1_pressed = false;
AstridVanneste 6:60b968c8f35c 99
AstridVanneste 6:60b968c8f35c 100 switch(multiclick_state)
AstridVanneste 6:60b968c8f35c 101 {
AstridVanneste 6:60b968c8f35c 102 case 1:
AstridVanneste 6:60b968c8f35c 103 buf = "upvote\n";
AstridVanneste 6:60b968c8f35c 104 sendMessage(&client, topic, buf, 0, false, false);
AstridVanneste 6:60b968c8f35c 105 printf("upvote\n");
AstridVanneste 6:60b968c8f35c 106 break;
AstridVanneste 6:60b968c8f35c 107 case 2:
AstridVanneste 6:60b968c8f35c 108 buf = "downvote\n";
AstridVanneste 6:60b968c8f35c 109 sendMessage(&client, topic, buf, 0, false, false);
AstridVanneste 6:60b968c8f35c 110 printf("downvote\n");
AstridVanneste 6:60b968c8f35c 111 break;
AstridVanneste 6:60b968c8f35c 112 case 4:
AstridVanneste 6:60b968c8f35c 113 mqttNetwork.disconnect();
AstridVanneste 6:60b968c8f35c 114 connected = false;
AstridVanneste 6:60b968c8f35c 115 }
AstridVanneste 5:ba94770ce1c7 116 }
AstridVanneste 4:08da93eb6014 117 }
AstridVanneste 6:60b968c8f35c 118 printf("DISCONNECTED!\n");
jensdehoog 0:fd29cd85e75e 119 return 0;
jensdehoog 0:fd29cd85e75e 120 }
AstridVanneste 6:60b968c8f35c 121
AstridVanneste 6:60b968c8f35c 122 void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* top, char* payload, int qos, bool retained, bool duplicate)
AstridVanneste 6:60b968c8f35c 123 {
AstridVanneste 6:60b968c8f35c 124 MQTT::Message message;
AstridVanneste 6:60b968c8f35c 125
AstridVanneste 6:60b968c8f35c 126 switch(qos)
AstridVanneste 6:60b968c8f35c 127 {
AstridVanneste 6:60b968c8f35c 128 case 0:
AstridVanneste 6:60b968c8f35c 129 message.qos = MQTT::QOS0;
AstridVanneste 6:60b968c8f35c 130 break;
AstridVanneste 6:60b968c8f35c 131 case 1:
AstridVanneste 6:60b968c8f35c 132 message.qos = MQTT::QOS1;
AstridVanneste 6:60b968c8f35c 133 break;
AstridVanneste 6:60b968c8f35c 134 case 2:
AstridVanneste 6:60b968c8f35c 135 message.qos = MQTT::QOS2;
AstridVanneste 6:60b968c8f35c 136 break;
AstridVanneste 6:60b968c8f35c 137 default:
AstridVanneste 6:60b968c8f35c 138 message.qos = MQTT::QOS0;
AstridVanneste 6:60b968c8f35c 139 }
AstridVanneste 6:60b968c8f35c 140 message.retained = retained;
AstridVanneste 6:60b968c8f35c 141 message.dup = duplicate;
AstridVanneste 6:60b968c8f35c 142 message.payload = (void*)payload;
AstridVanneste 6:60b968c8f35c 143 message.payloadlen = strlen(payload) + 1;
AstridVanneste 6:60b968c8f35c 144
AstridVanneste 6:60b968c8f35c 145 int error = client->publish(top, message);
AstridVanneste 6:60b968c8f35c 146 if(error != 0)
AstridVanneste 6:60b968c8f35c 147 {
AstridVanneste 6:60b968c8f35c 148 printf("ERROR: client.publish() = %d\n", error);
AstridVanneste 6:60b968c8f35c 149 }
AstridVanneste 6:60b968c8f35c 150 }