Robrecht Daems
/
PGO6_VoteController_template
Robrecht Daems
Fork of PGO6_VoteController_template by
Diff: main.cpp
- Revision:
- 2:b47f54fdea91
- Parent:
- 1:34e76c0cbe5a
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; }