├── ESP8266_Car_Battery_Monitor.ino ├── ESP8266_battery_monitor.pdf ├── ESP8266_battery_monitor_PCB.pdf ├── Photos ├── 1.JPG ├── 2.JPG └── new.txt ├── README.md └── libraries ├── Arduino_Vector-master ├── README.md ├── Vector.h ├── dev_files │ └── vec_test.cpp └── examples │ └── Vector_use │ └── Vector_use.ino └── EIoTCloudRestApiV1.1 ├── EIoTCloudRestApiV1.0.h ├── EIoTCloudRestApiV1.1.h └── examples ├── ESP8266_DHT22_humidity_sensor_REST_V1_0 └── ESP8266_DHT22_humidity_sensor_REST_V1_0.ino ├── ESP8266_DS18B20_temperature_sensor_REST_v1_0.ino └── ESP8266_DS18B20_temperature_sensor_REST_v1_0.ino.ino └── OTA_EIoTCloudRestApi └── OTA_EIoTCloudRestApi.ino /ESP8266_Car_Battery_Monitor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SNEEZY BATTERY MONITOR OVER WIFI - USES ESP8266 BREAKOUT MODULE BOARDS VIA ARDUINO IDE 3 | VERSION 1A - Nov 2016 4 | */ 5 | 6 | 7 | #include 8 | #include 9 | #include "EIoTCloudRestApiV1.1.h" 10 | #include 11 | #include 12 | #include 13 | 14 | //#define DEBUG_PROG //Remark to disable serial DEBUG 15 | /* 16 | #ifdef DEBUG_PROG 17 | #define DEBUG_PRINTLN(x) Serial.println(x) 18 | #define DEBUG_PRINT(x) Serial.print(x) 19 | #else 20 | #define DEBUG_PRINTLN(x) 21 | #define DEBUG_PRINT(x) 22 | #endif 23 | */ 24 | #ifdef DEBUG_PROG //cool way to have debug serial ONLY when you want it, and replaced with effectively no statement when not needed. Supports basic formatting of vars. 25 | #define DEBUG_PRINT(x,...) Serial.print(x, ##__VA_ARGS__) 26 | #define DEBUG_PRINTLN(x,...) Serial.println(x, ##__VA_ARGS__) 27 | #else 28 | #define DEBUG_PRINT(x,...) 29 | #define DEBUG_PRINTLN(x,...) 30 | #endif 31 | 32 | EIoTCloudRestApi eiotcloud; 33 | 34 | //#define AP 1 //AP 1 = hotspot //=home router 35 | 36 | #ifdef AP 1 37 | // change those lines 38 | #define AP_USERNAME "YOUR OWN WIFI NAME" 39 | #define AP_PASSWORD "YOUR PASSWORD" 40 | #define INSTANCE_ID "your cloud instance ID" 41 | #else 42 | // change those lines 43 | #define AP_USERNAME "YOUR OWN ALTERNATE WIFI NAME" 44 | #define AP_PASSWORD "YOUR ALTERNATE PASSWORD" 45 | #define INSTANCE_ID "your Eiot cloud instance ID number" 46 | #endif 47 | 48 | #define REPORT_INTERVAL 360 //Seconds before next report (multiplied by sleep_multiplier if not 0) 49 | #define sleep_multiplier 10 //deep sleep period multiplier. 50 | 51 | #define ONE_WIRE_BUS 2 // DS18B20 pin 52 | OneWire oneWire(ONE_WIRE_BUS); 53 | //DallasTemperature sensors(&oneWire); 54 | DallasTemperature DS18B20(&oneWire); 55 | 56 | #define ledPIN 2 // Also the serial data TX pin 57 | 58 | #define CONFIG_START 0 59 | #define CONFIG_VERSION "v01" 60 | 61 | struct StoreStruct { 62 | // This is for mere detection if they are your settings 63 | char version[4]; 64 | // The variables of your settings 65 | char token[41]; 66 | uint moduleId; 67 | //bool tokenOk; // valid token 68 | } storage = { 69 | CONFIG_VERSION, 70 | // token 71 | "1234567890123456789012345678901234567890", 72 | // The default module 0 - invalid module 73 | 0, 74 | //0 // not valid 75 | }; 76 | 77 | uint32_t rtcData = 0; //four byte bucket of RTC memory in position 0 78 | uint8_t rtcByte0 = 0; //var for first byte of that bucket 79 | 80 | //float batt_voltsOLD; 81 | //float tempOLD; 82 | 83 | String moduleId = ""; 84 | String parameterId1 = ""; 85 | String parameterId2 = ""; 86 | float tempOld = 0; 87 | 88 | //Function prototype references - C++ housekeeping for compiler 89 | void Go_sleep(); 90 | void Multi_sleeps(); 91 | void loadConfig(); 92 | void saveConfig(); 93 | 94 | void setup() { 95 | 96 | pinMode(ledPIN, OUTPUT); 97 | digitalWrite(ledPIN, HIGH); //LED off 98 | Serial.begin(115200); 99 | DEBUG_PRINTLN("Start..."); 100 | 101 | Multi_sleeps(); //Check the multiple deep sleep cycle count 102 | 103 | digitalWrite(ledPIN, LOW); //long LED flash to indicate a full code run through coming. 104 | delay(1000); //Chucking in a short delay for the RF module to stabilise in case that's causing some lockups... 105 | 106 | EEPROM.begin(512); 107 | loadConfig(); 108 | 109 | //Dallas sensor addition by Sneezy 110 | DS18B20.begin(); 111 | DS18B20.setResolution(9); 112 | DS18B20.setWaitForConversion(true); 113 | //------------------- 114 | 115 | DEBUG_PRINTLN("Setup running"); 116 | 117 | // eiotcloud.begin(AP_USERNAME, AP_PASSWORD); 118 | 119 | if (eiotcloud.begin(AP_USERNAME, AP_PASSWORD) == false) 120 | { 121 | DEBUG_PRINTLN("WiFi not connected"); //MCS 122 | ESP.deepSleep(REPORT_INTERVAL * 1000000, WAKE_RF_DEFAULT); //go to deep sleep for the regular report interval (not multiplied) 123 | delay(1000); //Deep sleep might take some time to occur 124 | } 125 | // if first time get new token and register new module 126 | // here hapend Plug and play logic to add module to Cloud 127 | if (storage.moduleId == 0) //*** REMARK OUT ONCE to reset the moduleID and recreate in EasyIoT Cloud *** 128 | { 129 | // get new token - alternarive is to manually create token and store it in EEPROM 130 | String token = eiotcloud.TokenNew(INSTANCE_ID); 131 | DEBUG_PRINT("Token: "); 132 | DEBUG_PRINTLN(token); 133 | eiotcloud.SetToken(token); 134 | 135 | // remember token 136 | token.toCharArray(storage.token, 41); 137 | 138 | // add new module and configure it 139 | moduleId = eiotcloud.ModuleNew(); 140 | DEBUG_PRINT("ModuleId: "); 141 | DEBUG_PRINTLN(moduleId); 142 | storage.moduleId = moduleId.toInt(); 143 | 144 | // set module type 145 | bool modtyperet = eiotcloud.SetModuleType(moduleId, "MT_GENERIC"); 146 | DEBUG_PRINT("SetModuleType: "); 147 | DEBUG_PRINTLN(modtyperet); 148 | 149 | // set module name 150 | bool modname = eiotcloud.SetModuleName(moduleId, "S2000 monitor"); 151 | DEBUG_PRINT("SetModuleName: "); 152 | DEBUG_PRINTLN(modname); 153 | 154 | // add image settings parameter 155 | String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1"); 156 | DEBUG_PRINT("parameterImgId: "); 157 | DEBUG_PRINTLN(parameterImgId); 158 | 159 | // set module image 160 | bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "analog.png"); 161 | DEBUG_PRINT("SetParameterValue: "); 162 | DEBUG_PRINTLN(valueRet1); 163 | 164 | // now add parameter to display battery voltage 165 | parameterId1 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1"); 166 | DEBUG_PRINT("parameterId1: "); 167 | DEBUG_PRINTLN(parameterId1); 168 | 169 | //set parameter description 170 | bool valueRet2 = eiotcloud.SetParameterDescription(parameterId1, "Voltage"); 171 | DEBUG_PRINT("SetParameterDescription: "); 172 | DEBUG_PRINTLN(valueRet2); 173 | 174 | //set unit 175 | bool valueRet3 = eiotcloud.SetParameterUnit(parameterId1, "V"); 176 | DEBUG_PRINT("SetParameterUnit: "); 177 | DEBUG_PRINTLN(valueRet3); 178 | 179 | //Set parameter LogToDatabase 180 | bool valueRet4 = eiotcloud.SetParameterLogToDatabase(parameterId1, true); 181 | DEBUG_PRINT("SetLogToDatabase: "); 182 | DEBUG_PRINTLN(valueRet4); 183 | 184 | //SetAvreageInterval 185 | bool valueRet5 = eiotcloud.SetParameterAverageInterval(parameterId1, "5"); 186 | DEBUG_PRINT("SetAvreageInterval: "); 187 | DEBUG_PRINTLN(valueRet5); 188 | 189 | //Second sensor parameter --------------------------------------- 190 | // now add parameter to display temperature (if DS18B20 installed). 191 | parameterId2 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter2"); 192 | DEBUG_PRINT("parameterId2: "); 193 | DEBUG_PRINTLN(parameterId2); 194 | 195 | //set parameter description 196 | bool valueRet6 = eiotcloud.SetParameterDescription(parameterId2, "Temperature"); 197 | DEBUG_PRINT("SetParameterDescription: "); 198 | DEBUG_PRINTLN(valueRet2); 199 | 200 | //set unit 201 | // see http://meyerweb.com/eric/tools/dencoder/ how to encode °C 202 | bool valueRet7 = eiotcloud.SetParameterUnit(parameterId2, "%C2%B0C"); 203 | DEBUG_PRINT("SetParameterUnit: "); 204 | DEBUG_PRINTLN(valueRet7); 205 | 206 | //Set parameter LogToDatabase 207 | bool valueRet8 = eiotcloud.SetParameterLogToDatabase(parameterId2, true); 208 | DEBUG_PRINT("SetLogToDatabase: "); 209 | DEBUG_PRINTLN(valueRet8); 210 | 211 | //SetAvreageInterval 212 | bool valueRet9 = eiotcloud.SetParameterAverageInterval(parameterId2, "5"); 213 | DEBUG_PRINT("SetAvreageInterval: "); 214 | DEBUG_PRINTLN(valueRet9); 215 | 216 | 217 | // save configuration 218 | saveConfig(); 219 | } 220 | 221 | // if something went wrong, wiat here 222 | if (storage.moduleId == 0) 223 | delay(1); 224 | 225 | // read module ID from storage 226 | moduleId = String(storage.moduleId); 227 | DEBUG_PRINT("ModuleID = "); 228 | DEBUG_PRINTLN(moduleId); 229 | // read token ID from storage 230 | eiotcloud.SetToken(storage.token); 231 | 232 | // read Sensor.Parameter1 ID from cloud 233 | parameterId1 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1"); 234 | DEBUG_PRINT("parameterId1: "); 235 | DEBUG_PRINTLN(parameterId1); 236 | 237 | parameterId2 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter2"); 238 | DEBUG_PRINT("parameterId2: "); 239 | DEBUG_PRINTLN(parameterId2); 240 | 241 | } 242 | 243 | 244 | void loop() { 245 | float tempC; 246 | float batt_volts; 247 | float temp_var=0; //holds cumulative reads for averaging 248 | unsigned int batt_raw; // can I use 'int' or does it need to be a float to do maths with another float below ? (yep seems to work fine with float math) 249 | DEBUG_PRINTLN("Loop running"); 250 | 251 | //Read voltage with 10 times average. 252 | for (int i=0; i <= 9; i++){ 253 | temp_var = temp_var + analogRead(A0); 254 | delay(10); 255 | } 256 | 257 | batt_raw = temp_var/10; 258 | 259 | batt_volts = batt_raw * 0.0146; //scalling factor for 15V full scale with ADC range of 1V 260 | 261 | DEBUG_PRINT("Battery volts: "); 262 | DEBUG_PRINTLN(batt_volts); 263 | 264 | //Read DS18B20 temperature with 10 times average. 265 | temp_var = 0; 266 | for (int i=0; i <= 9; i++){ 267 | DS18B20.requestTemperatures(); 268 | // delay(100); //Delay while sensor does temp sample and conversion in 9 bit resolution (could substitute Wait For Conversion if I understood how too). 269 | temp_var = temp_var + DS18B20.getTempCByIndex(0); 270 | 271 | } 272 | tempC = temp_var/10; 273 | 274 | DEBUG_PRINT("Temperature: "); 275 | DEBUG_PRINTLN(tempC); 276 | 277 | eiotcloud.SetParameterValues("[{\"Id\": \""+parameterId1+"\", \"Value\": \""+String(batt_volts)+"\" },{\"Id\": \""+parameterId2+"\", \"Value\": \""+String(tempC)+"\" }]"); 278 | 279 | Go_sleep(); 280 | } 281 | 282 | void loadConfig() { 283 | DEBUG_PRINTLN("do loadConfig"); 284 | // To make sure there are settings, and they are YOURS! 285 | // If nothing is found it will use the default settings. 286 | if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] && 287 | EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] && 288 | EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2]) 289 | for (unsigned int t=0; t sleep_multiplier) || (multi_sleep_count == 1)) { //if var 'sleep_multiplier' number of deep sleep cycles have passed run the whole program 321 | DEBUG_PRINTLN("Sleep_counter triggered"); 322 | rtcData = 0xAC000000 + 1; //Reset the sleep counter (+1 so an endless loop of triggering doesn't happen) 323 | ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData)); 324 | DEBUG_PRINT("rtcData_reset: "); 325 | DEBUG_PRINTLN(rtcData, HEX); 326 | // delay(500); // allow the serial data to be pushed over ttl before reset 327 | return; //then go back to the Setup function and continue 328 | } 329 | else 330 | { 331 | ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData)); 332 | DEBUG_PRINT("rtcData_incremented: "); 333 | DEBUG_PRINTLN(rtcData, HEX); 334 | // delay(500); // allow the serial data to be pushed over ttl before reset 335 | Go_sleep(); //go back to sleep again 336 | } 337 | } 338 | 339 | void Go_sleep() { 340 | // uint16_t temp_var = 0 341 | float cnt = REPORT_INTERVAL * 1000000; //Delay in units of second 342 | DEBUG_PRINT("Sleeping for "); 343 | DEBUG_PRINT((REPORT_INTERVAL * sleep_multiplier) - (REPORT_INTERVAL * ((rtcData & 0x000000FF) - 1))); 344 | DEBUG_PRINTLN(" seconds"); 345 | 346 | ESP.deepSleep(cnt, WAKE_RF_DEFAULT); 347 | delay(1000); //Deep sleep might take some time to occur 348 | } 349 | 350 | -------------------------------------------------------------------------------- /ESP8266_battery_monitor.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mr-sneezy/ESP8266-car-battery-monitor-to-EasyIoT/36060e5abb5d7e87174d51ea347a0d4cee613021/ESP8266_battery_monitor.pdf -------------------------------------------------------------------------------- /ESP8266_battery_monitor_PCB.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mr-sneezy/ESP8266-car-battery-monitor-to-EasyIoT/36060e5abb5d7e87174d51ea347a0d4cee613021/ESP8266_battery_monitor_PCB.pdf -------------------------------------------------------------------------------- /Photos/1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mr-sneezy/ESP8266-car-battery-monitor-to-EasyIoT/36060e5abb5d7e87174d51ea347a0d4cee613021/Photos/1.JPG -------------------------------------------------------------------------------- /Photos/2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mr-sneezy/ESP8266-car-battery-monitor-to-EasyIoT/36060e5abb5d7e87174d51ea347a0d4cee613021/Photos/2.JPG -------------------------------------------------------------------------------- /Photos/new.txt: -------------------------------------------------------------------------------- 1 | New file to create a folder 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-car-battery-monitor-to-EasyIoT 2 | Small diy project to monitor the 12V car battery voltage of a vehicle in winter storage. 3 | A learning project for ESP8266 usage with lots of my self learning of Arduino and C++ language. 4 | A lot of using examples found elsewhere, glued together with beginner code from me and much help from 'micooke'. 5 | Also uses a DS18B20 temp sensor because it was fun to include... 6 | Project connects to the EasyIoT cloud http://iot-playground.com/ 7 | 8 | The ESP8266 and PCB measure the cars battery voltage once per hour (using deepsleep with a multiplier value stored in NVRAM), longer delay times are a matter of increasing the deepsleep count value. It also measures ambient temperature, but that can be deleted simply enough. 9 | Once a measurement is taken the device connects to a local WiFi router and posts the values to the Easy IoT cloud website via an instance ID you have created previously. The Easy IoT cloud settings allow a user to set trigger threasholds for an email warnings to be sent, and I set the warning to trigger on at 12.2V. When I receive the email I plug the cars charger back in or take it out for a run... 10 | -------------------------------------------------------------------------------- /libraries/Arduino_Vector-master/README.md: -------------------------------------------------------------------------------- 1 | Arduino_Vector 2 | ============== 3 | 4 | Lightweight implementation of the STL vector for Arduino 5 | 6 | Dynamic memory management on a microcontroller can lead to overflows if not managed carefully. 7 | 8 | ###But... 9 | I've wanted to use a vector to store a limited number of objects like pointers to different sensors 10 | on my robot or a list of 10 control options to manuever based off the sonar's data. Instead of using 11 | an array I decided to implement the C++ STL vector with limited functionality to keep the code size 12 | small and the operations focused on teh basics. 13 | 14 | This first verion on my Vector has the basics you expect. 15 | 16 | a. No penatly for construction 17 | 18 | `Vector vf; //sizof(vf) == 0 until you push_back into it.` 19 | 20 | b. Normal C++ push_back for appending data to the end of the vector 21 | 22 | `vf.push_back(foo);` 23 | 24 | c. Normal access operator 25 | 26 | `vf[0] = foo2;` 27 | 28 | d. Normal .size() method for traversing the vector 29 | 30 | ```` 31 | for(int i=0; i 36 | 37 | //as far as I can tell placement new is not included with AVR or arduino.h 38 | template 39 | void* operator new(size_t s, T* v){ 40 | return v; 41 | } 42 | 43 | //********************************************************************************* 44 | // Allocator for Vector 45 | //********************************************************************************* 46 | template struct Simple_alloc { 47 | 48 | Simple_alloc() {}; 49 | 50 | //memory allocation 51 | T* allocate(int n) 52 | { return reinterpret_cast(new char[n*sizeof(T)]); } 53 | void deallocate(T* p, int n) 54 | { delete[] reinterpret_cast(p); } 55 | 56 | //construction/destruction 57 | void construct(T* p, const T& t) { new(p) T(t); } 58 | void destroy(T* p) { p->~T(); } 59 | }; 60 | 61 | //********************************************************************************* 62 | // Vector 63 | //********************************************************************************* 64 | template > 65 | class Vector { 66 | 67 | A alloc; 68 | 69 | int sz; 70 | T* elem; 71 | int space; 72 | 73 | Vector(const Vector&); //private copy constrution because I 74 | //have not got this working yet and don't 75 | //want to expose this for clients who might 76 | //be expecting it. 77 | 78 | public: 79 | Vector() : sz(0), elem(0), space(0) {} 80 | Vector(const int s) : sz(0) { 81 | reserve(s); 82 | } 83 | 84 | Vector& operator=(const Vector&); //copy assignment 85 | 86 | ~Vector() { 87 | for(int i=0; i 102 | Vector& Vector::operator=(const Vector& a) { 103 | if(this==&a) return *this; 104 | 105 | if(a.size()<=space) { //enough space, no need for new allocation 106 | for(int i=0; i void Vector::reserve(int newalloc){ 122 | if(newalloc <= space) return; //never decrease space 123 | T* p = alloc.allocate(newalloc); 124 | for(int i=0; i 132 | void Vector::push_back(const T& val){ 133 | if(space == 0) reserve(4); //start small 134 | else if(sz==space) reserve(2*space); 135 | alloc.construct(&elem[sz], val); 136 | ++sz; 137 | } 138 | 139 | #endif 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /libraries/Arduino_Vector-master/dev_files/vec_test.cpp: -------------------------------------------------------------------------------- 1 | #include "Vector.h" 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Person { 9 | string n; 10 | string ssn; 11 | public: 12 | 13 | Person(string social) 14 | :ssn(social) {} 15 | Person(); 16 | 17 | string name() const {return n; } 18 | string social() const {return ssn; } 19 | 20 | void set_name(string name) { n = name; } 21 | void set_ssn(string social) {ssn = social; } 22 | }; 23 | 24 | ostream& operator<<(ostream& os, const Person& p) { 25 | os<<"Name: "< zv; 44 | cout<<"size is: "< zvs; 52 | zvs.push_back("Elissa"); 53 | zvs.push_back("is"); 54 | zvs.push_back("an imp...with a great rack!"); 55 | 56 | for(int i = 0; i < zvs.size(); ++i) { 57 | cout<<"the item at "<< i<< " is: "<set_name("Zac"); 61 | Person* p2 = new Person("111-22-3333"); p2->set_name("Elissa"); 62 | Person* p3 = new Person("333-22-4444"); p3->set_name("Parker"); 63 | 64 | Vector vp; 65 | vp.push_back(p1); 66 | vp.push_back(p2); 67 | vp.push_back(p3); 68 | 69 | cout<<"size is: "< vs2; 76 | vs2 = zvs; 77 | cout<<"let's have a look inside vs2: "< 2 | 3 | class Person { 4 | char* n; 5 | char* ssn; 6 | public: 7 | 8 | Person(char* social) 9 | :ssn(social) {} 10 | Person(); 11 | 12 | char* name() const { 13 | return n; 14 | } 15 | 16 | char* social() const { 17 | return ssn; 18 | } 19 | 20 | void set_name(char* name) { 21 | n = name; 22 | } 23 | 24 | void set_ssn(char* social) { 25 | ssn = social; 26 | } 27 | }; 28 | 29 | 30 | void setup() { 31 | Serial.begin(9600); 32 | 33 | Vector vi; 34 | 35 | int sz = vi.size(); 36 | Serial.print("size of vi is: "); 37 | Serial.println(sz); 38 | 39 | for(int i=0; i<200; i+=10) { 40 | vi.push_back(i); 41 | } 42 | 43 | sz = vi.size(); 44 | Serial.print("size of vi is: "); 45 | Serial.println(sz); 46 | 47 | Serial.println("Let's have a look at vi: "); 48 | for(int i = 0; i < vi.size(); ++i) { 49 | Serial.print("\tthe element at index "); 50 | Serial.print(i); 51 | Serial.print(" is: "); 52 | Serial.println(vi[i]); 53 | } 54 | 55 | Person* p1 = new Person("123-45-6789"); 56 | p1->set_name("Zac"); 57 | Person* p2 = new Person("111-22-3333"); 58 | p2->set_name("Elissa"); 59 | Person* p3 = new Person("333-22-4444"); 60 | p3->set_name("Parker"); 61 | 62 | 63 | Vector vp; 64 | vp.push_back(p1); 65 | vp.push_back(p2); 66 | vp.push_back(p3); 67 | 68 | Serial.print("size of the person vector is: "); 69 | Serial.println(vp.size() ); 70 | Serial.print("Capacity of the person vector is: "); 71 | Serial.println(vp.capacity() ); 72 | for(int i = 0; i < vp.size(); ++i) { 73 | Serial.print("the person at "); 74 | Serial.print(i); 75 | Serial.print(" is: "); 76 | Serial.println( vp[i]->name() ); 77 | } 78 | 79 | //set size at construction 80 | Serial.println("-------------------------------"); 81 | Vector v_sized(3); 82 | Serial.print("size of the space constructed vector is: "); 83 | Serial.println(v_sized.size() ); 84 | Serial.print("Capacity of the space constructed vector is: "); 85 | Serial.println(v_sized.capacity() ); 86 | 87 | } 88 | 89 | void loop() { 90 | //create and destroy a local vector many times to see if the 91 | //Arduino crashes...which it does. 92 | //I believe this crash is from segmentation, but I have not 93 | //done any checks to verify this. 94 | 95 | Serial.println("--------------------------"); 96 | Vector vc; 97 | vc.push_back("first"); 98 | vc.push_back("second"); 99 | vc.push_back("third"); 100 | for(int i = 0; i < vc.size(); ++i) { 101 | Serial.print("the data at "); 102 | Serial.print(i); 103 | Serial.print(" is: "); 104 | Serial.println( vc[i] ); 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /libraries/EIoTCloudRestApiV1.1/EIoTCloudRestApiV1.0.h: -------------------------------------------------------------------------------- 1 | /* 2 | Created by Igor Jarc 3 | See http://iot-playground.com for details 4 | Please use community forum on website do not contact author directly 5 | 6 | 7 | This program is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU General Public License 9 | version 2 as published by the Free Software Foundation. 10 | 11 | Changelog 12 | ---------------- 13 | 10.02.2016 - first version 14 | 04.03.2016 - added SetParameterValues 15 | 22.08.2016 - set max number of wifi connection attempts inside wifiConnect(). 16 | Consolidate cpp code into the header so that EIoTCloudRestApiConfig.h is not required. 17 | Code cleanup: @micooke 18 | */ 19 | #ifndef EIoTCloudRestApi_h 20 | #define EIoTCloudRestApi_h 21 | 22 | #include 23 | 24 | #ifdef _DEBUG 25 | #define debug(x,...) Serial.print(x, ##__VA_ARGS__) 26 | #define debugln(x,...) Serial.println(x, ##__VA_ARGS__) 27 | #else 28 | #define debug(x,...) 29 | #define debugln(x,...) 30 | #endif 31 | 32 | #define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com" 33 | #define EIOT_CLOUD_PORT 40404 34 | 35 | //#define EIOT_CLOUD_ADDRESS "192.168.88.110" 36 | //#define EIOT_CLOUD_PORT 12345 37 | 38 | class EIoTCloudRestApi 39 | { 40 | private: 41 | const char* _ssid; 42 | const char* _password; 43 | String _token; 44 | uint8_t _wifi_max_attempts; 45 | uint32_t _wifi_interval; 46 | 47 | public: 48 | EIoTCloudRestApi(const uint8_t wifi_max_attempts = 5, const uint32_t wifi_interval = 1000) : _wifi_max_attempts(wifi_max_attempts), _wifi_interval(wifi_interval) {} 49 | bool begin(const char* ssid, const char* password, String token); 50 | bool begin(const char* ssid, const char* password) { return begin(ssid, password, ""); } 51 | 52 | String TokenNew(String instance); 53 | bool TokenList(String instance, int *, String**); 54 | void SetToken(String); 55 | String GetToken(); 56 | 57 | String ModuleNew(); 58 | bool SetModuleType(String id, String moduleType); 59 | bool SetModuleName(String id, String name); 60 | String NewModuleParameter(String id); 61 | String NewModuleParameter(String id, String name); 62 | 63 | String GetModuleParameterByName(String id, String parameterName); 64 | 65 | bool SetParameterName(String parameterId, String name); 66 | String GetParameterName(String parameterId); 67 | bool SetParameterDescription(String parameterId, String description); 68 | String GetParameterDescription(String parameterId); 69 | bool SetParameterUnit(String parameterId, String unit); 70 | String GetParameterUnit(String parameterId); 71 | bool SetParameterUINotification(String parameterId, bool uiNotification); 72 | String GetParameterUINotification(String parameterId); 73 | bool SetParameterLogToDatabase(String parameterId, bool logToDatabase); 74 | String GetParameterLogToDatabase(String parameterId); 75 | bool SetParameterAverageInterval(String parameterId, String avgInterval); 76 | String GetParameterAverageInterval(String parameterId); 77 | bool SetParameterChartSteps(String parameterId, bool chartSteps); 78 | String GetParameterChartSteps(String parameterId); 79 | bool SetParameterValue(String parameterId, String value); 80 | String GetParameterValue(String parameterId); 81 | 82 | bool SetParameterValues(String values); 83 | 84 | private: 85 | String parseId(WiFiClient* client); 86 | bool parseResponse(WiFiClient* client); 87 | bool setParameterProperty(String parameterId, String property, String value); 88 | bool setParameterProperty(String parameterId, String property, bool value); 89 | 90 | String getParameterProperty(String parameterId, String property); 91 | String parseParameter(WiFiClient* client, String property); 92 | 93 | protected: 94 | bool wifiConnect(); 95 | }; 96 | 97 | bool EIoTCloudRestApi::begin(const char* ssid, const char* password, String token) 98 | { 99 | _ssid = ssid; 100 | _password = password; 101 | _token = token; 102 | 103 | return wifiConnect(); 104 | } 105 | 106 | 107 | String EIoTCloudRestApi::TokenNew(String instance) 108 | { 109 | String ret = ""; 110 | 111 | WiFiClient client; 112 | 113 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 114 | { 115 | debug(F("connection failed")); 116 | wifiConnect(); 117 | } 118 | 119 | debugln(F("\r\nGET /RestApi/v1.0/Token/New")); 120 | 121 | client.print(String("POST /RestApi/v1.0/Token/New HTTP/1.1\r\n") + 122 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 123 | "EIOT-Instance: " + String(instance) + "\r\n" + 124 | "Connection: close\r\n" + 125 | "Content-Length: 0\r\n" + 126 | "\r\n"); 127 | 128 | while (!client.available()); 129 | //delay(300); 130 | String line = ""; 131 | while (client.available()) { 132 | line += client.readStringUntil('\r'); 133 | } 134 | 135 | debug(line.c_str()); 136 | 137 | int pos = 0; 138 | 139 | while ((pos = line.indexOf('{', pos)) != -1) 140 | { 141 | if (line.substring(pos, pos + 10) == "{\"Token\":\"") 142 | { 143 | int ix1 = line.indexOf("\"}", pos + 11); 144 | 145 | if (ix1 != -1) 146 | { 147 | ret = line.substring(pos + 10, ix1); 148 | debug(F("\r\n")); 149 | debugln(ret.c_str()); 150 | } 151 | break; 152 | } 153 | else 154 | pos++; 155 | } 156 | 157 | _token = ret; 158 | 159 | return ret; 160 | } 161 | 162 | // list all tokens 163 | bool EIoTCloudRestApi::TokenList(String instance, int *ptrTokenCnt, String** ptrArr) 164 | { 165 | WiFiClient client; 166 | 167 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 168 | { 169 | debug(F("connection failed")); 170 | wifiConnect(); 171 | } 172 | 173 | debug(F("GET TokenList: ")); 174 | 175 | client.print(String("GET /RestApi/v1.0/TokenList HTTP/1.1\r\n") + 176 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 177 | "EIOT-Instance: " + String(instance) + "\r\n" + 178 | "Connection: close\r\n" + 179 | "Content-Length: 0\r\n" + 180 | "\r\n"); 181 | 182 | while (!client.available()); 183 | //delay(300); 184 | String line = ""; 185 | while (client.available()) 186 | { 187 | line += client.readStringUntil('\r'); 188 | } 189 | 190 | int ix = line.indexOf('['); 191 | 192 | if (ix != -1) 193 | line = line.substring(ix); 194 | 195 | int tokenCnt = 0; 196 | int pos = 0; 197 | 198 | while ((pos = line.indexOf('T', pos)) != -1) 199 | { 200 | if (line.substring(pos, pos + 8) == "Token\":\"") 201 | { 202 | tokenCnt++; 203 | } 204 | pos++; 205 | } 206 | debug(F("tokenCnt : ")); 207 | debugln(tokenCnt); 208 | 209 | String tokens[tokenCnt]; 210 | 211 | *ptrTokenCnt = tokenCnt; 212 | ptrArr = (String **)&tokens; 213 | 214 | pos = 0; 215 | int ix1; 216 | int i = 0; 217 | while ((pos = line.indexOf('T', pos)) != -1) 218 | { 219 | if (line.substring(pos, pos + 8) == "Token\":\"") 220 | { 221 | line = line.substring(pos + 8); 222 | ix1 = line.indexOf("\"}"); 223 | 224 | debugln(line.substring(0, ix1)); 225 | 226 | tokens[i] = line.substring(0, ix1); 227 | i++; 228 | pos = 0; 229 | } 230 | else 231 | pos++; 232 | } 233 | return true; 234 | } 235 | 236 | void EIoTCloudRestApi::SetToken(String token) 237 | { 238 | _token = token; 239 | } 240 | 241 | String EIoTCloudRestApi::GetToken() 242 | { 243 | return _token; 244 | } 245 | 246 | String EIoTCloudRestApi::ModuleNew() 247 | { 248 | WiFiClient client; 249 | 250 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 251 | { 252 | debug(F("connection failed")); 253 | wifiConnect(); 254 | } 255 | 256 | debug(F("\r\nGET /RestApi/v1.0/Module/New\r\n")); 257 | 258 | client.print(String("POST /RestApi/v1.0/Module/New HTTP/1.1\r\n") + 259 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 260 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 261 | "Connection: close\r\n" + 262 | "Content-Length: 0\r\n" + 263 | "\r\n"); 264 | 265 | return parseId(&client); 266 | } 267 | 268 | bool EIoTCloudRestApi::SetModuleType(String id, String moduleType) 269 | { 270 | WiFiClient client; 271 | 272 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 273 | { 274 | debug(F("connection failed")); 275 | wifiConnect(); 276 | } 277 | 278 | String url = "POST /RestApi/v1.0/Module/" + id + "/Type/" + moduleType; 279 | debug(F("\r\n")); 280 | debugln(url.c_str()); 281 | 282 | client.print(String(url + " HTTP/1.1\r\n") + 283 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 284 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 285 | "Connection: close\r\n" + 286 | "Content-Length: 0\r\n" + 287 | "\r\n"); 288 | 289 | return parseResponse(&client); 290 | } 291 | 292 | bool EIoTCloudRestApi::SetModuleName(String id, String name) 293 | { 294 | WiFiClient client; 295 | 296 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 297 | { 298 | debug(F("connection failed")); 299 | wifiConnect(); 300 | } 301 | 302 | name.replace(" ", "%20"); 303 | 304 | String url = "POST /RestApi/v1.0/Module/" + id + "/Name/" + name; 305 | debug(F("\r\n")); 306 | debugln(url.c_str()); 307 | 308 | client.print(String(url + " HTTP/1.1\r\n") + 309 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 310 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 311 | "Connection: close\r\n" + 312 | "Content-Length: 0\r\n" + 313 | "\r\n"); 314 | 315 | return parseResponse(&client); 316 | } 317 | 318 | String EIoTCloudRestApi::NewModuleParameter(String id) 319 | { 320 | WiFiClient client; 321 | 322 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 323 | { 324 | debug(F("connection failed")); 325 | wifiConnect(); 326 | } 327 | 328 | String url = "POST /RestApi/v1.0/Module/" + id + "/NewParameter"; 329 | debug(F("\r\n")); 330 | debugln(url.c_str()); 331 | 332 | client.print(String(url + " HTTP/1.1\r\n") + 333 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 334 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 335 | "Connection: close\r\n" + 336 | "Content-Length: 0\r\n" + 337 | "\r\n"); 338 | 339 | return parseId(&client); 340 | } 341 | 342 | String EIoTCloudRestApi::NewModuleParameter(String id, String name) 343 | { 344 | WiFiClient client; 345 | 346 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 347 | { 348 | debug(F("connection failed")); 349 | wifiConnect(); 350 | } 351 | 352 | String url = "POST /RestApi/v1.0/Module/" + id + "/NewParameter/" + name; 353 | debug(F("\r\n")); 354 | debugln(url.c_str()); 355 | 356 | client.print(String(url + " HTTP/1.1\r\n") + 357 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 358 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 359 | "Connection: close\r\n" + 360 | "Content-Length: 0\r\n" + 361 | "\r\n"); 362 | 363 | return parseId(&client); 364 | } 365 | 366 | String EIoTCloudRestApi::GetModuleParameterByName(String id, String parameterName) 367 | { 368 | WiFiClient client; 369 | 370 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 371 | { 372 | debug(F("connection failed")); 373 | wifiConnect(); 374 | } 375 | 376 | String url = "GET /RestApi/v1.0/Module/" + id + "/ParameterByName/" + parameterName; 377 | debug(F("\r\n")); 378 | debugln(url.c_str()); 379 | 380 | client.print(String(url + " HTTP/1.1\r\n") + 381 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 382 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 383 | "Connection: close\r\n" + 384 | "Content-Length: 0\r\n" + 385 | "\r\n"); 386 | 387 | return parseId(&client); 388 | } 389 | 390 | bool EIoTCloudRestApi::SetParameterValue(String parameterId, String value) 391 | { 392 | return setParameterProperty(parameterId, "Value", value); 393 | } 394 | 395 | String EIoTCloudRestApi::GetParameterValue(String parameterId) 396 | { 397 | return getParameterProperty(parameterId, "Value"); 398 | } 399 | 400 | bool EIoTCloudRestApi::SetParameterName(String parameterId, String name) 401 | { 402 | name.replace(" ", "%20"); 403 | return setParameterProperty(parameterId, "Name", name); 404 | } 405 | 406 | String EIoTCloudRestApi::GetParameterName(String parameterId) 407 | { 408 | String name = getParameterProperty(parameterId, "Name"); 409 | 410 | name.replace("%20", " "); 411 | return name; 412 | } 413 | 414 | bool EIoTCloudRestApi::SetParameterDescription(String parameterId, String description) 415 | { 416 | description.replace(" ", "%20"); 417 | return setParameterProperty(parameterId, "Description", description); 418 | } 419 | 420 | String EIoTCloudRestApi::GetParameterDescription(String parameterId) 421 | { 422 | String description = getParameterProperty(parameterId, "Description"); 423 | description.replace("%20", " "); 424 | return description; 425 | } 426 | 427 | bool EIoTCloudRestApi::SetParameterUnit(String parameterId, String unit) 428 | { 429 | return setParameterProperty(parameterId, "Unit", unit); 430 | } 431 | 432 | String EIoTCloudRestApi::GetParameterUnit(String parameterId) 433 | { 434 | return getParameterProperty(parameterId, "Unit"); 435 | } 436 | 437 | bool EIoTCloudRestApi::SetParameterUINotification(String parameterId, bool uiNotification) 438 | { 439 | return setParameterProperty(parameterId, "UINotification", uiNotification); 440 | } 441 | 442 | String EIoTCloudRestApi::GetParameterUINotification(String parameterId) 443 | { 444 | return getParameterProperty(parameterId, "UINotification"); 445 | } 446 | 447 | bool EIoTCloudRestApi::SetParameterLogToDatabase(String parameterId, bool logToDatabase) 448 | { 449 | return setParameterProperty(parameterId, "LogToDatabase", logToDatabase); 450 | } 451 | 452 | String EIoTCloudRestApi::GetParameterLogToDatabase(String parameterId) 453 | { 454 | return getParameterProperty(parameterId, "LogToDatabase"); 455 | } 456 | 457 | 458 | bool EIoTCloudRestApi::SetParameterAverageInterval(String parameterId, String avgInterval) 459 | { 460 | return setParameterProperty(parameterId, "AverageInterval", avgInterval); 461 | } 462 | 463 | String EIoTCloudRestApi::GetParameterAverageInterval(String parameterId) 464 | { 465 | return getParameterProperty(parameterId, "AverageInterval"); 466 | } 467 | 468 | bool EIoTCloudRestApi::SetParameterChartSteps(String parameterId, bool chartSteps) 469 | { 470 | return setParameterProperty(parameterId, "ChartSteps", chartSteps); 471 | } 472 | 473 | String EIoTCloudRestApi::GetParameterChartSteps(String parameterId) 474 | { 475 | return getParameterProperty(parameterId, "ChartSteps"); 476 | } 477 | 478 | String EIoTCloudRestApi::getParameterProperty(String parameterId, String property) 479 | { 480 | 481 | WiFiClient client; 482 | 483 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 484 | { 485 | debug(F("connection failed")); 486 | wifiConnect(); 487 | } 488 | 489 | 490 | String url = "GET /RestApi/v1.0/Parameter/" + parameterId + "/" + property; 491 | debug(F("\r\n")); 492 | debugln(url.c_str()); 493 | 494 | client.print(String(url + " HTTP/1.1\r\n") + 495 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 496 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 497 | "Connection: close\r\n" + 498 | "Content-Length: 0\r\n" + 499 | "\r\n"); 500 | 501 | return parseParameter(&client, property); 502 | } 503 | 504 | String EIoTCloudRestApi::parseParameter(WiFiClient* client, String property) 505 | { 506 | String ret = ""; 507 | 508 | while (!client->available()); 509 | //delay(300); 510 | String line = ""; 511 | while (client->available()) 512 | { 513 | line += client->readStringUntil('\r'); 514 | } 515 | 516 | debug(line.c_str()); 517 | 518 | int pos = 0; 519 | int len = line.length(); 520 | int lenProp = property.length(); 521 | 522 | 523 | //#ifdef _DEBUG 524 | // debug(F("\r\nlen:\r\n")); 525 | // String(len).toCharArray(buff, 300); 526 | // debug(buff); 527 | // 528 | // debug(F("\r\nlenProp:\r\n")); 529 | // String(lenProp).toCharArray(buff, 300); 530 | // debug(buff); 531 | //#endif 532 | 533 | String propStr = "\"" + property + "\":\""; 534 | //#ifdef _DEBUG 535 | // debug(F("\r\npropStr:\r\n")); 536 | // propStr.toCharArray(buff, 300); 537 | // debug(buff); 538 | //#endif 539 | 540 | int pos1 = 0; 541 | 542 | while (pos < len) 543 | { 544 | if (line.substring(pos, pos + 4 + lenProp) == propStr) 545 | { 546 | pos = pos + 4 + lenProp; 547 | pos1 = pos; 548 | 549 | while (pos < len) 550 | { 551 | if ((pos = line.indexOf('\"', pos)) != -1) 552 | { 553 | //#ifdef _DEBUG 554 | // debug(F("\r\nPos 2:\r\n")); 555 | // String(pos).toCharArray(buff, 300); 556 | // debug(buff); 557 | //#endif 558 | 559 | ret = line.substring(pos1, pos); 560 | debug(F("\r\n")); 561 | debugln(ret.c_str()); 562 | break; 563 | } 564 | else 565 | break; 566 | } 567 | break; 568 | } 569 | else 570 | pos++; 571 | } 572 | 573 | return ret; 574 | } 575 | 576 | bool EIoTCloudRestApi::SetParameterValues(String values) 577 | { 578 | WiFiClient client; 579 | 580 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 581 | { 582 | debug(F("connection failed")); 583 | wifiConnect(); 584 | } 585 | 586 | String url = "POST /RestApi/v1.0/Parameter/Values"; 587 | debug(F("\r\n")); 588 | debugln(url.c_str()); 589 | 590 | client.print(String(url + " HTTP/1.1\r\n") + 591 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 592 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 593 | "Connection: close\r\n" + 594 | "Content-Length: " + values.length() + 595 | "\n\n" + 596 | values + 597 | "\r\n"); 598 | 599 | return parseResponse(&client); 600 | } 601 | 602 | bool EIoTCloudRestApi::setParameterProperty(String parameterId, String property, String value) 603 | { 604 | WiFiClient client; 605 | 606 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 607 | { 608 | debug(F("connection failed")); 609 | wifiConnect(); 610 | } 611 | 612 | String url = "POST /RestApi/v1.0/Parameter/" + parameterId + "/" + property + "/" + value; 613 | debug(F("\r\n")); 614 | debugln(url.c_str()); 615 | 616 | client.print(String(url + " HTTP/1.1\r\n") + 617 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 618 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 619 | "Connection: close\r\n" + 620 | "Content-Length: 0\r\n" + 621 | "\r\n"); 622 | 623 | return parseResponse(&client); 624 | } 625 | 626 | bool EIoTCloudRestApi::setParameterProperty(String parameterId, String property, bool value) 627 | { 628 | WiFiClient client; 629 | 630 | while (!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) 631 | { 632 | debug(F("connection failed")); 633 | wifiConnect(); 634 | } 635 | 636 | String url = "POST /RestApi/v1.0/Parameter/" + parameterId + "/" + property + "/"; 637 | 638 | url += (value) ? "true" : "false"; 639 | 640 | debug(F("\r\n")); 641 | debugln(url.c_str()); 642 | 643 | client.print(String(url + " HTTP/1.1\r\n") + 644 | "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 645 | "EIOT-AuthToken: " + String(_token) + "\r\n" + 646 | "Connection: close\r\n" + 647 | "Content-Length: 0\r\n" + 648 | "\r\n"); 649 | 650 | return parseResponse(&client); 651 | } 652 | 653 | String EIoTCloudRestApi::parseId(WiFiClient* client) 654 | { 655 | String ret = ""; 656 | 657 | while (!client->available()); 658 | //delay(300); 659 | String line = ""; 660 | while (client->available()) 661 | { 662 | line += client->readStringUntil('\r'); 663 | } 664 | 665 | debug(line.c_str()); 666 | 667 | int pos = 0; 668 | 669 | while ((pos = line.indexOf('{', pos)) != -1) 670 | { 671 | if (line.substring(pos, pos + 7) == "{\"Id\":\"") 672 | { 673 | int ix1 = line.indexOf("\"}", pos + 8); 674 | 675 | if (ix1 != -1) 676 | { 677 | ret = line.substring(pos + 7, ix1); 678 | debug(F("\r\n")); 679 | debugln(ret.c_str()); 680 | } 681 | break; 682 | } 683 | else 684 | pos++; 685 | } 686 | 687 | return ret; 688 | } 689 | 690 | bool EIoTCloudRestApi::parseResponse(WiFiClient* client) 691 | { 692 | while (!client->available()); 693 | //delay(300); 694 | String line = ""; 695 | while (client->available()) 696 | { 697 | line += client->readStringUntil('\r'); 698 | } 699 | 700 | debug(line.c_str()); 701 | 702 | int pos = 0; 703 | 704 | if ((pos = line.indexOf('{', pos)) != -1) 705 | { 706 | if (line.substring(pos, pos + 16) == "{\"Response\":\"0\"}") 707 | { 708 | return true; 709 | } 710 | } 711 | 712 | return false; 713 | } 714 | 715 | bool EIoTCloudRestApi::wifiConnect() 716 | { 717 | uint8_t connection_attempt_ = 0; 718 | debug(F("Connecting to AP")); 719 | WiFi.begin(_ssid, _password); 720 | 721 | while ((WiFi.status() != WL_CONNECTED) & (connection_attempt_++ < _wifi_max_attempts)) 722 | { 723 | delay(_wifi_interval); 724 | debug("."); 725 | } 726 | 727 | if (WiFi.status() == WL_CONNECTED) 728 | { 729 | debugln(F("\nWiFi connection succeeded")); 730 | } 731 | else 732 | { 733 | debugln(F("\nWiFi connection failed")); 734 | } 735 | 736 | return (WiFi.status() == WL_CONNECTED); 737 | } 738 | 739 | #endif -------------------------------------------------------------------------------- /libraries/EIoTCloudRestApiV1.1/EIoTCloudRestApiV1.1.h: -------------------------------------------------------------------------------- 1 | /* 2 | Created by Igor Jarc 3 | See http://iot-playground.com for details 4 | Please use community forum on website do not contact author directly 5 | 6 | 7 | This program is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU General Public License 9 | version 2 as published by the Free Software Foundation. 10 | 11 | Changelog 12 | ---------------- 13 | 10.02.2016 - @iot-playground : first version 14 | 04.03.2016 - @iot-playground : added SetParameterValues 15 | 22.08.2016 - @micooke : set max number of wifi connection attempts inside wifiConnect() 16 | Consolidated cpp code into the header. Modified to use _DEBUG define (MSVC++ standard) instead of DEBUG 17 | 31.08.2016 - @micooke : Consolidated all API calls into two main functions. 18 | Changed TokenList to use the Vector class (https://github.com/zacsketches/Arduino_Vector) 19 | Added SetParameterValues(Vector &Id, Vector &Value) 20 | */ 21 | #ifndef EIoTCloudRestApi_h 22 | #define EIoTCloudRestApi_h 23 | 24 | #include 25 | #include 26 | 27 | #ifdef _DEBUG 28 | #define debug(x,...) Serial.print(x, ##__VA_ARGS__) 29 | #define debugln(x,...) Serial.println(x, ##__VA_ARGS__) 30 | #else 31 | #define debug(x,...) 32 | #define debugln(x,...) 33 | #endif 34 | 35 | //#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com" //"192.168.88.110" 36 | //#define EIOT_CLOUD_PORT 40404 // 12345 37 | 38 | String REST_API_INSTANCE = String("%GET_POST% /RestApi/v1.0/%COMMAND% HTTP/1.1\r\n") + 39 | "Host: %EIOT_ADDRESS%\r\n" + 40 | "EIOT-Instance: %INSTANCE%\r\n" + 41 | "Connection: close\r\n" + 42 | "Content-Length: 0\r\n\r\n"; 43 | 44 | String REST_API_AUTH_TOKEN = String("%GET_POST% /RestApi/v1.0/%TYPE%/%COMMAND% HTTP/1.1\r\n") + 45 | "Host: %EIOT_ADDRESS%\r\n" + 46 | "EIOT-AuthToken: %TOKEN%\r\n" + 47 | "Connection: close\r\n" + 48 | "Content-Length: %LENGTH%\r\n" + 49 | "%CONTENT%\r\n"; 50 | 51 | class EIoTCloudRestApi 52 | { 53 | private: 54 | String _ssid; 55 | String _password; 56 | String _token; 57 | uint8_t _wifi_max_attempts; 58 | uint32_t _wifi_interval; 59 | String _EIoT_Address; 60 | uint16_t _EIoT_Port; 61 | 62 | public: 63 | EIoTCloudRestApi(String _EIoT_Address = "cloud.iot-playground.com", uint16_t _EIoT_Port = 40404, const uint8_t wifi_max_attempts = 10, const uint32_t wifi_interval = 2000) : _EIoT_Address(_EIoT_Address), _EIoT_Port(_EIoT_Port), _wifi_max_attempts(wifi_max_attempts), _wifi_interval(wifi_interval) {} 64 | 65 | bool begin(String ssid, String password, String token); 66 | bool begin(String ssid, String password) { return begin(ssid, password, ""); } 67 | 68 | String TokenNew(String instance); 69 | bool TokenList(String instance, Vector tokens); 70 | void SetToken(String token) { _token = token; } 71 | String GetToken() { return _token; } 72 | 73 | String ModuleNew(); 74 | bool SetModuleType(String id, String moduleType); 75 | bool SetModuleName(String id, String name); 76 | String NewModuleParameter(String id); 77 | String NewModuleParameter(String id, String name); 78 | 79 | String GetModuleParameterByName(String id, String parameterName); 80 | 81 | bool SetParameterName(String id, String name); 82 | String GetParameterName(String id); 83 | 84 | bool SetParameterDescription(String id, String description); 85 | String GetParameterDescription(String id); 86 | 87 | bool SetParameterUnit(String id, String unit) { return setParameterProperty(id, "Unit", unit); } 88 | String GetParameterUnit(String id) { return getParameterProperty(id, "Unit"); } 89 | 90 | bool SetParameterUINotification(String id, bool uiNotification) { return setParameterProperty(id, "UINotification", uiNotification); } 91 | String GetParameterUINotification(String id) { return getParameterProperty(id, "UINotification"); } 92 | 93 | bool SetParameterLogToDatabase(String id, bool logToDatabase) { return setParameterProperty(id, "LogToDatabase", logToDatabase); } 94 | String GetParameterLogToDatabase(String id) { return getParameterProperty(id, "LogToDatabase"); } 95 | 96 | bool SetParameterAverageInterval(String id, String avgInterval) { return setParameterProperty(id, "AverageInterval", avgInterval); } 97 | String GetParameterAverageInterval(String id) { return getParameterProperty(id, "AverageInterval"); } 98 | 99 | bool SetParameterChartSteps(String id, bool chartSteps) { return setParameterProperty(id, "ChartSteps", chartSteps); } 100 | String GetParameterChartSteps(String id) { return getParameterProperty(id, "ChartSteps"); } 101 | 102 | bool SetParameterValue(String id, String value) { return setParameterProperty(id, "Value", value); } 103 | String GetParameterValue(String id) { return getParameterProperty(id, "Value"); } 104 | 105 | bool SetParameterValues(String values); 106 | bool SetParameterValues(Vector &Id, Vector &Value); 107 | 108 | private: 109 | String parseId(String response); 110 | bool parseResponse(String response); 111 | String parseParameter(String response, String property); 112 | 113 | String getParameterProperty(String id, String property); 114 | 115 | bool setParameterProperty(String id, String property, String value); 116 | bool setParameterProperty(String id, String property, bool value); 117 | 118 | String RestApi_Instance(String getORpost, String command, String Instance); 119 | String RestApi_AuthToken(String getORpost, String id, String command, String token, String type = "Module", String content = ""); 120 | 121 | protected: 122 | bool wifiConnect(); 123 | }; 124 | 125 | bool EIoTCloudRestApi::begin(String ssid, String password, String token) 126 | { 127 | _ssid = ssid; 128 | _password = password; 129 | _token = token; 130 | 131 | return wifiConnect(); 132 | } 133 | 134 | bool EIoTCloudRestApi::wifiConnect() 135 | { 136 | uint8_t connection_attempt_ = 0; 137 | debug(F("Connecting to AP")); 138 | WiFi.begin(_ssid.c_str(), _password.c_str()); 139 | 140 | while ((WiFi.status() != WL_CONNECTED) & (connection_attempt_++ < _wifi_max_attempts)) 141 | { 142 | delay(_wifi_interval); 143 | debug("."); 144 | } 145 | 146 | if (WiFi.status() == WL_CONNECTED) 147 | { 148 | debugln(F("\nWiFi connection succeeded")); 149 | } 150 | else 151 | { 152 | debugln(F("\nWiFi connection failed")); 153 | } 154 | 155 | return (WiFi.status() == WL_CONNECTED); 156 | } 157 | 158 | bool EIoTCloudRestApi::SetParameterName(String id, String name) 159 | { 160 | name.replace(" ", "%20"); 161 | return setParameterProperty(id, "Name", name); 162 | } 163 | 164 | String EIoTCloudRestApi::GetParameterName(String id) 165 | { 166 | String name = getParameterProperty(id, "Name"); 167 | 168 | name.replace("%20", " "); 169 | return name; 170 | } 171 | 172 | bool EIoTCloudRestApi::SetParameterDescription(String id, String description) 173 | { 174 | description.replace(" ", "%20"); 175 | return setParameterProperty(id, "Description", description); 176 | } 177 | 178 | String EIoTCloudRestApi::GetParameterDescription(String id) 179 | { 180 | String description = getParameterProperty(id, "Description"); 181 | description.replace("%20", " "); 182 | return description; 183 | } 184 | 185 | // API calls 186 | String EIoTCloudRestApi::RestApi_Instance(String getORpost, String command, String instance) 187 | { 188 | WiFiClient client; 189 | 190 | uint8_t connection_attempts = 0; 191 | while (!client.connect(_EIoT_Address.c_str(), _EIoT_Port)) 192 | { 193 | debugln(F("connection failed")); 194 | wifiConnect(); 195 | 196 | if (++connection_attempts > _wifi_max_attempts) 197 | { 198 | debugln("RestApi_Instance : Maximum client connection attempts reached.\nReturning to main program"); 199 | return ""; 200 | } 201 | } 202 | 203 | // Generate the RestApi command 204 | String cmd = REST_API_INSTANCE; 205 | cmd.replace("%GET_POST%", getORpost); 206 | cmd.replace("%COMMAND%", command); 207 | cmd.replace("%EIOT_ADDRESS%", _EIoT_Address); 208 | cmd.replace("%INSTANCE%", instance); 209 | 210 | // Send the RestApi command 211 | client.print(cmd); 212 | debug(String(getORpost + " /RestApi/v1.0/ " + command + " : ").c_str()); 213 | 214 | // Get the RestApi response 215 | while (!client.available()); 216 | 217 | String response = ""; 218 | while (client.available()) 219 | { 220 | response += client.readStringUntil('\r'); 221 | } 222 | 223 | return response; 224 | } 225 | 226 | String EIoTCloudRestApi::RestApi_AuthToken(String getORpost, String id, String command, String token, String type, String content) 227 | { 228 | WiFiClient client; 229 | 230 | uint8_t connection_attempts = 0; 231 | while (!client.connect(_EIoT_Address.c_str(), _EIoT_Port)) 232 | { 233 | debugln(F("connection failed")); 234 | wifiConnect(); 235 | 236 | if (++connection_attempts > _wifi_max_attempts) 237 | { 238 | debugln("RestApi_AuthToken : Maximum client connection attempts reached.\nReturning to main program"); 239 | return ""; 240 | } 241 | } 242 | 243 | // Generate the RestApi command 244 | String cmd = REST_API_AUTH_TOKEN; 245 | cmd.replace("%GET_POST%", getORpost); 246 | cmd.replace("%TYPE%", type); 247 | 248 | if (id.length() == 0) 249 | { 250 | cmd.replace("%COMMAND%", command); 251 | debug(String(getORpost + " /RestApi/v1.0/" + type + "/" + command + " : ").c_str()); 252 | } 253 | else 254 | { 255 | cmd.replace("%COMMAND%", id + "/" + command); 256 | debug(String(getORpost + " /RestApi/v1.0/" + type + "/" + id + "/" + command + " : ").c_str()); 257 | } 258 | 259 | cmd.replace("%EIOT_ADDRESS%", _EIoT_Address); 260 | cmd.replace("%TOKEN%", token); 261 | cmd.replace("%LENGTH%", String(content.length())); 262 | cmd.replace("%CONTENT%", "\r\n" + content); 263 | debugln(cmd); 264 | 265 | // Send the RestApi command 266 | client.print(cmd); 267 | 268 | // Get the RestApi response 269 | while (!client.available()); 270 | String response = ""; 271 | while (client.available()) 272 | { 273 | response += client.readStringUntil('\r'); 274 | } 275 | return response; 276 | } 277 | 278 | String EIoTCloudRestApi::parseId(String response) 279 | { 280 | String ret = ""; 281 | int pos = 0; 282 | 283 | while ((pos = response.indexOf('{', pos)) != -1) 284 | { 285 | if (response.substring(pos, pos + 7) == "{\"Id\":\"") 286 | { 287 | int ix1 = response.indexOf("\"}", pos + 8); 288 | 289 | if (ix1 != -1) 290 | { 291 | ret = response.substring(pos + 7, ix1); 292 | debugln(ret.c_str()); 293 | } 294 | break; 295 | } 296 | else 297 | pos++; 298 | } 299 | 300 | return ret; 301 | } 302 | 303 | bool EIoTCloudRestApi::parseResponse(String response) 304 | { 305 | int pos = 0; 306 | 307 | if ((pos = response.indexOf('{', pos)) != -1) 308 | { 309 | if (response.substring(pos, pos + 16) == "{\"Response\":\"0\"}") 310 | { 311 | return true; 312 | } 313 | } 314 | 315 | return false; 316 | } 317 | 318 | String EIoTCloudRestApi::parseParameter(String response, String property) 319 | { 320 | String ret = ""; 321 | 322 | int pos = 0; 323 | int len = response.length(); 324 | int lenProp = property.length(); 325 | 326 | 327 | String propStr = "\"" + property + "\":\""; 328 | 329 | int pos1 = 0; 330 | 331 | while (pos < len) 332 | { 333 | if (response.substring(pos, pos + 4 + lenProp) == propStr) 334 | { 335 | pos = pos + 4 + lenProp; 336 | pos1 = pos; 337 | 338 | while (pos < len) 339 | { 340 | if ((pos = response.indexOf('\"', pos)) != -1) 341 | { 342 | ret = response.substring(pos1, pos); 343 | debugln(ret.c_str()); 344 | break; 345 | } 346 | else 347 | break; 348 | } 349 | break; 350 | } 351 | else 352 | pos++; 353 | } 354 | 355 | return ret; 356 | } 357 | 358 | String EIoTCloudRestApi::TokenNew(String instance) 359 | { 360 | String response = RestApi_Instance("POST", "Token/New", instance); 361 | 362 | String ret = ""; 363 | int pos = 0; 364 | while ((pos = response.indexOf('{', pos)) != -1) 365 | { 366 | if (response.substring(pos, pos + 10) == "{\"Token\":\"") 367 | { 368 | int ix1 = response.indexOf("\"}", pos + 11); 369 | 370 | if (ix1 != -1) 371 | { 372 | ret = response.substring(pos + 10, ix1); 373 | 374 | debugln(ret.c_str()); 375 | } 376 | break; 377 | } 378 | else 379 | pos++; 380 | } 381 | 382 | _token = ret; 383 | 384 | return ret; 385 | } 386 | 387 | bool EIoTCloudRestApi::TokenList(String instance, Vector tokens) 388 | { 389 | String response = RestApi_Instance("GET", "TokenList", instance); 390 | 391 | int ix = response.indexOf('['); 392 | 393 | if (ix != -1) 394 | response = response.substring(ix); 395 | 396 | int pos = 0; 397 | int ix1; 398 | int i = 0; 399 | while ((pos = response.indexOf('T', pos)) != -1) 400 | { 401 | if (response.substring(pos, pos + 8) == "Token\":\"") 402 | { 403 | response = response.substring(pos + 8); 404 | ix1 = response.indexOf("\"}"); 405 | 406 | tokens.push_back(response.substring(0, ix1)); 407 | 408 | debugln(tokens[i].c_str()); 409 | 410 | i++; 411 | pos = 0; 412 | } 413 | else 414 | pos++; 415 | } 416 | return (i > 0); 417 | } 418 | 419 | String EIoTCloudRestApi::ModuleNew() 420 | { 421 | String response = RestApi_AuthToken("POST", "", "New", _token); 422 | 423 | return parseId(response); 424 | } 425 | 426 | bool EIoTCloudRestApi::SetModuleType(String id, String moduleType) 427 | { 428 | String response = RestApi_AuthToken("POST", id, "Type/" + moduleType, _token); 429 | 430 | return parseResponse(response); 431 | } 432 | 433 | bool EIoTCloudRestApi::SetModuleName(String id, String name) 434 | { 435 | name.replace(" ", "%20"); 436 | 437 | String response = RestApi_AuthToken("POST", id, "Name/" + name, _token); 438 | 439 | return parseResponse(response); 440 | } 441 | 442 | String EIoTCloudRestApi::NewModuleParameter(String id) 443 | { 444 | String response = RestApi_AuthToken("POST", id, "NewParameter", _token); 445 | 446 | return parseId(response); 447 | } 448 | 449 | String EIoTCloudRestApi::NewModuleParameter(String id, String name) 450 | { 451 | //name.replace(" ", "%20"); // Addition? 452 | 453 | String response = RestApi_AuthToken("POST", id, "NewParameter/" + name, _token); 454 | 455 | return parseId(response); 456 | } 457 | 458 | String EIoTCloudRestApi::GetModuleParameterByName(String id, String name) 459 | { 460 | String response = RestApi_AuthToken("GET", id, "ParameterByName/" + name, _token); 461 | 462 | return parseId(response); 463 | } 464 | 465 | String EIoTCloudRestApi::getParameterProperty(String id, String property) 466 | { 467 | String response = RestApi_AuthToken("GET", id, property, _token, "Parameter"); 468 | 469 | return parseParameter(response, property); 470 | } 471 | 472 | bool EIoTCloudRestApi::SetParameterValues(String values) 473 | { 474 | String response = RestApi_AuthToken("POST", "", "Values", _token, "Parameter", values); 475 | 476 | return parseResponse(response); 477 | } 478 | 479 | bool EIoTCloudRestApi::SetParameterValues(Vector &Id, Vector &Value) 480 | { 481 | String content = "[{\"Id\": \"" + Id[0] + "\", \"Value\": \"" + Value[0] + "\" }"; 482 | for (uint8_t i = 1; i < Id.size(); ++i) 483 | { 484 | content += ",{\"Id\": \"" + Id[i] + "\", \"Value\": \"" + Value[i] + "\" }"; 485 | } 486 | content += "]"; 487 | 488 | String response = RestApi_AuthToken("POST", "", "Values", _token, "Parameter", content); 489 | 490 | return parseResponse(response); 491 | } 492 | 493 | bool EIoTCloudRestApi::setParameterProperty(String id, String property, String value) 494 | { 495 | String response = RestApi_AuthToken("POST", id, property + "/" + value, _token, "Parameter"); 496 | 497 | return parseResponse(response); 498 | } 499 | 500 | bool EIoTCloudRestApi::setParameterProperty(String id, String property, bool value) 501 | { 502 | String response = RestApi_AuthToken("POST", id, property + "/" + (value) ? "true" : "false", _token, "Parameter"); 503 | 504 | return parseResponse(response); 505 | } 506 | 507 | #endif -------------------------------------------------------------------------------- /libraries/EIoTCloudRestApiV1.1/examples/ESP8266_DHT22_humidity_sensor_REST_V1_0/ESP8266_DHT22_humidity_sensor_REST_V1_0.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EIoTCloudRestApiV1.0.h" 3 | #include 4 | #include "DHT.h" 5 | 6 | //#define DEBUG_PROG 7 | 8 | #ifdef DEBUG_PROG 9 | #define DEBUG_PRINTLN(x) Serial.println(x) 10 | #define DEBUG_PRINT(x) Serial.print(x) 11 | #else 12 | #define DEBUG_PRINTLN(x) 13 | #define DEBUG_PRINT(x) 14 | #endif 15 | 16 | 17 | EIoTCloudRestApi eiotcloud; 18 | 19 | // change those lines 20 | #define AP_USERNAME "xxx" 21 | #define AP_PASSWORD "xxx" 22 | #define INSTANCE_ID "xxx" 23 | 24 | 25 | #define CONFIG_START 0 26 | #define CONFIG_VERSION "v01" 27 | 28 | #define REPORT_INTERVAL 60 // in sec 29 | 30 | struct StoreStruct { 31 | // This is for mere detection if they are your settings 32 | char version[4]; 33 | // The variables of your settings 34 | char token[41]; 35 | uint moduleId; 36 | //bool tokenOk; // valid token 37 | } storage = { 38 | CONFIG_VERSION, 39 | // token 40 | "1234567890123456789012345678901234567890", 41 | // The default module 0 - invalid module 42 | 0, 43 | //0 // not valid 44 | }; 45 | 46 | float oldTemp; 47 | float oldHum; 48 | 49 | DHT dht; 50 | 51 | String moduleId = ""; 52 | String parameterId1 = ""; 53 | String parameterId2 = ""; 54 | 55 | 56 | void setup() { 57 | Serial.begin(115200); 58 | DEBUG_PRINTLN("Start..."); 59 | 60 | EEPROM.begin(512); 61 | loadConfig(); 62 | 63 | eiotcloud.begin(AP_USERNAME, AP_PASSWORD); 64 | 65 | // if first time get new token and register new module 66 | // here hapend Plug and play logic to add module to Cloud 67 | if (storage.moduleId == 0) 68 | { 69 | // get new token - alternarive is to manually create token and store it in EEPROM 70 | String token = eiotcloud.TokenNew(INSTANCE_ID); 71 | DEBUG_PRINT("Token: "); 72 | DEBUG_PRINTLN(token); 73 | eiotcloud.SetToken(token); 74 | 75 | // remember token 76 | token.toCharArray(storage.token, 41); 77 | 78 | // add new module and configure it 79 | moduleId = eiotcloud.ModuleNew(); 80 | DEBUG_PRINT("ModuleId: "); 81 | DEBUG_PRINTLN(moduleId); 82 | storage.moduleId = moduleId.toInt(); 83 | 84 | // set module type 85 | bool modtyperet = eiotcloud.SetModulType(moduleId, "MT_GENERIC"); 86 | DEBUG_PRINT("SetModulType: "); 87 | DEBUG_PRINTLN(modtyperet); 88 | 89 | // set module name 90 | bool modname = eiotcloud.SetModulName(moduleId, "Humidity sensor"); 91 | DEBUG_PRINT("SetModulName: "); 92 | DEBUG_PRINTLN(modname); 93 | 94 | // add image settings parameter 95 | String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1"); 96 | DEBUG_PRINT("parameterImgId: "); 97 | DEBUG_PRINTLN(parameterImgId); 98 | 99 | // set module image 100 | bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "humidity.png"); 101 | DEBUG_PRINT("SetParameterValue: "); 102 | DEBUG_PRINTLN(valueRet1); 103 | 104 | // now add parameter to display temperature 105 | parameterId1 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1"); 106 | DEBUG_PRINT("parameterId1: "); 107 | DEBUG_PRINTLN(parameterId1); 108 | 109 | //set parameter description 110 | bool valueRet2 = eiotcloud.SetParameterDescription(parameterId1, "Temperature"); 111 | DEBUG_PRINT("SetParameterDescription: "); 112 | DEBUG_PRINTLN(valueRet2); 113 | 114 | //set unit 115 | // see http://meyerweb.com/eric/tools/dencoder/ how to encode °C 116 | bool valueRet3 = eiotcloud.SetParameterUnit(parameterId1, "%C2%B0C"); 117 | DEBUG_PRINT("SetParameterUnit: "); 118 | DEBUG_PRINTLN(valueRet3); 119 | 120 | //Set parameter LogToDatabase 121 | bool valueRet4 = eiotcloud.SetParameterLogToDatabase(parameterId1, true); 122 | DEBUG_PRINT("SetLogToDatabase: "); 123 | DEBUG_PRINTLN(valueRet4); 124 | 125 | //SetAvreageInterval 126 | bool valueRet5 = eiotcloud.SetParameterAverageInterval(parameterId1, "10"); 127 | DEBUG_PRINT("SetAvreageInterval: "); 128 | DEBUG_PRINTLN(valueRet5); 129 | 130 | 131 | // now add parameter to display humidity 132 | parameterId2 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter2"); 133 | DEBUG_PRINT("parameterId2: "); 134 | DEBUG_PRINTLN(parameterId2); 135 | 136 | //set parameter description 137 | bool valueRet6 = eiotcloud.SetParameterDescription(parameterId2, "Humidity"); 138 | DEBUG_PRINT("SetParameterDescription: "); 139 | DEBUG_PRINTLN(valueRet2); 140 | 141 | //set unit 142 | bool valueRet7 = eiotcloud.SetParameterUnit(parameterId2, "%"); 143 | DEBUG_PRINT("SetParameterUnit: "); 144 | DEBUG_PRINTLN(valueRet7); 145 | 146 | //Set parameter LogToDatabase 147 | bool valueRet8 = eiotcloud.SetParameterLogToDatabase(parameterId2, true); 148 | DEBUG_PRINT("SetLogToDatabase: "); 149 | DEBUG_PRINTLN(valueRet8); 150 | 151 | //SetAvreageInterval 152 | bool valueRet9 = eiotcloud.SetParameterAverageInterval(parameterId2, "10"); 153 | DEBUG_PRINT("SetAvreageInterval: "); 154 | DEBUG_PRINTLN(valueRet9); 155 | 156 | // save configuration 157 | saveConfig(); 158 | } 159 | 160 | // if something went wrong, wiat here 161 | if (storage.moduleId == 0) 162 | delay(1); 163 | 164 | // read module ID from storage 165 | moduleId = String(storage.moduleId); 166 | // read token ID from storage 167 | eiotcloud.SetToken(storage.token); 168 | // read Sensor.Parameter1 ID from cloud 169 | parameterId1 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1"); 170 | DEBUG_PRINT("parameterId1: "); 171 | DEBUG_PRINTLN(parameterId1); 172 | 173 | parameterId2 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter2"); 174 | DEBUG_PRINT("parameterId2: "); 175 | DEBUG_PRINTLN(parameterId2); 176 | 177 | 178 | Serial.println(); 179 | Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)"); 180 | 181 | dht.setup(2); // data pin 2 182 | 183 | oldTemp = -1; 184 | oldHum = -1; 185 | } 186 | 187 | 188 | void loop() { 189 | delay(dht.getMinimumSamplingPeriod()); 190 | 191 | float hum = dht.getHumidity(); 192 | float temp = dht.getTemperature(); 193 | 194 | Serial.print(dht.getStatusString()); 195 | Serial.print("\t"); 196 | Serial.print(hum, 1); 197 | Serial.print("\t\t"); 198 | Serial.print(temp, 1); 199 | Serial.print("\t\t"); 200 | Serial.println(dht.toFahrenheit(temp), 1); 201 | 202 | if (temp != oldTemp || hum != oldHum) 203 | { 204 | //sendTeperature(temp); 205 | eiotcloud.SetParameterValues("[{\"Id\": \""+parameterId1+"\", \"Value\": \""+String(temp)+"\" },{\"Id\": \""+parameterId2+"\", \"Value\": \""+String(hum)+"\" }]"); 206 | oldTemp = temp; 207 | oldHum = hum; 208 | } 209 | 210 | int cnt = REPORT_INTERVAL; 211 | 212 | while(cnt--) 213 | delay(1000); 214 | 215 | } 216 | 217 | 218 | void loadConfig() { 219 | // To make sure there are settings, and they are YOURS! 220 | // If nothing is found it will use the default settings. 221 | if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] && 222 | EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] && 223 | EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2]) 224 | for (unsigned int t=0; t 2 | #include "EIoTCloudRestApiV1.0.h" 3 | #include 4 | #include 5 | #include 6 | 7 | #define DEBUG_PROG 8 | 9 | #ifdef DEBUG_PROG 10 | #define DEBUG_PRINTLN(x) Serial.println(x) 11 | #define DEBUG_PRINT(x) Serial.print(x) 12 | #else 13 | #define DEBUG_PRINTLN(x) 14 | #define DEBUG_PRINT(x) 15 | #endif 16 | 17 | 18 | EIoTCloudRestApi eiotcloud; 19 | 20 | // change those lines 21 | #define AP_USERNAME "xxxx" 22 | #define AP_PASSWORD "xxxx" 23 | #define INSTANCE_ID "xxxx" 24 | 25 | 26 | 27 | #define REPORT_INTERVAL 1 28 | 29 | #define ONE_WIRE_BUS 2 // DS18B20 pin 30 | OneWire oneWire(ONE_WIRE_BUS); 31 | DallasTemperature DS18B20(&oneWire); 32 | 33 | 34 | #define CONFIG_START 0 35 | #define CONFIG_VERSION "v01" 36 | 37 | struct StoreStruct { 38 | // This is for mere detection if they are your settings 39 | char version[4]; 40 | // The variables of your settings 41 | char token[41]; 42 | uint moduleId; 43 | //bool tokenOk; // valid token 44 | } storage = { 45 | CONFIG_VERSION, 46 | // token 47 | "1234567890123456789012345678901234567890", 48 | // The default module 0 - invalid module 49 | 0, 50 | //0 // not valid 51 | }; 52 | 53 | String moduleId = ""; 54 | String parameterId = ""; 55 | float tempOld = 0; 56 | 57 | void setup() { 58 | Serial.begin(115200); 59 | DEBUG_PRINTLN("Start..."); 60 | 61 | EEPROM.begin(512); 62 | loadConfig(); 63 | 64 | eiotcloud.begin(AP_USERNAME, AP_PASSWORD); 65 | 66 | // if first time get new token and register new module 67 | // here hapend Plug and play logic to add module to Cloud 68 | if (storage.moduleId == 0) 69 | { 70 | // get new token - alternarive is to manually create token and store it in EEPROM 71 | String token = eiotcloud.TokenNew(INSTANCE_ID); 72 | DEBUG_PRINT("Token: "); 73 | DEBUG_PRINTLN(token); 74 | eiotcloud.SetToken(token); 75 | 76 | // remember token 77 | token.toCharArray(storage.token, 41); 78 | 79 | // add new module and configure it 80 | moduleId = eiotcloud.ModuleNew(); 81 | DEBUG_PRINT("ModuleId: "); 82 | DEBUG_PRINTLN(moduleId); 83 | storage.moduleId = moduleId.toInt(); 84 | 85 | // set module type 86 | bool modtyperet = eiotcloud.SetModulType(moduleId, "MT_GENERIC"); 87 | DEBUG_PRINT("SetModulType: "); 88 | DEBUG_PRINTLN(modtyperet); 89 | 90 | // set module name 91 | bool modname = eiotcloud.SetModulName(moduleId, "Room temperature"); 92 | DEBUG_PRINT("SetModulName: "); 93 | DEBUG_PRINTLN(modname); 94 | 95 | // add image settings parameter 96 | String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1"); 97 | DEBUG_PRINT("parameterImgId: "); 98 | DEBUG_PRINTLN(parameterImgId); 99 | 100 | // set module image 101 | bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "temperature.png"); 102 | DEBUG_PRINT("SetParameterValue: "); 103 | DEBUG_PRINTLN(valueRet1); 104 | 105 | // now add parameter to display temperature 106 | parameterId = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1"); 107 | DEBUG_PRINT("ParameterId: "); 108 | DEBUG_PRINTLN(parameterId); 109 | 110 | //set parameter description 111 | bool valueRet2 = eiotcloud.SetParameterDescription(parameterId, "Temperature"); 112 | DEBUG_PRINT("SetParameterDescription: "); 113 | DEBUG_PRINTLN(valueRet2); 114 | 115 | //set unit 116 | // see http://meyerweb.com/eric/tools/dencoder/ how to encode °C 117 | bool valueRet3 = eiotcloud.SetParameterUnit(parameterId, "%C2%B0C"); 118 | DEBUG_PRINT("SetParameterUnit: "); 119 | DEBUG_PRINTLN(valueRet3); 120 | 121 | //Set parameter LogToDatabase 122 | bool valueRet4 = eiotcloud.SetParameterLogToDatabase(parameterId, true); 123 | DEBUG_PRINT("SetLogToDatabase: "); 124 | DEBUG_PRINTLN(valueRet4); 125 | 126 | //SetAvreageInterval 127 | bool valueRet5 = eiotcloud.SetParameterAverageInterval(parameterId, "10"); 128 | DEBUG_PRINT("SetAvreageInterval: "); 129 | DEBUG_PRINTLN(valueRet5); 130 | 131 | // save configuration 132 | saveConfig(); 133 | } 134 | 135 | // if something went wrong, wiat here 136 | if (storage.moduleId == 0) 137 | delay(1); 138 | 139 | // read module ID from storage 140 | moduleId = String(storage.moduleId); 141 | // read token ID from storage 142 | eiotcloud.SetToken(storage.token); 143 | // read Sensor.Parameter1 ID from cloud 144 | parameterId = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1"); 145 | DEBUG_PRINT("parameterId: "); 146 | DEBUG_PRINTLN(parameterId); 147 | } 148 | 149 | 150 | void loop() { 151 | float temp; 152 | 153 | do { 154 | DS18B20.requestTemperatures(); 155 | temp = DS18B20.getTempCByIndex(0); 156 | DEBUG_PRINT("Temperature: "); 157 | DEBUG_PRINTLN(temp); 158 | } while (temp == 85.0 || temp == (-127.0)); 159 | 160 | 161 | 162 | if (tempOld != temp) 163 | { 164 | // send temperature 165 | bool valueRet = eiotcloud.SetParameterValue(parameterId, String(temp)); 166 | DEBUG_PRINT("SetParameterValue: "); 167 | DEBUG_PRINTLN(valueRet); 168 | tempOld = temp; 169 | } 170 | delay(1000 * 60 * REPORT_INTERVAL); 171 | } 172 | 173 | 174 | void loadConfig() { 175 | // To make sure there are settings, and they are YOURS! 176 | // If nothing is found it will use the default settings. 177 | if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] && 178 | EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] && 179 | EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2]) 180 | for (unsigned int t=0; t 11 | #include 12 | //#include 13 | #include 14 | #include 15 | #include 16 | //#include 17 | #include 18 | 19 | // +----------------------+---------+---------+ 20 | // | Library version | FLASH | SRAM | 21 | // +----------------------+---------+---------+ 22 | // | EIoTCloudRestApiV1.0 | 250,194 | 33,880 | 23 | // | EIoTCloudRestApiV1.1 | 249,434 | 33,944 | 24 | // +----------------------+---------+---------+ 25 | 26 | EIoTCloudRestApi eiotcloud; 27 | 28 | const char* ssid = "AccessPoint"; 29 | const char* password = ""; 30 | const char* host = "OTA-EIoT"; 31 | 32 | #define INSTANCE_ID "57bccebdc943a0305d5aa4bb" 33 | #define CONFIG_START 0 34 | #define CONFIG_VERSION "v01" 35 | 36 | struct eepromStorage { 37 | // This is for mere detection if they are your settings 38 | char version[4]; 39 | // The variables of your settings 40 | char token[41]; 41 | uint moduleId; 42 | char parameterId1[17]; 43 | char parameterId2[17]; 44 | //bool tokenOk; // valid token 45 | } storage = { 46 | CONFIG_VERSION, 47 | // token 48 | "1234567890123456789012345678901234567890", 49 | // The default module 0 - invalid module 50 | 0, 51 | //0 // not valid 52 | }; 53 | 54 | String moduleId = ""; 55 | String parameterId1 = "0"; 56 | String parameterId2 = "0"; 57 | 58 | WiFiServer TelnetServer(8266); 59 | 60 | uint8_t _inc; 61 | uint32_t _t0; 62 | #define REPORT_INTERVAL 3000 63 | 64 | bool EIoT_setup(); 65 | void EIoT_update(const uint8_t _inc = 0); 66 | 67 | void setup() 68 | { 69 | WiFi.hostname(host); 70 | //wifi_station_set_hostname(host); 71 | TelnetServer.begin(); 72 | #ifdef _DEBUG 73 | Serial.begin(115200); 74 | #endif 75 | EEPROM.begin(512); 76 | loadConfig(); 77 | printConfig(); 78 | 79 | Serial.println("Booting"); 80 | 81 | // try connecting to the access point 82 | if (eiotcloud.begin(ssid, password)) 83 | { 84 | if (EIoT_setup() == false) 85 | { 86 | ESP.reset(); 87 | } 88 | 89 | EIoT_update(); 90 | 91 | ArduinoOTA.setHostname(host); 92 | ArduinoOTA.onStart([]() {}); 93 | ArduinoOTA.onEnd([]() {}); 94 | ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); }); 95 | 96 | /* setup the OTA server */ 97 | ArduinoOTA.begin(); 98 | Serial.println("Ready"); 99 | 100 | _t0 = millis(); 101 | } 102 | else 103 | { 104 | ESP.reset(); 105 | } 106 | } 107 | 108 | void loop() 109 | { 110 | ArduinoOTA.handle(); 111 | yield(); 112 | if (millis() - _t0 > REPORT_INTERVAL ) 113 | { 114 | EIoT_update(_inc); 115 | _inc = (_inc == 9) ? 0 : _inc + 1; 116 | _t0 = millis(); 117 | } 118 | } 119 | 120 | void EIoT_update(const uint8_t _inc) 121 | { 122 | const float batt_volts = 5 + float(_inc) / 10.0F; 123 | const float tempC = 12 + float(_inc) / 10.0F; 124 | 125 | Vector Id; 126 | Vector Value; 127 | Id.push_back(parameterId1); 128 | Value.push_back(String(tempC)); 129 | Id.push_back(parameterId2); 130 | Value.push_back(String(batt_volts)); 131 | 132 | String updateString = "[{\"Id\": \"" + parameterId1 + "\", \"Value\": \"" + String(tempC) + "\" },{\"Id\": \"" + parameterId2 + "\", \"Value\": \"" + String(batt_volts) + "\" }]"; 133 | DEBUG_PRINT("updateString : "); 134 | DEBUG_PRINTLN(updateString); 135 | 136 | eiotcloud.SetParameterValues(updateString); 137 | //eiotcloud.SetParameterValues(Id, Value); 138 | } 139 | 140 | bool EIoT_setup() 141 | { 142 | // if first time get new token and register new module 143 | // here hapend Plug and play logic to add module to Cloud 144 | if (storage.moduleId == 0) //*** set to != 0 to reset the moduleID and recreate in EasyIoT Cloud *** 145 | { 146 | // get new token - alternative is to manually create token and store it in EEPROM 147 | String token = eiotcloud.TokenNew(INSTANCE_ID); 148 | DEBUG_PRINT("Token: "); 149 | DEBUG_PRINTLN(token); 150 | eiotcloud.SetToken(token); 151 | 152 | // remember token 153 | token.toCharArray(storage.token, 41); 154 | 155 | // add new module and configure it 156 | moduleId = eiotcloud.ModuleNew(); 157 | DEBUG_PRINT("ModuleId: "); 158 | DEBUG_PRINTLN(moduleId); 159 | storage.moduleId = moduleId.toInt(); 160 | 161 | // set module type 162 | bool modtyperet = eiotcloud.SetModuleType(moduleId, "MT_GENERIC"); 163 | DEBUG_PRINT("SetModulType: "); 164 | DEBUG_PRINTLN(modtyperet); 165 | 166 | // set module name 167 | bool modname = eiotcloud.SetModuleName(moduleId, "Temp and Voltage monitor"); 168 | DEBUG_PRINT("SetModulName: "); 169 | DEBUG_PRINTLN(modname); 170 | 171 | // add image settings parameter 172 | String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1"); 173 | DEBUG_PRINT("parameterImgId: "); 174 | DEBUG_PRINTLN(parameterImgId); 175 | 176 | // set module image 177 | bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "analog.png"); 178 | DEBUG_PRINT("SetParameterValue: "); 179 | DEBUG_PRINTLN(valueRet1); 180 | 181 | // now add parameter to display temperature 182 | parameterId1 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1"); 183 | DEBUG_PRINT("parameterId1: "); 184 | DEBUG_PRINTLN(parameterId1); 185 | 186 | //set parameter description 187 | bool valueRet2 = eiotcloud.SetParameterDescription(storage.parameterId1, "Temperature"); 188 | DEBUG_PRINT("SetParameterDescription: "); 189 | DEBUG_PRINTLN(valueRet2); 190 | 191 | //set unit 192 | // see http://meyerweb.com/eric/tools/dencoder/ how to encode �C 193 | bool valueRet3 = eiotcloud.SetParameterUnit(storage.parameterId1, "%C2%B0C"); 194 | DEBUG_PRINT("SetParameterUnit: "); 195 | DEBUG_PRINTLN(valueRet3); 196 | 197 | //Set parameter LogToDatabase 198 | bool valueRet4 = eiotcloud.SetParameterLogToDatabase(storage.parameterId1, true); 199 | DEBUG_PRINT("SetLogToDatabase: "); 200 | DEBUG_PRINTLN(valueRet4); 201 | 202 | //SetAvreageInterval 203 | bool valueRet5 = eiotcloud.SetParameterAverageInterval(storage.parameterId1, "5"); 204 | DEBUG_PRINT("SetAvreageInterval: "); 205 | DEBUG_PRINTLN(valueRet5); 206 | 207 | //Second sensor parameter --------------------------------------- 208 | // now add parameter to display voltage 209 | parameterId2 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter2"); 210 | DEBUG_PRINT("parameterId2: "); 211 | DEBUG_PRINTLN(parameterId2); 212 | 213 | //set parameter description 214 | bool valueRet6 = eiotcloud.SetParameterDescription(parameterId2, "Voltage"); 215 | DEBUG_PRINT("SetParameterDescription: "); 216 | DEBUG_PRINTLN(valueRet6); 217 | 218 | //set unit 219 | bool valueRet7 = eiotcloud.SetParameterUnit(parameterId2, "V"); 220 | DEBUG_PRINT("SetParameterUnit: "); 221 | DEBUG_PRINTLN(valueRet7); 222 | 223 | //Set parameter LogToDatabase 224 | bool valueRet8 = eiotcloud.SetParameterLogToDatabase(parameterId2, true); 225 | DEBUG_PRINT("SetLogToDatabase: "); 226 | DEBUG_PRINTLN(valueRet8); 227 | 228 | //SetAvreageInterval 229 | bool valueRet9 = eiotcloud.SetParameterAverageInterval(parameterId2, "5"); 230 | DEBUG_PRINT("SetAvreageInterval: "); 231 | DEBUG_PRINTLN(valueRet9); 232 | 233 | // save configuration 234 | saveConfig(); 235 | } 236 | 237 | // if something went wrong, wait here 238 | if (storage.moduleId == 0) 239 | { 240 | DEBUG_PRINT("Error : Cannot connect to Easy IoT cloud"); 241 | return false; 242 | } 243 | 244 | // read module ID from storage 245 | moduleId = String(storage.moduleId); 246 | DEBUG_PRINT("ModuleID = "); 247 | DEBUG_PRINTLN(moduleId); 248 | 249 | // read token ID from storage 250 | eiotcloud.SetToken(storage.token); 251 | 252 | // read Sensor.Parameter1 ID from cloud 253 | parameterId1 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1"); 254 | parameterId2 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter2"); 255 | 256 | return true; 257 | } 258 | void printConfig() 259 | { 260 | const String version = String(storage.version); 261 | const String token = String(storage.token); 262 | //const String paramId1 = String(storage.parameterId1); 263 | //const String paramId2 = String(storage.parameterId2); 264 | 265 | DEBUG_PRINTLN("EEPROM storage"); 266 | DEBUG_PRINT("version : "); DEBUG_PRINTLN(version.c_str()); 267 | DEBUG_PRINT("token : "); DEBUG_PRINTLN(token.c_str()); 268 | DEBUG_PRINT("moduleId : "); DEBUG_PRINTLN(storage.moduleId); 269 | //DEBUG_PRINT("parameterId1 : "); DEBUG_PRINTLN(paramId1.c_str()); 270 | //DEBUG_PRINT("parameterId2 : "); DEBUG_PRINTLN(paramId2.c_str()); 271 | } 272 | 273 | void loadConfig() 274 | { 275 | // To make sure there are settings, and they are YOURS! 276 | // If nothing is found it will use the default settings. 277 | if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] && 278 | EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] && 279 | EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2]) 280 | { 281 | for (unsigned int t = 0; t < sizeof(storage); t++) 282 | { 283 | *((char*)&storage + t) = EEPROM.read(CONFIG_START + t); 284 | } 285 | } 286 | 287 | moduleId = String(storage.moduleId); 288 | //parameterId1 = String(storage.parameterId1); 289 | //parameterId2 = String(storage.parameterId2); 290 | } 291 | 292 | 293 | void saveConfig() 294 | { 295 | storage.moduleId = moduleId.toInt(); 296 | //parameterId1.toCharArray(storage.parameterId1, 17); 297 | //parameterId2.toCharArray(storage.parameterId2, 17); 298 | 299 | for (unsigned int t = 0; t < sizeof(storage); t++) 300 | { 301 | EEPROM.write(CONFIG_START + t, *((char*)&storage + t)); 302 | } 303 | EEPROM.commit(); 304 | } 305 | --------------------------------------------------------------------------------