Wouter Van den Bogaert
/
PGO6_VoteController_template
PGO6
main.cpp@3:14b20a4f36d0, 2019-10-28 (annotated)
- Committer:
- s0130594
- Date:
- Mon Oct 28 16:35:27 2019 +0000
- Revision:
- 3:14b20a4f36d0
- Parent:
- 2:5b7d055dbc91
- Child:
- 4:15e7cac255da
eerste 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" |
s0130594 | 3:14b20a4f36d0 | 11 | #include "mbed.h" |
jensdehoog | 0:fd29cd85e75e | 12 | |
jensdehoog | 0:fd29cd85e75e | 13 | char* topic; |
s0130594 | 3:14b20a4f36d0 | 14 | Serial pc(USBTX, USBRX); |
jensdehoog | 0:fd29cd85e75e | 15 | /** |
jensdehoog | 0:fd29cd85e75e | 16 | TODO |
jensdehoog | 0:fd29cd85e75e | 17 | ---- |
jensdehoog | 0:fd29cd85e75e | 18 | - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. |
jensdehoog | 0:fd29cd85e75e | 19 | - Make an MQTT-service which: |
jensdehoog | 0:fd29cd85e75e | 20 | - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. |
jensdehoog | 0:fd29cd85e75e | 21 | - makes a client and connects it to the broker using a client ID and credentials (username & password). |
jensdehoog | 0:fd29cd85e75e | 22 | - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service |
jensdehoog | 0:fd29cd85e75e | 23 | you are going to use. Make a separate function which handles the sending procedure. Therefore, this function |
jensdehoog | 0:fd29cd85e75e | 24 | can be called each time we want to send a certain message. |
jensdehoog | 0:fd29cd85e75e | 25 | - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times, |
jensdehoog | 0:fd29cd85e75e | 26 | the program disconnects from the broker and terminates. |
jensdehoog | 0:fd29cd85e75e | 27 | |
jensdehoog | 0:fd29cd85e75e | 28 | Extra |
jensdehoog | 0:fd29cd85e75e | 29 | ----- |
jensdehoog | 0:fd29cd85e75e | 30 | - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal. |
jensdehoog | 0:fd29cd85e75e | 31 | - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones. |
jensdehoog | 0:fd29cd85e75e | 32 | |
jensdehoog | 0:fd29cd85e75e | 33 | Tips & tricks |
jensdehoog | 0:fd29cd85e75e | 34 | ------------- |
jensdehoog | 0:fd29cd85e75e | 35 | - To generate an interrupt on the press of a button, use: |
jensdehoog | 0:fd29cd85e75e | 36 | InterruptIn button(USER_BUTTON); |
jensdehoog | 0:fd29cd85e75e | 37 | ... |
jensdehoog | 0:fd29cd85e75e | 38 | button.fall(callback(someFunction)); |
jensdehoog | 0:fd29cd85e75e | 39 | - Before implementing MQTT, test the multiclick feature first. |
jensdehoog | 0:fd29cd85e75e | 40 | - Have a look at the MQTT-library for Mbed and the HelloMQTT-example. |
jensdehoog | 0:fd29cd85e75e | 41 | - To have a uniform message sending procedure, use the following function usage: |
jensdehoog | 0:fd29cd85e75e | 42 | sendMessage(&client, topic, buf, qos, retained, duplicate) |
jensdehoog | 0:fd29cd85e75e | 43 | */ |
jensdehoog | 0:fd29cd85e75e | 44 | |
jensdehoog | 0:fd29cd85e75e | 45 | int main(int argc, char* argv[]) |
jensdehoog | 0:fd29cd85e75e | 46 | { |
s0130594 | 3:14b20a4f36d0 | 47 | |
s0130594 | 3:14b20a4f36d0 | 48 | InterruptIn button(USER_BUTTON); |
s0130594 | 3:14b20a4f36d0 | 49 | button.fall(callback(button1_onpressed_cb)); |
s0130594 | 3:14b20a4f36d0 | 50 | multiclick_state = 0; |
s0130594 | 3:14b20a4f36d0 | 51 | pc.printf("Print print \n"); |
s0130594 | 3:14b20a4f36d0 | 52 | while(true){ |
s0130594 | 3:14b20a4f36d0 | 53 | |
s0130594 | 3:14b20a4f36d0 | 54 | if(!button1_busy && !button1_pressed){ |
s0130594 | 3:14b20a4f36d0 | 55 | // Toon eindtotaal |
s0130594 | 3:14b20a4f36d0 | 56 | pc.printf("Er is zoveel keer geklikt: %d",button_count); |
s0130594 | 3:14b20a4f36d0 | 57 | pc.printf("\n"); |
s0130594 | 3:14b20a4f36d0 | 58 | |
s0130594 | 3:14b20a4f36d0 | 59 | } |
s0130594 | 3:14b20a4f36d0 | 60 | } |
jensdehoog | 0:fd29cd85e75e | 61 | return 0; |
jensdehoog | 0:fd29cd85e75e | 62 | } |