Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created February 27, 2020 16:43
Show Gist options
  • Select an option

  • Save benevpi/d23fc32da40b4ffbee1ac5deaf39649e to your computer and use it in GitHub Desktop.

Select an option

Save benevpi/d23fc32da40b4ffbee1ac5deaf39649e to your computer and use it in GitHub Desktop.
/*
Adapted from example file at https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFiClientSecure/examples/WiFiClientSecure/WiFiClientSecure.ino
by Evandro Copercini - Apache 2.0 License.
*/
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
// 16 servo objects can be created on the ESP32
int pos = 0; // variable to store the servo position
int servoPin = 13;
const char* ssid = "HH-1B"; // your network SSID (name of wifi network)
const char* password = "HaHoForward"; // your network password
//nox scale -- 0 =0 deg
// 40 = 180 deg (legal limit)
// == conversion = * 4.5
const char* server = "opendata.bristol.gov.uk"; // Server URL
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+yk13EiSdRxta67nsHjcAHJyse6cF6s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\n" \
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
"-----END CERTIFICATE-----\n";
WiFiClientSecure client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2500); // attaches the servo on pin 18 to the servo object
myservo.write(0);
}
void loop() {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
int timer = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
timer++;
if (timer > 120) { break; }
}
Serial.print("Connected to ");
Serial.println(ssid);
client.setCACert(test_root_ca);
Serial.println("\nStarting connection to server...");
if (!client.connect(server, 443))
Serial.println("Connection failed!");
else {
Serial.println("Connected to server!");
// Make a HTTP request:
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(server) + "\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
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 no2=json_doc["records"][0]["fields"]["no2"];
Serial.println(no2);
myservo.write(min(no2.toFloat()*4.5,180.0));
// Extract the data here
}
}
}
client.stop();
}
WiFi.disconnect(true);
delay(60000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment