UDP.begin() stuck program

Hi everybody, this is my first post and I need a help to solve a problem that I don’t understand.
I made a sketch that work fine with arduino UNO and an ethernet shield BUT…
if I put the string: Udp.begin(localPort) the program stuck in row 157.
I show the code until the problematic row.
Thanks and I apologize for my english
Alessandro.

1. include <SunLight.h>
2. include <SPI.h>
3. include <Ethernet.h>
4. include <EthernetUdp.h>
5. include <BlynkSimpleEthernet.h>
6. include <TimeLib.h>
7. include <WidgetRTC.h>

8. define BLYNK_GREEN     "#23C48E"
9. define BLYNK_BLUE      "#04C0F8"
10. #define BLYNK_YELLOW    "#ED9D00"
11. #define BLYNK_RED       "#D3435C"
12. #define BLYNK_DARK_BLUE "#5F7CD8"
13. SunLight mySun; // Declaration of the object
14. float myLatitude = 45.9;
15. float myLongitude = 12.6;

16. //----------------- Comunicazione UDP (User Datagram Protocol)  ---------------------
17. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //DE:AD:BE:EF:FE:ED
18. IPAddress ip(192,168,1,40);// arduino luci
19. unsigned int localPort = 7880; // arduino luci
20. char DatiRX[20];
21. EthernetUDP Udp;
22. IPAddress IP_Remote(192,168,1,41); //citofono
23. unsigned int Port_Remote = 7878;  //citofono

24. EthernetClient client;
25. char server[] =  { 64, 233, 187, 99 }; //"www.google.com";  

26. //----------------- TIME ---------------------
27. WidgetRTC rtc;
28. BLYNK_CONNECTED() { rtc.begin(); }// Synchronize time on connection
29. int oraPrecedente = 0;

30. //--------------------------------------
31. #define BLYNK_PRINT Serial
32. char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
33. //passw: alebl //
34. int BttBlynkLuci = LOW;
35. int BttBlinkAntifurto = LOW;
36. int BttBlinkDisattivaSensore = LOW;
37. int BttBlinkflash = LOW;
38. int ValueUltimaVisita = 0;
39. bool SensorePresenzaDisattivato = false;
40. bool presenzaAttivata = false;
41. bool contaSecondiLuceAccesaTemporaneaTimer = false;
42. String tutto = "";
43. BlynkTimer timer;
44. void checkPhysicalButton();
45. void checkPhysicalButton(){}
46. BLYNK_WRITE(V0) {BttBlynkLuci = param.asInt();}
47. BLYNK_WRITE(V1) {BttBlinkAntifurto = param.asInt();}
48. BLYNK_WRITE(V2) {BttBlinkflash = param.asInt();}
49. BLYNK_WRITE(V3) {ValueUltimaVisita = param.asInt();}
50. BLYNK_WRITE(V4) {BttBlinkDisattivaSensore = param.asInt();}

51. //BLYNK_READ(V2) {Blynk.
52. bool luceRossaAttivata = false;
53. bool luceGiallaAttivata = false;
54. bool luceVerdeAttivata = false;

55. WidgetLED ledWrosso(V11);
56. WidgetLED ledWgiallo(V12);
57. WidgetLED ledWverde(V13);

58. //Uscite:
59. #define LedSemaforoRosso 7
60. #define LedSemaforoGiallo 6
61. #define LedSemaforoVerde 5
62. #define ComandoRele 4     

63. //Ingressi:
64. #define ConsensoPLC 3
65. #define SensorePresenza 2

66. bool StatoLuceManualePLC = false;
67. bool StatoLuceManualeBlynk = false;
68. int  Presenza = LOW;    //SensorePresenza
69. int  Consenso = LOW;  //ConsensoPLC

70. int contaSecondiLuceAccesa = 10000;
71. int contaSecondiPresenzaRilevata = 15000;
72. int contaSecondiLuceAccesaMax = 10000;
73. int contaSecondiLuceAccesaTemporanea = 10000;

74. void AzzeraSemaforo()
75. {
76.     digitalWrite(LedSemaforoRosso, LOW);
77.     digitalWrite(LedSemaforoGiallo, LOW);
78.     digitalWrite(LedSemaforoVerde, HIGH);
79.     ledWrosso.off();
80.     ledWgiallo.off();
81.     ledWverde.on();
82.     luceRossaAttivata = false;
83.     luceGiallaAttivata = false;
84.     luceVerdeAttivata = true;
85. }
86. void lampeggio(int numeroLampeggi)
87. {
88.   
89. 	for (int i=0; i <= numeroLampeggi; i++)
90.     {
91. 		digitalWrite(ComandoRele, LOW); //Low = ON
92. 		delay(200);
93. 		digitalWrite(ComandoRele, HIGH); //Low = OFF
94. 		delay(200);
95.     }
96. }

97. void clockDisplay() //ogni 10 secondi
98. {
99. 	Serial.println("-- 10 sec. --------------------");
100.  	if (contaSecondiLuceAccesa <10000) {contaSecondiLuceAccesa += 10;}
101.  	if (contaSecondiPresenzaRilevata <15000) {contaSecondiPresenzaRilevata += 10;}
102.  	if (contaSecondiLuceAccesaMax <10000) {contaSecondiLuceAccesaMax += 10;}

103.  	
104.  	Blynk.virtualWrite(V3, contaSecondiPresenzaRilevata/60); 
105.  }

106.  void setup()
107.  {  
108.  	Blynk.begin(auth);
109.  	Serial.begin(9600);
110.  	Serial.println( "Inizia SETUP");
111.    
112.  // ----------- ATTIVA ETHERNET E VERIFICA LA CONNESSIONE ---------------
113.  	if (Ethernet.begin(mac) == 0)
114.  	{
115.  		Serial.println("Failed to configure Ethernet using DHCP");
116.  		Ethernet.begin(mac, ip);
117.  	}
118.  // give the Ethernet shield a second to initialize:
119.  	delay(1000);
120.  	Serial.println("connecting...");
121.   
122.  	// if you get a connection, report back via serial:
123.  	if (client.connect(server, 80))
124.  	{
125.  		Serial.println("connected");
126.  		client.println("GET /search?q=arduino HTTP/1.1");// Make a HTTP request:
127.  		client.println("Host: www.google.com");
128.  		client.println("Connection: close");
129.  		client.println();
130.  	} 
131.  	else
132.  	{
133.  		Serial.println("connection failed"); // if you didn't get a connection to the server:
134.  	} 
135.  //------------- UDP ------------------
136.  	Udp.begin(localPort);
137.  	delay(1500);
138.  	Serial.println("UDP started");
139.   
140.  //-------------------- prendi l'ora -------------------
141.  	setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
142.  	timer.setInterval(10000L, clockDisplay);
143.  	//timer.setInterval(5000L, TimerCinqueSecondi);
144.    
145.  	pinMode(LedSemaforoRosso, OUTPUT);
146.  	pinMode(LedSemaforoGiallo, OUTPUT);
147.  	pinMode(LedSemaforoVerde, OUTPUT);
148.  	pinMode(ComandoRele, OUTPUT);
149.  	digitalWrite(ComandoRele, HIGH);
150.  	pinMode(ConsensoPLC, INPUT);
151.  	pinMode(SensorePresenza, INPUT);
152.  	Serial.println("Setup ended");
153.  }  
154.    
155.  void loop() {
156.     Serial.println("Berfor Blynk ok");
157.  	Blynk.run();
158.  	Serial.println("After Blynk ok");
159.     timer.run();

160.   Presenza = digitalRead(SensorePresenza);
161.   Consenso = digitalRead(ConsensoPLC);
162.   if(BttBlinkflash == HIGH) { lampeggio(3); }
163. //----------------------------------- DISATTIVA SENSORE ---------------------------------
164. if(BttBlinkDisattivaSensore == HIGH && SensorePresenzaDisattivato == false)
165. 	{
166. 	Blynk.setProperty(V4, "color", BLYNK_RED);
167. 	BttBlinkDisattivaSensore = LOW;
168. 	SensorePresenzaDisattivato = true;
169. 	delay(3000);
170. 	}

171. if(BttBlinkDisattivaSensore == HIGH && SensorePresenzaDisattivato == true)
172. 	{ 
173. 	Blynk.setProperty(V4, "color", BLYNK_GREEN); 
174. 	BttBlinkDisattivaSensore = LOW;
175. 	SensorePresenzaDisattivato = false; 
176. 	delay(3000);
177. 	}

178. //----------------------------------- COMANDO MANUALE PLC ---------------------------------
179. if (Consenso == HIGH && StatoLuceManualePLC == false) //se la luce è spenta la accende
180.     {
181.     delay(1000);
182.     digitalWrite(ComandoRele, LOW); //Low = ON
183.     contaSecondiLuceAccesaMax=0;
184.     Serial.println("Comando PLC manuale ON");
185.     StatoLuceManualePLC = true;
186.     Consenso = LOW;
187.     } 
188.      
189. if (Consenso == HIGH && StatoLuceManualePLC == true) //se la luce è accesa la spegne
190.     {
191.     delay(1000);
192.     digitalWrite(ComandoRele, HIGH); //High = OFF
193.     contaSecondiLuceAccesaMax=10000;
194.     Serial.println("Comando PLC manuale OFF");
195.     StatoLuceManualePLC = false;
196.     Consenso = HIGH;
197.     }
198. //----------------------------------- COMANDO MANUALE BLYNK ---------------------------------
199. if (BttBlynkLuci == HIGH && StatoLuceManualeBlynk == false)
200. 	{
201. 	delay(1000);
202. 	digitalWrite(ComandoRele, LOW); //Low = ON
203. 	contaSecondiLuceAccesaMax=0;
204. 	Serial.println("Comando Blynk manuale ON");
205. 	Blynk.setProperty(V0, "color", BLYNK_YELLOW);
206. 	StatoLuceManualeBlynk = true;
207. 	} 

208. if (BttBlynkLuci == LOW && StatoLuceManualeBlynk == true)
209. 	{
210.     delay(1000);
211. 	digitalWrite(ComandoRele, HIGH); //High = OFF
212.     contaSecondiLuceAccesaMax=10000;
213.     Serial.println("Comando Blynk manuale OFF");
214. 	Blynk.setProperty(V0, "color", BLYNK_DARK_BLUE);
215. 	StatoLuceManualeBlynk = false;
216. 	}
217.   
218. if(contaSecondiLuceAccesaMax > 1600 && contaSecondiLuceAccesaMax < 9999) //se la luce è accesa da più di 10 min(600) la spegne
219. 	{
220.     digitalWrite(ComandoRele, HIGH);//High = OFF
221.     contaSecondiLuceAccesaMax=10000;
222. 	Blynk.setProperty(V0, "color", BLYNK_DARK_BLUE);
223. 	Blynk.virtualWrite(V0, LOW);
224. 	
225. 	}

226. //------------------------------------- DA SENSORE --------------------------------------
227.     
228. if (Presenza == HIGH && SensorePresenzaDisattivato == false)
229. 	{
230. 	//Serial.println(BttBlinkAntifurto);
231. 	contaSecondiPresenzaRilevata=0;
232. 	if(hour() < 7 || hour() > 16)
233. 		{
234. 		digitalWrite(ComandoRele, LOW);//Low = ON
235. 		contaSecondiLuceAccesa=0;
236. 		}
237. 		
238.     if(BttBlinkAntifurto == 1 )
239. 		{
240. 		Blynk.notify("Rilevata presenza");
241. 		}
242.     delay(10000);
243.     }
244.   
245.   if (contaSecondiLuceAccesa > 120 && contaSecondiLuceAccesa < 300 && contaSecondiLuceAccesaMax > 600)
246.   {
247.     digitalWrite(ComandoRele, HIGH); //High = OFF
248.     contaSecondiLuceAccesa = 10000;
249.   }  
250.   
251.   //------------------------------------- GESTIONE SEMAFORO --------------------------------------
252. if (contaSecondiPresenzaRilevata > 1 && contaSecondiPresenzaRilevata < 50 && luceRossaAttivata == false) //il semaforo parte con il rosso
253.     {
254.   luceRossaAttivata = true;
255.   luceGiallaAttivata = false;
256.   luceVerdeAttivata = false;
257.     digitalWrite(LedSemaforoRosso, HIGH);
258.     digitalWrite(LedSemaforoGiallo, LOW);
259.     digitalWrite(LedSemaforoVerde, LOW);
260.     ledWrosso.on();
261.     ledWgiallo.off();
262.     ledWverde.off();
263.     } 
264.      
265.     if(contaSecondiPresenzaRilevata > 1800 && contaSecondiPresenzaRilevata < 1900 && luceGiallaAttivata == false) // dopo 30 minuti diventa giallo
266.     {
267.     
268.   luceRossaAttivata = false;
269.   luceGiallaAttivata = true;
270.   luceVerdeAttivata = false;
271.     digitalWrite(LedSemaforoRosso, LOW);
272.     digitalWrite(LedSemaforoGiallo, HIGH);
273.     digitalWrite(LedSemaforoVerde, LOW);
274.     ledWrosso.off();
275.     ledWgiallo.on();
276.     ledWverde.off();
277.     }
278.     
279.   if(contaSecondiPresenzaRilevata > 14400 && luceVerdeAttivata == false) //dopo 4 ore diventa verde
280.     {
281.   contaSecondiPresenzaRilevata = 15000;
282.   luceRossaAttivata = false;
283.   luceGiallaAttivata = false;
284.   luceVerdeAttivata = true;
285.     AzzeraSemaforo();
286.     
287.     }
288. }

@AlexT: Would be good to see the full program (at least the full void loop()). Or is it classified top secret? :wink:

Hi IBK, off sure not! :smile: , I just updated the code. Thanks.

Whoa… not sure where to begin… lets start here…

Blynk requires a different way of utilizing loops and functions (actually so would any other really time and communication sensitive system)… so called “normal” Arduino style programming and heavy use of the “go as fast as possible” in the void loop() except when “screeching to a halt” with delay() is basically a Big Fat NO NO in the IoT world.

1 Like

Thanks Gunner, I will try to fix it, Probably I sould put all the code in a function and call it every second.
I will let you know if it works.
Thanks again.
P.S.: “Whoa… not sure where to begin… lets start here…” :joy: I will do a step at time :joy:

1 Like

here I am :slight_smile:, I moved all void code in a function as suggested but I noticed 2 stuff:

  1. NumDataRx never goes over 0 (I try to send udp with an app and I can see the ip with another app (based on mac just assigned))
  2. I have to change my mac address every time I write arduino or it doesn’t work.

Code:

void loop()
{
	Serial.println("Loop started");
	Blynk.run();
	Serial.println("Blynk ok");
	timer.run();
	Serial.println("Loop ended");
}  

void TimerUnSecondo()
{
Serial.println("-- 1 sec. --------------------");
	int NumDataRx = Udp.parsePacket();
	if (NumDataRx > 0)
	{
		Serial.print("Numero di byte da leggere: ");
		Serial.println(NumDataRx);
		Udp.read(DatiRX, UDP_TX_PACKET_MAX_SIZE);
		Serial.print(DatiRX);
	} 

and monitor show:

Blynk ok
Loop ended
Loop started
Blynk ok
Loop ended
Loop started
Blynk ok
Loop ended
Loop started
Blynk ok
Loop ended
Loop started
Blynk ok
Loop ended
Loop started
Blynk ok
Loop ended
Loop started
Blynk ok
Loop ended
Loop started
Blynk ok
-- 1 sec. --------------------