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.
Dependencies: easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code
Revision 3:1bc26dfb6d35, committed 2017-11-28
- Comitter:
- DuyLionTran
- Date:
- Tue Nov 28 07:39:05 2017 +0000
- Parent:
- 2:61d34bc22590
- Child:
- 4:d533fe8902b2
- Commit message:
- IBM bluemix version
Changed in this revision
--- a/MQTT.lib Sat Nov 25 14:25:44 2017 +0000 +++ b/MQTT.lib Tue Nov 28 07:39:05 2017 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/teams/mqtt/code/MQTT/#9cff7b6bbd01 +https://mbed.org/teams/mqtt/code/MQTT/#36e76acf2166
--- a/easy-connect.lib Sat Nov 25 14:25:44 2017 +0000 +++ b/easy-connect.lib Tue Nov 28 07:39:05 2017 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/easy-connect/#cb933fb19cda0a733a64d6b71d271fb6bdaf9e6d +https://os.mbed.com/users/DuyLionTran/code/easy-connect-customized/#309ff512fdc5
--- a/main.cpp Sat Nov 25 14:25:44 2017 +0000
+++ b/main.cpp Tue Nov 28 07:39:05 2017 +0000
@@ -6,6 +6,7 @@
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"
+#include "string.h"
/***************************************************************
* Definitions
@@ -23,11 +24,16 @@
#define RELAY_1_PIN (D11)
#define RELAY_2_PIN (D12)
+#define ORG "a4nvkh"
+#define DEVICE_TYPE "Nucleo_8266"
+#define DEVICE_ID "PROEVN"
+#define TOKEN "PROEVN2017"
+
/***************************************************************
* Variables
***************************************************************/
-float version = 0.6;
-char* topic = "command";
+float firmwareVersion = 0.85;
+//char* topic = "command";
char* event = "event";
const char* hostname = "m11.cloudmqtt.com";
int port = 16416;
@@ -35,8 +41,16 @@
char* username = "gbmswwpl";
char* password = "jZf8eSGw39X4";
+char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
+char topic[] = "iot-2/evt/status/fmt/json";
+char authMethod[] = "use-token-auth";
+char token[] = TOKEN;
+char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
+int ibmPort = 1883;
+
int arrivedcount = 0;
uint32_t lastRead = 0;
+uint32_t timer_last_read = 0;
uint8_t readVal1 = 0, readVal2 = 7;;
@@ -50,7 +64,6 @@
NetworkInterface* network = easy_connect(true);
MQTTNetwork mqttNetwork(network);
MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
-MQTT::Client<MQTTNetwork, Countdown> client_1(mqttNetwork);
/***************************************************************
* Callbacks
@@ -64,68 +77,69 @@
/***************************************************************
* Unity function definitions
***************************************************************/
-void publish(uint8_t uploadType);
+int MQTT_publish(char *sendMessage);
void clientYield();
/***************************************************************
* Main
***************************************************************/
-int main(int argc, char* argv[]) {
+void main(int argc, char* argv[]) {
serial.baud(115200);
- logMessage("IoT Water Monitor: version is %.2f\r\n", version);
-
+ logMessage("IoT Water Monitor project, firmware version is %.2f\r\n", firmwareVersion);
if (!network) {
logMessage("Failed to connect to the internet\r\n");
return ;
}
-
- logMessage("Connecting to %s:%d\r\n", hostname, port);
- int rc = mqttNetwork.connect(hostname, port);
+ logMessage("Connecting to %s:%d\r\n", server, ibmPort);
+ int rc = mqttNetwork.connect(server, ibmPort);
if (rc != 0)
logMessage("rc from TCP connect is %d\r\n", rc);
-
+
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
- data.clientID.cstring = clientID;
- data.username.cstring = username;
- data.password.cstring = password;
+ data.clientID.cstring = clientId;
+ data.username.cstring = authMethod;
+ data.password.cstring = token;
+
if ((rc = client.connect(data)) != 0)
logMessage("rc from MQTT connect is %d\r\n", rc);
-
- if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
- logMessage("rc from MQTT subscribe is %d\r\n", rc);
- if ((rc = client_1.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
- logMessage("rc from MQTT subscribe is %d\r\n", rc);
- if ((rc = client.subscribe(event, MQTT::QOS0, messageArrived)) != 0)
- logMessage("rc from MQTT subscribe is %d\r\n", rc);
-
- MQTT::Message message;
+
+ char* payload = "{\"d\":{\"Name\":\"PROEVN\",\"Firmware Version\":\"0.86\"}}";
- char buf[100];
- sprintf(buf, "Hello from IoT Water Monitor! QoS 0 message from app version %f\r\n", version);
- message.qos = MQTT::QOS0;
- message.retained = false;
- message.dup = false;
- message.payload = (void*)buf;
- message.payloadlen = strlen(buf)+1;
- rc = client.publish(event, message);
+ printf("Sending payload: %s\r\n", payload);
+ if (!MQTT_publish(payload)) {
+ printf("Publish ok\r\n");
+ }
+ else {
+ printf("Publish failed\r\n");
+ }
- logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
+// if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
+// logMessage("rc from MQTT subscribe is %d\r\n", rc);
+
+
readTime.start();
- thread.start(callback(clientYield));
+
+
+// thread.start(callback(clientYield));
+// timer.attach(&publish, 2);
+
while(true) {
- if ((uint32_t)(readTime.read_ms() - lastRead) > 5000) {
-
- publish(1);
- lastRead = readTime.read_ms();
- }
+
+// if ((uint32_t)(readTime.read() - lastRead) > 3) {
+// printf("MQTT publish\r\n");
+// int ret = MQTT_publish("Hello from STM32_pew_pew\r\n");
+// printf("Public result %d\r\n", ret);
+// lastRead = readTime.read();
+// }
// client.yield(100);
+
}
}
@@ -133,19 +147,16 @@
/***************************************************************
* Unity function declarations
***************************************************************/
-void publish(uint8_t uploadType) {
- readVal1++;
- readVal2 += 8;
- char buf[100];
+int MQTT_publish(char *sendMessage) {
+
MQTT::Message msg;
- sprintf(buf, "{\"Analog Value 1\":\"%d\", \"Analog Value 2\": \"%d\"}", readVal1, readVal2);
msg.qos = MQTT::QOS0;
msg.retained = false;
msg.dup = false;
- msg.payload = (void*)buf;
- msg.payloadlen = strlen(buf)+1;
- int ret = client.publish(event, msg);
+ msg.payload = sendMessage;
+ msg.payloadlen = strlen(sendMessage) + 1;
+
+ return client.publish(topic, msg);
- logMessage("Publish result: %d\r\n", ret);
}