Aleksander Chernyavskiy
/
pgo6_aleksander_chernyavskiy
opdracht mqtt nucleo
Fork of PGO6_VoteController_template by
main.cpp@3:abfa79c5b7bd, 2018-10-11 (annotated)
- Committer:
- alex69
- Date:
- Thu Oct 11 15:52:51 2018 +0000
- Revision:
- 3:abfa79c5b7bd
- Parent:
- 2:5b7d055dbc91
Initial commit
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 | |
jensdehoog | 0:fd29cd85e75e | 12 | char* topic; |
jensdehoog | 0:fd29cd85e75e | 13 | |
jensdehoog | 0:fd29cd85e75e | 14 | /** |
jensdehoog | 0:fd29cd85e75e | 15 | TODO |
jensdehoog | 0:fd29cd85e75e | 16 | ---- |
alex69 | 3:abfa79c5b7bd | 17 | |
jensdehoog | 0:fd29cd85e75e | 18 | - Make an MQTT-service which: |
jensdehoog | 0:fd29cd85e75e | 19 | - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service |
jensdehoog | 0:fd29cd85e75e | 20 | you are going to use. Make a separate function which handles the sending procedure. Therefore, this function |
jensdehoog | 0:fd29cd85e75e | 21 | can be called each time we want to send a certain message. |
jensdehoog | 0:fd29cd85e75e | 22 | - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times, |
jensdehoog | 0:fd29cd85e75e | 23 | the program disconnects from the broker and terminates. |
jensdehoog | 0:fd29cd85e75e | 24 | |
jensdehoog | 0:fd29cd85e75e | 25 | Extra |
jensdehoog | 0:fd29cd85e75e | 26 | ----- |
jensdehoog | 0:fd29cd85e75e | 27 | - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal. |
jensdehoog | 0:fd29cd85e75e | 28 | - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones. |
jensdehoog | 0:fd29cd85e75e | 29 | |
jensdehoog | 0:fd29cd85e75e | 30 | Tips & tricks |
jensdehoog | 0:fd29cd85e75e | 31 | - Before implementing MQTT, test the multiclick feature first. |
jensdehoog | 0:fd29cd85e75e | 32 | - Have a look at the MQTT-library for Mbed and the HelloMQTT-example. |
jensdehoog | 0:fd29cd85e75e | 33 | - To have a uniform message sending procedure, use the following function usage: |
jensdehoog | 0:fd29cd85e75e | 34 | sendMessage(&client, topic, buf, qos, retained, duplicate) |
jensdehoog | 0:fd29cd85e75e | 35 | */ |
alex69 | 3:abfa79c5b7bd | 36 | InterruptIn button1(USER_BUTTON); |
alex69 | 3:abfa79c5b7bd | 37 | int sendMessage(MQTT::Client<MQTTNetwork, Countdown> client, char* msg, char* topic) { |
alex69 | 3:abfa79c5b7bd | 38 | MQTT::Message message; |
alex69 | 3:abfa79c5b7bd | 39 | // QoS 0 |
alex69 | 3:abfa79c5b7bd | 40 | char buf[100]; |
alex69 | 3:abfa79c5b7bd | 41 | sprintf(buf, msg); |
alex69 | 3:abfa79c5b7bd | 42 | message.qos = MQTT::QOS0; |
alex69 | 3:abfa79c5b7bd | 43 | message.retained = false; |
alex69 | 3:abfa79c5b7bd | 44 | message.dup = false; |
alex69 | 3:abfa79c5b7bd | 45 | message.payload = (void*)buf; |
alex69 | 3:abfa79c5b7bd | 46 | message.payloadlen = strlen(buf)+1; |
alex69 | 3:abfa79c5b7bd | 47 | int rc = client.publish(topic, message); |
alex69 | 3:abfa79c5b7bd | 48 | // while (arrivedcount < 1) |
alex69 | 3:abfa79c5b7bd | 49 | // client.yield(100); |
alex69 | 3:abfa79c5b7bd | 50 | return rc; |
alex69 | 3:abfa79c5b7bd | 51 | } |
jensdehoog | 0:fd29cd85e75e | 52 | int main(int argc, char* argv[]) |
jensdehoog | 0:fd29cd85e75e | 53 | { |
alex69 | 3:abfa79c5b7bd | 54 | // enables the interrupt on the button |
alex69 | 3:abfa79c5b7bd | 55 | button1.fall(callback(button1_onpressed_cb)); |
alex69 | 3:abfa79c5b7bd | 56 | EthernetInterface eth; |
alex69 | 3:abfa79c5b7bd | 57 | // starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. |
alex69 | 3:abfa79c5b7bd | 58 | if (eth.connect() == 0) { |
alex69 | 3:abfa79c5b7bd | 59 | printf("Ip address: %s,\n", eth.get_ip_address()); |
alex69 | 3:abfa79c5b7bd | 60 | } else { |
alex69 | 3:abfa79c5b7bd | 61 | printf("Something went wrong with connecting the ethernet interface\n"); |
alex69 | 3:abfa79c5b7bd | 62 | return -1; |
alex69 | 3:abfa79c5b7bd | 63 | } |
alex69 | 3:abfa79c5b7bd | 64 | |
alex69 | 3:abfa79c5b7bd | 65 | MQTTNetwork mqttNetwork(ð); |
alex69 | 3:abfa79c5b7bd | 66 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
alex69 | 3:abfa79c5b7bd | 67 | printf("Connecting to %s on port %d", BROKER_NAME, BROKER_PORT); |
alex69 | 3:abfa79c5b7bd | 68 | // makes a client and connects it to the broker using a client ID and credentials (username & password). |
alex69 | 3:abfa79c5b7bd | 69 | int rc = mqttNetwork.connect(BROKER_NAME, BROKER_PORT); |
alex69 | 3:abfa79c5b7bd | 70 | if (rc != 0) { |
alex69 | 3:abfa79c5b7bd | 71 | printf("return code is %d", rc); |
alex69 | 3:abfa79c5b7bd | 72 | } |
alex69 | 3:abfa79c5b7bd | 73 | |
alex69 | 3:abfa79c5b7bd | 74 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
alex69 | 3:abfa79c5b7bd | 75 | data.MQTTVersion = 3; |
alex69 | 3:abfa79c5b7bd | 76 | data.clientID.cstring = "mbed-sample"; |
alex69 | 3:abfa79c5b7bd | 77 | data.username.cstring = "testusser"; |
alex69 | 3:abfa79c5b7bd | 78 | data.password.cstring = "testpassword"; |
alex69 | 3:abfa79c5b7bd | 79 | int rc2 = client.connect(data); |
alex69 | 3:abfa79c5b7bd | 80 | if ( rc2 != 0) |
alex69 | 3:abfa79c5b7bd | 81 | printf("rc from MQTT connect is %d\r\n", rc); |
alex69 | 3:abfa79c5b7bd | 82 | |
alex69 | 3:abfa79c5b7bd | 83 | while(1) { |
alex69 | 3:abfa79c5b7bd | 84 | // Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. |
alex69 | 3:abfa79c5b7bd | 85 | // printf("The button has been pressed %d times \n", multiclick_state); |
alex69 | 3:abfa79c5b7bd | 86 | |
alex69 | 3:abfa79c5b7bd | 87 | //makes sure there is no infinite loop in the switch case |
alex69 | 3:abfa79c5b7bd | 88 | if(multiclick_state != 0) { |
alex69 | 3:abfa79c5b7bd | 89 | switch(multiclick_state) { |
alex69 | 3:abfa79c5b7bd | 90 | case 1: |
alex69 | 3:abfa79c5b7bd | 91 | printf("Upvote has been sent\n"); |
alex69 | 3:abfa79c5b7bd | 92 | sendMessage(client, "Upvote", "alex"); |
alex69 | 3:abfa79c5b7bd | 93 | break; |
alex69 | 3:abfa79c5b7bd | 94 | case 2: |
alex69 | 3:abfa79c5b7bd | 95 | printf("Downvote has been sent\n"); |
alex69 | 3:abfa79c5b7bd | 96 | sendMessage(client, "Downvote", "alex"); |
alex69 | 3:abfa79c5b7bd | 97 | break; |
alex69 | 3:abfa79c5b7bd | 98 | case 3: |
alex69 | 3:abfa79c5b7bd | 99 | printf("No action\n"); |
alex69 | 3:abfa79c5b7bd | 100 | break; |
alex69 | 3:abfa79c5b7bd | 101 | case 4: |
alex69 | 3:abfa79c5b7bd | 102 | printf("Disconnecting...\n"); |
alex69 | 3:abfa79c5b7bd | 103 | sendMessage(client, "Disconnect", "alex"); |
alex69 | 3:abfa79c5b7bd | 104 | break; |
alex69 | 3:abfa79c5b7bd | 105 | default: |
alex69 | 3:abfa79c5b7bd | 106 | printf("No action\n"); |
alex69 | 3:abfa79c5b7bd | 107 | break; |
alex69 | 3:abfa79c5b7bd | 108 | } |
alex69 | 3:abfa79c5b7bd | 109 | multiclick_state = 0; |
alex69 | 3:abfa79c5b7bd | 110 | } |
alex69 | 3:abfa79c5b7bd | 111 | |
alex69 | 3:abfa79c5b7bd | 112 | } |
alex69 | 3:abfa79c5b7bd | 113 | |
jensdehoog | 0:fd29cd85e75e | 114 | return 0; |
jensdehoog | 0:fd29cd85e75e | 115 | } |