P1 poort slimme meter – Data naar COSM.com (Arduino)
Dit is een voorbeeldscript van “Zonnig Breda” om P1 Â gegevens weg te schrijven naar COSM.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
#include <AltSoftSerial.h> #define maxRunTime 600000L #define UPDATE_COSM 1 #define SDLOG 0 #define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below) // if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: #define FIXEDSERVER 1 // set to 1 to to use the numeric server #define etherPin 10 //pin to be used for the ethernet card #if UPDATE_COSM #include <EtherCard.h> #endif #if SDLOG #include <SD.h> #endif // AltSoftSerial always uses these pins: // // Board Transmit Receive PWM Unusable // ----- -------- ------- ------------ // Teensy 2.0 9 10 (none) // Teensy++ 2.0 25 4 26, 27 // Arduino Uno 9 8 10 // Arduino Mega 46 48 44, 45 // Wiring-S 5 6 4 // Sanguino 13 14 12 AltSoftSerial altSerial; //int pMode = 0; #define BUFSIZE 75 char buffer[BUFSIZE]; int bufpos = 0; int Pos = 0; #define StartChar = '/'; #define StopChar = '!'; long PowerUsage = 0; long PowerSolar = 0; float GasMeter = 0; float GasUsage = 0; long m1 = 0; long m2 = 0; long m3 = 0; long m4 = 0; bool readnextLine = false; #define MeterInterval 60000 // Send the meter values only once per 60s unsigned long previousMillis; bool sendMeter = true; #if UPDATE_COSM // change these settings to match your own setup #define APIKEY "ENTER YOUR KEY HERE" // your cosm api key #define FEED "ENTER FEED" // your feed ID #define USERAGENT "Cosm Arduino Example (62918)" // user agent is the project name // ethernet interface mac address static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x36 }; #if STATIC // ethernet interface ip address static byte myip[] = { 192,168,3,160 }; // gateway ip address static byte gwip[] = { 192,168,3,1 }; //dns server static byte dnsip[] = { 8,8,8,8 }; #endif #if FIXEDSERVER static byte hisip[] = { 216,52,233,122 }; #else char website[] PROGMEM = "api.pachube.com"; #endif byte Ethernet::buffer[265]; //byte Ethernet::buffer[250]; //uint32_t timer; Stash stash; #endif #include <MemoryFree.h> void setup() { Serial.begin(9600); Serial.println(F("Energie reading Begins...")); //memory friendly altSerial.begin(9600); #if SDLOG Serial.println(F("Initializing SD card...")); // On the Ethernet Shield, CS is pin 4. It's set as an output by default. // Note that even if it's not used as the CS pin, the hardware SS pin // (10 on most Arduino boards, 53 on the Mega) must be left as an output // or the SD library functions will not work. // pinMode(10, OUTPUT); // digitalWrite(10, HIGH); // digitalWrite(4, HIGH); if (!SD.begin(4)) { Serial.println(F("initialization failed!")); return; } #endif checkmem(); #if UPDATE_COSM netsetup(); #endif checkmem(); } void loop() { // forced realod after 10 min run time impl if(millis() > maxRunTime ) { softReset(); } #if UPDATE_COSM ether.packetLoop(ether.packetReceive()); // int tt = ether.packetLoop(ether.packetReceive()); // if (tt) Serial.println (tt); #endif if (millis() > (previousMillis + MeterInterval) ) { sendMeter = true ; previousMillis = millis(); } char c; if (Serial.available()) { c = Serial.read(); } if (altSerial.available()) { c = altSerial.peek(); if (c == '/' ){ //StartChar){ checkmem(); } // we've loaded a line. Now parse the values if (read2Line()){ Serial.print(buffer); long tl = 0; long tld =0; if (sendMeter) { //Meter //1-0:1.8.1(00391.000*kWh) if (sscanf(buffer,"1-0:1.8.1(%ld%.%ld%*s" , &tl, &tld) >0 ) //) { m1 = tl * 1000 + tld;} if (sscanf(buffer,"1-0:1.8.2(%ld%.%ld%*s" , &tl, &tld) >0 ) //) { m2 = tl * 1000 + tld;} if (sscanf(buffer,"1-0:2.8.1(%ld%.%ld%*s" , &tl, &tld) >0 ) //) { m3 = tl * 1000 + tld;} if (sscanf(buffer,"1-0:2.8.2(%ld%.%ld%*s" , &tl, &tld) >0 ) //) { m4 = tl * 1000 + tld;} } //sendmeter if (readnextLine){ if (sscanf(buffer,"(%ld.%ld%*s" , &tl, &tld) >0 ) { //) //(00127.969) GasMeter = float ( tl * 1000 + tld ) / 1000 ; //(00127.969) readnextLine = false; }} //gas 0-1:24.3.0 if (sscanf(buffer,"0-1:24.3.0(%6ld%4ld%*s" , &tl, &tld) > 0 ) { //) Serial.print (F("Gasreading time ")); Serial.print (tl) ; Serial.print (F("-")); Serial.println (tld) ; readnextLine = true; } // Consumption if (sscanf(buffer,"1-0:1.7.0(%ld.%ld%*s" , &tl , &tld) >0 ) //) { PowerUsage = tl * 1000 + tld * 10;} // Supply // 1-0:2.7.0(0000.00*kW) if (sscanf(buffer,"1-0:2.7.0(%ld.%ld%*s" , &tl , &tld) >0 ) //) { PowerSolar = tl * 1000 + tld * 10;} //we're done if (buffer[0] == '!' ) { checkmem(); GasUsage = GasMeter; printData(); checkmem(); #if UPDATE_COSM // SendData (GasUsage,PowerUsage,PowerSolar); SendData(); #endif if (sendMeter){ #if UPDATE_COSM // SendDataM (GasMeter,m1,m2,m3,m4); SendDataM (); #endif sendMeter = false; } checkmem(); #if SDLOG // SaveData(GasUsage,PowerUsage,PowerSolar); SaveData(); #endif checkmem(); } } } } void printData(){ Serial.print(F("Power : ")); Serial.println(PowerUsage); Serial.print(F("PowerSolar : ")); Serial.println(PowerSolar); Serial.print(F("GAS : ")); Serial.println(GasUsage,3 ); Serial.println(F("*Meters*")); Serial.println(m1); Serial.println(m2); Serial.println(m3); Serial.println(m4); } void checkmem(){ Serial.print(F("FreeMemory()=")); Serial.println(freeMemory()); } /* float ConvertStr(String TheNumber){ char bufx[TheNumber.length()+2]; TheNumber.toCharArray(bufx,TheNumber.length()+1); return float(atof(bufx)); } */ bool read2Line() { char c; // bufpos = 0; // buffer[0] = '\0'; c = altSerial.read(); if (bufpos + 1 < BUFSIZE) { buffer[bufpos] = c&127; bufpos++; } else { // return the partial line Serial.println(F("************************")); Serial.println(F("**Readline Buffer Full**")); Serial.println(F("************************")); buffer[bufpos] = '\0'; bufpos = 0; return true; } if (c == '\n'){ buffer[bufpos] = '\0'; bufpos = 0; return true; } else return false; } #if UPDATE_COSM void netsetup(){ Serial.println(F("\n[webClient]")); if (ether.begin(sizeof Ethernet::buffer, mymac,etherPin) == 0) Serial.println(F("Failed to access Ethernet controller")); #if STATIC ether.staticSetup(myip, gwip); // use the GoogleDNS ether.copyIp(ether.dnsip, dnsip); #else if (!ether.dhcpSetup()) Serial.println(F("DHCP failed")); #endif ether.printIp("IP: ", ether.myip); ether.printIp("GW: ", ether.gwip); ether.printIp("DNS: ", ether.dnsip); #if FIXEDSERVER ether.copyIp(ether.hisip, hisip); #else if (!ether.dnsLookup(website)) Serial.println(F("DNS failed")); #endif ether.printIp("SRV: ", ether.hisip); } #endif #if UPDATE_COSM void SendData(){ // by using a separate stash, // we can determine the size of the generated message ahead of time byte sd = stash.create(); stash.print("G,"); stash.println(GasUsage,3); stash.print("P,"); stash.println(PowerUsage); stash.print("S,"); stash.println(PowerSolar); stash.save(); checkmem(); // generate the header with payload - note that the stash size is used, // and that a "stash descriptor" is passed in as argument using "$H" // Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n" Stash::prepare(PSTR("PUT /v2/feeds/$F.csv HTTP/1.0" "\r\n" "Host: api.pachube.com" "\r\n" "X-PachubeApiKey: $F" "\r\n" "Content-Length: $D" "\r\n" "\r\n" "$H"), // website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd); PSTR(FEED), PSTR(APIKEY), stash.size(), sd); checkmem(); // send the packet - this also releases all stash buffers once done ether.tcpSend(); checkmem(); } #endif #if UPDATE_COSM void SendDataM(){ // by using a separate stash, // we can determine the size of the generated message ahead of time byte sd = stash.create(); if (GasMeter != 0){ stash.print("GM,"); stash.println(GasMeter,3); } m1 = m1+2; stash.print("m1,"); stash.println(float (m1/1000),3); stash.print("m2,"); stash.println(float (m2/1000),3); stash.print("m3,"); stash.println(float (m3/1000),3); stash.print("m4,"); stash.println(float (m4/1000),3); stash.save(); // generate the header with payload - note that the stash size is used, // and that a "stash descriptor" is passed in as argument using "$H" // Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n" Stash::prepare(PSTR("PUT /v2/feeds/$F.csv HTTP/1.0" "\r\n" "Host: api.pachube.com" "\r\n" "X-PachubeApiKey: $F" "\r\n" "Content-Length: $D" "\r\n" "\r\n" "$H"), // website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd); PSTR(FEED), PSTR(APIKEY), stash.size(), sd); // send the packet - this also releases all stash buffers once done // Serial.print("sending Size : "); // Serial.print(stash.size()); if (( stash.size() <= 0) || (stash.size() > 400 )){ } else{ ether.tcpSend(); } } #endif #if SDLOG //void SaveData(float value1, long value2 ,long value3){ void SaveData(){ char dataString[30]=""; checkmem(); sprintf (dataString,"%ld,%ld,%ld,%ld",millis(),long(GasMeter/1000),PowerUsage,PowerSolar); /* // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. */ checkmem(); Serial.println(dataString); checkmem(); File dataFile = SD.open("datalog.txt", FILE_WRITE); checkmem(); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: } // if the file isn't open, pop up an error: else { Serial.println(F("error opening datalog.txt")); } checkmem(); } #endif /* char *ftoa(char *a, double f, int precision) { long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; char *ret = a; long heiltal = (long)f; itoa(heiltal, a, 10); while (*a != '\0') a++; *a++ = '.'; long desimal = abs((long)((f - heiltal) * p[precision])); itoa(desimal, a, 10); return ret; } */ // soft reset implementation function void softReset() { asm volatile (" jmp 0"); } |