Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of PGO6_VoteController_template by
Revision 6:60b968c8f35c, committed 2018-10-11
- Comitter:
- AstridVanneste
- Date:
- Thu Oct 11 09:51:37 2018 +0000
- Parent:
- 5:ba94770ce1c7
- Commit message:
- MQTT service
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r ba94770ce1c7 -r 60b968c8f35c main.cpp
--- a/main.cpp Mon Oct 08 15:39:22 2018 +0000
+++ b/main.cpp Thu Oct 11 09:51:37 2018 +0000
@@ -9,7 +9,9 @@
#include "MQTTmbed.h"
#include "MQTTClient.h"
-char* topic;
+char* topic = "test/embedded\0";
+
+void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* topic, char* payload, int qos, bool retained, bool duplicate);
/**
TODO
@@ -48,6 +50,7 @@
button.fall(callback(&button1_onpressed_cb));
init_debouncer();
+ printf("Trying to connect to ethernet network\n");
EthernetInterface network;
int error = network.connect();
if(error != 0)
@@ -56,6 +59,9 @@
}
const char* ip_add = network.get_ip_address();
+ printf("Success!\n");
+ printf("Trying to connect to MQTT network\n");
+
MQTTNetwork mqttNetwork(&network);
MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
@@ -65,20 +71,80 @@
{
printf("ERROR: mqtt.connect() = %d\n", error);
}
+ printf("Success\n");
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = MQTT_VERSION;
+ data.clientID.cstring = "test";
+ data.username.cstring = "username";
+ data.password.cstring = "password";
+
+ error = client.connect(data);
+ if(error != 0)
+ {
+ printf("ERROR: client.connect() = %d\n", error);
+ }
printf("INITIALIZED\n");
- while(true)
+ bool connected = true;
+
+ while(connected)
{
if(button1_pressed)
{
+ char* buf = "";
printf("Clicks: %d\n", multiclick_state);
button1_pressed = false;
+
+ switch(multiclick_state)
+ {
+ case 1:
+ buf = "upvote\n";
+ sendMessage(&client, topic, buf, 0, false, false);
+ printf("upvote\n");
+ break;
+ case 2:
+ buf = "downvote\n";
+ sendMessage(&client, topic, buf, 0, false, false);
+ printf("downvote\n");
+ break;
+ case 4:
+ mqttNetwork.disconnect();
+ connected = false;
+ }
}
}
-
+ printf("DISCONNECTED!\n");
return 0;
}
+
+void sendMessage(MQTT::Client<MQTTNetwork, Countdown>* client, char* top, char* payload, int qos, bool retained, bool duplicate)
+{
+ MQTT::Message message;
+
+ switch(qos)
+ {
+ case 0:
+ message.qos = MQTT::QOS0;
+ break;
+ case 1:
+ message.qos = MQTT::QOS1;
+ break;
+ case 2:
+ message.qos = MQTT::QOS2;
+ break;
+ default:
+ message.qos = MQTT::QOS0;
+ }
+ message.retained = retained;
+ message.dup = duplicate;
+ message.payload = (void*)payload;
+ message.payloadlen = strlen(payload) + 1;
+
+ int error = client->publish(top, message);
+ if(error != 0)
+ {
+ printf("ERROR: client.publish() = %d\n", error);
+ }
+}
