1 | // ----------------------------------------------------------------------
|
2 |
|
3 | #include <SPI.h>
|
4 | #include <Ethernet.h>
|
5 | #include <stdio.h>
|
6 | #define MAX_PIN 13 //UNO
|
7 |
|
8 | // IP AND MAC ADRESS
|
9 | #define PORT 7777 //-> Type this under "TCP-Port" in the App
|
10 | byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xC1, 0x9C}; //If you're using more than one Arduino, set different Mac adresses for each board!
|
11 | IPAddress ip(192, 168,1, 168);
|
12 |
|
13 | String inputstream = "";
|
14 | String answer = "";
|
15 | String text = "";
|
16 | String command = "";
|
17 | String serialInput = "";
|
18 |
|
19 | int pin = 0;
|
20 | unsigned long value = 0;
|
21 | unsigned long ticks[MAX_PIN]; //TICKS FOR EACH OUTPUT. USED IF AN OFFTIMER FOR AN OUTPUT HAS BEEN SET. 1 TICK = 100ms
|
22 | unsigned long ticks_at_command; //IF WE GET NO COMMAND FOR XX SECONDS, THEN STOP CLIENT.
|
23 |
|
24 |
|
25 | char charBuf[250];
|
26 |
|
27 | EthernetServer server = (PORT);
|
28 | boolean verbunden = false;
|
29 | EthernetClient client;
|
30 |
|
31 | boolean pwm[MAX_PIN];
|
32 |
|
33 | //Variables for the PWM-Values. The PWM-Values are always between 0 and 255, no matter, what Min/Max-Values you have set in the App.
|
34 | //NEVER CHANGE!!! The Position of the slider depends on that value.
|
35 | //(You can't read pwm values directly from a pin.)
|
36 | int pwm_value[MAX_PIN];
|
37 |
|
38 |
|
39 |
|
40 | //________________________________________________________________________________________________________________________________________________
|
41 | void setup() {
|
42 |
|
43 | /*
|
44 | Arduino communicates with the shield using the SPI bus.
|
45 | This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega.
|
46 | On both boards, pin 10 is used as SS.
|
47 | On the Mega, the hardware SS pin, 53, is not used to select the W5500, but it must be kept as an output or the SPI interface won't work.
|
48 | */
|
49 |
|
50 |
|
51 | //SET ALL DIGITAL PINS AS OUTPUT
|
52 | //ATTENTION: DO NOT USE 11,12,13 ON THE UNO WITH ETHERNET SHIELD!
|
53 |
|
54 | for (int i = 0;i<MAX_PIN;i++){
|
55 | if ((i < 10) || (i >13)) pinMode(i, OUTPUT);
|
56 | ticks[i] = 0;
|
57 | }
|
58 |
|
59 | // start the Ethernet connection:
|
60 | Ethernet.begin(mac,ip);
|
61 | server.begin();
|
62 |
|
63 | //START SERIAL 0 (Debug Port)
|
64 | //Serial.begin(2400); //The Serial Port occupies Outputs 0 & 1
|
65 |
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | //________________________________________________________________________________________________________________________________________________
|
72 | void loop() {
|
73 |
|
74 | client = server.available();
|
75 |
|
76 |
|
77 | if(client.available())
|
78 | {
|
79 |
|
80 | ticks_at_command = millis();
|
81 |
|
82 | char zeichen = client.read();
|
83 |
|
84 | inputstream += zeichen;
|
85 |
|
86 |
|
87 | while (inputstream.indexOf(';')>0){
|
88 |
|
89 |
|
90 | inputstream.trim();
|
91 |
|
92 | //ALL COMMANDS COMING FROM THE APP ARE CONSTRUCTED AS FOLLOWS:
|
93 | // 4 CHARS COMMAND (SETD = Set digital Pin ,GETD = Get digital Pin, OFFT = Set Offtimer,SETP = Set PWM, GETP = Get PWM)
|
94 | // x CHARS PIN (0,1,2,3...51,52,53)
|
95 | // 1 POINT .
|
96 | // x CHARS VALUE (0 = Off, 1 = On, 0-255 = PWM Value)
|
97 | // 1 SEMICOLON ;
|
98 |
|
99 | //FOR EXAMPLE:
|
100 |
|
101 | //SETD1.1; Set digital Pin 1 on
|
102 | //SETD3.0; Set digital Pin 3 off
|
103 | //SETP15.255; Set PWM for Pin 15 to 255
|
104 | //OFFT17.200; Set offtimer for pin 17 to 200ticks (20 seconds)
|
105 | //GETA5; Get value of analogue Pin 5
|
106 |
|
107 | command = inputstream.substring(0,4);
|
108 | value = 0;
|
109 | pin = -1;
|
110 |
|
111 |
|
112 | value = inputstream.substring(inputstream.lastIndexOf('.')+1).toInt();
|
113 | pin = inputstream.substring(4,inputstream.lastIndexOf('.')).toInt();
|
114 |
|
115 |
|
116 | //UNKNOWN COMMAND?
|
117 | if ((command != "SETD" && command != "GETD" && command != "SETP" && command != "GETA" && command != "OFFT" && command != "GETP") || pin == -1 ){
|
118 | inputstream = "";
|
119 | command = "";
|
120 | value = 0;
|
121 | pin = 0;
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 | //GET STATE OF DIGITAL OUTPUT (GETDXX). ANSWER: "DXX.0;" OR "DXX.1;"
|
127 | if (command == "GETD"){
|
128 | answer += "D" + String(pin) + "." + digitalRead(pin) + ";";
|
129 | }
|
130 |
|
131 | //GET STATE OF ANALOG INPUT (GETAXX). ANSWER: "AXX.YYY;" YYY = 0 to 1023
|
132 | else if (command == "GETA"){
|
133 | answer += "A" + String(pin) + "." + analogRead(pin) + ";";
|
134 | }
|
135 | //SET DIGITAL OUTPUT ON (SETDXX.1) OR OFF (SETDXX.0). ANSWER: "DXX.0;" OR "DXX.1;"
|
136 | else if (command == "SETD"){
|
137 | ticks[pin] = 0;
|
138 | digitalWrite(pin,value);
|
139 | answer += "D" + String(pin) + "." + digitalRead(pin)+";";
|
140 |
|
141 | }
|
142 |
|
143 | //SET OFFTIMER FOR A DIGITAL OUTPUT (OFFTXX.SS) <- XX = PIN, SS = 1/10Sec (1 SECOND = 10), ANSWER: "DXX.1;"
|
144 | else if (command == "OFFT"){
|
145 | digitalWrite(pin,1);
|
146 | ticks[pin] = millis() + (value*100);
|
147 | answer += "D" + String(pin) + ".1;";//digitalRead(pin);
|
148 | }
|
149 |
|
150 | //SET PWM VALUE (SETPXX.YYY)
|
151 | else if (command == "SETP"){
|
152 | analogWrite(pin,value);
|
153 | pwm_value[pin] = value;
|
154 | answer += "P" + String(pin) + "." + String(pwm_value[pin], DEC) + ";";
|
155 |
|
156 |
|
157 | }
|
158 | //GET PWM VALUE (GETPXX). ANSWER: "PXX.YYY;"
|
159 | else if (command == "GETP"){
|
160 |
|
161 | answer += "P" + String(pin) + "." + String(pwm_value[pin], DEC) + ";";
|
162 |
|
163 | }
|
164 |
|
165 |
|
166 | inputstream = inputstream.substring(inputstream.indexOf(';')+1);
|
167 |
|
168 | }loop;//<-(While inputstream Contains ';')
|
169 |
|
170 |
|
171 | //clear Input Buffer
|
172 | client.flush();
|
173 |
|
174 |
|
175 | }//<-End if(client.available())
|
176 |
|
177 |
|
178 | //NO COMMANDS FOR 5 SECONDS->RESTART ETHERNET SERVER. (just "client.stop()" may cause problems)
|
179 | if ((millis() > ticks_at_command + 5000) && ticks_at_command > 0) {
|
180 | ticks_at_command = 0;
|
181 | client.stop();
|
182 | Ethernet.begin(mac,ip);
|
183 | server.begin();
|
184 | }
|
185 |
|
186 |
|
187 | //CHECK FOR OFFTIMERS
|
188 | for (int i = 0; i< MAX_PIN; i++){
|
189 | if ((millis() >= ticks[i]) && (ticks[i] > 0)){
|
190 | ticks[i] = 0;
|
191 | digitalWrite(i,0);
|
192 | text = String(i);
|
193 | answer +="D" + text + ".0;";
|
194 | }
|
195 | }
|
196 |
|
197 |
|
198 | //SEND ANSWER TO PHONE
|
199 | if (answer != ""){
|
200 |
|
201 | answer.toCharArray(charBuf, answer.length()+1);
|
202 | server.write(charBuf,answer.length()); //Writes to all Clients connected
|
203 | answer = "";
|
204 |
|
205 | }
|
206 |
|
207 | }//<-MAIN LOOP
|
208 |
|
209 | //________________________________________________________________________
|