MQTT configuration with WizFi310

Dependencies:   DHT MQTT WizFi310Interface_Legacynew mbed-src

Fork of w7500-mqtt-wizfi310 by Akshay Tom

Revision:
16:2794fcca7629
Parent:
15:d4604c47da41
--- a/main.cpp	Mon Sep 21 23:39:16 2015 +0000
+++ b/main.cpp	Thu Dec 28 06:32:51 2017 +0000
@@ -1,14 +1,25 @@
 #include "mbed.h"
+#include "WizFi310Interface.h"
 #include "DHT.h"
-#include "MQTTEthernet.h"
 #include "MQTTClient.h"
+#include "MQTTmbed.h"
+#include "MQTTSocket.h"
 
-#define ECHO_SERVER_PORT   7
+#define SECURE WizFi310::SEC_WPA2_MIXED
+#define SSID "CDI"
+#define PASS "Cdi*1717"
+/*
+    SET DHCP
+*/
+#define USE_DHCP    1
+//--------- Have to modify the mac address-------------
+unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
+#if defined(TARGET_WIZwiki_W7500)
+    WizFi310Interface wizfi310(D1, D0, D7, D6, D8, NC, 115200);
+    Serial pc(USBTX, USBRX);
+#endif
 
-#include "mbed.h"
- 
 int arrivedcount = 0;
- 
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
@@ -17,28 +28,43 @@
     ++arrivedcount;
 }
 
-void baud(int baudrate) {
-    Serial s(USBTX, USBRX);
-    s.baud(baudrate);
-}
-
-int main (void)
+class MQTTWIZ: public MQTTSocket
 {
-    DHT sensor(D4, DHT11);
-    AnalogIn ain(A1);
-    baud(115200);
-    printf("Wait a second...\r\n");
-    MQTTEthernet ipstack = MQTTEthernet();
+public:    
+    MQTTWIZ()
+    {
+        wait(1);
+        this->createSocket();
+    }
+};
+
+int main() {
+
+
+ 
+   pc.baud(115200);
+ 
+    printf("WizFi310  STATION. \r\n");
+    wizfi310.init();
+    printf("After Initialisation. \r\n");
+
+    printf("After Set Address. \r\n");
+    if ( wizfi310.connect(SECURE, SSID, PASS, WizFi310::WM_STATION))      return -1;
+    printf("After Connect. \r\n");
+    printf("IP Address is %s\r\n", wizfi310.getIPAddress()); 
     
-    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
+    MQTTWIZ ipstack = MQTTWIZ();
+    MQTT::Client<MQTTWIZ, Countdown> client = MQTT::Client<MQTTWIZ, Countdown>(ipstack);
     
-    char* hostname = "222.98.173.244";
+    DHT sensor(D14, DHT11);
+    char* hostname = "172.16.73.4";
     int port = 1883;
     
     int rc = ipstack.connect(hostname, port);
     if (rc != 0)
         printf("rc from TCP connect is %d\n", rc);
-        
+    printf("rc from TCP connect is %d\n", rc);
+    
     char MQTTClientID[30];
     
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
@@ -71,29 +97,14 @@
         sprintf(buf, "%3.1f", hum);
         message.payload = (void*)buf;
         message.payloadlen = strlen(buf);
-        rc = client.publish("Wiznet/humidity", message);
+        rc = client.publish("Wiz/humidity", message);
+        printf("Wiz/humidity : %s\r\n",message.payload);
         
         sprintf(buf, "%3.1f", temp);
         message.payload = (void*)buf;
         message.payloadlen = strlen(buf);
-        rc = client.publish("Wiznet/temperature", message);
-        
-        sprintf(buf, "%3.1f", (rand()%1000)/10.0);
-        message.payload = (void*)buf;
-        message.payloadlen = strlen(buf);
-        rc = client.publish("Wiznet/fluidgauge/A", message);
-        
-        sprintf(buf, "%3.1f", (rand()%1000)/10.0);
-        message.payload = (void*)buf;
-        message.payloadlen = strlen(buf);
-        rc = client.publish("Wiznet/fluidgauge/B", message);
-        
-        sprintf(buf, "%3.1f", (rand()%1000)/10.0);
-        message.payload = (void*)buf;
-        message.payloadlen = strlen(buf);
-        rc = client.publish("Wiznet/fluidgauge/C", message);
+        rc = client.publish("Wiz/temperature", message);
+        printf("Wiz/temperature : %s\r\n",message.payload);
         client.yield(1000);
-        //wait(100);
     }
 }
-