Created
March 9, 2020 15:53
-
-
Save benevpi/b62786b89a367c746300efea0186d9e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ArduinoJson.h> | |
| #include <WiFiClientSecure.h> | |
| #define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE | |
| #include "esp_log.h" | |
| // WiFi network name and password: | |
| const char * networkName = "YOUR NETWORK SSID HERE"; | |
| const char * networkPswd = "YOUR PASSWORD HERE"; | |
| // Internet domain to request from: | |
| //const char * hostDomain = "opendata.bristol.gov.uk"; | |
| const char * hostDomain = "www.howsmyssl.com"; | |
| const int hostPort = 443; | |
| const char* test_root_ca= \ | |
| "-----BEGIN CERTIFICATE-----\n" \ | |
| "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \ | |
| "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \ | |
| "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \ | |
| "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \ | |
| "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \ | |
| "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \ | |
| "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \ | |
| "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \ | |
| "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2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| "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \ | |
| "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \ | |
| "-----END CERTIFICATE-----\n"; | |
| void setup() | |
| { | |
| // Initilize hardware: | |
| Serial.begin(115200); | |
| // Connect to the WiFi network (see function below loop) | |
| connectToWiFi(networkName, networkPswd); | |
| } | |
| void loop() | |
| { | |
| requestURL(hostDomain, hostPort); // Connect to server | |
| delay(100000); | |
| } | |
| void connectToWiFi(const char * ssid, const char * pwd) | |
| { | |
| printLine(); | |
| Serial.println("Connecting to WiFi network: " + String(ssid)); | |
| WiFi.begin(ssid, pwd); | |
| while (WiFi.status() != WL_CONNECTED) | |
| { | |
| Serial.print("."); | |
| } | |
| Serial.println(); | |
| Serial.println("WiFi connected!"); | |
| Serial.print("IP address: "); | |
| Serial.println(WiFi.localIP()); | |
| } | |
| void requestURL(const char * host, uint8_t port) | |
| { | |
| // printLine(); | |
| Serial.println("Connecting to domain: " + String(host)); | |
| WiFiClientSecure client; | |
| client.setCACert(test_root_ca); | |
| if (!client.connect(host, port)) | |
| { | |
| Serial.println("Connection failed!"); | |
| char err_buf[100]; | |
| if (client.lastError(err_buf, 100) < 0) { | |
| Serial.println(err_buf); | |
| } else { | |
| Serial.println("Connection error"); | |
| } | |
| } | |
| Serial.println("Connected!"); | |
| printLine(); | |
| Serial.print("heap: "); | |
| Serial.println(ESP.getFreeHeap()); | |
| // This will send the request to the server | |
| client.print((String)"GET /api/records/1.0/search/?dataset=air-quality-data-continuous&rows=1&sort=date_time&facet=date_time&facet=location&facet=current&refine.location=AURN+St+Pauls HTTP/1.1\r\n" + | |
| "Host: " + String(host) + "\r\n" + | |
| "Connection: close\r\n\r\n"); | |
| unsigned long timeout = millis(); | |
| while (client.available() == 0) | |
| { | |
| if (millis() - timeout > 5000) | |
| { | |
| Serial.println(">>> Client Timeout !"); | |
| client.stop(); | |
| return; | |
| } | |
| } | |
| Serial.println("here!"); | |
| String line = ""; | |
| while (client.available()) { | |
| line = client.readStringUntil('\r'); | |
| Serial.println(line); | |
| if (line.indexOf('{')>=0) { | |
| Serial.println("parsingValues"); | |
| DynamicJsonDocument json_doc(6000); | |
| DeserializationError json_error = deserializeJson(json_doc, line); | |
| if (json_error) { | |
| Serial.println("parseing failed"); | |
| Serial.println(json_error.c_str()); | |
| } | |
| else { | |
| Serial.println("Nox"); | |
| String nox=json_doc["records"][0]["fields"]["nox"]; | |
| Serial.println(nox); | |
| // Extract the data here | |
| } | |
| } | |
| } | |
| } | |
| void printLine() | |
| { | |
| Serial.println(); | |
| for (int i=0; i<30; i++) | |
| Serial.print("-"); | |
| Serial.println(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment