Vincent Nys
/
PGO6_VoteController_template
Vincent Nys
Fork of PGO6_VoteController_template by
main.cpp@3:c53f27bdcfb5, 2017-10-31 (annotated)
- Committer:
- vincentnys
- Date:
- Tue Oct 31 18:31:34 2017 +0000
- Revision:
- 3:c53f27bdcfb5
- Parent:
- 2:5b7d055dbc91
PGO6
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 | |
vincentnys | 3:c53f27bdcfb5 | 12 | char* topic = "dansvloerVincent/votes"; |
vincentnys | 3:c53f27bdcfb5 | 13 | volatile bool button1_pressed = false; // Used in the main loop |
vincentnys | 3:c53f27bdcfb5 | 14 | volatile bool button1_enabled = true; // Used for debouncing |
vincentnys | 3:c53f27bdcfb5 | 15 | volatile int multiclick_state = 0; // Counts how many clicks occured in the time slot, used in main loop |
vincentnys | 3:c53f27bdcfb5 | 16 | volatile bool button1_busy = false; // Informs the mainloop that the user is clicking the button |
jensdehoog | 0:fd29cd85e75e | 17 | |
jensdehoog | 0:fd29cd85e75e | 18 | /** |
jensdehoog | 0:fd29cd85e75e | 19 | TODO |
jensdehoog | 0:fd29cd85e75e | 20 | ---- |
jensdehoog | 0:fd29cd85e75e | 21 | - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. |
jensdehoog | 0:fd29cd85e75e | 22 | - Make an MQTT-service which: |
jensdehoog | 0:fd29cd85e75e | 23 | - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. |
jensdehoog | 0:fd29cd85e75e | 24 | - makes a client and connects it to the broker using a client ID and credentials (username & password). |
jensdehoog | 0:fd29cd85e75e | 25 | - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service |
jensdehoog | 0:fd29cd85e75e | 26 | you are going to use. Make a separate function which handles the sending procedure. Therefore, this function |
jensdehoog | 0:fd29cd85e75e | 27 | can be called each time we want to send a certain message. |
jensdehoog | 0:fd29cd85e75e | 28 | - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times, |
jensdehoog | 0:fd29cd85e75e | 29 | the program disconnects from the broker and terminates. |
jensdehoog | 0:fd29cd85e75e | 30 | |
jensdehoog | 0:fd29cd85e75e | 31 | Extra |
jensdehoog | 0:fd29cd85e75e | 32 | ----- |
jensdehoog | 0:fd29cd85e75e | 33 | - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal. |
jensdehoog | 0:fd29cd85e75e | 34 | - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones. |
jensdehoog | 0:fd29cd85e75e | 35 | |
jensdehoog | 0:fd29cd85e75e | 36 | Tips & tricks |
jensdehoog | 0:fd29cd85e75e | 37 | ------------- |
jensdehoog | 0:fd29cd85e75e | 38 | - To generate an interrupt on the press of a button, use: |
jensdehoog | 0:fd29cd85e75e | 39 | InterruptIn button(USER_BUTTON); |
jensdehoog | 0:fd29cd85e75e | 40 | ... |
jensdehoog | 0:fd29cd85e75e | 41 | button.fall(callback(someFunction)); |
jensdehoog | 0:fd29cd85e75e | 42 | - Before implementing MQTT, test the multiclick feature first. |
jensdehoog | 0:fd29cd85e75e | 43 | - Have a look at the MQTT-library for Mbed and the HelloMQTT-example. |
jensdehoog | 0:fd29cd85e75e | 44 | - To have a uniform message sending procedure, use the following function usage: |
jensdehoog | 0:fd29cd85e75e | 45 | sendMessage(&client, topic, buf, qos, retained, duplicate) |
jensdehoog | 0:fd29cd85e75e | 46 | */ |
jensdehoog | 0:fd29cd85e75e | 47 | |
jensdehoog | 0:fd29cd85e75e | 48 | int main(int argc, char* argv[]) |
jensdehoog | 0:fd29cd85e75e | 49 | { |
vincentnys | 3:c53f27bdcfb5 | 50 | printf("Vote controller booting...\n"); |
vincentnys | 3:c53f27bdcfb5 | 51 | int rc; |
vincentnys | 3:c53f27bdcfb5 | 52 | |
vincentnys | 3:c53f27bdcfb5 | 53 | InterruptIn button(USER_BUTTON); |
vincentnys | 3:c53f27bdcfb5 | 54 | button.fall(&button1_onpressed_cb); |
vincentnys | 3:c53f27bdcfb5 | 55 | |
vincentnys | 3:c53f27bdcfb5 | 56 | EthernetInterface eth; |
vincentnys | 3:c53f27bdcfb5 | 57 | eth.connect(); |
vincentnys | 3:c53f27bdcfb5 | 58 | const char *ip = eth.get_ip_address(); |
vincentnys | 3:c53f27bdcfb5 | 59 | printf("IP address is: %s\n", ip ? ip : "No IP"); |
vincentnys | 3:c53f27bdcfb5 | 60 | |
vincentnys | 3:c53f27bdcfb5 | 61 | NetworkInterface *net = ð |
vincentnys | 3:c53f27bdcfb5 | 62 | MQTTNetwork mqtt( net ); |
vincentnys | 3:c53f27bdcfb5 | 63 | |
vincentnys | 3:c53f27bdcfb5 | 64 | MQTT::Client<MQTTNetwork, Countdown> client( mqtt ); |
vincentnys | 3:c53f27bdcfb5 | 65 | |
vincentnys | 3:c53f27bdcfb5 | 66 | char *host = "143.129.39.151";//"broker.mqttdashboard.com"; |
vincentnys | 3:c53f27bdcfb5 | 67 | int port = 1883; |
vincentnys | 3:c53f27bdcfb5 | 68 | rc = mqtt.connect(host, port); |
vincentnys | 3:c53f27bdcfb5 | 69 | if (rc != 0) |
vincentnys | 3:c53f27bdcfb5 | 70 | printf("rc from TCP connect is %d\n", rc); |
vincentnys | 3:c53f27bdcfb5 | 71 | |
vincentnys | 3:c53f27bdcfb5 | 72 | |
vincentnys | 3:c53f27bdcfb5 | 73 | |
vincentnys | 3:c53f27bdcfb5 | 74 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
vincentnys | 3:c53f27bdcfb5 | 75 | data.MQTTVersion = MQTT_VERSION; |
vincentnys | 3:c53f27bdcfb5 | 76 | data.clientID.cstring = "VincentController"; |
vincentnys | 3:c53f27bdcfb5 | 77 | data.username.cstring = "smartcity"; |
vincentnys | 3:c53f27bdcfb5 | 78 | data.password.cstring = "smartcity"; |
vincentnys | 3:c53f27bdcfb5 | 79 | if ((rc = client.connect(data)) != 0) |
vincentnys | 3:c53f27bdcfb5 | 80 | printf("rc from MQTT connect is %d\n", rc); |
vincentnys | 3:c53f27bdcfb5 | 81 | |
vincentnys | 3:c53f27bdcfb5 | 82 | MQTT::Message message; |
vincentnys | 3:c53f27bdcfb5 | 83 | message.qos = MQTT::QOS1; |
vincentnys | 3:c53f27bdcfb5 | 84 | printf("Start clicking\n"); |
vincentnys | 3:c53f27bdcfb5 | 85 | char buf[100]; |
vincentnys | 3:c53f27bdcfb5 | 86 | |
vincentnys | 3:c53f27bdcfb5 | 87 | while( multiclick_state < 4 ) |
vincentnys | 3:c53f27bdcfb5 | 88 | { |
vincentnys | 3:c53f27bdcfb5 | 89 | // Keep running |
vincentnys | 3:c53f27bdcfb5 | 90 | |
vincentnys | 3:c53f27bdcfb5 | 91 | |
vincentnys | 3:c53f27bdcfb5 | 92 | if(!button1_busy && multiclick_state > 0) |
vincentnys | 3:c53f27bdcfb5 | 93 | { |
vincentnys | 3:c53f27bdcfb5 | 94 | //printf("%d\n", multiclick_state); |
vincentnys | 3:c53f27bdcfb5 | 95 | if(multiclick_state == 1) |
vincentnys | 3:c53f27bdcfb5 | 96 | sprintf(buf, "like\n"); |
vincentnys | 3:c53f27bdcfb5 | 97 | else |
vincentnys | 3:c53f27bdcfb5 | 98 | //buf = "dislike\n"; |
vincentnys | 3:c53f27bdcfb5 | 99 | sprintf(buf, "dislike\n"); |
vincentnys | 3:c53f27bdcfb5 | 100 | multiclick_state = 0; |
vincentnys | 3:c53f27bdcfb5 | 101 | |
vincentnys | 3:c53f27bdcfb5 | 102 | printf("%s\n", buf); |
vincentnys | 3:c53f27bdcfb5 | 103 | message.payload = (void *)buf; |
vincentnys | 3:c53f27bdcfb5 | 104 | message.payloadlen = strlen(buf) + 1; |
vincentnys | 3:c53f27bdcfb5 | 105 | client.publish(topic, message); |
vincentnys | 3:c53f27bdcfb5 | 106 | } |
vincentnys | 3:c53f27bdcfb5 | 107 | |
vincentnys | 3:c53f27bdcfb5 | 108 | |
vincentnys | 3:c53f27bdcfb5 | 109 | //printf("%d\n", multiclick_state); |
vincentnys | 3:c53f27bdcfb5 | 110 | } |
vincentnys | 3:c53f27bdcfb5 | 111 | |
vincentnys | 3:c53f27bdcfb5 | 112 | |
vincentnys | 3:c53f27bdcfb5 | 113 | |
vincentnys | 3:c53f27bdcfb5 | 114 | if ((rc = client.disconnect()) != 0) |
vincentnys | 3:c53f27bdcfb5 | 115 | printf("rc from disconnect was %d\n", rc); |
vincentnys | 3:c53f27bdcfb5 | 116 | mqtt.disconnect(); |
vincentnys | 3:c53f27bdcfb5 | 117 | |
vincentnys | 3:c53f27bdcfb5 | 118 | printf("Terminate...\n"); |
jensdehoog | 0:fd29cd85e75e | 119 | |
jensdehoog | 0:fd29cd85e75e | 120 | return 0; |
jensdehoog | 0:fd29cd85e75e | 121 | } |