Components and supplies
Arduino UNO Wifi Rev.2
ecobee3 lite
Project description
Code
Arduino_secrets.h
arduino
1#define SECRET_SSID "add your wifi network name" 2#define SECRET_PASS "add you wifi network pasword" 3#define SECRET_Client_ID "add client ID from Ecobee developer" 4
EcoBee_API_Application.ino
arduino
This code will log into your local WiFi, then access https:\\api.ecobee.com to get Ecobee thermostat actual temperature and desired temperature. Information is displayed on the debug communications port stating the code progress.
1/* 2Edited by ChuggingAlong 02/13/2020 3 4 5*/ 6#include <EEPROM.h> 7#include <ArduinoJson.h> 8#include <SPI.h> 9#include <ArduinoHttpClient.h> 10#include <WiFiNINA.h> 11#include "arduino_secrets.h" 12 13//===== please enter your sensitive data in the Secret tab/arduino_secrets.h 14char ssid[] = SECRET_SSID; // your network SSID (name) 15char pass[] = SECRET_PASS; // your network password (use for WPA) 16 17//===== EEProm addresses ========= 18// A token refresh immediately expires the previously issued access and refresh tokens and issues brand new tokens. 19// EEProm is used to alway store the latest tokens 20//addr 0-31 refresh_token 21const int address_refresh_token = 0; 22//addr 32 '\\0' null termination char 23const int address_null_termination = 32; // used to check if EEProm is initialized 24 25String access_token = ""; 26String refresh_token = ""; 27String ecobeePin = ""; 28String code = ""; 29int LivingRoomTemp = 0; 30int DesiredRoomTemp = 0; 31 32int status = WL_IDLE_STATUS; 33const size_t MAX_CONTENT_SIZE = 1600; 34 35int statuscode = 0; 36String response = ""; 37 38char server[] = "api.ecobee.com"; 39 40WiFiSSLClient wifiClient; 41HttpClient client = HttpClient(wifiClient, server, 443); 42 43void setup() { 44 //Initialize serial and wait for port to open: 45 Serial.begin(9600); 46 while (!Serial) { 47 ; // wait for serial port to connect. Needed for native USB port only 48 } 49 50 // check for the WiFi module: 51 if (WiFi.status() == WL_NO_MODULE) { 52 Serial.println("Communication with WiFi module failed!"); 53 // don't continue 54 while (true); 55 } 56 57 String fv = WiFi.firmwareVersion(); 58 if (fv < "1.0.0") { 59 Serial.println("Please upgrade the firmware"); 60 } 61 62 // attempt to connect to Wifi network: 63 while (status != WL_CONNECTED) { // Connect to WPA/WPA2 network: 64 Serial.print("Attempting to connect to WPA SSID: "); 65 Serial.println(ssid); 66 status = WiFi.begin(ssid, pass); 67 delay(10000); // wait 10 seconds for connection: 68 } 69 70 Serial.println("You're connected to the network"); 71 //printCurrentNet(); Leave for Debug 72 //printWifiData(); 73 74 Serial.println("Starting connection to server..."); // if you get a connection, report back via serial: 75 client.beginRequest(); 76 77 //======= get latest refresh code from EEProm to use for refresh tokens =================== 78 refresh_token = readEEstring(address_refresh_token); 79 client.post("https://api.ecobee.com/token?grant_type=refresh_token&refresh_token=" + refresh_token + "&client_id=" + SECRET_Client_ID); 80 client.endRequest(); 81 // read the status code and body of the response 82 int statusCode = client.responseStatusCode(); 83 if (statusCode == 200) 84 { 85 String response = client.responseBody(); 86 Serial.print("Refresh Status code: "); 87 Serial.println(statusCode); 88 Serial.print("Refresh Response: "); 89 Serial.println(response); 90 91 Extract_Json_Strings(response, access_token, "access_token", refresh_token, "refresh_token"); 92 writeEEstring (address_refresh_token, refresh_token); // Save for next running of Setup/Refresh 93 94 Serial.print ("refresh_token: "); 95 Serial.println (refresh_token ); 96 Serial.print ("access_token: "); 97 Serial.println (access_token ); 98 } else { 99 Serial.println ("Error in obtaining access code, need to run create_new_refresh_keys ()"); 100 create_new_refresh_keys (refresh_token, access_token); 101 } 102 103 104 //======= Send Request for Ecobee rooom temperature & Desired temperature =================== 105 String postData = "{\\"selection\\":{\\"selectionType\\":\\"registered\\",\\"selectionMatch\\":\\"\\",\\"includeRuntime\\":true}}"; 106 client.beginRequest(); 107 client.get("https://api.ecobee.com/1/thermostat?format=json&body=" + postData); //moved postData here & changed to a GET 108 client.sendHeader("Authorization", ("Bearer " + access_token)); 109 Serial.println("Bearer " + access_token); 110 client.sendHeader("Content-Type", "text/json"); 111 client.endRequest(); 112 // ======== read the status code and body of the response =========== 113 statusCode = client.responseStatusCode(); 114 response = client.responseBody(); 115 Serial.print("StatusCode: "); 116 Serial.println(statusCode); 117 Serial.print("Response: "); 118 Serial.println(response); 119 120 // ======= Extract temperature and desired temperature ========= Using String functions, easier than ArduinoJSON calls. 121 int StartOf = response.indexOf ("actualTemperature"); 122 String actual_Temperature_Str (response.substring((StartOf + 20), StartOf + 23)); 123 LivingRoomTemp = actual_Temperature_Str.toInt(); 124 float x = LivingRoomTemp; 125 x = x / 10; 126 Serial.print("LivingRoomTemp: "); 127 Serial.println (x, 1); 128 StartOf = response.indexOf ("desiredHeat"); 129 String Disired_Heat_Str = (response.substring((StartOf + 14), StartOf + 17)); 130 DesiredRoomTemp = Disired_Heat_Str.toInt(); 131 x = DesiredRoomTemp; 132 x = x / 10; 133 Serial.print("Disired Temp: "); 134 Serial.println (x, 1); 135 136} 137 138void loop() { 139 140 while (true); 141 142} 143 144void printWifiData() { 145 // print your board's IP address: 146 IPAddress ip = WiFi.localIP(); 147 Serial.print("IP Address: "); 148 Serial.println(ip); 149 Serial.println(ip); 150 151 // print your MAC address: 152 byte mac[6]; 153 WiFi.macAddress(mac); 154 Serial.print("MAC address: "); 155 printMacAddress(mac); 156} 157 158void printCurrentNet() { 159 // print the SSID of the network you're attached to: 160 Serial.print("SSID: "); 161 Serial.println(WiFi.SSID()); 162 163 // print the MAC address of the router you're attached to: 164 byte bssid[6]; 165 WiFi.BSSID(bssid); 166 Serial.print("BSSID: "); 167 printMacAddress(bssid); 168 169 // print the received signal strength: 170 long rssi = WiFi.RSSI(); 171 Serial.print("signal strength (RSSI):"); 172 Serial.println(rssi); 173 174 // print the encryption type: 175 byte encryption = WiFi.encryptionType(); 176 Serial.print("Encryption Type:"); 177 Serial.println(encryption, HEX); 178 Serial.println(); 179} 180 181void printMacAddress(byte mac[]) { 182 for (int i = 5; i >= 0; i--) { 183 if (mac[i] < 16) { 184 Serial.print("0"); 185 } 186 Serial.print(mac[i], HEX); 187 if (i > 0) { 188 Serial.print(":"); 189 } 190 } 191 Serial.println(); 192} 193 194void writeEEstring(char add, String data) { 195 int _size = data.length(); 196 int i; 197 for (i = 0; i < _size; i++) 198 { 199 EEPROM.write(add + i, data[i]); 200 } 201 EEPROM.write(add + _size, '\\0'); //Add termination null character for String Data 202 // EEPROM.commit(); 203} 204 205 206String readEEstring(char add) { 207 int i; 208 char data[100]; //Max 100 Bytes 209 int len = 0; 210 unsigned char k; 211 k = EEPROM.read(add); 212 while (k != '\\0' && len < 500) //Read until null character 213 { 214 k = EEPROM.read(add + len); 215 data[len] = k; 216 len++; 217 } 218 data[len] = '\\0'; 219 return String(data); 220} 221 222void Extract_Json_Strings (String data, String & Var_1, String Var_1_json, String & Var_2, String Var_2_json) { // use of ampersand passes pointers 223 StaticJsonDocument <500> jsonDoc; 224 auto error = deserializeJson(jsonDoc, data); 225 if (error) { 226 Serial.print(F("deserializeJson() failed with code ")); 227 Serial.println(error.c_str()); 228 } 229 else { 230 String tmp1 = jsonDoc[Var_1_json]; 231 Var_1 = tmp1 ; // this is the only way I could get this access_token updated 232 String tmp2 = jsonDoc[Var_2_json]; 233 Var_2 = tmp2 ; // this is the only way I could get this access_token updated 234 /* 235 Serial.print (Var_1_json); 236 Serial.print (": "); 237 Serial.println (Var_1); 238 Serial.print (Var_2_json); 239 Serial.print (": "); 240 Serial.println (Var_2); 241 */ 242 } 243} 244 245void create_new_refresh_keys (String & refresh_token_loc, String & access_token_loc) { 246 client.beginRequest(); 247 248 //======= get ecobeePin =================== 249 // converted from JavaScript: https://www.ecobee.com/home/developer/api/examples/ex1.shtml 250 client.get("https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=dRc7CqSwIxKI7pNp3ZMDqJGPY7aT9akm&scope=smartWrite"); 251 client.endRequest(); 252 // read the status code and body of the response 253 int statusCode = client.responseStatusCode(); 254 String response = client.responseBody(); 255 256 Serial.print("ecobeePin Status code: "); 257 Serial.println(statusCode); 258 Serial.print("Refresh Response: "); 259 Serial.println(response); 260 261 Extract_Json_Strings(response, ecobeePin, "ecobeePin", code, "code"); 262 writeEEstring (address_refresh_token, refresh_token); // Save for next running of Setup/Refresh 263 264 Serial.print ("Aurthorization code: "); 265 Serial.println (code ); 266 Serial.print ("ecobeePin: "); 267 Serial.println (ecobeePin ); 268 Serial.println ("go to ecobee.com MY APPS"); 269 Serial.println ("click add appliaction & paste ecobeePin into autorization code"); 270 Serial.println ("click add app in lower right corner"); 271 272 //======= get ecobee Aurthorization code, final step to get Access and Refresh codes =================== 273 // converted from JavaScript: https://www.ecobee.com/home/developer/api/examples/ex1.shtml 274 refresh_token = "null"; 275 delay (20000); 276 int i = 0; 277 while (refresh_token == "null" || i == 6) { 278 client.post("https://api.ecobee.com/token?grant_type=ecobeePin&code=" + code + "&client_id=" + SECRET_Client_ID); 279 client.endRequest(); 280 // read the status code and body of the response 281 statusCode = client.responseStatusCode(); 282 response = client.responseBody(); 283 284 Serial.print("Aurthorization Status code: "); 285 Serial.println(statusCode); 286 Serial.print("Refresh Response: "); 287 Serial.println(response); 288 289 Extract_Json_Strings(response, access_token_loc, "access_token", refresh_token_loc, "refresh_token"); 290 writeEEstring (address_refresh_token, refresh_token_loc); // Save for next running of Setup/Refresh 291 292 Serial.print ("refresh_token: "); 293 Serial.println (refresh_token_loc ); 294 Serial.print ("access_token: "); 295 Serial.println (access_token_loc ); 296 delay (10000); //10 Second Delay 297 if (i == 6) { 298 Serial.println("Time out error > 1 minute"); 299 Serial.println("press reset to retry"); 300 while (true); 301 } 302 } 303 304} 305
Arduino_secrets.h
arduino
1#define SECRET_SSID "add your wifi network name" 2#define SECRET_PASS "add you wifi network pasword" 3#define SECRET_Client_ID "add client ID from Ecobee developer" 4
Arduino_secrets.h
arduino
1#define SECRET_SSID "add your wifi network name" 2#define SECRET_PASS 3 "add you wifi network pasword" 4#define SECRET_Client_ID "add client ID from 5 Ecobee developer" 6
EcoBee_API_Application.ino
arduino
This code will log into your local WiFi, then access https:\\api.ecobee.com to get Ecobee thermostat actual temperature and desired temperature. Information is displayed on the debug communications port stating the code progress.
1/* 2Edited by ChuggingAlong 02/13/2020 3 4 5*/ 6#include <EEPROM.h> 7#include <ArduinoJson.h> 8#include <SPI.h> 9#include <ArduinoHttpClient.h> 10#include <WiFiNINA.h> 11#include "arduino_secrets.h" 12 13//===== please enter your sensitive data in the Secret tab/arduino_secrets.h 14char ssid[] = SECRET_SSID; // your network SSID (name) 15char pass[] = SECRET_PASS; // your network password (use for WPA) 16 17//===== EEProm addresses ========= 18// A token refresh immediately expires the previously issued access and refresh tokens and issues brand new tokens. 19// EEProm is used to alway store the latest tokens 20//addr 0-31 refresh_token 21const int address_refresh_token = 0; 22//addr 32 '\\0' null termination char 23const int address_null_termination = 32; // used to check if EEProm is initialized 24 25String access_token = ""; 26String refresh_token = ""; 27String ecobeePin = ""; 28String code = ""; 29int LivingRoomTemp = 0; 30int DesiredRoomTemp = 0; 31 32int status = WL_IDLE_STATUS; 33const size_t MAX_CONTENT_SIZE = 1600; 34 35int statuscode = 0; 36String response = ""; 37 38char server[] = "api.ecobee.com"; 39 40WiFiSSLClient wifiClient; 41HttpClient client = HttpClient(wifiClient, server, 443); 42 43void setup() { 44 //Initialize serial and wait for port to open: 45 Serial.begin(9600); 46 while (!Serial) { 47 ; // wait for serial port to connect. Needed for native USB port only 48 } 49 50 // check for the WiFi module: 51 if (WiFi.status() == WL_NO_MODULE) { 52 Serial.println("Communication with WiFi module failed!"); 53 // don't continue 54 while (true); 55 } 56 57 String fv = WiFi.firmwareVersion(); 58 if (fv < "1.0.0") { 59 Serial.println("Please upgrade the firmware"); 60 } 61 62 // attempt to connect to Wifi network: 63 while (status != WL_CONNECTED) { // Connect to WPA/WPA2 network: 64 Serial.print("Attempting to connect to WPA SSID: "); 65 Serial.println(ssid); 66 status = WiFi.begin(ssid, pass); 67 delay(10000); // wait 10 seconds for connection: 68 } 69 70 Serial.println("You're connected to the network"); 71 //printCurrentNet(); Leave for Debug 72 //printWifiData(); 73 74 Serial.println("Starting connection to server..."); // if you get a connection, report back via serial: 75 client.beginRequest(); 76 77 //======= get latest refresh code from EEProm to use for refresh tokens =================== 78 refresh_token = readEEstring(address_refresh_token); 79 client.post("https://api.ecobee.com/token?grant_type=refresh_token&refresh_token=" + refresh_token + "&client_id=" + SECRET_Client_ID); 80 client.endRequest(); 81 // read the status code and body of the response 82 int statusCode = client.responseStatusCode(); 83 if (statusCode == 200) 84 { 85 String response = client.responseBody(); 86 Serial.print("Refresh Status code: "); 87 Serial.println(statusCode); 88 Serial.print("Refresh Response: "); 89 Serial.println(response); 90 91 Extract_Json_Strings(response, access_token, "access_token", refresh_token, "refresh_token"); 92 writeEEstring (address_refresh_token, refresh_token); // Save for next running of Setup/Refresh 93 94 Serial.print ("refresh_token: "); 95 Serial.println (refresh_token ); 96 Serial.print ("access_token: "); 97 Serial.println (access_token ); 98 } else { 99 Serial.println ("Error in obtaining access code, need to run create_new_refresh_keys ()"); 100 create_new_refresh_keys (refresh_token, access_token); 101 } 102 103 104 //======= Send Request for Ecobee rooom temperature & Desired temperature =================== 105 String postData = "{\\"selection\\":{\\"selectionType\\":\\"registered\\",\\"selectionMatch\\":\\"\\",\\"includeRuntime\\":true}}"; 106 client.beginRequest(); 107 client.get("https://api.ecobee.com/1/thermostat?format=json&body=" + postData); //moved postData here & changed to a GET 108 client.sendHeader("Authorization", ("Bearer " + access_token)); 109 Serial.println("Bearer " + access_token); 110 client.sendHeader("Content-Type", "text/json"); 111 client.endRequest(); 112 // ======== read the status code and body of the response =========== 113 statusCode = client.responseStatusCode(); 114 response = client.responseBody(); 115 Serial.print("StatusCode: "); 116 Serial.println(statusCode); 117 Serial.print("Response: "); 118 Serial.println(response); 119 120 // ======= Extract temperature and desired temperature ========= Using String functions, easier than ArduinoJSON calls. 121 int StartOf = response.indexOf ("actualTemperature"); 122 String actual_Temperature_Str (response.substring((StartOf + 20), StartOf + 23)); 123 LivingRoomTemp = actual_Temperature_Str.toInt(); 124 float x = LivingRoomTemp; 125 x = x / 10; 126 Serial.print("LivingRoomTemp: "); 127 Serial.println (x, 1); 128 StartOf = response.indexOf ("desiredHeat"); 129 String Disired_Heat_Str = (response.substring((StartOf + 14), StartOf + 17)); 130 DesiredRoomTemp = Disired_Heat_Str.toInt(); 131 x = DesiredRoomTemp; 132 x = x / 10; 133 Serial.print("Disired Temp: "); 134 Serial.println (x, 1); 135 136} 137 138void loop() { 139 140 while (true); 141 142} 143 144void printWifiData() { 145 // print your board's IP address: 146 IPAddress ip = WiFi.localIP(); 147 Serial.print("IP Address: "); 148 Serial.println(ip); 149 Serial.println(ip); 150 151 // print your MAC address: 152 byte mac[6]; 153 WiFi.macAddress(mac); 154 Serial.print("MAC address: "); 155 printMacAddress(mac); 156} 157 158void printCurrentNet() { 159 // print the SSID of the network you're attached to: 160 Serial.print("SSID: "); 161 Serial.println(WiFi.SSID()); 162 163 // print the MAC address of the router you're attached to: 164 byte bssid[6]; 165 WiFi.BSSID(bssid); 166 Serial.print("BSSID: "); 167 printMacAddress(bssid); 168 169 // print the received signal strength: 170 long rssi = WiFi.RSSI(); 171 Serial.print("signal strength (RSSI):"); 172 Serial.println(rssi); 173 174 // print the encryption type: 175 byte encryption = WiFi.encryptionType(); 176 Serial.print("Encryption Type:"); 177 Serial.println(encryption, HEX); 178 Serial.println(); 179} 180 181void printMacAddress(byte mac[]) { 182 for (int i = 5; i >= 0; i--) { 183 if (mac[i] < 16) { 184 Serial.print("0"); 185 } 186 Serial.print(mac[i], HEX); 187 if (i > 0) { 188 Serial.print(":"); 189 } 190 } 191 Serial.println(); 192} 193 194void writeEEstring(char add, String data) { 195 int _size = data.length(); 196 int i; 197 for (i = 0; i < _size; i++) 198 { 199 EEPROM.write(add + i, data[i]); 200 } 201 EEPROM.write(add + _size, '\\0'); //Add termination null character for String Data 202 // EEPROM.commit(); 203} 204 205 206String readEEstring(char add) { 207 int i; 208 char data[100]; //Max 100 Bytes 209 int len = 0; 210 unsigned char k; 211 k = EEPROM.read(add); 212 while (k != '\\0' && len < 500) //Read until null character 213 { 214 k = EEPROM.read(add + len); 215 data[len] = k; 216 len++; 217 } 218 data[len] = '\\0'; 219 return String(data); 220} 221 222void Extract_Json_Strings (String data, String & Var_1, String Var_1_json, String & Var_2, String Var_2_json) { // use of ampersand passes pointers 223 StaticJsonDocument <500> jsonDoc; 224 auto error = deserializeJson(jsonDoc, data); 225 if (error) { 226 Serial.print(F("deserializeJson() failed with code ")); 227 Serial.println(error.c_str()); 228 } 229 else { 230 String tmp1 = jsonDoc[Var_1_json]; 231 Var_1 = tmp1 ; // this is the only way I could get this access_token updated 232 String tmp2 = jsonDoc[Var_2_json]; 233 Var_2 = tmp2 ; // this is the only way I could get this access_token updated 234 /* 235 Serial.print (Var_1_json); 236 Serial.print (": "); 237 Serial.println (Var_1); 238 Serial.print (Var_2_json); 239 Serial.print (": "); 240 Serial.println (Var_2); 241 */ 242 } 243} 244 245void create_new_refresh_keys (String & refresh_token_loc, String & access_token_loc) { 246 client.beginRequest(); 247 248 //======= get ecobeePin =================== 249 // converted from JavaScript: https://www.ecobee.com/home/developer/api/examples/ex1.shtml 250 client.get("https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=dRc7CqSwIxKI7pNp3ZMDqJGPY7aT9akm&scope=smartWrite"); 251 client.endRequest(); 252 // read the status code and body of the response 253 int statusCode = client.responseStatusCode(); 254 String response = client.responseBody(); 255 256 Serial.print("ecobeePin Status code: "); 257 Serial.println(statusCode); 258 Serial.print("Refresh Response: "); 259 Serial.println(response); 260 261 Extract_Json_Strings(response, ecobeePin, "ecobeePin", code, "code"); 262 writeEEstring (address_refresh_token, refresh_token); // Save for next running of Setup/Refresh 263 264 Serial.print ("Aurthorization code: "); 265 Serial.println (code ); 266 Serial.print ("ecobeePin: "); 267 Serial.println (ecobeePin ); 268 Serial.println ("go to ecobee.com MY APPS"); 269 Serial.println ("click add appliaction & paste ecobeePin into autorization code"); 270 Serial.println ("click add app in lower right corner"); 271 272 //======= get ecobee Aurthorization code, final step to get Access and Refresh codes =================== 273 // converted from JavaScript: https://www.ecobee.com/home/developer/api/examples/ex1.shtml 274 refresh_token = "null"; 275 delay (20000); 276 int i = 0; 277 while (refresh_token == "null" || i == 6) { 278 client.post("https://api.ecobee.com/token?grant_type=ecobeePin&code=" + code + "&client_id=" + SECRET_Client_ID); 279 client.endRequest(); 280 // read the status code and body of the response 281 statusCode = client.responseStatusCode(); 282 response = client.responseBody(); 283 284 Serial.print("Aurthorization Status code: "); 285 Serial.println(statusCode); 286 Serial.print("Refresh Response: "); 287 Serial.println(response); 288 289 Extract_Json_Strings(response, access_token_loc, "access_token", refresh_token_loc, "refresh_token"); 290 writeEEstring (address_refresh_token, refresh_token_loc); // Save for next running of Setup/Refresh 291 292 Serial.print ("refresh_token: "); 293 Serial.println (refresh_token_loc ); 294 Serial.print ("access_token: "); 295 Serial.println (access_token_loc ); 296 delay (10000); //10 Second Delay 297 if (i == 6) { 298 Serial.println("Time out error > 1 minute"); 299 Serial.println("press reset to retry"); 300 while (true); 301 } 302 } 303 304} 305
Downloadable files
Arduino Uno Wifi Rev2, no changes
Arduino Uno Wifi Rev2, no changes
Arduino Uno Wifi Rev2, no changes
Arduino Uno Wifi Rev2, no changes
Comments
Only logged in users can leave comments
Andrewjohnsen31
0 Followers
•0 Projects
0