Ruben De Swaef
/
PGO6_VoteController_code
Votecontroller
Fork of PGO6_VoteController_template by
Diff: main.cpp
- Revision:
- 3:793425387c91
- Parent:
- 2:5b7d055dbc91
- Child:
- 4:bb892177ce23
diff -r 5b7d055dbc91 -r 793425387c91 main.cpp --- a/main.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/main.cpp Mon Oct 15 14:30:31 2018 +0000 @@ -9,7 +9,14 @@ #include "MQTTmbed.h" #include "MQTTClient.h" -char* topic; +char* topic = "embedded/controller"; +bool connected = false; + +DigitalOut led2(LED2); //led if connected +InterruptIn button(USER_BUTTON); +EthernetInterface eth; +MQTTNetwork network(ð); +MQTT::Client<MQTTNetwork, Countdown> client(network); /** TODO @@ -41,8 +48,60 @@ sendMessage(&client, topic, buf, qos, retained, duplicate) */ +void MQQTConnect(){ + eth.connect(); + while(eth.get_ip_address()== NULL){ + } + + network.connect(BROKER_NAME, BROKER_PORT); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + + data.MQTTVersion = MQTT_VERSION; + data.clientID.cstring = "client-ruben"; + data.username.cstring = "user"; + data.password.cstring = "pass"; + client.connect(data); + connected = true; + led2 = 1; +} + +void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* topic, char* buf, int qos, bool retained, bool duplicate){ + MQTT::Message message; + message.qos = MQTT::QOS0; + message.retained = retained; + message.dup = duplicate; + message.payload = (void*)buf; + message.payloadlen = strlen(buf) + 1; + + client->publish(topic, message); +} + int main(int argc, char* argv[]) { - + printf("boot"); + button.fall(callback(&button1_onpressed_cb)); + MQQTConnect(); + while(connected){ + if(!button1_busy && multiclick_state != 0){ + char* buf = ""; + switch(multiclick_state) + { + case 1: + buf = "upvote"; + sendMessage(&client, topic, buf, 0, false, false); + break; + case 2: + buf = "downvote"; + sendMessage(&client, topic, buf, 0, false, false); + break; + case 4: + client.disconnect(); + network.disconnect(); + connected = false; + led2 = 0; + } + printf("\n click %d: %s", multiclick_state, buf); + } + } return 0; }