das

Dependencies:   HTS221 MQTT

Revision:
2:983b7f5dde1e
Parent:
1:ddef44f2db39
Child:
3:647e37ef0eec
diff -r ddef44f2db39 -r 983b7f5dde1e main.cpp
--- a/main.cpp	Sun Jul 29 10:15:49 2018 +0000
+++ b/main.cpp	Wed Aug 01 23:01:53 2018 +0000
@@ -10,16 +10,34 @@
 // Definitions ---------------------------------------------------------
 
 // Change it with your WiFi network name
+//#define WIFI_NETWORK_NAME       "farnell_iot_lab"
 #define WIFI_NETWORK_NAME       "rucola"
 // Change it with your WiFi password name
+//#define WIFI_NETWORK_PASSWORD   "smartlab"
 #define WIFI_NETWORK_PASSWORD   "Rosmarino_10"
 #define WIFI_SECURITY           NSAPI_SECURITY_WPA_WPA2
 
+#define COMM_PROTO              0
+
+// scegliere il protocollo di trasporto dati
+// COMM_PROTO = 0 -> accesso MQTT (default);
+// COMM_PROTO = 1 -> access  HTTP;
+// COMM_PROTO = 2 -> accesso COAP;
+#ifndef COMM_PROTO
+#define COMM_PROTO 0
+#endif
+
+#if COMM_PROTO==0
+
 #define MQTT_HOST               "demo.thingsboard.io"
 #define MQTT_PORT               1883
 #define MQTT_TOPIC              "v1/devices/me/telemetry"
 #define DEVICE_ACCESS_TOKEN     "nNcsYA9xXL8OsEmHd6Cq"
 
+#endif
+
+
+#if COMM_PROTO==0
 class MQTTNetwork
 {
 public:
@@ -52,6 +70,7 @@
     NetworkInterface* network;
     TCPSocket* socket;
 };
+#endif
 
 int main()
 {
@@ -99,27 +118,29 @@
     printf("Gateway: %s\n", wifi.get_gateway());
     printf("RSSI: %d\n\n", wifi.get_rssi());
 
-    printf("Collegamento MQTT server\n");
-
-MQTTNetwork network(&wifi);
-MQTT::Client<MQTTNetwork, Countdown> client(network);
+#if COMM_PROTO==0
+    printf("Collegamento MQTT server: " MQTT_HOST  "\n");
 
-char assess_token[] = DEVICE_ACCESS_TOKEN;
+    MQTTNetwork network(&wifi);
+    MQTT::Client<MQTTNetwork, Countdown> client(network);
 
-MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer;
-conn_data.username.cstring = assess_token;
+    char assess_token[] = DEVICE_ACCESS_TOKEN;
+
+    MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer;
+    conn_data.username.cstring = assess_token;
 
-if (network.connect(MQTT_HOST, MQTT_PORT) < 0) {
-    printf("failed to connect to " MQTT_HOST  "\n");
-    return -1;
-}
+    if (network.connect(MQTT_HOST, MQTT_PORT) < 0) {
+      printf("failed to connect to " MQTT_HOST  "\n");
+      return -1;
+    }
 
-if (client.connect(conn_data) < 0) {
-    printf("failed to send MQTT connect message\n");
-    return -1;
-}
+    if (client.connect(conn_data) < 0) {
+      printf("failed to send MQTT connect message\n");
+      return -1;
+    }
 
-printf("successfully connect!\n");
+    printf("successfully connect to MQTT server!\n");
+#endif
 
 // Initialize sensors --------------------------------------------------
 
@@ -150,21 +171,19 @@
     size_t payload_len = n;
 
     printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC);
-
+#if COMM_PROTO==0
     if (client.publish(MQTT_TOPIC, payload, n) < 0) {
         printf("failed to publish MQTT message");
     }
-
+#endif
     wait_ms(5000);
 }
 
 
-
+//client.disconnect();
+//wifi.disconnect();
 
-client.disconnect();
-wifi.disconnect();
-
-printf("\ndone\n");
-return 0;
+//printf("\ndone\n");
+//return 0;
 
 }
\ No newline at end of file