Votecontroller

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
rubends
Date:
Mon Oct 15 15:58:17 2018 +0000
Revision:
5:592f4272c549
Parent:
4:bb892177ce23
Final

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 5:592f4272c549 63
rubends 3:793425387c91 64 connected = true;
rubends 3:793425387c91 65 led2 = 1;
rubends 5:592f4272c549 66 printf("connected\n");
rubends 3:793425387c91 67 }
rubends 3:793425387c91 68
rubends 5:592f4272c549 69 void sendMessage(MQTT::Client<MQTTNetwork, Countdown>& client, char* topic, char* buf, int qos, bool retained, bool duplicate){
rubends 3:793425387c91 70 MQTT::Message message;
rubends 5:592f4272c549 71
rubends 5:592f4272c549 72 switch(qos)
rubends 5:592f4272c549 73 {
rubends 5:592f4272c549 74 case 0:
rubends 5:592f4272c549 75 message.qos = MQTT::QOS0;
rubends 5:592f4272c549 76 break;
rubends 5:592f4272c549 77 case 1:
rubends 5:592f4272c549 78 message.qos = MQTT::QOS1;
rubends 5:592f4272c549 79 break;
rubends 5:592f4272c549 80 case 2:
rubends 5:592f4272c549 81 message.qos = MQTT::QOS2;
rubends 5:592f4272c549 82 break;
rubends 5:592f4272c549 83 default:
rubends 5:592f4272c549 84 message.qos = MQTT::QOS0;
rubends 4:bb892177ce23 85 }
rubends 5:592f4272c549 86
rubends 3:793425387c91 87 message.retained = retained;
rubends 3:793425387c91 88 message.dup = duplicate;
rubends 3:793425387c91 89 message.payload = (void*)buf;
rubends 3:793425387c91 90 message.payloadlen = strlen(buf) + 1;
rubends 5:592f4272c549 91 client.publish(topic, message);
rubends 3:793425387c91 92 }
rubends 3:793425387c91 93
jensdehoog 0:fd29cd85e75e 94 int main(int argc, char* argv[])
jensdehoog 0:fd29cd85e75e 95 {
rubends 5:592f4272c549 96 printf("\nBoot controller \nConnecting... \n");
rubends 3:793425387c91 97 button.fall(callback(&button1_onpressed_cb));
rubends 3:793425387c91 98 MQQTConnect();
rubends 3:793425387c91 99 while(connected){
rubends 3:793425387c91 100 if(!button1_busy && multiclick_state != 0){
rubends 3:793425387c91 101 char* buf = "";
rubends 3:793425387c91 102 switch(multiclick_state)
rubends 3:793425387c91 103 {
rubends 3:793425387c91 104 case 1:
rubends 3:793425387c91 105 buf = "upvote";
rubends 5:592f4272c549 106 sendMessage(client, topic, buf, 0, false, false);
rubends 3:793425387c91 107 break;
rubends 3:793425387c91 108 case 2:
rubends 3:793425387c91 109 buf = "downvote";
rubends 5:592f4272c549 110 sendMessage(client, topic, buf, 0, false, false);
rubends 3:793425387c91 111 break;
rubends 3:793425387c91 112 case 4:
rubends 3:793425387c91 113 client.disconnect();
rubends 3:793425387c91 114 network.disconnect();
rubends 3:793425387c91 115 connected = false;
rubends 3:793425387c91 116 led2 = 0;
rubends 5:592f4272c549 117 break;
rubends 3:793425387c91 118 }
rubends 5:592f4272c549 119 printf("click %d: %s \n", multiclick_state, buf);
rubends 4:bb892177ce23 120 multiclick_state = 0;
rubends 3:793425387c91 121 }
rubends 3:793425387c91 122 }
jensdehoog 0:fd29cd85e75e 123 return 0;
jensdehoog 0:fd29cd85e75e 124 }