-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS0_Counter.ino
976 lines (757 loc) · 23.3 KB
/
S0_Counter.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
// USE_GITHUB_USERNAME=anno73
/*-------------------------------------------------------
xAP S0 Electric Energy Counter
Count S0 Pulses
Send to xAP network
Used Pins:
Uno Leonardo
Serial: FTDI Prog + Debug
D0 - RX
D1 - TX
SPI: Ethernet/SDCard
D13 - SCK
D12 - MISO
D11 - MOSI
D10 SS Ethernet
D4 SS SD Card
Display: IIC/TWI
A4 D2 SDA
A5 D3 SCL
D3 INT1 IIC Interrupt
D5 PWM LCD Backlight
ModBus: SoftSerial/Serial1
D2 D0 INT0 RX
D6 D1 TX
D7 D7 Direction
S0 IRQ Inputs:
D8 S0 4
D9 S0 5
A0 S0 0
A1 S0 1
A2 S0 2
A3 S0 3
*D4 S0 6 Potentially
*D5 S0 7 Potentially
.
Send regular updates to xPL:
- Every minute or
- *Every ?kWh
ToDo:
- Send on every kWh
- Read config from EEPROM
- MAC
- IP
- xPL instance name
- xPL port
- xPL broadcast IP
- xPL heartbeat interval
- Change config via xPL message
- Preset counters via xPL message
- Brownout detection - persist counters + config to EEProm
/*-------------------------------------------------------*/
#define PIN_S0_0 A0 // FBH
#define PIN_S0_1 A1 // Alarm
#define PIN_S0_2 A2 // KWL
#define PIN_S0_3 A3 // KNX
#define PIN_S0_4 8 // Daten
#define PIN_S0_5 9 // Main
// http://www.pjrc.com/teensy/td_libs_Time.html
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// http://code.google.com/p/livebox-hah/source/browse/#svn%2Ftrunk%2Fuserapps%2Farduino%2Flibraries%2FxAP
// #include <xAP.h>
// http://code.google.com/p/oopinchangeint/
#include <ooPinChangeInt.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include "S0Impulse.h"
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
// Streaming c++ like output
// http://playground.arduino.cc/Main/StreamingOutput
// Serial << "text 1" << someVariable << "Text 2" ... ;
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
#define FIRMWARE_VERSION "0.0.2.0"
#define EEPROM_MAIN_BASE 0
#define EEPROM_MAIN_FLAGS 0
#define EEPROM_MAIN_IP 2
#define EEPROM_MAIN_ 6
#define EEPROM_MAIN_XPL_INSTANCE 16
// Flags
#define EEPROM_MAIN_USE_DHCP 0
/*----------------------------------------------------------------------
Ethernet related structures
/*----------------------------------------------------------------------*/
byte mac[] = {
0x02, 0x00, 0x00, 0x00, 0x00, 0x01
};
byte ip[] = { 192, 168, 2, 21 };
EthernetUDP Udp;
//uint8_t packetBuffer[512];
/*----------------------------------------------------------------------
xPL related structures
/*----------------------------------------------------------------------*/
#define XPL_HEARTBEAT_INTERVAL 10000L
#define XPL_VENDOR "ah" // max 8 chars
#define XPL_DEVICE "energy" // max 8 chars
#define XPL_INSTANCE "counter1" // max 16 chars
#define XPL_SOURCE XPL_VENDOR "-" XPL_DEVICE
#define XPL_ENDPOINT XPL_SOURCE "." XPL_INSTANCE
unsigned int xPLPort = 3865; // xPL Port to listen on and send to
IPAddress xPLBroadcast(255, 255, 255, 255);
uint8_t seenOwnHeartbeat = 0;
/*----------------------------------------------------------------------
S0 related structures
/*----------------------------------------------------------------------*/
#define MAX_S0_INPUTS 8
S0Impulse S0[MAX_S0_INPUTS];
//#define S0BusUpdateIntervall (60 * 1000)
#define S0BusUpdateIntervall (5 * 1000)
/*----------------------------------------------------------------------
Function declarations
/*----------------------------------------------------------------------*/
void setup(void);
void loop(void);
void initEEPROM(uint8_t value, uint16_t from, uint16_t count);
void dumpEEPROM(uint16_t from, uint16_t count);
/*----------------------------------------------------------------------
Arduino Setup
/*----------------------------------------------------------------------*/
void setup() {
wdt_disable();
Serial.begin(115200);
// Dump internal config on startup
Serial << F(
"Welcome to S0 Electric Energy Counter\n\n"
"My name is: " XPL_ENDPOINT "\n"
"xPL Port: ") << xPLPort << F("\n"
);
// while (! Serial); // For Leonardo
// Start Ethernet connection
#if 1
Serial << F("Init Ethernet - DHCP...\n");
if (Ethernet.begin(mac) == 0) // DHCP
{
for (;;)
{
Serial << F("Failed to get IP via DHCP. Shutting down.\n");
delay(1000);
}
}
Serial << F("DHCP gave IP Address: ") << Ethernet.localIP() << "\n";
#else
Ethernet.begin(mac, ip); // Fixed
Serial << F("Using IP Address: ") << Ethernet.localIP() << "\n";
#endif
// Serial << F("Free RAM: ") << freeRam() << '\n';
Udp.begin(xPLPort);
// Initialize S0 counters
Serial << F("Init S0 counters...\n");
S0[0] = S0Impulse(PIN_S0_0, 0); // FBH
S0[1] = S0Impulse(PIN_S0_1, 1); // Alarm
S0[2] = S0Impulse(PIN_S0_2, 2); // KWL
S0[3] = S0Impulse(PIN_S0_3, 3); // KNX
S0[4] = S0Impulse(PIN_S0_4, 4); // Daten
S0[5] = S0Impulse(PIN_S0_5, 5); // Main
S0[6] = S0Impulse(PIN_S0_4, 6);
S0[7] = S0Impulse(PIN_S0_5, 7);
// initEEPROM();
#if 0
S0[0].setCountLowPerHigh(2000); // FBH
S0[0].setName("S0_FBH");
S0[1].setCountLowPerHigh(2000); // Alarm
S0[1].setName("S1_Alarm");
S0[2].setCountLowPerHigh(2000); // KWL
S0[2].setName("S2_KWL");
S0[3].setCountLowPerHigh(2000); // KNX
S0[3].setName("S3_KNX");
S0[4].setCountLowPerHigh(2000); // Daten
S0[4].setName("S4_EDV");
S0[5].setCountLowPerHigh(400); // Main
S0[5].setName("S5_Main");
S0[6].setCountLowPerHigh(2000);
S0[6].setName("S6_Whatever");
S0[7].setCountLowPerHigh(2000);
S0[7].setName("S7_Whatever");
// Set all to active
// ### callback/byte ptr for update request
for (uint8_t i = 0; i < MAX_S0_INPUTS; i++)
{
// S0[i].setActive();
}
// Set all to active
// ### callback/byte ptr for update request
for (uint8_t i = 0; i < MAX_S0_INPUTS; i++)
{
S0[i].setActive();
}
dumpEEPROM(0, 512);
#endif
Serial << F("Init watchdog...\n");
wdt_enable(WDTO_2S);
// xPL standard advises to:
// http://xplproject.org.uk/wiki/index.php?title=XPL_Specification_Document#Heartbeat_behaviour_at_initial_device_startup
// Send xPL heartbeats every 3 .. 10 seconds and wait for our own echo
// If there is no ersponse within some minutes switch to 30s interval
// This should go here
// while (! seenOwnHeartbeat); ... blah blah
Serial <<F("Init done. Let's rock...\n");
} // setup
/*----------------------------------------------------------------------
Arduino Main Loop
/*----------------------------------------------------------------------*/
//time_t oldTime;
uint32_t nextUpdateOnBusTS = millis() + S0BusUpdateIntervall;
uint32_t nextDTTMRequest = millis() + 5000;
uint32_t nextS0Increment = millis() + 1000;
uint32_t nextXplHeartbeat = millis() + XPL_HEARTBEAT_INTERVAL;
uint32_t msNow;
void loop()
{
wdt_reset();
msNow = millis();
// Serial << F("Free RAM: ") << freeRam() << '\n';
// Check if we possibly got a new UDP packet to process
if (Udp.parsePacket() > 0)
{
processXplMessage();
}
// Check for heartbeat interval due
if (msNow >= nextXplHeartbeat)
{
nextXplHeartbeat = msNow + XPL_HEARTBEAT_INTERVAL;
sendXplHeartbeat();
}
// Generate some test data internally
if (msNow >= nextS0Increment)
{
nextS0Increment = msNow + 1000;
for (uint8_t i = 0; i < MAX_S0_INPUTS; i++)
{
S0[i].cbmethod();
}
}
// Every S0BusUpdateIntervall millis send update of values on bus
if (msNow >= nextUpdateOnBusTS)
{
nextUpdateOnBusTS = msNow + S0BusUpdateIntervall;
for (uint8_t i = 50; i; i--) Serial << '-';
// Serial << '\n' << year() << month() << day() << hour() << minute() << second() << ':' << now();
Serial << '\n' << F("S0 update to bus:\n");
for (uint8_t i = 0; i < MAX_S0_INPUTS; i++)
{
if (S0[i].getIsActive())
{
Serial << F("countLow: ") << S0[i].getCountLow() << '\n';
sendXplS0TriggerMsg(&S0[i]);
}
else Serial << "Not active: S0[" << i << "] = " << S0[i].getID() << '\n';
}
// for (uint8_t i = 0; i < MAX_S0_INPUTS; i++) Serial << S0[i].getID() << ":" << S0[i].getCountLowPerHigh() << ":" << S0[i].getCountHigh() << "." << S0[i].getCountLow() << ":" << S0[i].getCount() << "\n";
// for (uint8_t i = 0; i < MAX_S0_INPUTS; i++) Serial << S0[i].getID() << ":" << S0[i].getCount() << "\n";
}
} // loop
#if 1 // xPL related code
//--------------------------------------------------------
// xPL Related Code
//--------------------------------------------------------
uint8_t XplReadLine(char * buff, uint8_t buffLen)
{
char * cp = buff;
uint8_t size = 0;
buff[buffLen] = 0;
for (; buffLen; buffLen--)
{
*cp = Udp.read();
// Serial << F("XplReadLine: ") << size << ':' << buff << ".\n";
if (*cp == '\n')
{
// *cp = 0;
// return size;
break;
}
cp++;
size++;
}
*cp = 0;
Serial << F("XplReadLine: ") << size << ':' << buff << ".\n";
return size;
} // XplReadLine
/*---------------------------------------------------------------------
processXplMessage
http://xplproject.org.uk/wiki/index.php?title=XPL_Specification_Document
Try to parse an xPL message and possibly trigger some actions.
---------------------------------------------------------------------*/
void processXplMessage(void)
{
#define XPL_READLN_BUFFER_SIZE 45
char buffer[XPL_READLN_BUFFER_SIZE];
uint16_t packetSize = Udp.available();
if (! packetSize)
return;
for (uint8_t i = 50; i; i--) Serial << '-';
Serial << F("\nReceived packet of size ") << packetSize;
Serial << F("\nFrom ") << Udp.remoteIP() << ':' << Udp.remotePort() << '\n';
uint8_t msgType = 0;
uint8_t broadcast = 0;
uint8_t unicast = 0;
char * msgType_cp;
char * hop_cp;
char * source_cp;
char * target_cp;
char * class_cp;
char * command_cp;
// char * cp = (char *) buffer;
uint8_t readChar;
/*
Message type identifier
8 Bytes, terminated with '\n'
Valid values are:
xpl-cmnd
xpl-stat
xpl-trig - ignore
*/
// Get message type
readChar = XplReadLine(buffer, 32);
Serial << readChar << ':' << buffer << ".\n";
// Serial << F("UDP Available: ") << Udp.available() << ".\n";
// Needs to be exactly 8 bytes
if (readChar != 8)
{
Serial << F("Not xPL: Message Type Length: ") << readChar << ".\n";
return;
}
// Must start off with 'xpl-'
if (strncmp_P(buffer, PSTR("xpl-cmnd"), 8) == 0)
{
msgType = 'c';
}
else if (strncmp_P(buffer, PSTR("xpl-stat"), 8) == 0)
{
msgType = 's';
}
else if (strncmp_P(buffer, PSTR("xpl-trig"), 8) == 0)
{
msgType = 't';
}
else
{
Serial << F("Not xPL: Message Type: ") << buffer << ".\n";
return;
}
Serial << F("xPL Message Type: ") << buffer << " : " << (char) msgType << ".\n";
// '{\n'
XplReadLine(buffer, 4);
// 'hop=1'
XplReadLine(buffer, 8);
// 'source=###-###.###'
// 7 + 8 + '-' + 8 + '.' + 16 + '\n'
readChar = XplReadLine(buffer, 45);
// 'target=###-###.###'
// 7 + 8 + '-' + 8 + '.' + 16 + '\n'
// target may be '*' for broadcast
// of my name
readChar = XplReadLine(buffer, 45);
broadcast = unicast = 0;
if (readChar == 7 && buffer[7] == '*')
{
broadcast = 1;
}
if (! broadcast)
{
// fetch own name from eeprom and compare
// if (strncmp(buffer[7], XPL_ENDPOINT, sizeof(XPL_ENDPOINT)) == 0) // ### hardcoded instance id ... !!!
if (strncmp((const char *) &buffer[7], "mhouse.item", sizeof("mhouse.item")) == 0) // ### hardcoded instance id ... !!!
{
unicast = 1;
}
}
if (! broadcast && ! unicast)
{
Serial << F("xPL Message not for me: Target: ") << &buffer[7] << " : " << readChar << ".\n";
return;
}
Serial << F("xPL Message Target: ") << buffer << " : " << readChar << ".\n";
// '}'
XplReadLine(buffer, 4);
/*
xPL Message Class
###.###
8 + '.' + 8 + '\n'
hbeat.basic
hbeat.app
hbeat.request <-
sensor.request
config.list
config.current
config.response
*/
readChar = XplReadLine(buffer, 20);
if (readChar > 17)
return;
if (strncmp_P(class_cp, PSTR("hbeat.request"), sizeof("hbeat.request")) == 0)
{
Serial << F("Got a 'hbeat.request' message.\n");
// '{\n'
XplReadLine(buffer, 4);
readChar = XplReadLine(buffer, XPL_READLN_BUFFER_SIZE);
if (strncmp_P(buffer, PSTR("command=request"), sizeof("command=request")) != 0)
{
return;
}
Serial << F("Go to send a heartbeat message.\n");
sendXplHeartbeat();
return;
}
else if (strncmp(class_cp, "config.list", sizeof("config.list")) == 0)
{
Serial << F("Got a 'config.list' message\n");
// '{\n'
XplReadLine(buffer, 4);
readChar = XplReadLine(buffer, XPL_READLN_BUFFER_SIZE);
if (strncmp_P(buffer, PSTR("command=request"), sizeof("command=request")) != 0)
{
return;
}
// Process config.list request
return;
}
else if (strncmp(buffer, "config.current", sizeof("config.current")) == 0)
{
Serial << F("Got a 'config.current' message\n");
// '{\n'
XplReadLine(buffer, 4);
readChar = XplReadLine(buffer, XPL_READLN_BUFFER_SIZE);
if (strncmp_P(buffer, PSTR("command=request"), sizeof("command=request")) != 0)
return;
// Process config.current request
return;
}
else if (strncmp(buffer, "config.response", sizeof("config.response")) == 0)
{
Serial << F("Got a 'config.response' message\n");
// '{\n'
XplReadLine(buffer, 4);
// Process config.response request
readChar = XplReadLine(buffer, XPL_READLN_BUFFER_SIZE);
if (strncmp_P(buffer, PSTR("newconf=???"), sizeof("newconf=???")) != 0)
return;
return;
}
else
{
Serial << F("Unknown Message Class: ") << buffer << " : " << readChar << ".\n";
return;
}
return;
#if 0
// Serial << F("msgType: ") << msgType_cp << ".\n";
// Serial << F("hop: ") << hop_cp << ".\n";
// Serial << F("source: ") << source_cp << ".\n";
// Serial << F("target: ") << target_cp << ".\n";
// Serial << F("class: ") << class_cp << ".\n";
// Serial << F("Command: ") << command_cp << ".\n";
#endif
} // processXplMessage
#if 0
void processXplMessage_(void)
{
uint16_t packetSize = Udp.available();
if (! packetSize)
return;
for (uint8_t i = 50; i; i--) Serial << '-';
// Serial << '\n' << year() << month() << day() << hour() << minute() << second() << ':' << now();
Serial << F("\nReceived packet of size ") << packetSize;
Serial << F("\nFrom ") << Udp.remoteIP() << ':' << Udp.remotePort() << '\n';
if (packetSize > 255)
packetSize = 255;
// Read the packet into packetBuffer
Udp.read(packetBuffer, packetSize);
// Terminate packet with \0
packetBuffer[packetSize] = 0;
// Serial << F("Contents: ");
// Serial << (char *)packetBuffer << '\n';
uint8_t msgType = 0;
uint8_t broadcast = 0;
uint8_t unicast = 0;
char * msgType_cp;
char * hop_cp;
char * source_cp;
char * target_cp;
char * class_cp;
char * command_cp;
char * cp = (char *) packetBuffer;
// check for valid xPL message
// xPL message type is max 8 chars long and terminated with '\n'
// Valid values are: xpl-cmnd, xpl-stat, xpl-trig
if (strncmp(cp, "xpl-", 4) != 0)
return;
cp += 4;
if (strncmp(cp, "cmnd", 4) == 0)
{
msgType = 'c';
}
else if (strncmp(cp, "stat", 4) == 0)
{
msgType = 's';
}
else if (strncmp(cp, "trig", 4) == 0)
{
msgType = 't';
// return;
}
else
return;
msgType_cp = (char *) packetBuffer;
cp += 4;
*cp = 0;
// Serial << F("msgType: ") << msgType_cp << ".\n";
// xPL Header: hop
for (; *cp != '='; cp++);
hop_cp = ++cp;
for (; *cp != '\n'; cp++);
*cp = 0;
// Serial << F("hop: ") << hop_cp << ".\n";
// xPL Header: source
for (; *cp != '='; cp++);
source_cp = ++cp;
for (; *cp != '\n'; cp++);
*cp = 0;
// Serial << F("source: ") << source_cp << ".\n";
// xPL Header: target
for (; *cp != '='; cp++);
target_cp = ++cp;
for (; *cp != '\n'; cp++);
*cp = 0;
// Serial << F("target: ") << target_cp << ".\n";
// Packet must be either broadcast or unicast
// Ignore otherwise
if (strncmp(target_cp, "*", 1) == 0)
{
broadcast = 1;
}
// else if (strncmp(target_cp, XPL_ENDPOINT, sizeof(XPL_ENDPOINT)) == 0) // ### hardcoded instance id ... !!!
else if (strncmp(target_cp, "mhouse.item", sizeof("mhouse.item")) == 0) // ### hardcoded instance id ... !!!
{
unicast = 1;
}
else
return;
// xPL Class
cp += 3;
class_cp = cp;
for (; *cp != '\n'; cp++);
*cp = 0;
// Serial << F("class: ") << class_cp << ".\n";
/*
Special classes
hbeat.basic
hbeat.app
hbeat.request <-
sensor.request
config.list
config.current
*/
// xPL Body: ...
cp += 3;
command_cp = cp;
for (; *cp != '\n'; cp++);
*cp = 0;
// Serial << F("msgType: ") << msgType_cp << ".\n";
// Serial << F("hop: ") << hop_cp << ".\n";
// Serial << F("source: ") << source_cp << ".\n";
// Serial << F("target: ") << target_cp << ".\n";
// Serial << F("class: ") << class_cp << ".\n";
// Serial << F("Command: ") << command_cp << ".\n";
if (strncmp(class_cp, "hbeat.request", sizeof("hbeat.request")) == 0)
{
Serial << F("Got a 'hbeat.request' message.\n");
if (strncmp(command_cp, "command=request", sizeof("command=request")) != 0)
return;
Serial << F("Go to send a heartbeat message.\n");
sendXplHeartbeat();
return;
}
else if (strncmp(class_cp, "config.list", sizeof("config.list")) == 0)
{
Serial << F("Got a 'config.list' message\n");
if (strncmp(cp, "command=request", sizeof("command=request")) != 0)
return;
// Process config.list request
}
else if (strncmp(class_cp, "config.current", sizeof("config.current")) == 0)
{
Serial << F("Got a 'config.current' message\n");
if (strncmp(cp, "command=request", sizeof("command=request")) != 0)
return;
}
else
return;
} // processXplMessage
#endif
/*
sendXplS0ConfigMsg
*/
void sendXplS0ConfigMsg(S0Impulse *S0)
{
// Serial << year() << month() << day() << hour() << minute() << second() << ':' << now() << '\n';
Serial << F("Send xPL S0 Update for ") << S0->getID() << '\n';
Udp.beginPacket(xPLBroadcast, xPLPort);
Udp << F("xpl-stat\n"
"{\n"
"hop=1\n"
"source=" XPL_ENDPOINT "\n"
"target=*\n"
"}\n"
"config.list\n"
"{\n"
"reconf=newconf\n"
"device=") << S0->getID() << F("\n"
"type=energy\n"
"current=");
// Udp.print(S0->getCount() );
Udp << F("\n"
"units=kWh\n"
"}\n"
);
Udp.endPacket();
} // sendXplS0ConfigMsg
/*
sendXplS0TriggerMsg
*/
void sendXplS0TriggerMsg(S0Impulse *S0)
{
// Serial << year() << month() << day() << hour() << minute() << second() << ':' << now() << '\n';
Serial << F("Send xPL S0 Update for ") << S0->getID() << '\n';
Udp.beginPacket(xPLBroadcast, xPLPort);
Udp << F("xpl-trig\n"
"{\n"
"hop=1\n"
"source=" XPL_ENDPOINT "\n"
"target=*\n"
"}\n"
"sensor.basic\n"
"{\n"
"device=") << S0->getID() << F("\n"
"type=energy\n"
"current=");
Udp.print(S0->getCount(), 3);
Udp << F("\n"
"units=kWh\n"
"}\n"
);
Udp.endPacket();
} // sendXplS0TriggerMsg
void sendXplHeartbeat(void)
{
// Serial << year() << month() << day() << hour() << minute() << second() << ':' << now() << '\n';
Serial << F("Send xPL Heartbeat\n");
Udp.beginPacket(xPLBroadcast, xPLPort);
Udp << F(
"xpl-stat\n"
"{\n"
"hop=1\n"
"source=" XPL_ENDPOINT "\n"
"target=*\n"
"}\n"
"hbeat.basic\n"
"{\n"
"interval=1\n"
"version=" FIRMWARE_VERSION "\n"
"source=" XPL_ENDPOINT "\n"
#if 0
"s0_0_lph=") << S0[0].getCountLowPerHigh() << F("\n"
"s0_1_lph=") << S0[1].getCountLowPerHigh() << F("\n"
"s0_2_lph=") << S0[2].getCountLowPerHigh() << F("\n"
"s0_3_lph=") << S0[3].getCountLowPerHigh() << F("\n"
"s0_4_lph=") << S0[4].getCountLowPerHigh() << F("\n"
"s0_5_lph=") << S0[5].getCountLowPerHigh() << F("\n"
"s0_6_lph=") << S0[6].getCountLowPerHigh() << F("\n"
"s0_7_lph=") << S0[7].getCountLowPerHigh() << F("\n"
#endif
"}\n"
);
Udp.endPacket();
} // sendXplHeartbeat
#endif
#if 1 // EEPROM related functions
/*---------------------------------------------------------------------
initEEPROM
Initialize EEPROM with some reasonable values
---------------------------------------------------------------------*/
void initEEPROM(uint8_t value, uint16_t from, uint16_t count)
{
uint8_t i;
Serial << F("initEEPROM with ") << value << F(" from ") << from << F(" to ") << from + count;
for (i = from; i < from + count; i++)
eeprom_write_byte((uint8_t *) i, value);
Serial << F(" ... done.\n");
} // initEEPROM
/*---------------------------------------------------------------------
dumpEEPROM
Dump EEPROM values
---------------------------------------------------------------------*/
void dumpEEPROM(uint16_t from, uint16_t count)
{
char buf[16];
Serial << F("Dump EEPROM contents from ") << from << F(" to ") << from + count << '(' << count << F(")bytes\n");
snprintf(buf, 15, "%04d %03X: ", 0, 0);
Serial << buf;
uint16_t i = from;
uint8_t j = 1;
while (i < from + count)
{
snprintf(buf, 15, "%02X ", eeprom_read_byte((uint8_t *)i));
Serial << buf;
i++; // advance here for the print below
if (j < 16)
Serial << ' ';
else
{
if (i < from + count) // skip the last empty preamble of the line as while would end anyway
{
j = 0;
snprintf(buf, 15, "\n%04d %03X: ", i, i);
Serial << buf;
}
}
j++;
}
Serial << '\n';
} // dumpEEPROM
#endif
#if 1 // Various stuff -
/**********************************************************************
freeRam
Taken from
http://www.controllerprojects.com/2011/05/23/determining-sram-usage-on-arduino/
---
As stated on the JeeLabs site:
There are three areas in RAM:
* static data, i.e. global variables and arrays … and strings !
* the “heap”, which gets used if you call malloc() and free()
* the “stack”, which is what gets consumed as one function calls
another
The heap grows up, and is used in a fairly unpredictable manner.
If you release areas, then they will be lead to unused gaps in
the heap, which get re-used by new calls to malloc() if the
requested block fits in those gaps.
At any point in time, there is a highest point in RAM occupied
by the heap. This value can be found in a system variable
called __brkval.
The stack is located at the end of RAM, and expands and
contracts down towards the heap area. Stack space gets
allocated and released as needed by functions calling other
functions. That’s where local variables get stored.
---
IMO int v; sits at the lowest address of the stack currently
**********************************************************************/
int freeRam(void)
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
} // freeRam
#endif