-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNexaServer.ino
187 lines (128 loc) · 3.67 KB
/
NexaServer.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
#include <NexaTransmitter.h>
#include <stdlib.h>
//#include <LiquidCrystal_I2C.h>
#include <Wire.h>
/*
Sender:
14424562
110111000001100111110010
14424562,1,2,3
14424562,0,1,0
0123456789101112
--------------------------------
Byte0: 11110010
Byte1: 00011001
Byte2: 11011100
*/
const int txPin = 2;
const int SENDER = 1;
//const int UNIT = 2;
const int STATE = 2;
const int DIMLEVEL = 3;
const unsigned int MAXLENGTH = 20;
//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
//NexaTransmitter remote(txPin, 14424562); // Create the nexa remote on pin12 with remote id
// 63161283 works :D:D
void setup() {
Serial.begin(9600);
Serial.println("Start of NEXA Transmitter");
Serial.println("");
Serial.println("");
Serial.println("");
//lcd.begin(16,2);
}
void ReadData(const byte incomingByte) {
static char buf [MAXLENGTH];
static unsigned int inputPos = 0;
switch (incomingByte) {
case '\n': // end of text
buf [inputPos] = 0; // terminating null byte
Serial.print("buf = ");
Serial.println(buf);
// terminator reached! process input_line here ...
ProcessData(buf, inputPos);
// reset buffer for next time
inputPos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (inputPos < (MAXLENGTH - 1)) buf [inputPos++] = incomingByte;
break;
} // end of switch
}
void ProcessData(const char* data, int length) {
String senderTemp = "";
uint32_t sender = 0;
int state = 0;
int firstBreak = 0;
int secondBreak = 0;
int thirdBreak = 0;
int dimLevel = 0;
String dimTemp = "";
int unit = 0;
int separatorCounter = 0;
int i = 0;
//Serial.print("data: ");
//Serial.println(data);
//while(i < strlen(data)) {
while(i < length) {
//for(i = 0; i < strlen(data); i++) {
if(data[i] == ',') {
separatorCounter++;
switch (separatorCounter) {
case SENDER:
for(int x = 0; x < i; x++) senderTemp += data[x];
sender = senderTemp.toInt();
Serial.print("sender = ");
Serial.println(sender);
unit = data[i+1] - '0';
Serial.print("unit = ");
Serial.println(unit);
break;
case STATE:
state = data[i+1] - '0';
Serial.print("state = ");
Serial.println(state);
break;
case DIMLEVEL:
if(i == strlen(data)-2) dimLevel = data[i+1] - '0';
else if(i == strlen(data)-3) {
dimTemp += data[i+1];
dimTemp += data[i+2];
dimLevel = dimTemp.toInt();
}
Serial.print("dimLevel = ");
Serial.println(dimLevel);
if(dimLevel > 15) Serial.println("dimLevel > 15 not allowed. Set to 15 instead.");
break;
}
i++;
}
else i++;
}
//if(senderTemp.length() > 0 && dimTemp.length() > 0) {
NexaTransmitter remote(txPin, sender);
for(int i = 0; i < 5; i++) remote.setSwitch(state, unit, dimLevel); // switch on/down the unit 0 to dim level 0 (out of 15)
Serial.println("Message transmitted.");
Serial.println("");
//for(int i = 0; i < 5; i++)
//remote.setSwitch(state, unit, dimLevel); // switch on/down the unit 0 to dim level 0 (out of 15)
//remote.setSwitch(1, 0, dimLevel);
//remote.setSwitch(1, 1, dimLevel);
//for(int i = 0; i < 5; i++) remote.setSwitch(true, 0); // switch on/down the unit 0 to dim level 0 (out of 15)
}
void loop() {
//int i = 0;
//int incomingByte = 0;
//int numBytes;
//byte incomingByte[24];
//char buf[16];
//NexaTransmitter remote(2, 14424562);
while (Serial.available() > 0) ReadData(Serial.read());
//for(int i = 0; i < 5; i++) remote.setSwitch(true, 0);
//Serial.println("sent");
//delay(1000);
//memset
}