IoTKitV3 / Mbed OS MQTTPublish

Dependencies:   QEI MFRC522 HTS221 IoTKit BMP180 MQTT

Fork of MQTTPublish by smd.iotkit2.ch

Revision:
27:43f3a9552db2
Parent:
26:cf9cf40c63ea
Child:
28:aa3b3fa5b5a7
--- a/main.cpp	Mon Sep 10 16:47:34 2018 +0000
+++ b/main.cpp	Thu Feb 07 12:56:43 2019 +0000
@@ -1,13 +1,16 @@
 /** MQTT Publish von Sensordaten */
 #include "mbed.h"
 #include "HTS221Sensor.h"
-#include "easy-connect.h"
 #include "MQTTNetwork.h"
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
 #include "OLEDDisplay.h"
 #include "Motor.h"
 
+#include "ESP8266Interface.h"
+#include "TCPSocket.h"
+
+ESP8266Interface wifi( MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX );
 static DevI2C devI2c(PTE0,PTE1);
 static HTS221Sensor hum_temp(&devI2c);
 AnalogIn hallSensor( PTC0 );
@@ -16,8 +19,8 @@
 char* topicTEMP =  "iotkit/sensor";
 char* topicALERT = "iotkit/alert";
 // MQTT Brocker
-char* hostname = "192.168.3.10";
-int port = 31883;
+char* hostname = "iot.eclipse.org";
+int port = 1883;
 // MQTT Message
 MQTT::Message message;
 // I/O Buffer
@@ -86,12 +89,16 @@
     oled.printf( "MQTTPublish\r\n" );
     oled.printf( "host: %s:%s\r\n", hostname, port );
 
-    NetworkInterface* network = easy_connect(true);
-    if (!network) 
+    printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
+    oled.printf( "SSID: %s\r\n", MBED_CONF_APP_WIFI_SSID );
+    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
+    if (ret != 0) {
+        printf("\nConnection error: %d\n", ret);
         return -1;
+    }
 
     // TCP/IP und MQTT initialisieren (muss in main erfolgen)
-    MQTTNetwork mqttNetwork(network);
+    MQTTNetwork mqttNetwork( &wifi );
     MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
     
     /* Init all sensors with default params */
@@ -141,7 +148,7 @@
             speaker = 0.5f;
             wait( 0.5f );
             speaker.period( 1.0 / 2800.0 );
-            wait( 0.5f );            
+            wait( 0.5f );
         }
         else
         {