WizFi310 IoTMakers Example
Dependencies: NetworkSocketAPI WizFi310Interface mbed
main.cpp
00001 /* NetworkSocketAPI Example Program 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "WizFi310Interface.h" 00019 #include "TCPSocket.h" 00020 00021 #if defined(TARGET_NUCLEO_F401RE) 00022 Serial pc(USBTX,USBRX); 00023 WizFi310Interface wifi(PA_11, PA_12, D6, D7, D3, NC, 115200); 00024 #endif 00025 00026 AnalogIn sensor_illu(PA_1); 00027 DigitalOut led(PC_7); 00028 InterruptIn button(PC_13); 00029 00030 00031 #define AP_SSID "<AP SSID>" 00032 #define AP_PASSWORD "<AP Password>" 00033 #define AP_SECURITY NSAPI_SECURITY_WPA2 00034 00035 #define IoTMakers_GW_ID "<Gateway ID>" 00036 #define IoTMakers_DEV_ID "<Device ID>" 00037 #define IoTMakers_DEV_PWD "<Device Password>" 00038 00039 #define IoTMakers_MQTT_IP "220.90.216.90" 00040 #define IoTMakers_MQTT_PORT 10030 00041 00042 00043 #define DELAY_SEND_TIME_MS 5000 00044 00045 unsigned long g_time_ms = 0; 00046 unsigned long g_prev_send_time = 0; 00047 unsigned long g_prev_clicked_time = 0; 00048 00049 //Button IRQ 00050 int button_status = 0; 00051 int button_clicked = 0; 00052 00053 00054 enum Connection_Step { 00055 csDISASSOCIATE = 0, 00056 csASSOCIATE, 00057 csKTCONNECTED, 00058 }CON_STEP; 00059 00060 TCPSocket socket(&wifi); 00061 WizFi310* pwizfi310 = wifi.get_WizFi310_Pointer(); 00062 00063 void Connect_to_AP(); 00064 void Connect_to_IoTMakers(); 00065 void Send_num_value_to_IoTMakers(const char* tag_stream, const float dvalue); 00066 void Send_str_value_to_IoTMakers(const char* tag_stream, const char* svalue); 00067 00068 void Button_IRQ(); 00069 void Button_Push_Event(); 00070 void Periodic_Event(); 00071 00072 void time_ms(); 00073 00074 void Button_IRQ() 00075 { 00076 if( button_clicked == 0 && (g_prev_clicked_time + 500) < g_time_ms) 00077 { 00078 button_clicked = 1; 00079 g_prev_clicked_time = g_time_ms; 00080 } 00081 } 00082 void Button_Push_Event() 00083 { 00084 00085 if( CON_STEP == csKTCONNECTED) 00086 { 00087 if( button_status == 0 ) 00088 { 00089 Send_str_value_to_IoTMakers("switch", "on"); 00090 button_status = 1; 00091 } 00092 else if( button_status == 1 ) 00093 { 00094 Send_str_value_to_IoTMakers("switch", "off"); 00095 button_status = 0; 00096 } 00097 } 00098 } 00099 00100 void Periodic_Event() 00101 { 00102 float illumination; 00103 if( CON_STEP == csKTCONNECTED) 00104 { 00105 illumination = sensor_illu.read() * 100; 00106 00107 Send_num_value_to_IoTMakers("illumination", (float)illumination); 00108 } 00109 } 00110 00111 00112 int main() 00113 { 00114 pc.baud(115200); 00115 00116 //Timer settings 00117 Ticker timer_ms; 00118 timer_ms.attach(time_ms, 0.001f); 00119 g_prev_send_time = g_time_ms; 00120 00121 //button Interrupt 00122 button.rise(&Button_IRQ); 00123 00124 00125 //normal settings 00126 CON_STEP = csDISASSOCIATE; 00127 00128 printf("KT IoTMakers Example\r\n"); 00129 00130 char buffer[256]; 00131 int count; 00132 00133 while(1) 00134 { 00135 switch (CON_STEP) 00136 { 00137 case csDISASSOCIATE: 00138 Connect_to_AP(); 00139 break; 00140 case csASSOCIATE: 00141 Connect_to_IoTMakers(); 00142 break; 00143 00144 case csKTCONNECTED: 00145 00146 if( ( g_prev_send_time + DELAY_SEND_TIME_MS ) < g_time_ms ) 00147 { 00148 Periodic_Event(); 00149 g_prev_send_time = g_time_ms; 00150 } 00151 00152 if( button_clicked == 1 ) 00153 { 00154 Button_Push_Event(); 00155 button_clicked = 0; 00156 } 00157 00158 if( pwizfi310->readable(0) > 0) 00159 { 00160 pwizfi310->recv(0, buffer, sizeof(buffer)); 00161 printf("RECV : %s\r\n", buffer); 00162 } 00163 00164 00165 break; 00166 default: 00167 break; 00168 }; 00169 } 00170 00171 00172 00173 00174 } 00175 00176 00177 void Connect_to_AP() 00178 { 00179 int i; 00180 int ret; 00181 00182 for( i=0; i<5; i++ ) 00183 { 00184 00185 if( CON_STEP != csDISASSOCIATE ) 00186 { 00187 break; 00188 } 00189 00190 ret = wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); 00191 00192 if( ret == 0 ) //connection success 00193 { 00194 //const char *ip = wifi.get_ip_address(); 00195 //const char *mac = wifi.get_mac_address(); 00196 //printf("IP address is: %s\r\n", ip ? ip : "No IP"); 00197 //printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); 00198 00199 CON_STEP = csASSOCIATE; 00200 wait(1); 00201 break; 00202 } 00203 else 00204 { 00205 printf("CONNECTION FAIL\r\n", ret); 00206 CON_STEP = csASSOCIATE; 00207 } 00208 } 00209 } 00210 00211 void Connect_to_IoTMakers() 00212 { 00213 char cmd[100]; 00214 int ret = 0; 00215 00216 //set_information 00217 sprintf(cmd, "AT+TKTSET1=%s,%s,%s,0", IoTMakers_GW_ID, IoTMakers_DEV_ID, IoTMakers_DEV_PWD); 00218 pwizfi310->sendCommand(cmd); 00219 00220 //connect IoTMakers via MQTT 00221 sprintf(cmd, "AT+TKTCON=2,%s,%d,0,1111", IoTMakers_MQTT_IP, IoTMakers_MQTT_PORT); 00222 ret = pwizfi310->sendCommand(cmd,WizFi310::RES_NULL,5000); 00223 00224 if(ret == 0) 00225 { 00226 CON_STEP = csKTCONNECTED; 00227 } 00228 } 00229 00230 void Send_num_value_to_IoTMakers(const char* tag_stream, const float dvalue) 00231 { 00232 char cmd[100]; 00233 //send numberic data 00234 sprintf(cmd, "AT+TKTSEND=d,%s,%.1f", tag_stream, dvalue); 00235 pwizfi310->sendCommand(cmd); 00236 } 00237 00238 void Send_str_value_to_IoTMakers(const char* tag_stream, const char* svalue) 00239 { 00240 char cmd[100]; 00241 //send string data 00242 sprintf(cmd, "AT+TKTSEND=s,%s,%s", tag_stream, svalue); 00243 pwizfi310->sendCommand(cmd); 00244 } 00245 00246 00247 void time_ms() 00248 { 00249 g_time_ms++; 00250 }
Generated on Fri Jul 15 2022 19:48:29 by 1.7.2