-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2gauge.ino
127 lines (99 loc) · 3.71 KB
/
CO2gauge.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* This sketch sends data via HTTP GET requests to $host.
*/
#if defined(ESP8266)
#pragma message "ESP8266 stuff happening!"
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#pragma message "ESP32 stuff happening!"
#include <WiFi.h>
#else
#error "This ain't a ESP8266 or ESP32.. NO WIFI FOR YOU!"
#endif
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
//ESP PWM RANGE: 1023
//GAUGE PWM RANGE: 0-540ish
const int GAUGE_PWM_MAX=540; //find your own
int gaugePin = D1;
const char* ssid = "top";
const char* password = "secret";
//https://api.co2signal.com/v1/latest?countryCode=DK-DK1 -H 'auth-token: XxXxXxXxXxXxXxXx'
const String host = "api.co2signal.com";
const String url = "/v1/latest?countryCode=DK-DK1";
const String authToken = "XxXxXxXxXxXxXxXx"
//json stuff:
//HAVE A LOOK AT https://arduinojson.org FOR HELP TO CALCULATE YOUR OWN CAPACITY!!!
const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(5) + 310;
DynamicJsonDocument doc(capacity);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Boop");
pinMode(gaugePin, OUTPUT);
for(int i=0;i<541;i++){
analogWrite(gaugePin,i);
//Serial.print(".");
delay(5);
}
}
float fossilFuelPercentage=0.0;
void loop() {
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED) {delay(500); Serial.print(".");}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClientSecure client;
client.setInsecure(); //eliminate the need for at certificate fingerprint... but allows an attacker to impersonate the webserver...
const int httpsPort = 443;
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.println("GET " + url + " HTTP/1.1");
client.println("Host: " + host);
client.println("Accept: */*");
client.println("auth-token: " + authToken);
//client.println("Connection: close");
client.println(); //end of header
delay(1000); //wait a while to let the server respond (increase on shitty connections)
// Read all the lines of the reply from server
String line;
while(client.available()){
yield();
String line = client.readStringUntil('\n'); //newline return as delimiter
//Serial.println(line);
if(line.startsWith("{\"_disclaimer\":")) {
deserializeJson(doc, line); //if the current line is the json string we want, parse it to make a json object.
Serial.println("found json!");
fossilFuelPercentage = doc["data"]["fossilFuelPercentage"];
//Serial.println("fossilFuelPercentage: " + String(data_fossilFuelPercentage));
}
}
if(fossilFuelPercentage!=0.0) { //if data exists
Serial.println("Fossil fuel percentage: " + String(fossilFuelPercentage));
float renewablePercentage=100.0-fossilFuelPercentage;
int pwmVal=int(float(GAUGE_PWM_MAX)*(renewablePercentage/100.0));
Serial.println("pwmVal: " + String(pwmVal));
analogWrite(gaugePin,pwmVal);
}
else Serial.println("fossilFuelPercentage at 0.0.. weird..");
Serial.println("closing connection");
client.stop();
Serial.println("Turning off wifi");
WiFi.disconnect();
delay(15*60*1000); //wait 15 minutes... API rate limit is: 1/sec and 30/hour
}