
Vote Controller IOT
Fork of PGO6_VoteController_template by
Revision 3:b2c9de2f45c7, committed 2017-11-06
- Comitter:
- kjellkleyn
- Date:
- Mon Nov 06 14:18:21 2017 +0000
- Parent:
- 2:5b7d055dbc91
- Commit message:
- Vote Controller einde laatste les
Changed in this revision
diff -r 5b7d055dbc91 -r b2c9de2f45c7 debounce_button.cpp --- a/debounce_button.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.cpp Mon Nov 06 14:18:21 2017 +0000 @@ -1,5 +1,7 @@ #include "debounce_button.h" - +DigitalOut led1(LED1); +Timeout second; +Timeout debounce; /** Some tips and tricks: - To use the built-in LED: @@ -36,7 +38,7 @@ */ void button1_enabled_cb(void) { - + button1_enabled = true; } /** @@ -54,5 +56,26 @@ */ void button1_onpressed_cb(void) { + if(button1_enabled){ + button1_enabled = false; + debounce.attach(&button1_enabled_cb,0.08); + + if(countSecond == false){ + led1 = 1; + multiclick_state = 1; + countSecond = true; + } + else{ + multiclick_state++; + } + second.attach(&second_ended_cd,1); + + } +} +void second_ended_cd(void) +{ + led1 = 0; + countSecond = false; + readyClick =true; } \ No newline at end of file
diff -r 5b7d055dbc91 -r b2c9de2f45c7 debounce_button.h --- a/debounce_button.h Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.h Mon Nov 06 14:18:21 2017 +0000 @@ -13,7 +13,10 @@ 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 bool button1_busy; // Informs the mainloop that the user is clicking the button +extern volatile bool countSecond; +extern volatile bool readyClick; 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 +void button1_onpressed_cb(void); // Callback which is called when the user presses the button +void second_ended_cd(void); // Callback when the second is over since first clicck \ No newline at end of file
diff -r 5b7d055dbc91 -r b2c9de2f45c7 main.cpp --- a/main.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/main.cpp Mon Nov 06 14:18:21 2017 +0000 @@ -9,7 +9,18 @@ #include "MQTTmbed.h" #include "MQTTClient.h" -char* topic; +using namespace MQTT; +char* topic = "dansvloerVincent/votes"; +char buf[100]; + + +volatile bool button1_pressed; // Used in the main loop +volatile bool button1_enabled = true; // Used for debouncing +volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop +volatile bool button1_busy; // Informs the mainloop that the user is clicking the button +volatile bool countSecond = false; +volatile bool readyClick = false; +int arrivedcount = 0; /** TODO @@ -41,8 +52,78 @@ sendMessage(&client, topic, buf, qos, retained, duplicate) */ +void messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); + printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); + ++arrivedcount; +} + int main(int argc, char* argv[]) { + printf("Starting program \r\nConnecting...\r\n"); + EthernetInterface eInterface; + //eInterface.set_network("192.168.0.95","255.255.255.0","192.168.0.1"); + eInterface.connect(); + + + //const char *ip = eInterface.get_ip_address(); + printf("IP address is: %s\r\n", eInterface.get_ip_address()); + + //________________________________________________________________ + NetworkInterface *network = &eInterface; + if (!network) { + return -1; + } + + MQTTNetwork mqttNetwork(network); + + MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); + + const char* hostname = "143.129.39.151"; + //const char* hostname = "broker.mqttdashboard.com"; + int port = 1883; + printf("Connecting to %s:%d\r\n", hostname, port); + int rc = mqttNetwork.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 = "kjell"; + data.username.cstring = "smartcity"; + data.password.cstring = "smartcity"; + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\r\n", rc); + + MQTT::Message message; + message.qos = MQTT::QOS1; + InterruptIn button(USER_BUTTON); + button.fall(button1_onpressed_cb); + while(multiclick_state < 4){ + if(readyClick){ + printf("TimesClicked %d \r\n" , multiclick_state); + + if(multiclick_state == 1){ + sprintf(buf,"likeKjell\n"); + }else if(multiclick_state == 2 || multiclick_state == 3){ + sprintf(buf,"dislikeKjell\n"); + } + printf("buf : %s\r\n" ,buf); + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + rc = client.publish(topic, message); + + readyClick = false; + } + } + + if ((rc = client.disconnect()) != 0) + printf("rc from disconnect was %d\r\n", rc); + + mqttNetwork.disconnect(); + printf("exiting program...\r\n"); return 0; }