Robrecht Daems
/
PGO6_VoteController_template
Robrecht Daems
Fork of PGO6_VoteController_template by
Revision 2:b47f54fdea91, committed 2017-10-30
- Comitter:
- Robrecht
- Date:
- Mon Oct 30 16:44:56 2017 +0000
- Parent:
- 1:34e76c0cbe5a
- Commit message:
- initial commit
Changed in this revision
diff -r 34e76c0cbe5a -r b47f54fdea91 MQTT.lib --- a/MQTT.lib Sun Oct 29 23:01:06 2017 +0000 +++ b/MQTT.lib Mon Oct 30 16:44:56 2017 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/teams/mqtt/code/MQTT/#c37c8236e84a +https://os.mbed.com/users/Robrecht/code/MQTT/#7f492cd5bc5d
diff -r 34e76c0cbe5a -r b47f54fdea91 debounce_button.cpp --- a/debounce_button.cpp Sun Oct 29 23:01:06 2017 +0000 +++ b/debounce_button.cpp Mon Oct 30 16:44:56 2017 +0000 @@ -35,7 +35,48 @@ This function is called when the button has been pressed by the user. */ +Timeout timeout1; +Timeout timeout2; +DigitalOut busy_led(LED1); +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 int last_multiclick_state=0; // Counts how many clicks occured in the previous timeslot +volatile bool button1_busy = false; // Informs the mainloop that the user is clicking the button +volatile bool result_ready=false; + void button1_onpressed_cb(void) { - -} \ No newline at end of file + if(button1_enabled) + { + if(multiclick_state == 0) //first press this second + { + button1_busy=true; + busy_led = 1; + timeout1.attach(callback(&button1_multiclick_reset_cb), 1.0); + } + multiclick_state += 1; + button1_enabled=false; + timeout2.attach(callback(&button1_enabled_cb), 0.075); + } +} + +/** + Resets the amount of clicks, but stores this value for the usage in the main loop +*/ +void button1_multiclick_reset_cb() +{ + last_multiclick_state = multiclick_state; + multiclick_state = 0; + button1_busy=false; + result_ready=true; + busy_led=0; +} + +/** + Enables the button again after a timeout, used for debouncing the button +*/ +void button1_enabled_cb() +{ + button1_enabled=true; +} \ No newline at end of file
diff -r 34e76c0cbe5a -r b47f54fdea91 debounce_button.h --- a/debounce_button.h Sun Oct 29 23:01:06 2017 +0000 +++ b/debounce_button.h Mon Oct 30 16:44:56 2017 +0000 @@ -12,7 +12,9 @@ extern volatile bool button1_pressed; // Used in the main loop extern volatile bool button1_enabled; // Used for debouncing extern volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop +extern volatile int last_multiclick_state; // Counts how many clicks occured in the previous timeslot extern volatile bool button1_busy; // Informs the mainloop that the user is clicking the button +extern volatile bool result_ready; 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
diff -r 34e76c0cbe5a -r b47f54fdea91 main.cpp --- a/main.cpp Sun Oct 29 23:01:06 2017 +0000 +++ b/main.cpp Mon Oct 30 16:44:56 2017 +0000 @@ -1,6 +1,6 @@ #define APP_VERSION 0.6f #define MQTT_VERSION 3 -#define BROKER_NAME "broker.hivemq.com" +#define BROKER_NAME "143.129.39.151" #define BROKER_PORT 1883 #include "debounce_button.h" @@ -9,8 +9,6 @@ #include "MQTTmbed.h" #include "MQTTClient.h" -char* topic; - /** TODO ---- @@ -43,8 +41,116 @@ sendMessage(&client, topic, buf, qos, retained, duplicate) */ -int main(int argc, char* argv[]) +InterruptIn button(USER_BUTTON); +DigitalOut myled(LED1); +EthernetInterface eth_interface; +MQTTNetwork network(ð_interface); +MQTT::Client<MQTTNetwork, Countdown> client(network); +char* topic = "clubIOT/feedback"; +char* subTopic = "clubIOT/songmeta + +/** + called when the button generates an interrupt +*/ +void buttonPress() +{ + button1_pressed=true; +} + +/** + called to initialize MQTT +*/ +void initMQTT() +{ + eth_interface.connect(); + if(eth_interface.get_ip_address() == NULL) + printf("ip adres kaput \r\n"); + else + printf("IP: %s\r\n", eth_interface.get_ip_address()); + const char* hostname = BROKER_NAME; + int port = BROKER_PORT; + int rc = network.connect(hostname, port); + if (rc != 0) + printf("rc from TCP connect is %d\r\n", rc); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "robrecht"; + data.username.cstring = "smartcity"; + data.password.cstring = "smartcity"; + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\r\n", rc); + else + printf("connected\r\n"); +} + +/** + send a message over MQTT +*/ +void sendMessage(MQTT::Client<MQTTNetwork, Countdown> *client, char* tempTopic, char* buf ,int qos, bool retained, bool duplicate) { + MQTT::Message message; + if(qos==0) + message.qos= MQTT::QOS0; + else if(qos==1) + message.qos= MQTT::QOS1; + else + message.qos= MQTT::QOS2; + message.retained=retained; + message.dup=duplicate; + message.payload= (void*) buf; + message.payloadlen = strlen(buf)+1; + client->publish(tempTopic, message); +} +/** + disconnect from MQTT +*/ +void disconnect() +{ + char buf[100]; + sprintf(buf, "disconnect"); + sendMessage(&client, topic, buf, 0, false, false); + client.disconnect(); + eth_interface.disconnect(); +} + +/** + Called when the controller receives a song +*/ +void printMessage(MQTT::MessageData& data) +{ + printf("MQTT message: %s\r\n", data.message.payload); +} + +int main(int argc, char* argv[]) +{ + initMQTT(); + button.fall(callback(&buttonPress)); + client.subscribe(subTopic, MQTT::QOS1, printMessage); + + while(1) + { + if(button1_pressed){ + button1_pressed = false; + button1_onpressed_cb(); + } + if(!button1_busy && result_ready){ + result_ready=false; + printf("The button was pressed %d times in the last second \r\n", last_multiclick_state); + char buf[100]; + if(last_multiclick_state==1){ + sprintf(buf, "like"); + sendMessage(&client, topic, buf, 0, false, false); + } + else if(last_multiclick_state==2){ + sprintf(buf, "dislike"); + sendMessage(&client, topic, buf, 0, false, false); + } + else if(last_multiclick_state==4){ + disconnect(); + return 0; + } + } + } return 0; }