Duy tran / Mbed OS iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Files at this revision

API Documentation at this revision

Comitter:
DuyLionTran
Date:
Tue Nov 28 13:47:02 2017 +0000
Parent:
4:d533fe8902b2
Child:
6:3efc470a9f35
Commit message:
IBM uploaded

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Nov 28 07:39:51 2017 +0000
+++ b/main.cpp	Tue Nov 28 13:47:02 2017 +0000
@@ -32,17 +32,11 @@
 /***************************************************************
  * Variables
  ***************************************************************/
-float firmwareVersion = 0.85;
-//char* topic = "command";
-char* event = "event";
-const char* hostname = "m11.cloudmqtt.com";
-int port = 16416;
-char* clientID = "Duy";
-char* username = "gbmswwpl";
-char* password = "jZf8eSGw39X4";
-
+float firmwareVersion = 0.86; 
+ 
 char server[]     = ORG ".messaging.internetofthings.ibmcloud.com";
-char topic[]      = "iot-2/evt/status/fmt/json";
+char topicCMD[]   = "iot-2/cmd/test/fmt/json";
+char topicEvent[] = "iot-2/evt/status/fmt/json";
 char authMethod[] = "use-token-auth";
 char token[]      = TOKEN;
 char clientId[]   = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
@@ -107,38 +101,48 @@
     
     if ((rc = client.connect(data)) != 0)
         logMessage("rc from MQTT connect is %d\r\n", rc);
-    
-    char* payload = "{\"d\":{\"Name\":\"PROEVN\",\"Firmware Version\":\"0.86\"}}";    
+    if ((rc = client.subscribe(topicCMD, MQTT::QOS0, messageArrived)) != 0)
+        logMessage("rc from MQTT subscribe is %d\r\n", rc);
     
-    printf("Sending payload: %s\r\n", payload);
-    if (!MQTT_publish(payload)) {
+//    char* payload = "{\"d\":{\"Name\":\"PROEVN\",\"Firmware Version\":\"0.86\"}}";    
+    char greetingMessage[100];
+    sprintf(greetingMessage, "{\"d\":{\"Device ID\":\"PROEVN\",\"Firmware Version\":\"%f\"}}", firmwareVersion);
+    
+    printf("Sending payload: %s\r\n", greetingMessage);
+    if (!MQTT_publish((char *)greetingMessage)) {
         printf("Publish ok\r\n");
     }    
     else {
         printf("Publish failed\r\n");
     }
 
-//    if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
-//        logMessage("rc from MQTT subscribe is %d\r\n", rc);
-
-
     readTime.start();
     
-    
+    wait(2);
     
 //    thread.start(callback(clientYield));
 //    timer.attach(&publish, 2);
-
+    uint8_t counter = 0;
+    uint16_t cmdID  = 0;
         
     while(true) {  
         
-//        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);
+        if ((uint32_t)(readTime.read() - lastRead) > 10) {
+            counter++;
+            char payload[100];
+            sprintf(payload, "{\"d\":{\"Type\":\"1\",\"Command ID\":\"%d\",\"ADC0\":\"%d\",\"ADC1\":\"%d\"}}", cmdID, counter, counter++);
+            printf("MQTT publish %s\r\n", payload);
+            if (!MQTT_publish((char *)payload)) {
+                printf("Publish ok\r\n");
+                cmdID++;
+            }    
+            else {
+                printf("Publish failed\r\n");
+            }
+            
+            lastRead = readTime.read();
+        }
+        client.yield(100);
     
     }
         
@@ -156,7 +160,7 @@
     msg.payload = sendMessage;
     msg.payloadlen = strlen(sendMessage) + 1;
 
-    return client.publish(topic, msg);
+    return client.publish(topicEvent, msg);
            
 }