![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
opdracht mqtt nucleo
Fork of PGO6_VoteController_template by
Revision 3:abfa79c5b7bd, committed 2018-10-11
- Comitter:
- alex69
- Date:
- Thu Oct 11 15:52:51 2018 +0000
- Parent:
- 2:5b7d055dbc91
- Commit message:
- Initial commit
Changed in this revision
diff -r 5b7d055dbc91 -r abfa79c5b7bd debounce_button.cpp --- a/debounce_button.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.cpp Thu Oct 11 15:52:51 2018 +0000 @@ -15,17 +15,26 @@ going to make optimisations. */ -/** - TODO - ---- - This function: - - stores the amount of clicks in a variable which is read by the main loop. - - resets the click counter which is used inside this file. - - lowers a flag which tells the main loop that the user stopped pressing the button - such that it can proceed its program. - - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. -*/ + +// init the extern variables +volatile int multiclick_state = 0; +volatile int tempCount = 0; +volatile bool button1_busy = false; +volatile bool button1_pressed = false; +volatile bool button1_enabled = true; +DigitalOut myled(LED1); +Timeout enableButtonTimer; +Timeout resetButtonTimer; + void button1_multiclick_reset_cb(void) { + //stores the amount of clicks in a variable which is read by the main loop. + multiclick_state = tempCount; + //resets the click counter which is used inside this file. + tempCount = 0; + //lowers a flag which tells the main loop that the user stopped pressing the button such that it can proceed its program. + button1_busy = false; + //turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. + myled = 0; } @@ -36,23 +45,28 @@ */ void button1_enabled_cb(void) { - + button1_enabled = true; } -/** - TODO - ---- - This function: - - turns the built-in LED on, so the user gets informed that the program has started with counting clicks - - disables the button such that the debouncer is active - - enables the button again after a certain amount of time - (use interrupts with "button1_enabled_cb()" as callback. - - counts the amount of clicks within a period of 1 second - - informs the main loop that the button has been pressed - - informs the main loop that the user is clicking the button. - Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. -*/ void button1_onpressed_cb(void) { - + if(button1_enabled) { + //disables the button such that the debouncer is active + button1_enabled = false; + //enables the button again after a certain amount of time (use interrupts with "button1_enabled_cb()" as callback.) + enableButtonTimer.attach(callback(&button1_enabled_cb), 0.1); + //informs the main loop that the button has been pressed + button1_pressed = true; + //informs the main loop that the user is clicking the button. + //Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. + if(!button1_busy) { + //turns the built-in LED on, so the user gets informed that the program has started with counting clicks + myled = 1; + button1_busy = true; + resetButtonTimer.attach(callback(&button1_multiclick_reset_cb), 1); + } + //counts the amount of clicks within a period of 1 second + tempCount++; + + } } \ No newline at end of file
diff -r 5b7d055dbc91 -r abfa79c5b7bd debounce_button.h --- a/debounce_button.h Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.h Thu Oct 11 15:52:51 2018 +0000 @@ -14,6 +14,7 @@ extern volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop extern volatile bool button1_busy; // Informs the mainloop that the user is clicking the button + void button1_multiclick_reset_cb(void); // Resets the amount of clicks, but stores this value for the usage in the main loop void button1_enabled_cb(void); // Enables the button again after a timeout, used for debouncing the button void button1_onpressed_cb(void); // Callback which is called when the user presses the button \ No newline at end of file
diff -r 5b7d055dbc91 -r abfa79c5b7bd main.cpp --- a/main.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/main.cpp Thu Oct 11 15:52:51 2018 +0000 @@ -14,10 +14,8 @@ /** TODO ---- - - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. + - Make an MQTT-service which: - - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. - - makes a client and connects it to the broker using a client ID and credentials (username & password). - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service you are going to use. Make a separate function which handles the sending procedure. Therefore, this function can be called each time we want to send a certain message. @@ -30,19 +28,88 @@ - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones. Tips & tricks - ------------- - - To generate an interrupt on the press of a button, use: - InterruptIn button(USER_BUTTON); - ... - button.fall(callback(someFunction)); - Before implementing MQTT, test the multiclick feature first. - Have a look at the MQTT-library for Mbed and the HelloMQTT-example. - To have a uniform message sending procedure, use the following function usage: sendMessage(&client, topic, buf, qos, retained, duplicate) */ - +InterruptIn button1(USER_BUTTON); +int sendMessage(MQTT::Client<MQTTNetwork, Countdown> client, char* msg, char* topic) { + MQTT::Message message; + // QoS 0 + char buf[100]; + sprintf(buf, msg); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + int rc = client.publish(topic, message); + // while (arrivedcount < 1) + // client.yield(100); + return rc; +} int main(int argc, char* argv[]) { - + // enables the interrupt on the button + button1.fall(callback(button1_onpressed_cb)); + EthernetInterface eth; + // starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. + if (eth.connect() == 0) { + printf("Ip address: %s,\n", eth.get_ip_address()); + } else { + printf("Something went wrong with connecting the ethernet interface\n"); + return -1; + } + + MQTTNetwork mqttNetwork(ð); + MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); + printf("Connecting to %s on port %d", BROKER_NAME, BROKER_PORT); + // makes a client and connects it to the broker using a client ID and credentials (username & password). + int rc = mqttNetwork.connect(BROKER_NAME, BROKER_PORT); + if (rc != 0) { + printf("return code is %d", rc); + } + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "mbed-sample"; + data.username.cstring = "testusser"; + data.password.cstring = "testpassword"; + int rc2 = client.connect(data); + if ( rc2 != 0) + printf("rc from MQTT connect is %d\r\n", rc); + + while(1) { + // Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. + // printf("The button has been pressed %d times \n", multiclick_state); + + //makes sure there is no infinite loop in the switch case + if(multiclick_state != 0) { + switch(multiclick_state) { + case 1: + printf("Upvote has been sent\n"); + sendMessage(client, "Upvote", "alex"); + break; + case 2: + printf("Downvote has been sent\n"); + sendMessage(client, "Downvote", "alex"); + break; + case 3: + printf("No action\n"); + break; + case 4: + printf("Disconnecting...\n"); + sendMessage(client, "Disconnect", "alex"); + break; + default: + printf("No action\n"); + break; + } + multiclick_state = 0; + } + + } + return 0; }