client

Dependencies:   DISCO_L475VG_IOT01A_wifi mbed stm-spirit1-rf-driver

Files at this revision

API Documentation at this revision

Comitter:
apate131
Date:
Mon Sep 25 16:19:27 2017 +0000
Parent:
3:1f8ef0fd4347
Commit message:
asdjnakjsd

Changed in this revision

DISCO_L475VG_IOT01A_wifi.lib Show annotated file Show diff for this revision Revisions of this file
MQTT_wifi.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
stm-spirit1-rf-driver.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf DISCO_L475VG_IOT01A_wifi.lib
--- a/DISCO_L475VG_IOT01A_wifi.lib	Thu Aug 17 16:37:16 2017 +0000
+++ b/DISCO_L475VG_IOT01A_wifi.lib	Mon Sep 25 16:19:27 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/ST/code/DISCO_L475VG_IOT01A_wifi/#c61a93635433
+https://os.mbed.com/users/apate131/code/DISCO_L475VG_IOT01A_wifi/#50681dc8ff42
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf MQTT_wifi.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT_wifi.h	Mon Sep 25 16:19:27 2017 +0000
@@ -0,0 +1,50 @@
+#include <string>
+
+#include "wifi.h"
+
+
+class MQTT_wifi
+{
+public:
+    MQTT_wifi() {
+    }
+
+    int connect(char* hostname, int port, int timeout=1000) {
+
+        uint8_t  addr[4];
+        if (WIFI_GetHostAddress(hostname, addr) != WIFI_STATUS_OK) {
+            return -1;
+        }
+
+        if (WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", addr, port, 100) != WIFI_STATUS_OK) {
+            return -1;
+        }
+
+        socket = 0;
+        return socket;
+    }
+
+    int read(unsigned char* buffer, int len, int timeout) {
+        uint16_t read_len = -1;
+        if(WIFI_ReceiveData(socket, buffer, len, &read_len, timeout) == WIFI_STATUS_OK) {
+            return read_len;
+        }
+
+        return -1;
+    }
+
+    int write(unsigned char* buffer, int len, int timeout) {
+        uint16_t sent_len = -1;
+        if(WIFI_SendData(socket, buffer, len, &sent_len, timeout) == WIFI_STATUS_OK) {
+            return sent_len;
+        }
+        
+        return -1;
+    }
+
+
+
+private:
+    int socket;
+
+};
\ No newline at end of file
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf main.cpp
--- a/main.cpp	Thu Aug 17 16:37:16 2017 +0000
+++ b/main.cpp	Mon Sep 25 16:19:27 2017 +0000
@@ -1,22 +1,21 @@
 #include "mbed.h"
+#include <string>
+
 #include "wifi.h"
-/*------------------------------------------------------------------------------
-Hyperterminal settings: 115200 bauds, 8-bit data, no parity
 
-This example 
-  - connects to a wifi network (SSID & PWD to set in mbed_app.json)
-  - Connects to a TCP server (set the address in RemoteIP)
-  - Sends "Hello" to the server when data is received
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
+#include "MQTT_wifi.h"
 
-This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13), 
-wifi_dataready pin (PE_1), wifi reset pin (PE_8)
-------------------------------------------------------------------------------*/
+#include "SimpleSpirit1.h"
+
 
 /* Private defines -----------------------------------------------------------*/
 #define WIFI_WRITE_TIMEOUT 10000
 #define WIFI_READ_TIMEOUT  10000
 #define CONNECTION_TRIAL_MAX          10
 
+
 /* Private typedef------------------------------------------------------------*/
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
@@ -26,67 +25,119 @@
 char* modulename;
 uint8_t TxData[] = "STM32 : Hello!\n";
 uint16_t RxLen;
-uint8_t  MAC_Addr[6]; 
-uint8_t  IP_Addr[4]; 
+uint8_t  MAC_Addr[6];
+uint8_t  IP_Addr[4];
+
+void messageArrived(MQTT::MessageData& md)
+{
+    MQTT::Message &message = md.message;
+    printf("message recieved\r\n");
+}
+
+static volatile bool tx_done_flag = false;
+static void callback_func(int event)
+{
+    if (event == SimpleSpirit1::TX_DONE) {
+        tx_done_flag = true;
+    }
+}
 
 int main()
 {
-    int32_t Socket = -1;
-    uint16_t Datalen;
-    uint16_t Trials = CONNECTION_TRIAL_MAX;
+
 
     pc.baud(115200);
 
-    printf("\n");
-    printf("************************************************************\n");
-    printf("***   STM32 IoT Discovery kit for STM32L475 MCU          ***\n");
-    printf("***      WIFI Module in TCP Client mode demonstration    ***\n\n");
-    printf("*** TCP Client Instructions :\n");
-    printf("*** 1- Make sure your Phone is connected to the same network that\n");
-    printf("***    you configured using the Configuration Access Point.\n");
-    printf("*** 2- Create a server by using the android application TCP Server\n");
-    printf("***    with port(8002).\n");
-    printf("*** 3- Get the Network Name or IP Address of your phone from the step 2.\n\n"); 
-    printf("************************************************************\n");
-
     /*Initialize  WIFI module */
     if(WIFI_Init() ==  WIFI_STATUS_OK) {
-        printf("> WIFI Module Initialized.\n");  
+
+        printf("> WIFI Module Initialized.\r\n");
         if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
-            printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",     
+            printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\r\n",
                    MAC_Addr[0],
                    MAC_Addr[1],
                    MAC_Addr[2],
                    MAC_Addr[3],
                    MAC_Addr[4],
-                   MAC_Addr[5]);   
+                   MAC_Addr[5]);
         } else {
-            printf("> ERROR : CANNOT get MAC address\n");
+            printf("> ERROR : CANNOT get MAC address\r\n");
         }
-    
+
         if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
-            printf("> es-wifi module connected \n");
+            printf("> es-wifi module connected \r\n");
             if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
-                printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",     
+                printf("> es-wifi module got IP Address : %d.%d.%d.%d\r\n",
                        IP_Addr[0],
                        IP_Addr[1],
                        IP_Addr[2],
-                       IP_Addr[3]); 
-        
-                printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\n",     
+                       IP_Addr[3]);
+
+                printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\r\n",
                        RemoteIP[0],
                        RemoteIP[1],
                        RemoteIP[2],
                        RemoteIP[3]);
-        
-                while (Trials--){ 
-                    if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 8002, 0) == WIFI_STATUS_OK){
-                        printf("> TCP Connection opened successfully.\n"); 
-                        Socket = 0;
+
+
+                uint8_t  colca_addr[4];
+                WIFI_GetHostAddress("colcaweb01.duckdns.org", colca_addr);
+
+                printf("---- Colca IP address : %d.%d.%d.%d\r\n",
+                       colca_addr[0],
+                       colca_addr[1],
+                       colca_addr[2],
+                       colca_addr[3]);
+
+                MQTT_wifi wf;
+                int rc;
+                printf("rc from connect:%d\r\n", rc);
+                MQTT::Client<MQTT_wifi, Countdown> client(wf);
+                wf.connect("colcaweb01.duckdns.org", 1883);
+
+                MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+                data.MQTTVersion = 3;
+                data.clientID.cstring = "test-client";
+                data.username.cstring = "test-user";
+                data.password.cstring = "Tall5Duck";
+                data.cleansession = 1;
+                if ((rc = client.connect(data)) != 0)
+                    printf("rc from MQTT connect is %d\r\n", rc);
+
+
+                SimpleSpirit1 &subghz = SimpleSpirit1::CreateInstance(PC_12, PC_11, PC_10, PE_5, PB_5, PB_15);
+                subghz.attach_irq_callback(callback_func);
+                subghz.on();
+
+                while(true) {
+
+                    
+                    char buf[SPIRIT1_MAX_PAYLOAD];
+                    int size = subghz.read(buf, 25);
+                    buf[size] = '\0';
+
+
+                    rc = client.yield(5000);
+                    if (rc) {
+                        printf("Problem with client.yeild()\r\n");
+                        client.connect(data);
+                    } else {
+                        printf("Still connected\r\n");
                     }
-                }
-                if(!Trials) {
-                    printf("> ERROR : Cannot open Connection\n");
+
+                    if (!strncmp(buf, "complete message", 16)) {
+                        MQTT::Message message;
+                        char msg_buf[100];
+                        sprintf(msg_buf, buf);
+                        message.qos = MQTT::QOS2;
+                        message.retained = false;
+                        message.dup = false;
+                        message.payload = (void*)msg_buf;
+                        message.payloadlen = strlen(msg_buf)+1;
+                        client.publish("test/st/range", message);
+                        printf("Received subghz message, sent mqtt message\r\n");
+                        
+                    }
                 }
             } else {
                 printf("> ERROR : es-wifi module CANNOT get IP address\n");
@@ -95,20 +146,6 @@
             printf("> ERROR : es-wifi module NOT connected\n");
         }
     } else {
-        printf("> ERROR : WIFI Module cannot be initialized.\n"); 
-    }
-  
-    while(1){
-        if(Socket != -1) {
-            if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK){
-                if(Datalen > 0) {
-                    if(WIFI_SendData(Socket, TxData, sizeof(TxData), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
-                        printf("> ERROR : Failed to send Data.\n");   
-                    } 
-                }
-            } else {
-                printf("> ERROR : Failed to Receive Data.\n");  
-            }
-        }
+        printf("> ERROR : WIFI Module cannot be initialized.\n");
     }
 }
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf mbed-os.lib
--- a/mbed-os.lib	Thu Aug 17 16:37:16 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/ARMmbed/mbed-os/#4c256f04596179699c4f14b6863b07cc024ca9be
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Sep 25 16:19:27 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/675da3299148
\ No newline at end of file
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf mbed_app.json
--- a/mbed_app.json	Thu Aug 17 16:37:16 2017 +0000
+++ b/mbed_app.json	Mon Sep 25 16:19:27 2017 +0000
@@ -2,11 +2,11 @@
     "config": {
         "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"SSID\""
+            "value": "\"mHub_IoT\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"PASSWORD\""
+            "value": "\"DHA6Jo8iPwS7pqfNrFHE\""
         },
         "server-ip-1": {
             "help": "TCP server IP address 1st value",
diff -r 1f8ef0fd4347 -r 40e6ed9dbfaf stm-spirit1-rf-driver.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stm-spirit1-rf-driver.lib	Mon Sep 25 16:19:27 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/apate131/code/stm-spirit1-rf-driver/#3d185fef70f3