-
Notifications
You must be signed in to change notification settings - Fork 9
/
uni_mote_2p7.ino
executable file
·1789 lines (1588 loc) · 53.7 KB
/
uni_mote_2p7.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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Colin Reese, CuPID Controls, Interface Innovations
// UniMote sketch, based on the great example by lowpowerlab
// RFM69 and SPIFlash Library by Felix Rusu - felix@lowpowerlab.com
// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/
#include <RFM69.h>
//#include <RFM69registers.h>
#include <SPI.h>
#include <SPIFlash.h>
#include <OneWire.h>
#include <LowPower.h>
#include <EEPROM.h>
#include <Flash.h>
//#include <LedControl.h>
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define REG_SYNCVALUE2 0x30
//#ifdef __AVR_ATmega1284P__
// #define LED 15 // Moteino MEGAs have LEDs on D15
// #define FLASH_SS 23 // and FLASH SS on D23
//#else
#define LED 9 // Moteinos have LEDs on D9
#define FLASH_SS 8 // and FLASH SS on D8
//#endif
#define SERIAL_BAUD 115200
#define DEBUG 1
#define INIT 0
#define ACK_TIME 500 // max # of ms to wait for an ack
#define RETRIES 1
// These values are either initialized or retrieved from Flash
byte NODEID;
byte NETWORKID;
byte GATEWAYID;
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
byte SLEEPMODE;
unsigned int SLEEPDELAY; // ms
unsigned int SLEEPDELAYTIMER; // ms
//int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms)
boolean requestACK = false;
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond chip (W25X40CL)
RFM69 radio;
unsigned int LOOPPERIOD = 100; // ms
// constants
//PROGMEM char ionames[13][3] = { "D3","D4","D5","D6","D7","A0","A1","A2","A3","A4","A5","A6","A7" };
byte iopins[13] = { 3,4,5,6,7,A0,A1,A2,A3,A4,A5,A6,A7 };
byte owpin;
byte serialrfecho;
// user-assigned variables
int8_t ioenabled[13];
int8_t iomode[13];
float iovalue[13];
int8_t ioreportenabled[13];
unsigned long ioreportfreq[13];
unsigned long ioreadfreq[13];
int8_t chanenabled[8] = {1,0,0,0,0,0,0,0};
int8_t chanposfdbk[8] = {0,0,0,0,0,0,0,0}; // -1 is no pos fdbk
int8_t channegfdbk[8] = {-1,0,0,0,0,0,0,0}; // -1 is no neg fdbk
int8_t chanmode[8]; // reserved
int8_t chandeadband[8];
int8_t chanstate[8];
int8_t chanpvindex[8] = {5,0,0,0,0,0,0,0};
float chanpv[8];
float chansv[8] = {15,0,0,0,0,0,0,0};
//LedControl lc=LedControl(17,18,19,1);
unsigned long ioreporttimer[13];
unsigned long ioreadtimer[13];
unsigned long chanreporttimer[8];
unsigned long prevtime = 0;
unsigned long looptime = 0;
char buff[61];
// make these global to allow subroutines without crazy pointers
char charstr1[57];
char charstr2[12];
char charstr3[25];
char charstr4[40];
byte str1len;
byte str2len;
byte str3len;
byte str4len;
char replystring[62];
byte replylength=0;
void setup() {
Serial.begin(SERIAL_BAUD);
// Initialize variables to/from EEPROM
if (INIT) {
Serial.println(F("Running init"));
initparams();
storeparams();
} // INIT
else { // not init
Serial.println(F("Not running init"));
} // not INIT
getparams();
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
radio.setHighPower(); //uncomment only for RFM69HW!
#endif
if (flash.initialize())
{
Serial.println(F("SPI Flash Init OK"));
// Serial.print(F("UniqueID (MAC): "));
// flash.readUniqueId();
// for (byte i=0;i<8;i++)
// {
// Serial.print(flash.UNIQUEID[i], HEX);
// Serial.print(' ');
// }
// Serial.println();
}
else {
Serial.println(F("SPI Flash Init FAIL)"));
}
// These are values that are not ever stored permanently
int i;
for (i=0;i<13;i++) {
ioreportfreq[i] = 0; // 0 means report when read
ioreporttimer[i] = 9999999;
ioreadtimer[i] = 9999999;
}
radio.encrypt(ENCRYPTKEY);
sendInitMessage();
// lc.shutdown(0,false);
/* Set the brightness to a medium values */
// lc.setIntensity(0,8);
/* and clear the display */
// lc.clearDisplay(0);
}
void loop() {
// Serial.print(F("Free memory: "));
// Serial.println(freeRam());
// READ IO
looptime += millis() - prevtime; // includes time taken to process
// Serial.println(looptime);
int i;
for (i=0;i<13;i++) {
ioreporttimer[i] += looptime;
ioreadtimer[i] += looptime;
}
prevtime = millis();
looptime = 0; // we keep this around to add time that isn't counted (sleeptime)
// for each io
for (i=0;i<13;i++){
if (ioenabled[i]) {
if (ioreadtimer[i] > ioreadfreq[i]) {
// Serial.print(F("Time to check data for pin "));
// Serial.print(iopins[i]);
// Serial.print(F(", io number "));
// Serial.println(i);
ioreadtimer[i] = 0;
// Determine mode and what to read
if (iomode[i] == 0) { // Digital Input
// Serial.print(F("Digital input configured for pin "));
// Serial.println(iopins[i]);
pinMode(iopins[i], INPUT); // sets the digital pin 7 as input
iovalue[i] = digitalRead(iopins[i]);
// send/broadcast if enabled and freq is 0
if ((ioreportenabled[i]) && (ioreportfreq[i] == 0)){
sendIOMessage(i, iomode[i], iovalue[i]);
} // ioreportfreq == 0
}
else if (iomode[i] == 1) { // Digital Output
// Serial.print(F("Digital output configured for pin "));
// Serial.println(iopins[i]);
pinMode(iopins[i], OUTPUT);
digitalWrite(iopins[i],iovalue[i]);
// send/broadcast if enabled and freq is 0
if ((ioreportenabled[i]) && (ioreportfreq[i] == 0)){
sendIOMessage(i, iomode[i], iovalue[i]);
} // ioreportfreq == 0
}
else if (iomode[i] == 2) { // Analog Input
// Serial.print(F("Analog input configured for pin "));
// Serial.println(iopins[i]);
pinMode(iopins[i], INPUT); // sets the digital pin 7 as input
iovalue[i] = analogRead(iopins[i]);
// Serial.print(F("Value: "));
// Serial.println(iovalue[i]);
// int wholePart = iovalue[i];
if ((ioreportenabled[i]) && (ioreportfreq[i] == 0)){
sendIOMessage(i, iomode[i], iovalue[i]);
} // ioreportfreq == 0
}
else if (iomode[i] == 3) { // PWM
// Serial.print(F("PWM Configured for pin"));
// Serial.print(iopins[i]);
// pwm code goes here
} // If PWM
// This 1Wire code is not optimized and reads synchronously
// asynchronous has been written but needs to be squeezed in here
else if (iomode[i] == 4) { //OneWire
// Serial.print(F("1Wire configured for pin "));
// Serial.println(iopins[i]);
// pass ioindex to have the routine handle it
handleOWIO(i);
} // If OneWire
} // If timer
} // If enabled
else { // not enabled
} // not enabled
} // for i=0 --> 13
// END READ IO
// REPORTING
for (i=0;i<13;i++){
// remember that ioreportfreq=0 means report when read
if (ioreportenabled[i] && ioreportfreq[i] > 0 && ioreporttimer[i] > ioreportfreq[i]){
// this is a bit more general than the on-the-fly reporting
// most notably, no onewire address or type
// Initialize send stringsend
sendIOMessage(i,iomode[i],iovalue[i]);
} // time to report
}
// END REPORTING
// PROCESS CHANNELS
for (i=0;i<8;i++) {
if (chanenabled[i]) {
// Serial.print(F("Channel "));
// Serial.print(i);
// Serial.println(F(" value: "));
chanpv[i]=iovalue[chanpvindex[i]];
// Serial.println(chanpv[i]);
// Serial.println(F("Setpoint value: "));
// Serial.println(chansv[i]);
if ((chanpv[i] - chansv[i]) > chandeadband[i]) {
// Serial.println(F("Setting negative action"));
chanstate[i]=-1;
// Serial.print(F("Neg feedback: "));
// set opposing feedback to zero first
if (chanposfdbk[i] >=0){
iovalue[chanposfdbk[i]]=0;
// update now
ioreadtimer[chanposfdbk[i]]=999999;
}
// Then set desired feedback
if (channegfdbk[i] >=0){
iovalue[channegfdbk[i]]=1;
// update now
ioreadtimer[channegfdbk[i]]=999999;
}
// Serial.println(channegfdbk[i]);
}
else if ((chansv[i] - chanpv[i]) > chandeadband[i]) {
// Serial.println(F("Setting positive action"));
chanstate[i]=1;
// Serial.print(F("Pos feedback: "));
// set opposing feedback to zero first
if (channegfdbk[i] >=0){
iovalue[channegfdbk[i]]=0;
// update now
ioreadtimer[channegfdbk[i]]=999999;
}
// Then set desired feedback
if (chanposfdbk[i] >=0){
iovalue[chanposfdbk[i]]=1;
// update now
ioreadtimer[chanposfdbk[i]]=999999;
}
// Serial.println(chanposfdbk[i]);
}
else {
// Serial.println(F("Setting no action"));
chanstate[i]=0;
}
} // channel enabled
} // for channels
// END PROCESS CHANNELS
// If we're in SLEEPMODE, we wait for a set period to receive packets on serial and radio
int millistart;
SLEEPDELAYTIMER = 0;
if (SLEEPMODE) {
millistart = millis();
Serial.println(F("Entering Sleep Seq."));
}
// failsafe. must listen.
if (SLEEPDELAY < 1000){
SLEEPDELAY = 1000;
}
while (SLEEPDELAYTIMER < SLEEPDELAY){
// SERIAL RECEIVE AND PROCESSING
// Check for any received packets
int cmdlength =0;
if (Serial.available() > 0)
{
Blink(LED,5);
cmdlength = Serial.readBytes(buff, 60);
for (i=0; i<cmdlength;i++){
// Serial.print(buff[i]);
}
// Serial.println();
// Send to gateway
buff[cmdlength]=0;
processcmdstring(buff, cmdlength, 0);
// Reset sleepdelay timer
millistart = millis();
} // Serial available
// END SERIAL RECEIVE
// RADIO RECEIVE AND PROCESSING
// Check for any received packets
if (radio.receiveDone())
{
Blink(LED,5);
Serial.println(F("BEGIN RECEIVED"));
Serial.print(F("nodeid:"));Serial.print(radio.SENDERID, DEC);Serial.print(F(","));
cmdlength=0;
for (byte i = 0; i < radio.DATALEN; i++) {
Serial.print((char)radio.DATA[i]);
buff[i] = (char)radio.DATA[i];
cmdlength+=1;
}
Serial.print(F(",RX_RSSI:"));
Serial.println(radio.RSSI);
Serial.println(F("END RECEIVED"));
byte replynode = radio.SENDERID;
if (radio.ACKRequested())
{
radio.sendACK();
Blink(LED,5);
Serial.println(F(" - ACK sent"));
} // ack requested
if (cmdlength > 0){
processcmdstring(buff, cmdlength, replynode);
}
// Reset sleepdelay timer
millistart = millis();
} // Radio Receive
// END RADIO RECEIVE
if (SLEEPMODE) {
if (SLEEPDELAYTIMER < 65535){
// if timer is at 65535, means we are intentionally exiting
SLEEPDELAYTIMER = millis() - millistart;
}
}
else { // exit loop after one iteration if not in sleep mode
SLEEPDELAYTIMER = 65535;
}
// Serial.println(SLEEPDELAYTIMER);
} // SLEEPDELAY while
if (SLEEPMODE){
Serial.println(F("Exit Sleep"));
}
// Blink(LED,3);
// Do our sleep or delay
if (SLEEPMODE) {
// Serial.println(F("Going to sleep for "));
// Serial.println(LOOPPERIOD);
// Set cutoffs to optimize number of times we have to wake
// vs. accuracy of sleep time. We'll use 10x as rule of thumb
period_t sleepperiod;
unsigned int sleepremaining = LOOPPERIOD;
while (sleepremaining > 0) {
if (sleepremaining > 8000) {
// Use 8s interval
sleepperiod = SLEEP_8S;
sleepremaining -= 8000;
}
else if (sleepremaining >= 4000) {
// Use 4s interval
sleepremaining-=4000;
sleepperiod = SLEEP_4S;
}
else if (sleepremaining >= 2000) {
// Use 2s interval
sleepremaining-=2000;
sleepperiod = SLEEP_2S;
}
else if (sleepremaining >= 1000) {
// Use 1s interval
sleepremaining-=1000;
sleepperiod = SLEEP_1S;
}
else if (sleepremaining >= 500) {
// Use 500ms interval
sleepremaining-=500;
sleepperiod = SLEEP_500MS;
}
else if (sleepremaining >= 250) {
// Use 250ms interval
sleepremaining-=250;
sleepperiod = SLEEP_250MS;
}
else if (sleepremaining >= 120) {
// Use 120ms interval
sleepremaining-=120;
sleepperiod = SLEEP_120MS;
}
else if (sleepremaining >= 60) {
// Use 60ms interval
sleepremaining-=60;
sleepperiod = SLEEP_60MS;
}
else if (sleepremaining >= 30) {
// Use 30ms interval
sleepremaining-=30;
sleepperiod = SLEEP_30MS;
}
else {
// Use 15ms interval
sleepperiod = SLEEP_15Ms;
sleepremaining = 0;
}
Serial.flush();
radio.sleep();
LowPower.powerDown(sleepperiod, ADC_OFF, BOD_OFF);
// Serial.print(F("Sleep remaining: "));
// Serial.println(sleepremaining);
}
// Sleeptime eludes millis()
looptime += LOOPPERIOD;
}
else {
delay(LOOPPERIOD);
}
} // end main loop
void processcmdstring(String cmdstring, int cmdlength, byte replynode){
// Serial.print(F("Free memory: "));
// Serial.println(freeRam());
// Serial.println(F("processing cmdstring"));
// Serial.println(cmdstring);
// String //replystring="";
int i;
// this is as effective as clearing charstrings, since we'll just not
// send what we haven't already written
str1len=0;
str2len=0;
str3len=0;
str4len=0;
replylength=0;
replystring[0]=0;
// Serial.println(F("Command character received"));
// we need to prune ~ from first string only
int strord=1;
for (i=0;i<cmdlength;i++){
if ((cmdstring[i] != ';' && cmdstring[i] != '\n' )|| strord == 4){
if (cmdstring[i] != '\n' && cmdstring[i] != '\0' && cmdstring[i] != '\r' && ! (i==0 && cmdstring[i] == '~')){
if (strord == 1){
if (str1len < 56) {
charstr1[str1len]=cmdstring[i];
}
else {
charstr1[str1len]='#';
strord++;
}
str1len++;
}
else if (strord == 2){
charstr2[str2len]=cmdstring[i];
str2len++;
}
else if (strord == 3){
charstr3[str3len]=cmdstring[i];
str3len++;
}
else if (strord == 4){
charstr4[str4len]=cmdstring[i];
str4len++;
}
else {
Serial.println(F("Error in parse"));
}
} // if not special character
} // if not ;
else { // cmdstring is ; or \n
strord ++;
} //cmdstring is ;
} // for each character
// Terminate strings
charstr1[str1len]=0;
charstr2[str2len]=0;
charstr3[str3len]=0;
charstr4[str4len]=0;
// Serial.println(charstr1);
// Serial.println(charstr2);
// Serial.println(charstr3);
// Serial.println(charstr4);
if (cmdstring[0] == '~'){
if (strcmp(charstr1,"lp")==0) {
listparams(charstr2,atoi(charstr3));
// Serial.println(F("THE REPLYLENGTH"));
// Serial.println(replylength);
}
else if (strcmp(charstr1,"reset")==0) {
FLASH_STRING(msgbuffer, "cmd:reset");
msgbuffer.copy(&replystring[replylength], 9);
replylength+=9;
// resetMote();
}
else if (strcmp(charstr1,"gosleep")==0) {
SLEEPDELAYTIMER=65535; // send mote to sleep
}
else if (strcmp(charstr1,"mp")==0) {
// modparams sequence
FLASH_STRING(msgbuffer, "cmd:mp,");
msgbuffer.copy(&replystring[replylength], 7);
replylength+=7;
// Serial.println(charstr2);
if (strcmp(charstr2,"loop")==0) {
FLASH_STRING(msgbuffer2, "param:loop,sv:");
msgbuffer2.copy(&replystring[replylength], 14);
replylength+=14;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr3)*10;
if (newvalue > 0 && newvalue < 600000){
addstatusok(replystring, replylength);
// deliver in seconds, translate to msio
LOOPPERIOD = newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
} // loopperiod
else if (strcmp(charstr2,"rfech")==0) {
FLASH_STRING(msgbuffer2, "param:rfech,sv:");
msgbuffer2.copy(&replystring[replylength], 17);
replylength+=17;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr3);
if (newvalue >= 0 && newvalue <= 1){
addstatusok(replystring, replylength);
// deliver in seconds, translate to msio
serialrfecho = newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
} // loopperiod
else if (strcmp(charstr2,"slpmd")==0) {
FLASH_STRING(msgbuffer2, "param:slpmd,sv:");
msgbuffer2.copy(&replystring[replylength], 15);
replylength+=15;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
byte newvalue = atoi(charstr3);
if (newvalue == 0) {
SLEEPMODE = 0;
storeparams();
addstatusok(replystring, replylength);
}
else if (newvalue == 1) {
SLEEPMODE = 1;
storeparams();
addstatusok(replystring, replylength);
}
else {
addsverror(replystring,replylength);
}
} // sleepmode
else if (strcmp(charstr2,"slpdly")==0) {
FLASH_STRING(msgbuffer2, "param:slpdly,sv:");
msgbuffer2.copy(&replystring[replylength], 16);
replylength+=16;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr3)*100;
if (newvalue >= 300 && newvalue < 60000){
// deliver in 100s ms, translate to ms
SLEEPDELAY = newvalue;
storeparams();
}
else {
//replystring+="Sleepdelay value out of range";
}
} // sleepdelay
else if (strcmp(charstr2,"node")==0) {
FLASH_STRING(msgbuffer2, "param:node,sv:");
msgbuffer2.copy(&replystring[replylength], 14);
replylength+=14;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr3);
if (newvalue >= 2 && newvalue < 256 && str3len>0){
addstatusok(replystring, replylength);
// deliver in seconds, translate to ms
NODEID = newvalue;
radio.setAddress(NODEID);
storeparams();
}
else {
addsverror(replystring,replylength);
}
} // nodeid
else if (strcmp(charstr2,"nw")==0) {
FLASH_STRING(msgbuffer2, "param:nw,sv:");
msgbuffer2.copy(&replystring[replylength], 12);
replylength+=12;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr3);
if (newvalue >= 1 && newvalue < 256 && str3len>0){
addstatusok(replystring, replylength);
NETWORKID = newvalue;
radio.writeReg(REG_SYNCVALUE2, NETWORKID);
storeparams();
}
else {
addsverror(replystring,replylength);
}
} // network
else if (strcmp(charstr2,"gw")==0) {
FLASH_STRING(msgbuffer2, "param:gw,sv:");
msgbuffer2.copy(&replystring[replylength], 12);
replylength+=12;
memcpy(&replystring[replylength], charstr3, str3len);
replylength+=str3len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr3);
if (newvalue >= 1 && newvalue < 256 && str3len>0){
addstatusok(replystring, replylength);
GATEWAYID = newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
} // network
else if (strcmp(charstr2,"iomd")==0) {
FLASH_STRING(msgbuffer2, "param:iomd,ionum:");
msgbuffer2.copy(&replystring[replylength], 17);
replylength+=17;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int ionumber = atoi(charstr3);
if (ionumber >=0 && ionumber <14) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr4, str4len );
replylength+=str4len;
addcomma(replystring,replylength);
int newvalue = atoi(charstr4);
if (newvalue >= 0 && newvalue <5 && str4len>0) {
addstatusok(replystring, replylength);
iomode[ionumber]=newvalue;
ioreadtimer[ionumber] = 9999999; // read now
storeparams();
}
else {
addsverror(replystring,replylength);
}
}
else {
addindexerror(replystring,replylength);
}
} // iomode
else if (strcmp(charstr2,"ioen")==0) {
FLASH_STRING(msgbuffer2, "param:ioen,ionum:");
msgbuffer2.copy(&replystring[replylength], 17);
replylength+=17;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int ionumber = atoi(charstr3);
if (ionumber >=0 && ionumber <14 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr4, str4len );
replylength+=str4len;
addcomma(replystring,replylength);
int newvalue = atoi(charstr4);
if (newvalue == 0) {
addstatusok(replystring, replylength);
ioenabled[ionumber] = 0;
ioreadtimer[ionumber] = 9999999; // read now
storeparams();
}
else if (newvalue == 1) {
addstatusok(replystring, replylength);
ioenabled[ionumber] = 1;
ioreadtimer[ionumber] = 9999999; // read now
storeparams();
}
else {
addsverror(replystring,replylength);
}
}
else {
addindexerror(replystring,replylength);
}
} // ioenabled
else if (strcmp(charstr2,"iordf")==0) {
FLASH_STRING(msgbuffer2, "param:iordf,ionum:");
msgbuffer2.copy(&replystring[replylength], 18);
replylength+=18;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int ionumber = atoi(charstr3);
if (ionumber >=0 && ionumber <14 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr4, str4len );
replylength+=str4len;
addcomma(replystring,replylength);
long newvalue = atoi(charstr4)*100;
if (newvalue >= 0 && newvalue <600000) {
addstatusok(replystring, replylength);
ioreadfreq[ionumber]=newvalue;
storeparams();
}
else{
addsverror(replystring,replylength);;
}
}
else {
addindexerror(replystring,replylength);
}
} // ioreadfreq
else if (strcmp(charstr2,"iorpe")==0) {
FLASH_STRING(msgbuffer2, "param:iorpe,ionumber:");
msgbuffer2.copy(&replystring[replylength], 21);
replylength+=21;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int ionumber = atoi(charstr3);
if (ionumber >=0 && ionumber <14 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr4, str4len );
replylength+=str4len;
addcomma(replystring,replylength);
int newvalue = atoi(charstr4);
if (newvalue == 0) {
addstatusok(replystring, replylength);
ioreportenabled[ionumber] = 0;
storeparams();
}
else if (newvalue == 1) {
addstatusok(replystring, replylength);
ioreportenabled[ionumber] = 1;
storeparams();
}
else {
addsverror(replystring,replylength);
}
}
else {
addindexerror(replystring,replylength);
}
} // ioreport enabled
else if (strcmp(charstr2,"iorpf")==0) {
FLASH_STRING(msgbuffer2, "param:iorpf,ionum:");
msgbuffer2.copy(&replystring[replylength], 18);
replylength+=18;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int ionumber = atoi(charstr3);
if (ionumber >=0 && ionumber <14 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr4, str4len );
replylength+=str4len;
long newvalue = atoi(charstr4)*1000;
if (newvalue >= 0 && newvalue <600000) {
addstatusok(replystring, replylength);
ioreportfreq[ionumber]=newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
}
else {
addindexerror(replystring,replylength);
}
} // ioreportfreq
else if (strcmp(charstr2,"iov")==0) {
FLASH_STRING(msgbuffer2, "param:iov,ionum:");
msgbuffer2.copy(&replystring[replylength], 16);
replylength+=16;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int ionumber = atoi(charstr3);
if (ionumber >=0 && ionumber <14 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr4, str4len );
replylength+=str4len;
addcomma(replystring,replylength);
int newvalue = atoi(charstr4);
if (newvalue >= 0) {
addstatusok(replystring, replylength);
iovalue[ionumber]=newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
}
else {
addindexerror(replystring,replylength);
}
} // iovalues
else if (strcmp(charstr2,"chansv")==0) {
FLASH_STRING(msgbuffer2, "param:chansv,chnum:");
msgbuffer2.copy(&replystring[replylength], 24);
replylength+=24;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int channumber = atoi(charstr3);
if (channumber >=0 && channumber <=8 && str3len>0) {
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
// need to allow for floats
int newvalue = atoi(charstr4);
addstatusok(replystring, replylength);
chansv[channumber]=newvalue;
storeparams();
}
else {
addindexerror(replystring,replylength);
}
}// chansv
else if (strcmp(charstr2,"chpvind")==0) {
FLASH_STRING(msgbuffer2, "param:chanpvind,ionum:");
msgbuffer2.copy(&replystring[replylength], 22);
replylength+=22;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int channumber = atoi(charstr3);
if (channumber >=0 && channumber <=8 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int newvalue = atoi(charstr4);
if (newvalue >=0 and newvalue <13){
addstatusok(replystring, replylength);
chanpvindex[channumber]=newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
}
else {
addindexerror(replystring,replylength);
}
}// chanpvindex
else if (strcmp(charstr2,"chpf")==0) {
FLASH_STRING(msgbuffer2, "param:chpf,ionum:");
msgbuffer2.copy(&replystring[replylength], 17);
replylength+=17;
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int channumber = atoi(charstr3);
if (channumber >=0 && channumber <=8 && str3len>0) {
addsv(replystring,replylength);
memcpy(&replystring[replylength], charstr3, str3len );
replylength+=str3len;
addcomma(replystring,replylength);
int newvalue = atoi(charstr4);
if (newvalue >=0 and newvalue <13 && str3len>0){
addstatusok(replystring, replylength);
chanposfdbk[channumber]=newvalue;
storeparams();
}
else {
addsverror(replystring,replylength);
}
}