Example of AWS IoT connection and Web Dashboard thru STM32 Nucleo evaluation board and mbed OS.
Dependencies: X_NUCLEO_IKS01A1 mbed FP MQTTPacket DnsQuery ATParser
Introduction
The demo is aimed to STM32 Nucleo board with WiFi and sensors expansions. The board is a "thing" for the AWS IoT service. It updates IoT service shadow with sensors data every second and checks subscription messages.
Hardware Configuration
- NUCLEO-F401RE - Nucleo Development Board
- X-NUCLEO-IDW01M1 - Wi-Fi expansion board for STM32 Nucleo
- X-NUCLEO-IKS01A1 - Motion MEMS and environmental sensor expansion board for STM32 Nucleo
Software Configuration
- Import this Project to mbed online compiler
- Find the next part of code in main.cpp file ...
WiFi network credential
#include "mbed.h" // WiFi network credential #define SSID "" // Network must be visible otherwise it can't connect #define PASSW "" #error "Wifi SSID & password empty"
- ... And set it to your Network Name and Password. Do not forget to remove "#error" pragma line.
Information
Nucleo WiFi module is not the same as your smartphone or laptope - it is based on demo board. To avoid connection problems:
- Place Nucleo as close to WiFi hot spot as possible. Or...
- Turn on mobile hot spot in your laptop as close to the device as possible.
- Make sure that hot spot permits 2.4 GHz band communications
- Setup BackEnd and store certificates using this backend setup instruction
- Find AWS_IOT_MQTT_HOST define and change it to HTTPS point mentioned in your AWS IoT thing properties named "interact"
#define AWS_IOT_MQTT_HOST "xxxxxxxxxx.iot.us-east-1.amazonaws.com" //Use your own host.
- Find the certificate defines clientCRT and clientKey in main.cpp file and change it to ones provided by Amazon.
/********************************************************************************************** *********************************************************************************************** Device Identity Certificates: Modify for your AWS IoT Thing *********************************************************************************************** ***********************************************************************************************/ /**************************************** (somecode)-certificate.pem.crt - Amazon signed PEM sertificate. *****************************************/ //This Client cert is example. Use own instead. const uint8_t clientCRT[] = "\ -----BEGIN CERTIFICATE-----\n\ MIIDBjCCAe6gAwIBAgIUVph856omeIxW3UPioq+UrX1DbwowDQYJKoZIhvcNAQEL\ BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g\ SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTE3MDUyNTExNTEy\ OVoXDTQ5MTIzMTIzNTk1OVowgZUxCzAJBgNVBAYTAkJZMQ4wDAYDVQQIDAVNaW5z\ azEOMAwGA1UEBwwFTWluc2sxFzAVBgNVBAoMDktsaWthLVRlY2ggTExDMRcwFQYD\ VQQLDA5LbGlrYS1UZWNoIExMQzEMMAoGA1UEAwwDUm5EMSYwJAYJKoZIhvcNAQkB\ FhdtdmF0YWxldUBrbGlrYS10ZWNoLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEH\ A0IABCJgOQJmoTBJVPfli9Hm/JVixaxkY5rtlgrYO3hSl633A2hg0P/ue0wXDbF3\ aQ0X57IRFE4k4FEbr3UXjT/IczKjYDBeMB8GA1UdIwQYMBaAFK3YzTUPlYB2Li75\ i/z8rEogr1d6MB0GA1UdDgQWBBT18HXBaXFJuAR/0SwegnxJ+pyJ6TAMBgNVHRMB\ Af8EAjAAMA4GA1UdDwEB/wQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAb0Ux1aH5\ RLxjrfGqXN6rPVqh8QQRS+AyBfzmaQN8HaPZMkX5WxXLvcn0A3uWlwQxPPkcZ4zf\ 51GHtFFQWB4YZ8dx8mUQ0v/j7onHjCJgZ8iDgwOyKMGtnsDZWCakQw+a6cj+NrMZ\ tzhjwCzEEP6ePcbXwErI5OOzLuWns2L/JEr2wWNkokgRuS8ewr/SQ9OLWIWa2rFM\ ahPNTb3y/qBeWdjeJmhI+TOxdqIpsF8roWP25zwo/zkzCHCjXFBrL+0CA4MpxIl9\ x02i7aAhlJ6ys80lDxdeWeeQJXRKkGknP8mcmKn3iEqqJ5s1dQePj2b5d3ldatya\ wsxQBqqZXzIWEw==\ \n\ -----END CERTIFICATE-----\n"; /********************************************************************************************** *********************************************************************************************** Private Key: Modify for your AWS IoT Thing *********************************************************************************************** ***********************************************************************************************/ /********************************************************************8**************************************** nucleo.key.pem - client key generated according to readme. **************************************************************************************************************/ //This Client Key is example. Use own instead. const uint8_t clientKey[] ="\ -----BEGIN EC PARAMETERS-----\n\ BggqhkjOPQMBBw==\ -----END EC PARAMETERS-----\n\ -----BEGIN EC PRIVATE KEY-----\n\ MHcCAQEEIHPRfWSC8/k/BsqDWKuP15dXsI9fGwpkTIsLZe6mIrAAoAoGCCqGSM49\ AwEHoUQDQgAEImA5AmahMElU9+WL0eb8lWLFrGRjmu2WCtg7eFKXrfcDaGDQ/+57\ TBcNsXdpDRfnshEUTiTgURuvdReNP8hzMg==\ -----END EC PRIVATE KEY-----\n";
Build and Check
- Plugin your board to USB of your PC. USB Disk Drive and USB COM Port should appear in your system.
- Open any Serial Console, connect it to your USB Serial Port and setup speed equal to 115200.
- Compile this Project and save .bin file to USB Disk Drive
- After board reset you should see next log in serial console:
X-NUCLEO-IDW01M1 mbed Application connecting to AP LOG: int main() L#361 Connected to WiFI. LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#186 ===================================== LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#187 Connecting WiFi. LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#188 Nucleo IP ADDRESS: X.X.X.X LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#189 Nucleo MAC ADDRESS: 00:11:22:33:44:55 LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#190 Server Hostname: xxxxxxxx.iot.us-east-1.amazonaws.com port: 8883 LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#191 Client ID: Nucleo LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#194 ===================================== LOG: int MQTTSocket::getNTPtime(int) L#58 Success receiving time from ntp server. Tick from 1 Jan 1970 is equal to 1505399292. --->TCP Connected --->MQTT Connected --->>>MQTT subscribed to: Nucleo/test Length - 245, Publishing {"state": {"reported": {"temperature": 23.690001, "humidity": 98.190002, "pressure": 982.869141, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.420000, -2.660000, 1.750000], "magnetometer": [-3.600000, -7.100000, 53.300000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.660000, "humidity": 98.010002, "pressure": 982.770264, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.770000, -2.310000, 1.470000], "magnetometer": [-3.100000, -8.300000, 54.200000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.670000, "humidity": 98.129997, "pressure": 982.724121, "accelerometer": [-0.008000, 0.029000, 0.971000], "gyroscope": [0.630000, -2.380000, 1.400000], "magnetometer": [-3.100000, -7.900000, 53.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.690001, "humidity": 98.019997, "pressure": 982.840088, "accelerometer": [-0.009000, 0.030000, 0.972000], "gyroscope": [0.700000, -2.450000, 1.540000], "magnetometer": [-3.700000, -7.900000, 53.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.709999, "humidity": 98.040001, "pressure": 982.828613, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.630000, -2.520000, 1.470000], "magnetometer": [-2.900000, -7.400000, 52.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.719999, "humidity": 97.860001, "pressure": 982.917236, "accelerometer": [-0.026000, 0.103000, 0.891000], "gyroscope": [1.050000, -2.310000, 1.260000], "magnetometer": [-3.300000, -7.100000, 53.500000]}}}
Information
Device connection state might be checked by Green Led on the board. Green light means that device is connected and transferring data to cloud.
- Configure and start your dashboard using instruction and corresponding sources from github
- Use Blue button to set up markers to charts.
- Use AWS IoT console MQTT Client to test device subscription to "Nucleo/test". Just publish any message to this topic and serial port output.
- PROFIT!
Diff: X_NUCLEO_IDW01M1v2/SPWF01SA/SPWFSA01.cpp
- Revision:
- 0:4cdaf9b1e7d0
- Child:
- 1:042ca9148926
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X_NUCLEO_IDW01M1v2/SPWF01SA/SPWFSA01.cpp Wed Sep 27 14:40:52 2017 +0300 @@ -0,0 +1,492 @@ +/* SPWFInterface Example + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SPWFSA01.h" +#include "mbed_debug.h" +#include "stdint.h" + +#define SPWFSA01_CONNECT_TIMEOUT 15000 +#define SPWFSA01_SEND_TIMEOUT 500 +#define SPWFSA01_RECV_TIMEOUT 1500//some commands like AT&F/W takes some time to get the result back! +#define SPWFSA01_MISC_TIMEOUT 500 +#define SPWFSA01_SOCKQ_TIMEOUT 3000 + +#define EPOCH_TIME 1453727657//Human time (GMT): Mon, 25 Jan 2016 13:14:17 GMT + + SPWFSA01::SPWFSA01(PinName tx, PinName rx, PinName reset, PinName wakeup, bool debug) + : _serial(tx, rx, 1024), _parser(_serial,"\r\n", 2048), + _wakeup(wakeup, PIN_OUTPUT, PullNone, 0), + _reset(reset, PIN_OUTPUT, PullNone, 1), + dbg_on(debug) +{ + _serial.baud(115200); // LICIO FIXME increase the speed + _parser.debugOn(debug); +} + +bool SPWFSA01::startup(int mode) +{ + _parser.setTimeout(SPWFSA01_MISC_TIMEOUT); + /*Test module before reset*/ + waitSPWFReady(); + /*Reset module*/ + reset(); + + /*set local echo to 0*/ + if(!(_parser.send("AT+S.SCFG=localecho1,%d\r", 0) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error local echo set\r\n"); + return false; + } + /*reset factory settings*/ + if(!(_parser.send("AT&F") && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error AT&F\r\n"); + return false; + } + + /*set Wi-Fi mode and rate to b/g*/ + if(!(_parser.send("AT+S.SCFG=wifi_ht_mode,%d\r", 0) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error setting ht_mode\r\n"); + return false; + } + + //if(!(_parser.send("AT+S.SCFG=wifi_opr_rate_mask,0x003FFFCF\r") && _parser.recv("OK"))) + if(!(_parser.send("AT+S.SCFG=wifi_opr_rate_mask,0x00000001\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting operational rates\r\n"); + return false; + } + + if(!(_parser.send("AT+S.SCFG=wifi_bas_rate_mask,0x0000000F\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting basic rates\r\n"); + return false; + } + + if(!(_parser.send("AT+S.SCFG=wifi_powersave,0\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting power save mode\r\n"); + return false; + } + + if(!(_parser.send("AT+S.SCFG=sleep_enabled,0\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting sleep mode\r\n"); + return false; + } + + if(!(_parser.send("AT+S.SCFG=wifi_tx_power,18\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting transmit power\r\n"); + return false; + } + + if(!(_parser.send("AT+S.SCFG=ip_use_dhcp,1\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting dhcp on\r\n"); + return false; + } + + if(!(_parser.send("AT+S.SCFG=ip_dhcp_timeout,20\r") && _parser.recv("OK")))// set most effective speed for such kind of device + { + debug_if(dbg_on, "SPWF> error setting dhcp timeout\r\n"); + return false; + } + + /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/ + if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", mode) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error wifi mode set\r\n"); + return false; + } + + /* save current setting in flash */ + if(!(_parser.send("AT&W") && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error AT&W\r\n"); + return false; + } + + /*reset again and send AT command and check for result (AT->OK)*/ + reset(); + + return true; +} + +bool SPWFSA01::hw_reset(void) +{ + if (_reset.is_connected()) { + /* reset the pin PC12 */ + _reset.write(0); + wait_ms(200); + _reset.write(1); + wait_ms(100); + return 1; + } else { return 0; } +} + +bool SPWFSA01::reset(void) +{ + if(!_parser.send("AT+CFUN=1")) return false; + while(1) { + if (_parser.recv("+WIND:32:WiFi Hardware Started\r")) { + return true; + } + } +} + +void SPWFSA01::waitSPWFReady(void) +{ + //wait_ms(200); + while(1) + if(_parser.send("AT") && _parser.recv("OK")) + //till we get OK from AT command + //printf("\r\nwaiting for reset to complete..\n"); + return; + +} + +/* Security Mode + None = 0, + WEP = 1, + WPA_Personal = 2, +*/ +bool SPWFSA01::connect(const char *ap, const char *passPhrase, int securityMode) +{ + uint32_t n1, n2, n3, n4; + + _parser.setTimeout(SPWFSA01_CONNECT_TIMEOUT); + //AT+S.SCFG=wifi_wpa_psk_text,%s\r + if(!(_parser.send("AT+S.SCFG=wifi_wpa_psk_text,%s", passPhrase) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error pass set\r\n"); + return false; + } + //AT+S.SSIDTXT=%s\r + if(!(_parser.send("AT+S.SSIDTXT=%s", ap) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error ssid set\r\n"); + return false; + } + //AT+S.SCFG=wifi_priv_mode,%d\r + if(!(_parser.send("AT+S.SCFG=wifi_priv_mode,%d", securityMode) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error security mode set\r\n"); + return false; + } + //"AT+S.SCFG=wifi_mode,%d\r" + /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/ + if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 1) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error wifi mode set\r\n"); + return false; + } + //AT&W + /* save current setting in flash */ + if(!(_parser.send("AT&W") && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error AT&W\r\n"); + return false; + } + //reset module + reset(); + + while(1) + if((_parser.recv("+WIND:24:WiFi Up:%u.%u.%u.%u",&n1, &n2, &n3, &n4))) + { + break; + } + + return true; +} + +bool SPWFSA01::disconnect(void) +{ + //"AT+S.SCFG=wifi_mode,%d\r" + /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/ + if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 0) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error wifi mode set\r\n"); + return false; + } + //AT&W + /* save current setting in flash */ + if(!(_parser.send("AT&W") && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error AT&W\r\n"); + return false; + } + //reset module + reset(); + return true; +} + +bool SPWFSA01::dhcp(int mode) +{ + //only 3 valid modes + //0->off(ip_addr must be set by user), 1->on(auto set by AP), 2->on&customize(miniAP ip_addr can be set by user) + if(mode < 0 || mode > 2) { + return false; + } + + return _parser.send("AT+S.SCFG=ip_use_dhcp,%d\r", mode) + && _parser.recv("OK"); +} + + +const char *SPWFSA01::getIPAddress(void) +{ + uint32_t n1, n2, n3, n4; + + if (!(_parser.send("AT+S.STS=ip_ipaddr") + && _parser.recv("# ip_ipaddr = %u.%u.%u.%u", &n1, &n2, &n3, &n4) + && _parser.recv("OK"))) { + debug_if(dbg_on, "SPWF> getIPAddress error\r\n"); + return 0; + } + + sprintf((char*)_ip_buffer,"%u.%u.%u.%u", n1, n2, n3, n4); + + return _ip_buffer; +} + +const char *SPWFSA01::getMACAddress(void) +{ + uint32_t n1, n2, n3, n4, n5, n6; + + if (!(_parser.send("AT+S.GCFG=nv_wifi_macaddr") + && _parser.recv("# nv_wifi_macaddr = %x:%x:%x:%x:%x:%x", &n1, &n2, &n3, &n4, &n5, &n6) + && _parser.recv("OK"))) { + debug_if(dbg_on, "SPWF> getMACAddress error\r\n"); + return 0; + } + + sprintf((char*)_mac_buffer,"%02X:%02X:%02X:%02X:%02X:%02X", n1, n2, n3, n4, n5, n6); + + return _mac_buffer; +} + +bool SPWFSA01::isConnected(void) +{ + return getIPAddress() != 0; +} + +bool SPWFSA01::open(const char *type, int* id, const char* addr, int port) +{ + Timer timer; + timer.start(); + socket_closed = 0; + + if(!_parser.send("AT+S.SOCKON=%s,%d,%s,ind", addr, port, type)) + { + debug_if(dbg_on, "SPWF> error opening socket\r\n"); + return false; + } + + while(1) + { + if( _parser.recv(" ID: %d", id) + && _parser.recv("OK")) + break; + + if (timer.read_ms() > SPWFSA01_CONNECT_TIMEOUT) { + return false; + } + + //TODO:implement time-out functionality in case of no response + //if(timeout) return false; + //TODO: deal with errors like "ERROR: Failed to resolve name" + //TODO: deal with errors like "ERROR: Data mode not available" + } + + return true; +} + +bool SPWFSA01::send(int id, const void *data, uint32_t amount) +{ + char _buf[18]; + _parser.setTimeout(SPWFSA01_SEND_TIMEOUT); + + sprintf((char*)_buf,"AT+S.SOCKW=%d,%d\r", id, amount); + + //May take a second try if device is busy + for (unsigned i = 0; i < 2; i++) { + if (_parser.write((char*)_buf, strlen(_buf)) >=0 + && _parser.write((char*)data, (int)amount) >= 0 + && _parser.recv("OK")) { + return true; + } + } + return false; +} + + +int32_t SPWFSA01::recv(int id, void *data, uint32_t amount) +{ + uint32_t recv_amount=0; + int wind_id; + + if (socket_closed) { + socket_closed = 0; + return -3; + } + if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets) + && _parser.recv(" DATALEN: %u", &recv_amount) + && _parser.recv("OK"))) { + return -2; + } + if (recv_amount==0) { return -1; } + if(recv_amount > amount) + recv_amount = amount; + + int par_timeout = _parser.getTimeout(); + _parser.setTimeout(0); + + while(_parser.recv("+WIND:%d:", &wind_id)) { +// printf("Wind received: %d\n\r", wind_id); + if (wind_id == 58) { + socket_closed = 1; + _parser.flush(); + } + } + _parser.setTimeout(par_timeout); + + _parser.flush(); + if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))){ + return -2; + } + if(!((_parser.read((char*)data, recv_amount) >0) + && _parser.recv("OK"))) { + return -2; + } + return recv_amount; +} + +bool SPWFSA01::close(int id) +{ + uint32_t recv_amount=0; + void * data = NULL; + + _parser.setTimeout(SPWFSA01_MISC_TIMEOUT); + _parser.flush(); + /* socket flush */ + if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets) + && _parser.recv(" DATALEN: %u", &recv_amount) + && _parser.recv("OK"))) { + return -2; + } + if (recv_amount>0) { + data = malloc (recv_amount+4); + if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))) { + free (data); + return -2; + } + // printf ("--->>>Close flushing recv_amount: %d \n\r",recv_amount); + if(!((_parser.read((char*)data, recv_amount) >0) + && _parser.recv("OK"))) { + free (data); + return -2; + } + free (data); + } + + //May take a second try if device is busy or error is returned + for (unsigned i = 0; i < 2; i++) { + if (_parser.send("AT+S.SOCKC=%d", id) + && _parser.recv("OK")) { + socket_closed = 1; + return true; + } + else + { + if(_parser.recv("ERROR: Pending data")) { + debug_if(dbg_on, "SPWF> ERROR!!!!\r\n"); + return false; + } + } + //TODO: Deal with "ERROR: Pending data" (Closing a socket with pending data) + } + return false; +} + + +bool SPWFSA01::readable() +{ + return _serial.readable(); +} + +bool SPWFSA01::writeable() +{ + return _serial.writeable(); +} + +int SPWFSA01::setSocketClientSecurity(uint8_t* tls_mode, uint8_t* root_ca_server, uint8_t* client_cert, uint8_t* client_key, uint8_t* client_domain, uint32_t tls_epoch_time) +{ + int err = 0; + unsigned long epoch_time; + + if(!(_parser.send("AT+S.TLSCERT2=clean,all\r", 0) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> TLSCERT2 cleared\r\n"); + return -1; + } + + if(tls_epoch_time==0) + epoch_time = EPOCH_TIME; + else + epoch_time = tls_epoch_time; + + if(!(_parser.send("AT+S.SETTIME=%lu\r", (unsigned long)epoch_time) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> time is set to %d\r\n", epoch_time); + return -1; + } + + /*AT+S.TLSCERT=f_ca,<size><CR><data>*/ + if(!(_parser.send("AT+S.TLSCERT=f_ca,%d\r%s\r", strlen((const char *)root_ca_server) - 1, root_ca_server) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error TLSCERT=f_ca set\r\n"); + return -1; + } + + /*AT+S.TLSCERT=f_cert,<size><CR><data>*/ + if(tls_mode[0]=='m') + { + if(!(_parser.send("AT+S.TLSCERT=f_cert,%d\r%s", strlen((const char *)client_cert) - 1, client_cert) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error TLSCERT=f_cert set\r\n"); + return -1; + } + + /*AT+S.TLSCERT=f_key,<size><CR><data>*/ + if(!(_parser.send("AT+S.TLSCERT=f_key,%d\r%s", strlen((const char *)client_key) - 1, client_key) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error TLSCERT=f_key set\r\n"); + return -1; + } + } + + /*AT+S.TLSDOMAIN=f_domain,<server domain>*/ + if(!(_parser.send("AT+S.TLSDOMAIN=f_domain,%s\r", client_domain) && _parser.recv("OK"))) + { + debug_if(dbg_on, "SPWF> error TLSDOMAIN=f_domain set\r\n"); + return -1; + } + + return err; +} +