vote controller implementatie van Jonas De Rynck
Fork of PGO6_VoteController_template by
Diff: main.cpp
- Revision:
- 2:5f2104244c27
- Parent:
- 1:34e76c0cbe5a
--- a/main.cpp Sun Oct 29 23:01:06 2017 +0000 +++ b/main.cpp Mon Oct 30 16:43:58 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 ---- @@ -34,7 +32,7 @@ - To generate an interrupt on the press of a button, use: InterruptIn button(USER_BUTTON); ... - button.fall(callback(someFunction)); + button.fall(callback(&someFunction)); The function someFunction(void) is called when this interrupt occurs. - Before implementing MQTT, test the multiclick feature first. You can simply use 'printf()' to print to a serial terminal. The baud rate is 9600. @@ -43,8 +41,93 @@ sendMessage(&client, topic, buf, qos, retained, duplicate) */ +InterruptIn button(USER_BUTTON); +bool flag = false; //set on interrupt +DigitalOut connected_led(LED3); +char* topic = "clubIOT/feedback"; //publish topic +char* songTopic="clubIOT/songmeta"; //subscribe topic + + +void buttonpress() +{ + flag = true; +} + +void sendMessage(MQTT::Client<MQTTNetwork, Countdown> *client, int qos, char* mqtt_message) +{ + MQTT::Message message; + char buf[100]; + sprintf(buf, mqtt_message); + if(qos==0) + message.qos = MQTT::QOS0; + else if(qos==1) + message.qos = MQTT::QOS1; + else + message.qos = MQTT::QOS2; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client->publish(topic, message); +} + +void newSong(MQTT::MessageData &data) +{ + MQTT::Message &message = data.message; + printf("new song: %s \n", (char*)message.payload); +} + + int main(int argc, char* argv[]) { - + EthernetInterface eth_interface; //maak ethernet interface + MQTTNetwork mqttNetwork(ð_interface); //maak mqtt network + MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); //maak een client + eth_interface.connect(); // connect de ethernet interface + int rc = mqttNetwork.connect(BROKER_NAME, BROKER_PORT); // connect to the MQTTbroker + if(rc!=0) + printf("'tis kapot %d \n", rc); + client.subscribe(songTopic, MQTT::QOS1, newSong); //subscribe op de songmeta + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "jonas"; + data.username.cstring = "smartcity"; + data.password.cstring = "smartcity"; + client.connect(data); + + char* mqtt_message = ""; + + button.fall(callback(&buttonpress )); + while(1) + { + if(flag) + { + button1_onpressed_cb(); //roep debouncer op + flag = false; //reset interrupt flag + } + + if(!button1_busy && isready) + { + if (multiclick_state_mem == 1) + { + mqtt_message = "like"; + sendMessage(&client, 0, mqtt_message); + } + else if(multiclick_state_mem == 2) + { + mqtt_message = "dislike"; + sendMessage(&client, 0, mqtt_message); + } + else if(multiclick_state_mem == 4) + { + client.unsubscribe(topic); + client.disconnect(); + eth_interface.disconnect(); + } + isready = false; + } + } return 0; } +