Wouter Van den Bogaert
/
PGO6_VoteController_template
PGO6
main.cpp
- Committer:
- s0130594
- Date:
- 2019-11-14
- Revision:
- 6:754d3e8f9ae9
- Parent:
- 4:15e7cac255da
File content as of revision 6:754d3e8f9ae9:
#define APP_VERSION 0.6f #define MQTT_VERSION 3 #define BROKER_NAME "broker.hivemq.com" #define BROKER_PORT 1883 #include "debounce_button.h" #include "EthernetInterface.h" #include "MQTTNetwork.h" #include "MQTTmbed.h" #include "MQTTClient.h" #include "mbed.h" #include "easy-connect/easy-connect.h" char* topic; Serial pc(USBTX, USBRX); /** hier copy */ volatile bool button1_pressed = false; // Used in the main loop volatile bool button1_enabled = true; // Used for debouncing volatile int multiclick_state =0 ; // Counts how many clicks occured in the time slot, used in main loop volatile bool button1_busy = false; // Informs the mainloop that the user is clicking the button volatile int button_count =0; // counter for number of clicks within 1 second volatile bool disp = false; // show eindtotaal 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 DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); Timeout someTimeout; Timeout someTimeout2; int arrivedcount = 0; /** end copy */ #include "debounce_button.h" /** Some tips and tricks: - To use the built-in LED: DigitalOut led1(LED1); ... led1 = 1; - To delay the call of a function: Timeout someTimeout; ... someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds - The variables that are used in interrupt callbacks have to be volatile, because these variables can change at any time. Therefore, the compiler is not 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. */ void button1_multiclick_reset_cb(void) { led3=1; //pc.printf("Time to reset alles"); button_count = multiclick_state; multiclick_state =0; disp=true; button1_busy = false; button1_pressed = false; DigitalOut led1(LED1); led1 = 0; } /** TODO ---- This function enables the button again, such that unwanted clicks of the bouncing button get ignored. */ void button1_enabled_cb(void) { button1_enabled = true; led2=0; } /** 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) { // Turn led on led1 = 1; led2= 0; // Set button disabled if(button1_enabled){ multiclick_state++; button1_enabled = false; led2=1; //pc.printf("Nu is de button1_enabled false"); //pc.printf("Multiclickstate is nu: %d", multiclick_state); someTimeout.attach(callback(button1_enabled_cb), 0.2); } // Inform main loop that user is clicking the button button1_busy = true; // Do a count after 1 second if(!button1_pressed){ // Inform main loop that button has been pressed //pc.printf("Registering callback for within 1 second"); button1_pressed = true; led3=0; someTimeout2.attach(callback(button1_multiclick_reset_cb), 1); } } /** 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. - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times, the program disconnects from the broker and terminates. Extra ----- - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal. - 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) */ int main(int argc, char* argv[]) { led3=1; InterruptIn button(USER_BUTTON); button.fall(callback(&button1_onpressed_cb)); pc.printf("Print print \n"); topic = "IGNISVOTESPOSTINGBETA"; NetworkInterface* network = easy_connect(true); if (!network) { pc.printf("Je netwerk is kapot"); return -1; } MQTTNetwork mqttNetwork(network); MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); const char* hostname = BROKER_NAME; int port = BROKER_PORT; pc.printf("Connecting to %s:%d\r\n", hostname, port); int rc = mqttNetwork.connect(hostname, port); if (rc != 0) pc.printf("rc from TCP connect is %d\r\n", rc); pc.printf("result van connecten %d\r\n", rc); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; data.clientID.cstring = "mbed-sample"; data.username.cstring = "testuser"; data.password.cstring = "testpassword"; if ((rc = client.connect(data)) != 0) pc.printf("rc from MQTT connect is %d\r\n", rc); pc.printf("rc from MQTT connect is %d\r\n", rc); while(true){ if(disp){ // Toon eindtotaal pc.printf("Er is zoveel keer geklikt: %d \n",button_count); disp=false; // QoS 2 MQTT::Message message; char buf[100]; sprintf(buf, "Er is %d keer geklikt",button_count); message.qos = MQTT::QOS1; message.retained = false; message.dup = false; message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; pc.printf("ik ga nu publishen"); rc = client.publish(topic, message); } } pc.printf("result van publishen %d\r\n", rc); if ((rc = client.unsubscribe(topic)) != 0) pc.printf("rc from unsubscribe was %d\r\n", rc); if ((rc = client.disconnect()) != 0) pc.printf("rc from disconnect was %d\r\n", rc); pc.printf("Version %.2f: finish %d msgs\r\n", 1111, arrivedcount); mqttNetwork.disconnect(); return 0; }