Ruben De Swaef
/
PGO6_VoteController_code
Votecontroller
Fork of PGO6_VoteController_template by
main.cpp@3:793425387c91, 2018-10-15 (annotated)
- Committer:
- rubends
- Date:
- Mon Oct 15 14:30:31 2018 +0000
- Revision:
- 3:793425387c91
- Parent:
- 2:5b7d055dbc91
- Child:
- 4:bb892177ce23
MQTT
Who changed what in which revision?
User | Revision | Line number | New 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(ð); |
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 | |
rubends | 3:793425387c91 | 59 | data.MQTTVersion = MQTT_VERSION; |
rubends | 3:793425387c91 | 60 | data.clientID.cstring = "client-ruben"; |
rubends | 3:793425387c91 | 61 | data.username.cstring = "user"; |
rubends | 3:793425387c91 | 62 | data.password.cstring = "pass"; |
rubends | 3:793425387c91 | 63 | client.connect(data); |
rubends | 3:793425387c91 | 64 | connected = true; |
rubends | 3:793425387c91 | 65 | led2 = 1; |
rubends | 3:793425387c91 | 66 | } |
rubends | 3:793425387c91 | 67 | |
rubends | 3:793425387c91 | 68 | void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* topic, char* buf, int qos, bool retained, bool duplicate){ |
rubends | 3:793425387c91 | 69 | MQTT::Message message; |
rubends | 3:793425387c91 | 70 | message.qos = MQTT::QOS0; |
rubends | 3:793425387c91 | 71 | message.retained = retained; |
rubends | 3:793425387c91 | 72 | message.dup = duplicate; |
rubends | 3:793425387c91 | 73 | message.payload = (void*)buf; |
rubends | 3:793425387c91 | 74 | message.payloadlen = strlen(buf) + 1; |
rubends | 3:793425387c91 | 75 | |
rubends | 3:793425387c91 | 76 | client->publish(topic, message); |
rubends | 3:793425387c91 | 77 | } |
rubends | 3:793425387c91 | 78 | |
jensdehoog | 0:fd29cd85e75e | 79 | int main(int argc, char* argv[]) |
jensdehoog | 0:fd29cd85e75e | 80 | { |
rubends | 3:793425387c91 | 81 | printf("boot"); |
rubends | 3:793425387c91 | 82 | button.fall(callback(&button1_onpressed_cb)); |
rubends | 3:793425387c91 | 83 | MQQTConnect(); |
rubends | 3:793425387c91 | 84 | while(connected){ |
rubends | 3:793425387c91 | 85 | if(!button1_busy && multiclick_state != 0){ |
rubends | 3:793425387c91 | 86 | char* buf = ""; |
rubends | 3:793425387c91 | 87 | switch(multiclick_state) |
rubends | 3:793425387c91 | 88 | { |
rubends | 3:793425387c91 | 89 | case 1: |
rubends | 3:793425387c91 | 90 | buf = "upvote"; |
rubends | 3:793425387c91 | 91 | sendMessage(&client, topic, buf, 0, false, false); |
rubends | 3:793425387c91 | 92 | break; |
rubends | 3:793425387c91 | 93 | case 2: |
rubends | 3:793425387c91 | 94 | buf = "downvote"; |
rubends | 3:793425387c91 | 95 | sendMessage(&client, topic, buf, 0, false, false); |
rubends | 3:793425387c91 | 96 | break; |
rubends | 3:793425387c91 | 97 | case 4: |
rubends | 3:793425387c91 | 98 | client.disconnect(); |
rubends | 3:793425387c91 | 99 | network.disconnect(); |
rubends | 3:793425387c91 | 100 | connected = false; |
rubends | 3:793425387c91 | 101 | led2 = 0; |
rubends | 3:793425387c91 | 102 | } |
rubends | 3:793425387c91 | 103 | printf("\n click %d: %s", multiclick_state, buf); |
rubends | 3:793425387c91 | 104 | } |
rubends | 3:793425387c91 | 105 | } |
jensdehoog | 0:fd29cd85e75e | 106 | return 0; |
jensdehoog | 0:fd29cd85e75e | 107 | } |