Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:f1d3878b8dd9 1 /* mbed main.cpp to test a secure MQTT connection using MBED TLS library
vpcola 0:f1d3878b8dd9 2 * Copyright (c) 2017 Vergil Perez Cola
vpcola 0:f1d3878b8dd9 3 *
vpcola 0:f1d3878b8dd9 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
vpcola 0:f1d3878b8dd9 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
vpcola 0:f1d3878b8dd9 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
vpcola 0:f1d3878b8dd9 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
vpcola 0:f1d3878b8dd9 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
vpcola 0:f1d3878b8dd9 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
vpcola 0:f1d3878b8dd9 10 * THE SOFTWARE.
vpcola 0:f1d3878b8dd9 11 */
vpcola 0:f1d3878b8dd9 12
vpcola 0:f1d3878b8dd9 13 /* *
vpcola 0:f1d3878b8dd9 14 * @note This program is derived from the MQTT sample program.
vpcola 0:f1d3878b8dd9 15 */
vpcola 0:f1d3878b8dd9 16 /**
vpcola 0:f1d3878b8dd9 17 * @note To make this work with FRDM-K64F
vpcola 0:f1d3878b8dd9 18 * @note PTA0 must be disconnected from the swd clk by cutting J11.
vpcola 0:f1d3878b8dd9 19 * @note But to re-active SWD you need to put jumper header to J11
vpcola 0:f1d3878b8dd9 20 * @note so that it can be re-connected by a jumper.
vpcola 0:f1d3878b8dd9 21 */
vpcola 0:f1d3878b8dd9 22
vpcola 0:f1d3878b8dd9 23 #include "mbed.h"
vpcola 0:f1d3878b8dd9 24 #include "rtos.h"
vpcola 0:f1d3878b8dd9 25 #include "XbeeMonitor.h"
vpcola 0:f1d3878b8dd9 26 #include "easy-connect.h"
vpcola 0:f1d3878b8dd9 27 #include "NTPClient.h"
vpcola 0:f1d3878b8dd9 28 #include "SDBlockDevice.h"
vpcola 0:f1d3878b8dd9 29 #include "FATFileSystem.h"
vpcola 0:f1d3878b8dd9 30 #include "DownloadFile.h"
vpcola 0:f1d3878b8dd9 31 #include "MQTTSManager.h"
vpcola 0:f1d3878b8dd9 32 #include "WeatherInfo.h"
vpcola 0:f1d3878b8dd9 33 #include "RadioCfg.h"
vpcola 0:f1d3878b8dd9 34 #include "config.h"
vpcola 0:f1d3878b8dd9 35
vpcola 0:f1d3878b8dd9 36 #include <string>
vpcola 0:f1d3878b8dd9 37 #include <sstream>
vpcola 0:f1d3878b8dd9 38
vpcola 0:f1d3878b8dd9 39 #if defined(ENABLE_LOGGING)
vpcola 0:f1d3878b8dd9 40 #include "DigiLoggerMbedSerial.h"
vpcola 0:f1d3878b8dd9 41 using namespace DigiLog;
vpcola 0:f1d3878b8dd9 42 #endif
vpcola 0:f1d3878b8dd9 43
vpcola 0:f1d3878b8dd9 44 #if defined(TARGET_NUCLEO_L476RG)
vpcola 0:f1d3878b8dd9 45 //SDBlockDevice sd(D11, D12, D13, D9);
vpcola 0:f1d3878b8dd9 46 #else
vpcola 0:f1d3878b8dd9 47 SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
vpcola 0:f1d3878b8dd9 48 #endif
vpcola 0:f1d3878b8dd9 49
vpcola 0:f1d3878b8dd9 50 #if !defined(TARGET_NUCLEO_L476RG)
vpcola 0:f1d3878b8dd9 51 FATFileSystem fs("sd");
vpcola 0:f1d3878b8dd9 52 #endif
vpcola 0:f1d3878b8dd9 53
vpcola 0:f1d3878b8dd9 54 Serial pc(USBTX, USBRX, 115200);
vpcola 0:f1d3878b8dd9 55
vpcola 0:f1d3878b8dd9 56 Thread xbeeThd;
vpcola 0:f1d3878b8dd9 57
vpcola 0:f1d3878b8dd9 58 #if !defined(TARGET_NUCLEO_L476RG)
vpcola 0:f1d3878b8dd9 59 static const char * radioconfigfile = "/sd/radioconfig.json";
vpcola 0:f1d3878b8dd9 60 #endif
vpcola 0:f1d3878b8dd9 61
vpcola 0:f1d3878b8dd9 62 /* List of trusted root CA certificates
vpcola 0:f1d3878b8dd9 63 * currently only "letsencrypt", the CA for mbedhacks.com
vpcola 0:f1d3878b8dd9 64 *
vpcola 0:f1d3878b8dd9 65 * To add more than one root, just concatenate them.
vpcola 0:f1d3878b8dd9 66 *
vpcola 0:f1d3878b8dd9 67 * TODO: Move this certificate file onto the SD card.
vpcola 0:f1d3878b8dd9 68 */
vpcola 0:f1d3878b8dd9 69 static const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
vpcola 0:f1d3878b8dd9 70 "MIIFETCCA/mgAwIBAgISA2ktlb1Y6ap4GCH7dg3wS37XMA0GCSqGSIb3DQEBCwUA\n"
vpcola 0:f1d3878b8dd9 71 "MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD\n"
vpcola 0:f1d3878b8dd9 72 "ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xNzAzMDkwMTQ4MDBaFw0x\n"
vpcola 0:f1d3878b8dd9 73 "NzA2MDcwMTQ4MDBaMBgxFjAUBgNVBAMTDW1iZWRoYWNrcy5jb20wggEiMA0GCSqG\n"
vpcola 0:f1d3878b8dd9 74 "SIb3DQEBAQUAA4IBDwAwggEKAoIBAQC4ppYHlH8lfB7lkWOjMSnOJGaLtCBfz57I\n"
vpcola 0:f1d3878b8dd9 75 "VVOd1Rngsz7nE5fg3joa7lkazRY1ZqtuC2UloS+4LYoQZX4Z887dhdug/TPA4J1A\n"
vpcola 0:f1d3878b8dd9 76 "GppA4xVCb2kUFODMjZ2r4pMLp+MjFFMBaHrL4cgx/n4aJUB+N9Z+HW0p2Yr5TsOQ\n"
vpcola 0:f1d3878b8dd9 77 "ghIOPkNxFr2q6klm49+BMUbO98hAwFwsIISLf6IbHM93gx1ltqkvb55N87ZM1hYH\n"
vpcola 0:f1d3878b8dd9 78 "fkq+J+YqjleiLaqRN2MVlNMNfy9MDbqM5uCyGiWGtq8eiQLaWpZkxnA2MC5zPsO/\n"
vpcola 0:f1d3878b8dd9 79 "fzEWiVjn2uazlXZ5xZwiK22KMxVasqWMitvETtmPOl9mocRbLQdxAgMBAAGjggIh\n"
vpcola 0:f1d3878b8dd9 80 "MIICHTAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUF\n"
vpcola 0:f1d3878b8dd9 81 "BwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFCsgG+z1BTjrN3K+/tF0C4k818Yv\n"
vpcola 0:f1d3878b8dd9 82 "MB8GA1UdIwQYMBaAFKhKamMEfd265tE5t6ZFZe/zqOyhMHAGCCsGAQUFBwEBBGQw\n"
vpcola 0:f1d3878b8dd9 83 "YjAvBggrBgEFBQcwAYYjaHR0cDovL29jc3AuaW50LXgzLmxldHNlbmNyeXB0Lm9y\n"
vpcola 0:f1d3878b8dd9 84 "Zy8wLwYIKwYBBQUHMAKGI2h0dHA6Ly9jZXJ0LmludC14My5sZXRzZW5jcnlwdC5v\n"
vpcola 0:f1d3878b8dd9 85 "cmcvMCsGA1UdEQQkMCKCDW1iZWRoYWNrcy5jb22CEXd3dy5tYmVkaGFja3MuY29t\n"
vpcola 0:f1d3878b8dd9 86 "MIH+BgNVHSAEgfYwgfMwCAYGZ4EMAQIBMIHmBgsrBgEEAYLfEwEBATCB1jAmBggr\n"
vpcola 0:f1d3878b8dd9 87 "BgEFBQcCARYaaHR0cDovL2Nwcy5sZXRzZW5jcnlwdC5vcmcwgasGCCsGAQUFBwIC\n"
vpcola 0:f1d3878b8dd9 88 "MIGeDIGbVGhpcyBDZXJ0aWZpY2F0ZSBtYXkgb25seSBiZSByZWxpZWQgdXBvbiBi\n"
vpcola 0:f1d3878b8dd9 89 "eSBSZWx5aW5nIFBhcnRpZXMgYW5kIG9ubHkgaW4gYWNjb3JkYW5jZSB3aXRoIHRo\n"
vpcola 0:f1d3878b8dd9 90 "ZSBDZXJ0aWZpY2F0ZSBQb2xpY3kgZm91bmQgYXQgaHR0cHM6Ly9sZXRzZW5jcnlw\n"
vpcola 0:f1d3878b8dd9 91 "dC5vcmcvcmVwb3NpdG9yeS8wDQYJKoZIhvcNAQELBQADggEBABFH6YcvHh8foHeg\n"
vpcola 0:f1d3878b8dd9 92 "NM7iR9HnYRqa5gSERcCtq6jm8PcTsAbsdQ/BNpIHK7AZSg2kk17kj+JFeyMuNJWq\n"
vpcola 0:f1d3878b8dd9 93 "lmabV0dtzdC8ejp1d7hGb/HjuQ400th/QRayvyrDVzQPfCNyJ0C82Q2DFjeUgnqv\n"
vpcola 0:f1d3878b8dd9 94 "oJMcV6i4ICW0boI7GUf7oeHCmrUEHKffAbeFvx3c85c39IHJEFa59UWj1linU/Tr\n"
vpcola 0:f1d3878b8dd9 95 "g9i5AaSKB95d706u1XRA7WLV/Hu7yunhxEjlj33bfdifBb/ZLBd0LtrXPwtXi6E8\n"
vpcola 0:f1d3878b8dd9 96 "r6obp+B+Ce89G7WEhdT9BX0ck1KTK+yP7uAC7tvvsiejxXOoCtVyBAumBJS7mRuv\n"
vpcola 0:f1d3878b8dd9 97 "I5hmKgE=\n"
vpcola 0:f1d3878b8dd9 98 "-----END CERTIFICATE-----\n"
vpcola 0:f1d3878b8dd9 99 "-----BEGIN CERTIFICATE-----\n"
vpcola 0:f1d3878b8dd9 100 "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"
vpcola 0:f1d3878b8dd9 101 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
vpcola 0:f1d3878b8dd9 102 "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n"
vpcola 0:f1d3878b8dd9 103 "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n"
vpcola 0:f1d3878b8dd9 104 "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"
vpcola 0:f1d3878b8dd9 105 "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n"
vpcola 0:f1d3878b8dd9 106 "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n"
vpcola 0:f1d3878b8dd9 107 "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n"
vpcola 0:f1d3878b8dd9 108 "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n"
vpcola 0:f1d3878b8dd9 109 "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n"
vpcola 0:f1d3878b8dd9 110 "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n"
vpcola 0:f1d3878b8dd9 111 "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n"
vpcola 0:f1d3878b8dd9 112 "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n"
vpcola 0:f1d3878b8dd9 113 "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n"
vpcola 0:f1d3878b8dd9 114 "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n"
vpcola 0:f1d3878b8dd9 115 "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n"
vpcola 0:f1d3878b8dd9 116 "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"
vpcola 0:f1d3878b8dd9 117 "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
vpcola 0:f1d3878b8dd9 118 "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
vpcola 0:f1d3878b8dd9 119 "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
vpcola 0:f1d3878b8dd9 120 "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
vpcola 0:f1d3878b8dd9 121 "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
vpcola 0:f1d3878b8dd9 122 "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
vpcola 0:f1d3878b8dd9 123 "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
vpcola 0:f1d3878b8dd9 124 "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
vpcola 0:f1d3878b8dd9 125 "-----END CERTIFICATE-----";
vpcola 0:f1d3878b8dd9 126
vpcola 0:f1d3878b8dd9 127 const char * openweathermap_id = "1bfddd34faa0e0216769d688b0b0c743";
vpcola 0:f1d3878b8dd9 128 float lastTemperature;
vpcola 0:f1d3878b8dd9 129
vpcola 0:f1d3878b8dd9 130 #define BASIC_AUTH_USER "tinong"
vpcola 0:f1d3878b8dd9 131 #define BASIC_AUTH_PASSWORD "tatay"
vpcola 0:f1d3878b8dd9 132
vpcola 0:f1d3878b8dd9 133 template <class T>
vpcola 0:f1d3878b8dd9 134 std::string to_string (const T& t)
vpcola 0:f1d3878b8dd9 135 {
vpcola 0:f1d3878b8dd9 136 std::stringstream ss;
vpcola 0:f1d3878b8dd9 137 ss << t;
vpcola 0:f1d3878b8dd9 138 return ss.str();
vpcola 0:f1d3878b8dd9 139 }
vpcola 0:f1d3878b8dd9 140
vpcola 0:f1d3878b8dd9 141
vpcola 0:f1d3878b8dd9 142 void UpdateWeatherInfo(NetworkInterface* network)
vpcola 0:f1d3878b8dd9 143 {
vpcola 0:f1d3878b8dd9 144 printf("Downloading weather information ...\r\n");
vpcola 0:f1d3878b8dd9 145 DownloadFile * wInfo = new DownloadFile(network);
vpcola 0:f1d3878b8dd9 146 if (wInfo)
vpcola 0:f1d3878b8dd9 147 {
vpcola 0:f1d3878b8dd9 148 std::string url = "http://api.openweathermap.org/data/2.5/weather?q=Singapore,sg&appid=";
vpcola 0:f1d3878b8dd9 149 url += openweathermap_id;
vpcola 0:f1d3878b8dd9 150 printf("Getting weather from [%s]\r\n", url.c_str());
vpcola 0:f1d3878b8dd9 151 HttpResponse* result = wInfo->get_file(url.c_str());
vpcola 0:f1d3878b8dd9 152 if (result != NULL)
vpcola 0:f1d3878b8dd9 153 {
vpcola 0:f1d3878b8dd9 154
vpcola 0:f1d3878b8dd9 155 std::string content = result->get_body_as_string();
vpcola 0:f1d3878b8dd9 156
vpcola 0:f1d3878b8dd9 157 printf("\r\n========= Weather Info =============\r\n");
vpcola 0:f1d3878b8dd9 158 printf("%s\r\n", content.c_str());
vpcola 0:f1d3878b8dd9 159 printf("==============================\r\n");
vpcola 0:f1d3878b8dd9 160
vpcola 0:f1d3878b8dd9 161 const char * weather = content.c_str();
vpcola 0:f1d3878b8dd9 162 WeatherInfo info;
vpcola 0:f1d3878b8dd9 163 if ( parseweatherinfo(weather, info) == 0)
vpcola 0:f1d3878b8dd9 164 {
vpcola 0:f1d3878b8dd9 165 printf("Weather : %s\r\n", info.weather.c_str());
vpcola 0:f1d3878b8dd9 166 printf("Weather Icon : %s\r\n", info.icon.c_str());
vpcola 0:f1d3878b8dd9 167 printf("Current Temperature : %.2f\r\n", info.temperature - 273.15);
vpcola 0:f1d3878b8dd9 168 printf("Current Pressure : %.2f\r\n", info.pressure);
vpcola 0:f1d3878b8dd9 169 printf("Current Humidity : %.2f\r\n", info.humidity);
vpcola 0:f1d3878b8dd9 170
vpcola 0:f1d3878b8dd9 171 // TODO: process the information and send updates
vpcola 0:f1d3878b8dd9 172 // to GUI
vpcola 0:f1d3878b8dd9 173 lastTemperature = info.temperature - 273.15;
vpcola 0:f1d3878b8dd9 174 }
vpcola 0:f1d3878b8dd9 175 }
vpcola 0:f1d3878b8dd9 176
vpcola 0:f1d3878b8dd9 177 delete wInfo;
vpcola 0:f1d3878b8dd9 178 }
vpcola 0:f1d3878b8dd9 179 }
vpcola 0:f1d3878b8dd9 180
vpcola 0:f1d3878b8dd9 181 int main()
vpcola 0:f1d3878b8dd9 182 {
vpcola 0:f1d3878b8dd9 183 uint64_t radioID;
vpcola 0:f1d3878b8dd9 184 int lasthour = 0;
vpcola 0:f1d3878b8dd9 185 RadioCfg cfg;
vpcola 0:f1d3878b8dd9 186
vpcola 0:f1d3878b8dd9 187 #if !defined(TARGET_NUCLEO_L476RG)
vpcola 0:f1d3878b8dd9 188 // Mount the SD card first!
vpcola 0:f1d3878b8dd9 189 printf("Mounting the filesystem on \"/sd\". ");
vpcola 0:f1d3878b8dd9 190 fs.mount(&sd);
vpcola 0:f1d3878b8dd9 191 #endif
vpcola 0:f1d3878b8dd9 192
vpcola 0:f1d3878b8dd9 193 #if defined(ENABLE_LOGGING)
vpcola 0:f1d3878b8dd9 194 new DigiLoggerMbedSerial(&pc, LogLevelInfo);
vpcola 0:f1d3878b8dd9 195 #endif
vpcola 0:f1d3878b8dd9 196 pc.printf("Initializing XbeeMonitor ...\r\n");
vpcola 0:f1d3878b8dd9 197 initXbeeMonitor();
vpcola 0:f1d3878b8dd9 198 // TODO: get XBee radio id
vpcola 0:f1d3878b8dd9 199 // and request configuration settings
vpcola 0:f1d3878b8dd9 200 // from the web.
vpcola 0:f1d3878b8dd9 201 radioID = getXbeeId();
vpcola 0:f1d3878b8dd9 202 cfg.radioID = radioID;
vpcola 0:f1d3878b8dd9 203 pc.printf("Radio ID : %llu\r\n", radioID);
vpcola 0:f1d3878b8dd9 204
vpcola 0:f1d3878b8dd9 205 pc.printf("Connecting to ethernet ...\r\n");
vpcola 0:f1d3878b8dd9 206 // Get the network interface via
vpcola 0:f1d3878b8dd9 207 // easy connect
vpcola 0:f1d3878b8dd9 208 NetworkInterface* network = easy_connect(true);
vpcola 0:f1d3878b8dd9 209 if (!network) {
vpcola 0:f1d3878b8dd9 210 return 1;
vpcola 0:f1d3878b8dd9 211 }
vpcola 0:f1d3878b8dd9 212
vpcola 0:f1d3878b8dd9 213 pc.printf("IP Address : %s\r\n", network->get_ip_address());
vpcola 0:f1d3878b8dd9 214 pc.printf("MAC Address : %s\r\n", network->get_mac_address());
vpcola 0:f1d3878b8dd9 215
vpcola 0:f1d3878b8dd9 216 time_t ctTime;
vpcola 0:f1d3878b8dd9 217 ctTime = time(NULL);
vpcola 0:f1d3878b8dd9 218
vpcola 0:f1d3878b8dd9 219 printf("Getting coordinator info from the internet!...\r\n");
vpcola 0:f1d3878b8dd9 220 #if !defined(TARGET_NUCLEO_L476RG)
vpcola 0:f1d3878b8dd9 221 DownloadFile * df = new DownloadFile(network, radioconfigfile, SSL_CA_PEM );
vpcola 0:f1d3878b8dd9 222 #else
vpcola 0:f1d3878b8dd9 223 DownloadFile * df = new DownloadFile(network, NULL, SSL_CA_PEM );
vpcola 0:f1d3878b8dd9 224 #endif
vpcola 0:f1d3878b8dd9 225 if (df)
vpcola 0:f1d3878b8dd9 226 {
vpcola 0:f1d3878b8dd9 227 std::string url = "https://www.mbedhacks.com/Garden/getconfig.php?id=";
vpcola 0:f1d3878b8dd9 228 url += to_string(radioID);
vpcola 0:f1d3878b8dd9 229 printf("Getting config from [%s]\r\n", url.c_str());
vpcola 0:f1d3878b8dd9 230 df->basic_auth(BASIC_AUTH_USER, BASIC_AUTH_PASSWORD);
vpcola 0:f1d3878b8dd9 231 HttpResponse* result = df->get_file(url.c_str());
vpcola 0:f1d3878b8dd9 232 if (result != NULL)
vpcola 0:f1d3878b8dd9 233 {
vpcola 0:f1d3878b8dd9 234 #if defined(TARGET_NUCLEO_L476RG)
vpcola 0:f1d3878b8dd9 235 std::string content = result->get_body_as_string();;
vpcola 0:f1d3878b8dd9 236 #else
vpcola 0:f1d3878b8dd9 237 std::string content = df->get_file_content();
vpcola 0:f1d3878b8dd9 238 #endif
vpcola 0:f1d3878b8dd9 239 printf("\r\n==========================\r\n");
vpcola 0:f1d3878b8dd9 240 printf("%s\r\n", content.c_str());
vpcola 0:f1d3878b8dd9 241 printf("==============================\r\n");
vpcola 0:f1d3878b8dd9 242
vpcola 0:f1d3878b8dd9 243 // TODO: pass the downloaded file to the RadioCfg
vpcola 0:f1d3878b8dd9 244 if (parseradioconfig(content.c_str(), cfg) == 0)
vpcola 0:f1d3878b8dd9 245 {
vpcola 0:f1d3878b8dd9 246 cfg.debug();
vpcola 0:f1d3878b8dd9 247 }
vpcola 0:f1d3878b8dd9 248 }
vpcola 0:f1d3878b8dd9 249 delete df;
vpcola 0:f1d3878b8dd9 250 }
vpcola 0:f1d3878b8dd9 251
vpcola 0:f1d3878b8dd9 252 NTPClient ntp(network, cfg.utc_offset);
vpcola 0:f1d3878b8dd9 253 pc.printf("Initial System Time is: %s\r\n", ctime(&ctTime));
vpcola 0:f1d3878b8dd9 254 pc.printf("Trying to update time...\r\n");
vpcola 0:f1d3878b8dd9 255 if (ntp.setTime("0.pool.ntp.org") == 0) {
vpcola 0:f1d3878b8dd9 256
vpcola 0:f1d3878b8dd9 257 pc.printf("Set time successfully\r\n");
vpcola 0:f1d3878b8dd9 258 pc.printf("Updated time is : %s\r\n", ctime(&ctTime));
vpcola 0:f1d3878b8dd9 259
vpcola 0:f1d3878b8dd9 260 pc.printf("Starting the Xbee manager thread ...\r\n");
vpcola 0:f1d3878b8dd9 261 xbeeThd.start(runXbeeMonitor);
vpcola 0:f1d3878b8dd9 262 pc.printf("Starting MQTTS subscriber thread ...\r\n");
vpcola 0:f1d3878b8dd9 263 // Initialize MQTT
vpcola 0:f1d3878b8dd9 264 mqttsInit(network, SSL_CA_PEM);
vpcola 0:f1d3878b8dd9 265 if ( runMQTTS() != 0 )
vpcola 0:f1d3878b8dd9 266 pc.printf("Failed launching MQTT thread ...\r\n");
vpcola 0:f1d3878b8dd9 267
vpcola 0:f1d3878b8dd9 268 while(true) {
vpcola 0:f1d3878b8dd9 269 time_t currentTime;
vpcola 0:f1d3878b8dd9 270 struct tm *localTime;
vpcola 0:f1d3878b8dd9 271
vpcola 0:f1d3878b8dd9 272 time( &currentTime ); // Get the current time
vpcola 0:f1d3878b8dd9 273 localTime = localtime( &currentTime ); // Convert the current time to local time
vpcola 0:f1d3878b8dd9 274
vpcola 0:f1d3878b8dd9 275 // TODO: Update the weather information every hour ...
vpcola 0:f1d3878b8dd9 276 if (lasthour != localTime->tm_hour )
vpcola 0:f1d3878b8dd9 277 //if (lasthour != localTime->tm_min )
vpcola 0:f1d3878b8dd9 278 {
vpcola 0:f1d3878b8dd9 279 printf("An hour has passed!!! ...\r\n");
vpcola 0:f1d3878b8dd9 280 UpdateWeatherInfo(network);
vpcola 0:f1d3878b8dd9 281 // Update ...
vpcola 0:f1d3878b8dd9 282 //lasthour = localTime->tm_min;
vpcola 0:f1d3878b8dd9 283 lasthour = localTime->tm_hour;
vpcola 0:f1d3878b8dd9 284 }
vpcola 0:f1d3878b8dd9 285
vpcola 0:f1d3878b8dd9 286 Thread::wait(10000);
vpcola 0:f1d3878b8dd9 287 }
vpcola 0:f1d3878b8dd9 288 }
vpcola 0:f1d3878b8dd9 289 else
vpcola 0:f1d3878b8dd9 290 {
vpcola 0:f1d3878b8dd9 291 pc.printf("Failed to setup time from NTP ...\r\n");
vpcola 0:f1d3878b8dd9 292 while(true)
vpcola 0:f1d3878b8dd9 293 {
vpcola 0:f1d3878b8dd9 294 Thread::wait(100);
vpcola 0:f1d3878b8dd9 295 }
vpcola 0:f1d3878b8dd9 296 }
vpcola 0:f1d3878b8dd9 297 }