Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DISCO_L475VG_IOT01A_wifi mbed stm-spirit1-rf-driver
main.cpp
00001 #include "mbed.h" 00002 #include <string> 00003 00004 #include "wifi.h" 00005 00006 #include "MQTTmbed.h" 00007 #include "MQTTClient.h" 00008 #include "MQTT_wifi.h" 00009 00010 #include "SimpleSpirit1.h" 00011 00012 00013 /* Private defines -----------------------------------------------------------*/ 00014 #define WIFI_WRITE_TIMEOUT 10000 00015 #define WIFI_READ_TIMEOUT 10000 00016 #define CONNECTION_TRIAL_MAX 10 00017 00018 00019 /* Private typedef------------------------------------------------------------*/ 00020 /* Private macro -------------------------------------------------------------*/ 00021 /* Private variables ---------------------------------------------------------*/ 00022 Serial pc(SERIAL_TX, SERIAL_RX); 00023 uint8_t RemoteIP[] = {MBED_CONF_APP_SERVER_IP_1,MBED_CONF_APP_SERVER_IP_2,MBED_CONF_APP_SERVER_IP_3, MBED_CONF_APP_SERVER_IP_4}; 00024 uint8_t RxData [500]; 00025 char* modulename; 00026 uint8_t TxData[] = "STM32 : Hello!\n"; 00027 uint16_t RxLen; 00028 uint8_t MAC_Addr[6]; 00029 uint8_t IP_Addr[4]; 00030 00031 void messageArrived(MQTT::MessageData& md) 00032 { 00033 MQTT::Message &message = md.message; 00034 printf("message recieved\r\n"); 00035 } 00036 00037 static volatile bool tx_done_flag = false; 00038 static void callback_func(int event) 00039 { 00040 if (event == SimpleSpirit1::TX_DONE) { 00041 tx_done_flag = true; 00042 } 00043 } 00044 00045 int main() 00046 { 00047 00048 00049 pc.baud(115200); 00050 00051 /*Initialize WIFI module */ 00052 if(WIFI_Init() == WIFI_STATUS_OK) { 00053 00054 printf("> WIFI Module Initialized.\r\n"); 00055 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { 00056 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\r\n", 00057 MAC_Addr[0], 00058 MAC_Addr[1], 00059 MAC_Addr[2], 00060 MAC_Addr[3], 00061 MAC_Addr[4], 00062 MAC_Addr[5]); 00063 } else { 00064 printf("> ERROR : CANNOT get MAC address\r\n"); 00065 } 00066 00067 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { 00068 printf("> es-wifi module connected \r\n"); 00069 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { 00070 printf("> es-wifi module got IP Address : %d.%d.%d.%d\r\n", 00071 IP_Addr[0], 00072 IP_Addr[1], 00073 IP_Addr[2], 00074 IP_Addr[3]); 00075 00076 printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\r\n", 00077 RemoteIP[0], 00078 RemoteIP[1], 00079 RemoteIP[2], 00080 RemoteIP[3]); 00081 00082 00083 uint8_t colca_addr[4]; 00084 WIFI_GetHostAddress("colcaweb01.duckdns.org", colca_addr); 00085 00086 printf("---- Colca IP address : %d.%d.%d.%d\r\n", 00087 colca_addr[0], 00088 colca_addr[1], 00089 colca_addr[2], 00090 colca_addr[3]); 00091 00092 MQTT_wifi wf; 00093 int rc; 00094 printf("rc from connect:%d\r\n", rc); 00095 MQTT::Client<MQTT_wifi, Countdown> client(wf); 00096 wf.connect("colcaweb01.duckdns.org", 1883); 00097 00098 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00099 data.MQTTVersion = 3; 00100 data.clientID.cstring = "test-client"; 00101 data.username.cstring = "test-user"; 00102 data.password.cstring = "Tall5Duck"; 00103 data.cleansession = 1; 00104 if ((rc = client.connect(data)) != 0) 00105 printf("rc from MQTT connect is %d\r\n", rc); 00106 00107 00108 SimpleSpirit1 &subghz = SimpleSpirit1::CreateInstance(PC_12, PC_11, PC_10, PE_5, PB_5, PB_15); 00109 subghz.attach_irq_callback(callback_func); 00110 subghz.on(); 00111 00112 while(true) { 00113 00114 00115 char buf[SPIRIT1_MAX_PAYLOAD]; 00116 int size = subghz.read(buf, 25); 00117 buf[size] = '\0'; 00118 00119 00120 rc = client.yield(5000); 00121 if (rc) { 00122 printf("Problem with client.yeild()\r\n"); 00123 client.connect(data); 00124 } else { 00125 printf("Still connected\r\n"); 00126 } 00127 00128 if (!strncmp(buf, "complete message", 16)) { 00129 MQTT::Message message; 00130 char msg_buf[100]; 00131 sprintf(msg_buf, buf); 00132 message.qos = MQTT::QOS2; 00133 message.retained = false; 00134 message.dup = false; 00135 message.payload = (void*)msg_buf; 00136 message.payloadlen = strlen(msg_buf)+1; 00137 client.publish("test/st/range", message); 00138 printf("Received subghz message, sent mqtt message\r\n"); 00139 00140 } 00141 } 00142 } else { 00143 printf("> ERROR : es-wifi module CANNOT get IP address\n"); 00144 } 00145 } else { 00146 printf("> ERROR : es-wifi module NOT connected\n"); 00147 } 00148 } else { 00149 printf("> ERROR : WIFI Module cannot be initialized.\n"); 00150 } 00151 }
Generated on Sat Jul 30 2022 04:21:29 by
1.7.2