-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_lamp_TCPok_SAVE.ino
318 lines (277 loc) · 8.49 KB
/
arduino_lamp_TCPok_SAVE.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
#include <FastLED.h>
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
// VARIABILI LAMPADA
String lampName = "Studio Smartlamp";
String lampImg = "https://i.imgur.com/xRgIEAf.png";
// VARIABILI PIN
const int ledPin = 5;
const int sensorPin = D6;
const int builtinLed = 2;
// VARIABILI LED
const int numLED = 5; // numero di led sulla striscia
CRGB leds[numLED];
int ledColor = 100;
int ledIntensity = 25;
int ledIncrement = 5;
// VARIBIABILI TEMPO
const int ciclo = numLED + 1;
const int waitTime = 50;
const int TIMEOUT = 5000;
int i=0;
int r=0;
int nLoops = 1;
int udpTimeInit = 0;
int udpTimeEnd = 0;
// VARIABILI SENSORE IR
int sensorState = HIGH;
int oldSensorState = LOW;
// VARIABILI MOTORI
Servo motoreSx;
Servo motoreDx;
int angleSx = 90;
int angleDx = 90;
// VARIABILI STATO
int oldState = 0; // si userà vecchio_val per conservare lo stato del pin di input al passo precedente
int ledState = 0; //io // ricorda lo stato in cui si trova il led, stato = 0 led spento, stato = 1 led acceso
int intensityMode = 0;
// VARIABILI WIFI
IPAddress ip;
WiFiUDP Udp;
WiFiServer server(2048);
unsigned int localUdpPort = 4096;
const char* ssid = "iPhone di Mattia";
const char* password = "mlnmtt92";
String rx_buffer = ""; // Buffer to store INPUT data
String tx_buffer = ""; // Buffer to store OUTPUT data
const int INPUT_SIZE = 5; // 5 byte: 1 per il char del comando e 4 per l'int del valore
byte incomingPacket[INPUT_SIZE];
String initParam = "";
char outPacket[128] = "";
char command;
String value = "";
// VARIABILI MEMORIA
int memAddrIo = 0;
int memAddrIntensity = 1;
int memAddrColor = 2;
int memAddrAngleSx = 3;
int memAddrAngleDx = 4;
// FUNZIONI
// LED ON
void ledOn() {
for (int j=0; j<numLED; j++)
leds[j] = CHSV(ledColor,255,ledIntensity);
FastLED.show();
delay(100);
tx_buffer = "y1"; // yes
ledState = 1;
}
// LED OFF
void ledOff() {
for (int j=0; j<numLED; j++)
leds[j] = CHSV(0,0,0);
FastLED.show();
delay(100);
tx_buffer = "n0"; // no
ledState = 0;
}
// CAMBIA COLORE
void setColor(int val) {
ledColor = val;
for (int j=0; j<numLED; j++)
leds[j] = CHSV(ledColor,255,ledIntensity);
FastLED.show();
delay(100);
Serial.println("New color "+ val);
String tmp = String(val); // da int a String
tx_buffer = "c"+tmp; // aggiorna buffer OUT per rispondere ad Android
}
// CAMBIA INTENSITA
void setIntensity(int val) {
ledIntensity = val;
for (int j=0; j<numLED; j++)
leds[j] = CHSV(ledColor,255,ledIntensity);
FastLED.show();
Serial.println("New intensity");
String tmp = String(ledIntensity); // da int a String
tx_buffer = "i"+tmp; // aggiorna buffer OUT per rispondere ad Android
}
// CAMBIO ANGOLO SX
void setAngleSx(int val) {
angleSx = val;
motoreSx.write(angleSx);
delay(100); // aspetta che il motore agisca
Serial.println("New angle "+ val);
String tmp = String(val); // da int a String
tx_buffer = "s"+tmp; // aggiorna buffer OUT per rispondere ad Android
}
// CAMBIO ANGOLO DX
void setAngleDx(int val) {
angleDx = val;
motoreDx.write(angleDx);
delay(100); // aspetta che il motore agisca
Serial.println("New angle "+ val);
String tmp = String(val); // da int a String
tx_buffer = "d"+tmp; // aggiorna buffer OUT per rispondere ad Android
}
// SALVA STATO LAMAPDA
void saveState() {
EEPROM.write(memAddrIo, ledState);
EEPROM.write(memAddrColor, ledColor);
EEPROM.write(memAddrIntensity, ledIntensity);
EEPROM.write(memAddrAngleSx, angleSx);
EEPROM.write(memAddrAngleDx, angleDx);
EEPROM.commit();
}
// RECUPERA STATO LAMPADA
void loadState() {
ledState = EEPROM.read(0);
ledIntensity = EEPROM.read(1);
ledColor = EEPROM.read(2);
angleSx = EEPROM.read(3);
angleDx = EEPROM.read(4);
}
// SPLIT: DIVIDE IL PACCHETTO IN INGRESSO
void split (String msg) { // Splits message in 2 parts: commands and values
command = msg[0]; // primo char per il comando da interpretare
value = msg.substring(1);
}
// INVIA TX
void send_message(WiFiClient client, String msg) { // Send a message to the given client
client.println(msg);
Serial.println("[TX] " + msg);
}
// GESTIONE RX
void got_message(String msg) { // Called whenever a newline-delimited message is received
Serial.println("[RX] " + msg);
split(msg);
int newVal = value.toInt(); // da stringa a int
// CASI DI UPDATE
switch (command) {
case 'y' : ledOn(); break;
case 'n' : ledOff(); break;
case 'c' : setColor(newVal); break;
case 'i' : setIntensity(newVal); break;
case 's' : setAngleSx(newVal); break;
case 'd' : setAngleDx(newVal); break;
}
}
// INIZIALIZZAZIONE
void setup() {
// LED
FastLED.addLeds<NEOPIXEL, ledPin>(leds, numLED);
// led interno per debug udp
pinMode(LED_BUILTIN,OUTPUT);
pinMode(builtinLed, OUTPUT);
// sensore IR
pinMode(sensorPin, INPUT);
// MOTORI
/*
motoreSx.attach(); // MANCANO I PIN DEI MOTORI
motoreDx.attach();
*/
// MEMORIA
EEPROM.begin(10);
loadState();
// MONITOR SERIALE PER DEBUG
Serial.begin(115200);
Serial.println(ip);
Serial.printf("Connecting to %s ", ssid);
// Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
Serial.println("Connected to IP: ");
Serial.println(WiFi.localIP());
IPAddress ip = WiFi.localIP(); // indirizzo assegnato
// UDP BROADCAST
Udp.begin(localUdpPort);
initParam = lampName + ";https://i.imgur.com/xRgIEAf.png";
initParam.toCharArray(outPacket, 128);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
udpTimeInit = millis(); // attiva timer per mandare pacchetto ogni 5 secondi
// SERVER TCP
server.begin();
Serial.println("WiFi server initialized!");
}
// RUN
void loop() {
// UDP BROADCAST
udpTimeEnd = millis() - udpTimeInit; // timer che manda un pacchetto UDP broadcast ogni 5 secondi
if (udpTimeEnd >= TIMEOUT) { // ogni 5 secondi
digitalWrite(builtinLed, LOW); // accendi il led integrato di arduino
Udp.beginPacket("255.255.255.255", 4096);
Udp.write(outPacket);
Udp.endPacket();
delay(500);
Serial.println(outPacket); // print su console
digitalWrite(builtinLed, HIGH);
udpTimeInit = millis(); // ripristina timeout
}
// CONNESSIONE TCP
WiFiClient client = server.available();
if (client) { // Arduino rimane sempre in ascolto e Android gli chiede come sta ogni mezzo secondo, ricevendo sempre una risposta
while (client.connected()){
// RX
if (client.available()) { // Read data from the client till we hit a newline
while (client.available()) {
char c = client.read();
if (c == '\n') {
got_message(rx_buffer);
rx_buffer = "";
} else rx_buffer += c;
}
// TX
if (tx_buffer.equals(""))
break;
else {
send_message(client, tx_buffer); // se il buffer è vuoto Android non fa nulla, altrimenti lo interpreta
tx_buffer = "";
}
}
}
}
// STATI DEL SENSORE PER DISTINGUERE TRA ON/OFF E INTENSITY
sensorState = digitalRead(sensorPin); // sensore IR
if ((sensorState == LOW) && (ledState == 0)) {
// ON MODE
ledOn();
delay(50);
}
else if ((sensorState == LOW) && (ledState == 1)) {
delay(250);
if (digitalRead(sensorPin) == LOW) {
while (digitalRead(sensorPin) == LOW) {
// INTENSITA MODE
ledIntensity = (ledIntensity+ledIncrement);
setIntensity(ledIntensity);
//FastLED.show();
delay(100);
// DECREMENTA
if (ledIntensity == 255) {
ledIncrement = -5;
}
// INCREMENTA
if (ledIntensity == 0) {
ledIncrement = +5;
}
}
}
else {
// OFF MODE
ledOff();
delay(50);
}
}
// SALVA STATO IN MEMORIA
saveState();
delay(100);
// close connection. Give the client time to receive data before closing
// Serial.println("Disconnecting from client");
delay(100);
client.flush();
client.stop();
} // end loop