VoteController

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
Dieter97
Date:
Thu Oct 11 16:09:27 2018 +0000
Revision:
4:74736eccf6bb
Parent:
3:6b6cab4ad185
fixed sending to wrong topic

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
jensdehoog 0:fd29cd85e75e 12 char* topic;
jensdehoog 0:fd29cd85e75e 13
jensdehoog 0:fd29cd85e75e 14 /**
jensdehoog 0:fd29cd85e75e 15 TODO
jensdehoog 0:fd29cd85e75e 16 ----
jensdehoog 0:fd29cd85e75e 17 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
jensdehoog 0:fd29cd85e75e 18 - Make an MQTT-service which:
jensdehoog 0:fd29cd85e75e 19 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
jensdehoog 0:fd29cd85e75e 20 - makes a client and connects it to the broker using a client ID and credentials (username & password).
jensdehoog 0:fd29cd85e75e 21 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
jensdehoog 0:fd29cd85e75e 22 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
jensdehoog 0:fd29cd85e75e 23 can be called each time we want to send a certain message.
jensdehoog 0:fd29cd85e75e 24 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
jensdehoog 0:fd29cd85e75e 25 the program disconnects from the broker and terminates.
jensdehoog 0:fd29cd85e75e 26
jensdehoog 0:fd29cd85e75e 27 Extra
jensdehoog 0:fd29cd85e75e 28 -----
jensdehoog 0:fd29cd85e75e 29 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
jensdehoog 0:fd29cd85e75e 30 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
jensdehoog 0:fd29cd85e75e 31
jensdehoog 0:fd29cd85e75e 32 Tips & tricks
jensdehoog 0:fd29cd85e75e 33 -------------
jensdehoog 0:fd29cd85e75e 34 - To generate an interrupt on the press of a button, use:
jensdehoog 0:fd29cd85e75e 35 InterruptIn button(USER_BUTTON);
jensdehoog 0:fd29cd85e75e 36 ...
jensdehoog 0:fd29cd85e75e 37 button.fall(callback(someFunction));
jensdehoog 0:fd29cd85e75e 38 - Before implementing MQTT, test the multiclick feature first.
jensdehoog 0:fd29cd85e75e 39 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
jensdehoog 0:fd29cd85e75e 40 - To have a uniform message sending procedure, use the following function usage:
jensdehoog 0:fd29cd85e75e 41 sendMessage(&client, topic, buf, qos, retained, duplicate)
jensdehoog 0:fd29cd85e75e 42 */
jensdehoog 0:fd29cd85e75e 43
Dieter97 3:6b6cab4ad185 44 DigitalOut led2(LED2);
Dieter97 3:6b6cab4ad185 45 InterruptIn button1(USER_BUTTON);
Dieter97 3:6b6cab4ad185 46 EthernetInterface eth;
Dieter97 3:6b6cab4ad185 47
Dieter97 3:6b6cab4ad185 48 /**
Dieter97 3:6b6cab4ad185 49 Send the MQTT message to a requested topic
Dieter97 3:6b6cab4ad185 50 */
Dieter97 3:6b6cab4ad185 51 int sendMessage(char* message,char* topic, MQTT::Client<MQTTNetwork, Countdown>* client){
Dieter97 3:6b6cab4ad185 52 MQTT::Message msg;
Dieter97 3:6b6cab4ad185 53
Dieter97 3:6b6cab4ad185 54 // QoS 0
Dieter97 3:6b6cab4ad185 55 char buf[100];
Dieter97 3:6b6cab4ad185 56 sprintf(buf, message);
Dieter97 3:6b6cab4ad185 57 msg.qos = MQTT::QOS0;
Dieter97 3:6b6cab4ad185 58 msg.retained = false;
Dieter97 3:6b6cab4ad185 59 msg.dup = false;
Dieter97 3:6b6cab4ad185 60 msg.payload = (void*)buf;
Dieter97 3:6b6cab4ad185 61 msg.payloadlen = strlen(buf)+1;
Dieter97 3:6b6cab4ad185 62 return client->publish(topic, msg);
Dieter97 3:6b6cab4ad185 63 }
jensdehoog 0:fd29cd85e75e 64
Dieter97 3:6b6cab4ad185 65 int main(int argc, char* argv[])
Dieter97 3:6b6cab4ad185 66 {
Dieter97 3:6b6cab4ad185 67 //Startup
Dieter97 3:6b6cab4ad185 68 if(eth.connect() != 0){
Dieter97 3:6b6cab4ad185 69 printf("Error initializing the ethernet interface!");
Dieter97 3:6b6cab4ad185 70 return -1;
Dieter97 3:6b6cab4ad185 71 }
Dieter97 3:6b6cab4ad185 72 printf("IP address %s\n",eth.get_ip_address());
Dieter97 3:6b6cab4ad185 73
Dieter97 3:6b6cab4ad185 74 MQTTNetwork mqttNetwork(&eth);
Dieter97 3:6b6cab4ad185 75 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
Dieter97 3:6b6cab4ad185 76 int rc = mqttNetwork.connect(BROKER_NAME, BROKER_PORT);
Dieter97 3:6b6cab4ad185 77 if (rc != 0){
Dieter97 3:6b6cab4ad185 78 printf("Error connection to MQTT Broker code: %d\n",rc);
Dieter97 3:6b6cab4ad185 79 return -1;
Dieter97 3:6b6cab4ad185 80 }
Dieter97 3:6b6cab4ad185 81 printf("MQTT Broker connected!\n");
Dieter97 3:6b6cab4ad185 82 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
Dieter97 3:6b6cab4ad185 83 data.MQTTVersion = 3;
Dieter97 3:6b6cab4ad185 84 data.clientID.cstring = "clientId-d9oe9aQeGh";
Dieter97 3:6b6cab4ad185 85 data.username.cstring = "dieter";
Dieter97 3:6b6cab4ad185 86 data.password.cstring = "123456";
Dieter97 3:6b6cab4ad185 87 rc = client.connect(data);
Dieter97 3:6b6cab4ad185 88 if (rc != 0){
Dieter97 3:6b6cab4ad185 89 printf("Error MQTT connetion data code: %d\n",rc);
Dieter97 3:6b6cab4ad185 90 return -1;
Dieter97 3:6b6cab4ad185 91 }
Dieter97 3:6b6cab4ad185 92 printf("MQTT client connected!\n");
Dieter97 3:6b6cab4ad185 93
Dieter97 3:6b6cab4ad185 94 // Attach ISR to handle button press event
Dieter97 3:6b6cab4ad185 95 button1.fall(callback(button1_onpressed_cb));
Dieter97 3:6b6cab4ad185 96
Dieter97 3:6b6cab4ad185 97 //Program
Dieter97 3:6b6cab4ad185 98 while(1){
Dieter97 3:6b6cab4ad185 99
Dieter97 3:6b6cab4ad185 100 if(multiclick_state != 0){
Dieter97 3:6b6cab4ad185 101 printf("Button pressed %d times\n", multiclick_state);
Dieter97 3:6b6cab4ad185 102 switch(multiclick_state){
Dieter97 3:6b6cab4ad185 103 case 1:
Dieter97 3:6b6cab4ad185 104 printf("Upvote\n");
Dieter97 4:74736eccf6bb 105 sendMessage("UPVOTE","dieter",&client);
Dieter97 3:6b6cab4ad185 106 break;
Dieter97 3:6b6cab4ad185 107 case 2:
Dieter97 3:6b6cab4ad185 108 printf("Downvote\n");
Dieter97 3:6b6cab4ad185 109 sendMessage("DOWNVOTE","dieter",&client);
Dieter97 3:6b6cab4ad185 110 break;
Dieter97 3:6b6cab4ad185 111 case 3:
Dieter97 3:6b6cab4ad185 112 printf("Nothing\n");
Dieter97 3:6b6cab4ad185 113 break;
Dieter97 3:6b6cab4ad185 114 case 4:
Dieter97 3:6b6cab4ad185 115 printf("Disconnecting...\n");
Dieter97 3:6b6cab4ad185 116 mqttNetwork.disconnect();
Dieter97 3:6b6cab4ad185 117 printf("Bye ;)\n");
Dieter97 3:6b6cab4ad185 118 return 1;
Dieter97 3:6b6cab4ad185 119 default:
Dieter97 3:6b6cab4ad185 120 //DO nothing
Dieter97 3:6b6cab4ad185 121 break;
Dieter97 3:6b6cab4ad185 122 }
Dieter97 3:6b6cab4ad185 123 //Reset the multiclick state
Dieter97 3:6b6cab4ad185 124 multiclick_state = 0;
Dieter97 3:6b6cab4ad185 125 }
Dieter97 3:6b6cab4ad185 126 }
jensdehoog 0:fd29cd85e75e 127 return 0;
Dieter97 3:6b6cab4ad185 128 }