Viktor Shin
/
Mbed-Azure-Atcmd-WizFi360
Initial release
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 00002 /* mbed Example Program 00003 * Copyright (c) 2006-2014 ARM Limited 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "mbed.h" 00018 00019 #include "Dht11.h" 00020 00021 // ---------------------------------------------------------------------------------------------------- 00022 // Define 00023 // ---------------------------------------------------------------------------------------------------- 00024 /* Default setting */ 00025 #define ON 1 00026 #define OFF 0 00027 #define ENABLE 1 00028 #define DISABLE 0 00029 #define RET_OK 1 00030 #define RET_NOK -1 00031 00032 /* Pin configuraiton */ 00033 #define MBED_CONF_WIZFI360_TX D8 00034 #define MBED_CONF_WIZFI360_RX D2 00035 #define MBED_CONF_WIZFI360_RESET D9 00036 #define MBED_CONF_WIZFI360_SENSOR_CDS A0 00037 #define MBED_CONF_WIZFI360_SENSOR_DHT D14 00038 00039 /* Serial setting */ 00040 #define WIZFI360_DEFAULT_BAUD_RATE 115200 00041 00042 /* Parser setting */ 00043 #define WIZFI360_PARSER_DELIMITER "\r\n" 00044 00045 /* Debug message setting */ 00046 #define DEBUG_ENABLE 1 00047 #define DEBUG_DISABLE 0 00048 00049 #define WIZFI360_PARSER_DEBUG DEBUG_DISABLE 00050 00051 /* Timeout setting */ 00052 #define WIZFI360_DEFAULT_TIMEOUT 1000 00053 00054 /* Wi-Fi */ 00055 #define STATION 1 00056 #define SOFTAP 2 00057 #define STATION_SOFTAP 3 00058 00059 #define WIFI_MODE STATION 00060 00061 // ---------------------------------------------------------------------------------------------------- 00062 // Variables 00063 // ---------------------------------------------------------------------------------------------------- 00064 Serial pc(USBTX, USBRX); 00065 00066 UARTSerial *_serial; 00067 ATCmdParser *_parser; 00068 00069 DigitalOut _RESET_WIZFI360(MBED_CONF_WIZFI360_RESET); 00070 AnalogIn _cdsVal(MBED_CONF_WIZFI360_SENSOR_CDS); 00071 Dht11 _dhtVal(MBED_CONF_WIZFI360_SENSOR_DHT); 00072 00073 /* Sensor info */ 00074 struct sensor 00075 { 00076 int ill; // illuminance 00077 int cel; // celsius 00078 float fah; // fahrenheit 00079 int hum; // humidity 00080 }; 00081 00082 /* WiFi info */ 00083 char ssid[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 00084 char password[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 00085 00086 /* MQTT info */ 00087 char hub_name[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 00088 char device_id[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 00089 char device_primary_key[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 00090 00091 // ---------------------------------------------------------------------------------------------------- 00092 // Functions 00093 // ---------------------------------------------------------------------------------------------------- 00094 /* Util */ 00095 void delay(int ms); 00096 00097 /* Serial */ 00098 void serialPcInit(void); 00099 void serialDeviceInit(PinName tx, PinName rx, int baudrate); 00100 void serialAtParserInit(const char *delimiter, bool debug_en); 00101 00102 /* Device */ 00103 void deviceInit_WizFi360(void); 00104 void deviceReset_WizFi360(void); 00105 void deviceIsReady_WizFi360(void); 00106 00107 /* Sensor */ 00108 int deviceGetIllVal_WizFi360(void); 00109 int deviceGetCelVal_WizFi360(void); 00110 float deviceGetFahVal_WizFi360(void); 00111 int deviceGetHumVal_WizFi360(void); 00112 00113 /* Wi-Fi */ 00114 int8_t deviceSetWifiMode_WizFi360(int mode); 00115 int8_t deviceSetDhcp_WizFi360(int mode, int en_dhcp); 00116 int8_t deviceConnAP_WizFi360(int mode, char *ssid, char *password); 00117 00118 /* Azure */ 00119 int8_t deviceSetAzureConf_WizFi360(char *iot_hub, char *device_id, char *sas_token); 00120 int8_t devicePublishMsg_WizFi360(char *device_id, sensor *sensor_info); 00121 int8_t deviceAzureCon_WizFi360(); 00122 int8_t deviceSetTopic_WizFi360(char *device_id); 00123 00124 00125 // ---------------------------------------------------------------------------------------------------- 00126 // Main 00127 // ---------------------------------------------------------------------------------------------------- 00128 int main() 00129 { 00130 sensor sensor_info; 00131 00132 /* Set serial */ 00133 serialPcInit(); 00134 00135 /* Set device */ 00136 deviceInit_WizFi360(); 00137 deviceReset_WizFi360(); 00138 deviceIsReady_WizFi360(); 00139 00140 /* Set Wi-Fi */ 00141 deviceSetWifiMode_WizFi360(WIFI_MODE); 00142 deviceSetDhcp_WizFi360(WIFI_MODE, ENABLE); 00143 deviceConnAP_WizFi360(WIFI_MODE, ssid, password); 00144 00145 /* Set Azure */ 00146 00147 deviceSetAzureConf_WizFi360(hub_name,device_id,device_primary_key); 00148 deviceSetTopic_WizFi360(device_id); 00149 deviceAzureCon_WizFi360(); 00150 00151 while(true) 00152 { 00153 /* Get sensor info */ 00154 sensor_info.ill = deviceGetIllVal_WizFi360(); 00155 sensor_info.cel = deviceGetCelVal_WizFi360(); 00156 sensor_info.fah = deviceGetFahVal_WizFi360(); 00157 sensor_info.hum = deviceGetHumVal_WizFi360(); 00158 00159 /* Publish message */ 00160 devicePublishMsg_WizFi360(device_id, &sensor_info); 00161 00162 delay(3000); 00163 } 00164 } 00165 00166 // ---------------------------------------------------------------------------------------------------- 00167 // Functions : Util 00168 // ---------------------------------------------------------------------------------------------------- 00169 void delay(int ms) 00170 { 00171 wait_us(ms * 1000); 00172 } 00173 00174 // ---------------------------------------------------------------------------------------------------- 00175 // Functions : Serial 00176 // ---------------------------------------------------------------------------------------------------- 00177 void serialPcInit(void) 00178 { 00179 pc.baud(115200); 00180 pc.format(8, Serial::None, 1); 00181 } 00182 00183 void serialDeviceInit(PinName tx, PinName rx, int baudrate) 00184 { 00185 _serial = new UARTSerial(tx, rx, baudrate); 00186 } 00187 00188 void serialAtParserInit(const char *delimiter, bool debug_en) 00189 { 00190 _parser = new ATCmdParser(_serial); 00191 _parser->debug_on(debug_en); 00192 _parser->set_delimiter(delimiter); 00193 _parser->set_timeout(WIZFI360_DEFAULT_TIMEOUT); 00194 } 00195 00196 // ---------------------------------------------------------------------------------------------------- 00197 // Functions : Device 00198 // ---------------------------------------------------------------------------------------------------- 00199 void deviceInit_WizFi360(void) 00200 { 00201 serialDeviceInit(MBED_CONF_WIZFI360_TX, MBED_CONF_WIZFI360_RX, WIZFI360_DEFAULT_BAUD_RATE); 00202 serialAtParserInit(WIZFI360_PARSER_DELIMITER, WIZFI360_PARSER_DEBUG); 00203 } 00204 00205 void deviceReset_WizFi360(void) 00206 { 00207 _RESET_WIZFI360 = 1; 00208 delay(300); 00209 00210 _RESET_WIZFI360 = 0; 00211 delay(400); 00212 00213 _RESET_WIZFI360 = 1; 00214 delay(1000); 00215 } 00216 00217 void deviceIsReady_WizFi360(void) 00218 { 00219 while(1) 00220 { 00221 if(_parser->recv("ready")) 00222 { 00223 pc.printf("WizFi360 is available\r\n"); 00224 00225 return; 00226 } 00227 else if(_parser->send("AT") && _parser->recv("OK")) 00228 { 00229 pc.printf("WizFi360 is already available\r\n"); 00230 00231 return; 00232 } 00233 else 00234 { 00235 pc.printf("WizFi360 is not available\r\n"); 00236 00237 return; 00238 } 00239 } 00240 } 00241 00242 // ---------------------------------------------------------------------------------------------------- 00243 // Functions : Sensor 00244 // ---------------------------------------------------------------------------------------------------- 00245 /* illuminance */ 00246 int deviceGetIllVal_WizFi360(void) 00247 { 00248 int val = _cdsVal.read_u16() / 100; 00249 00250 return val; 00251 } 00252 00253 /* celsius */ 00254 int deviceGetCelVal_WizFi360(void) 00255 { 00256 _dhtVal.read(); 00257 00258 int val = _dhtVal.getCelsius(); 00259 00260 return val; 00261 } 00262 00263 /* fahrenheit */ 00264 float deviceGetFahVal_WizFi360(void) 00265 { 00266 _dhtVal.read(); 00267 00268 int val = _dhtVal.getFahrenheit(); 00269 00270 return val; 00271 } 00272 00273 /* humidity */ 00274 int deviceGetHumVal_WizFi360(void) 00275 { 00276 _dhtVal.read(); 00277 00278 int val = _dhtVal.getHumidity(); 00279 00280 return val; 00281 } 00282 00283 // ---------------------------------------------------------------------------------------------------- 00284 // Functions : Wi-Fi 00285 // ---------------------------------------------------------------------------------------------------- 00286 int8_t deviceSetWifiMode_WizFi360(int mode) 00287 { 00288 int8_t ret = RET_NOK; 00289 00290 if(_parser->send("AT+CWMODE_CUR=%d", mode) && _parser->recv("OK")) 00291 { 00292 pc.printf("Set Wi-Fi mode : %d\r\n", mode); 00293 00294 ret = RET_OK; 00295 } 00296 else 00297 { 00298 pc.printf("Set Wi-Fi mode : ERROR\r\n"); 00299 } 00300 00301 return ret; 00302 } 00303 00304 int8_t deviceSetDhcp_WizFi360(int mode, int en_dhcp) 00305 { 00306 int8_t ret = RET_NOK; 00307 int mode_dhcp; 00308 00309 if(mode == STATION) 00310 { 00311 mode_dhcp = 1; 00312 } 00313 else if(mode == SOFTAP) 00314 { 00315 mode_dhcp = 0; 00316 } 00317 else if(mode == STATION_SOFTAP) 00318 { 00319 mode_dhcp = 2; 00320 } 00321 else 00322 { 00323 pc.printf("Set DHCP : ERROR\r\n"); 00324 } 00325 00326 if(_parser->send("AT+CWDHCP_CUR=%d,%d", mode_dhcp, en_dhcp) && _parser->recv("OK")) 00327 { 00328 pc.printf("Set DHCP : %d, %d\r\n", mode_dhcp, en_dhcp); 00329 00330 ret = RET_OK; 00331 } 00332 else 00333 { 00334 pc.printf("Set DHCP : ERROR\r\n"); 00335 } 00336 00337 return ret; 00338 } 00339 00340 int8_t deviceConnAP_WizFi360(int mode, char *ssid, char *password) 00341 { 00342 int8_t ret = RET_NOK; 00343 bool done = false; 00344 Timer t; 00345 00346 if((mode == STATION) || (mode == STATION_SOFTAP)) 00347 { 00348 _parser->send("AT+CWJAP_CUR=\"%s\",\"%s\"", ssid ,password); 00349 00350 t.start(); 00351 00352 while(done != true) 00353 { 00354 done = (_parser->recv("WIFI CONNECTED") && _parser->recv("WIFI GOT IP") && _parser->recv("OK")); 00355 00356 if(t.read_ms() >= (WIZFI360_DEFAULT_TIMEOUT * 20)) 00357 { 00358 t.stop(); 00359 t.reset(); 00360 00361 break; 00362 } 00363 } 00364 00365 if(done) 00366 { 00367 pc.printf("Connect AP : %s, %s\r\n", ssid, password); 00368 00369 ret = RET_OK; 00370 } 00371 else 00372 { 00373 pc.printf("Connect AP : ERROR\r\n"); 00374 } 00375 } 00376 else 00377 { 00378 pc.printf("Connect AP : ERROR\r\n"); 00379 } 00380 00381 return ret; 00382 } 00383 00384 // ---------------------------------------------------------------------------------------------------- 00385 // Functions : Azure 00386 // ---------------------------------------------------------------------------------------------------- 00387 00388 int8_t deviceSetAzureConf_WizFi360(char *iot_hub, char *device_id, char *sas_token) 00389 { 00390 int8_t ret = RET_NOK; 00391 00392 if(_parser->send("AT+AZSET=\"%s\",\"%s\",\"%s\"", iot_hub, device_id, sas_token) && _parser->recv("OK")) 00393 { 00394 pc.printf("Azure Set configuration : %s, %s, %s\r\n", iot_hub, device_id, sas_token); 00395 00396 ret = RET_OK; 00397 } 00398 else 00399 { 00400 pc.printf("Azure Set configuration : ERROR\r\n"); 00401 } 00402 00403 return ret; 00404 00405 } 00406 00407 00408 00409 int8_t deviceSetTopic_WizFi360(char *device_id) 00410 { 00411 int8_t ret = RET_NOK; 00412 00413 if(_parser->send("AT+MQTTTOPIC=\"devices/%s/messages/events/\",\"devices/%s/messages/devicebound/#\"", device_id, device_id) && _parser->recv("OK")) 00414 { 00415 pc.printf("Set topic : %s\r\n", device_id); 00416 00417 ret = RET_OK; 00418 } 00419 else 00420 { 00421 pc.printf("Set topic : ERROR\r\n"); 00422 } 00423 00424 return ret; 00425 } 00426 00427 int8_t deviceAzureCon_WizFi360() 00428 { 00429 int8_t ret = RET_NOK; 00430 bool done = false; 00431 Timer t; 00432 00433 _parser->send("AT+AZCON"); 00434 00435 t.start(); 00436 00437 while(done != true) 00438 { 00439 done = (_parser->recv("CONNECT") && _parser->recv("OK")); 00440 00441 if(t.read_ms() >= (WIZFI360_DEFAULT_TIMEOUT * 3)) 00442 { 00443 t.stop(); 00444 t.reset(); 00445 00446 break; 00447 } 00448 } 00449 00450 if(done) 00451 { 00452 pc.printf("Connected to Azure successfully\r\n"); 00453 00454 ret = RET_OK; 00455 } 00456 else 00457 { 00458 pc.printf("Connection to Azure failed: ERROR\r\n"); 00459 } 00460 00461 return ret; 00462 } 00463 00464 int8_t devicePublishMsg_WizFi360(char *device_id, sensor *sensor_info) 00465 { 00466 int8_t ret = RET_NOK; 00467 00468 if(_parser->send("AT+MQTTPUB=\"{\"deviceId\":\"%s\",\"temperature\":%d,\"humidity\":%d}\"",device_id, sensor_info->cel, sensor_info->hum)) 00469 { 00470 pc.printf("Publish message : %d, %d\r\n", sensor_info->cel, sensor_info->hum); 00471 } 00472 00473 return ret; 00474 }
Generated on Mon Jul 25 2022 09:00:29 by 1.7.2