vote controller implementatie van Jonas De Rynck
Fork of PGO6_VoteController_template by
main.cpp@2:5f2104244c27, 2017-10-30 (annotated)
- Committer:
- jonasdr
- Date:
- Mon Oct 30 16:43:58 2017 +0000
- Revision:
- 2:5f2104244c27
- Parent:
- 1:34e76c0cbe5a
Jonas De Rynck - embedded vote controller
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 |
jonasdr | 2:5f2104244c27 | 3 | #define BROKER_NAME "143.129.39.151" |
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 | /** |
jensdehoog | 0:fd29cd85e75e | 13 | TODO |
jensdehoog | 0:fd29cd85e75e | 14 | ---- |
jensdehoog | 0:fd29cd85e75e | 15 | - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. |
jensdehoog | 0:fd29cd85e75e | 16 | - Make an MQTT-service which: |
jensdehoog | 0:fd29cd85e75e | 17 | - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. |
jensdehoog | 0:fd29cd85e75e | 18 | - makes a client and connects it to the broker using a client ID and credentials (username & password). |
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 | ------------- |
jensdehoog | 0:fd29cd85e75e | 32 | - To generate an interrupt on the press of a button, use: |
jensdehoog | 0:fd29cd85e75e | 33 | InterruptIn button(USER_BUTTON); |
jensdehoog | 0:fd29cd85e75e | 34 | ... |
jonasdr | 2:5f2104244c27 | 35 | button.fall(callback(&someFunction)); |
jensdehoog | 1:34e76c0cbe5a | 36 | The function someFunction(void) is called when this interrupt occurs. |
jensdehoog | 0:fd29cd85e75e | 37 | - Before implementing MQTT, test the multiclick feature first. |
jensdehoog | 1:34e76c0cbe5a | 38 | You can simply use 'printf()' to print to a serial terminal. The baud rate is 9600. |
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 | |
jonasdr | 2:5f2104244c27 | 44 | InterruptIn button(USER_BUTTON); |
jonasdr | 2:5f2104244c27 | 45 | bool flag = false; //set on interrupt |
jonasdr | 2:5f2104244c27 | 46 | DigitalOut connected_led(LED3); |
jonasdr | 2:5f2104244c27 | 47 | char* topic = "clubIOT/feedback"; //publish topic |
jonasdr | 2:5f2104244c27 | 48 | char* songTopic="clubIOT/songmeta"; //subscribe topic |
jonasdr | 2:5f2104244c27 | 49 | |
jonasdr | 2:5f2104244c27 | 50 | |
jonasdr | 2:5f2104244c27 | 51 | void buttonpress() |
jonasdr | 2:5f2104244c27 | 52 | { |
jonasdr | 2:5f2104244c27 | 53 | flag = true; |
jonasdr | 2:5f2104244c27 | 54 | } |
jonasdr | 2:5f2104244c27 | 55 | |
jonasdr | 2:5f2104244c27 | 56 | void sendMessage(MQTT::Client<MQTTNetwork, Countdown> *client, int qos, char* mqtt_message) |
jonasdr | 2:5f2104244c27 | 57 | { |
jonasdr | 2:5f2104244c27 | 58 | MQTT::Message message; |
jonasdr | 2:5f2104244c27 | 59 | char buf[100]; |
jonasdr | 2:5f2104244c27 | 60 | sprintf(buf, mqtt_message); |
jonasdr | 2:5f2104244c27 | 61 | if(qos==0) |
jonasdr | 2:5f2104244c27 | 62 | message.qos = MQTT::QOS0; |
jonasdr | 2:5f2104244c27 | 63 | else if(qos==1) |
jonasdr | 2:5f2104244c27 | 64 | message.qos = MQTT::QOS1; |
jonasdr | 2:5f2104244c27 | 65 | else |
jonasdr | 2:5f2104244c27 | 66 | message.qos = MQTT::QOS2; |
jonasdr | 2:5f2104244c27 | 67 | message.retained = false; |
jonasdr | 2:5f2104244c27 | 68 | message.dup = false; |
jonasdr | 2:5f2104244c27 | 69 | message.payload = (void*)buf; |
jonasdr | 2:5f2104244c27 | 70 | message.payloadlen = strlen(buf)+1; |
jonasdr | 2:5f2104244c27 | 71 | client->publish(topic, message); |
jonasdr | 2:5f2104244c27 | 72 | } |
jonasdr | 2:5f2104244c27 | 73 | |
jonasdr | 2:5f2104244c27 | 74 | void newSong(MQTT::MessageData &data) |
jonasdr | 2:5f2104244c27 | 75 | { |
jonasdr | 2:5f2104244c27 | 76 | MQTT::Message &message = data.message; |
jonasdr | 2:5f2104244c27 | 77 | printf("new song: %s \n", (char*)message.payload); |
jonasdr | 2:5f2104244c27 | 78 | } |
jonasdr | 2:5f2104244c27 | 79 | |
jonasdr | 2:5f2104244c27 | 80 | |
jensdehoog | 0:fd29cd85e75e | 81 | int main(int argc, char* argv[]) |
jensdehoog | 0:fd29cd85e75e | 82 | { |
jonasdr | 2:5f2104244c27 | 83 | EthernetInterface eth_interface; //maak ethernet interface |
jonasdr | 2:5f2104244c27 | 84 | MQTTNetwork mqttNetwork(ð_interface); //maak mqtt network |
jonasdr | 2:5f2104244c27 | 85 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); //maak een client |
jonasdr | 2:5f2104244c27 | 86 | eth_interface.connect(); // connect de ethernet interface |
jonasdr | 2:5f2104244c27 | 87 | int rc = mqttNetwork.connect(BROKER_NAME, BROKER_PORT); // connect to the MQTTbroker |
jonasdr | 2:5f2104244c27 | 88 | if(rc!=0) |
jonasdr | 2:5f2104244c27 | 89 | printf("'tis kapot %d \n", rc); |
jonasdr | 2:5f2104244c27 | 90 | client.subscribe(songTopic, MQTT::QOS1, newSong); //subscribe op de songmeta |
jonasdr | 2:5f2104244c27 | 91 | |
jonasdr | 2:5f2104244c27 | 92 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
jonasdr | 2:5f2104244c27 | 93 | data.MQTTVersion = 3; |
jonasdr | 2:5f2104244c27 | 94 | data.clientID.cstring = "jonas"; |
jonasdr | 2:5f2104244c27 | 95 | data.username.cstring = "smartcity"; |
jonasdr | 2:5f2104244c27 | 96 | data.password.cstring = "smartcity"; |
jonasdr | 2:5f2104244c27 | 97 | client.connect(data); |
jonasdr | 2:5f2104244c27 | 98 | |
jonasdr | 2:5f2104244c27 | 99 | char* mqtt_message = ""; |
jonasdr | 2:5f2104244c27 | 100 | |
jonasdr | 2:5f2104244c27 | 101 | button.fall(callback(&buttonpress )); |
jonasdr | 2:5f2104244c27 | 102 | while(1) |
jonasdr | 2:5f2104244c27 | 103 | { |
jonasdr | 2:5f2104244c27 | 104 | if(flag) |
jonasdr | 2:5f2104244c27 | 105 | { |
jonasdr | 2:5f2104244c27 | 106 | button1_onpressed_cb(); //roep debouncer op |
jonasdr | 2:5f2104244c27 | 107 | flag = false; //reset interrupt flag |
jonasdr | 2:5f2104244c27 | 108 | } |
jonasdr | 2:5f2104244c27 | 109 | |
jonasdr | 2:5f2104244c27 | 110 | if(!button1_busy && isready) |
jonasdr | 2:5f2104244c27 | 111 | { |
jonasdr | 2:5f2104244c27 | 112 | if (multiclick_state_mem == 1) |
jonasdr | 2:5f2104244c27 | 113 | { |
jonasdr | 2:5f2104244c27 | 114 | mqtt_message = "like"; |
jonasdr | 2:5f2104244c27 | 115 | sendMessage(&client, 0, mqtt_message); |
jonasdr | 2:5f2104244c27 | 116 | } |
jonasdr | 2:5f2104244c27 | 117 | else if(multiclick_state_mem == 2) |
jonasdr | 2:5f2104244c27 | 118 | { |
jonasdr | 2:5f2104244c27 | 119 | mqtt_message = "dislike"; |
jonasdr | 2:5f2104244c27 | 120 | sendMessage(&client, 0, mqtt_message); |
jonasdr | 2:5f2104244c27 | 121 | } |
jonasdr | 2:5f2104244c27 | 122 | else if(multiclick_state_mem == 4) |
jonasdr | 2:5f2104244c27 | 123 | { |
jonasdr | 2:5f2104244c27 | 124 | client.unsubscribe(topic); |
jonasdr | 2:5f2104244c27 | 125 | client.disconnect(); |
jonasdr | 2:5f2104244c27 | 126 | eth_interface.disconnect(); |
jonasdr | 2:5f2104244c27 | 127 | } |
jonasdr | 2:5f2104244c27 | 128 | isready = false; |
jonasdr | 2:5f2104244c27 | 129 | } |
jonasdr | 2:5f2104244c27 | 130 | } |
jensdehoog | 0:fd29cd85e75e | 131 | return 0; |
jensdehoog | 0:fd29cd85e75e | 132 | } |
jonasdr | 2:5f2104244c27 | 133 |