Static IP for UDP and Blynk

Hi,

I have recently added blynk to a program that sent data via udp to a PC. I am having issues now I have added blynk with the static IP (the pc only listens for this IP address) Blynk is working fine but not assigning IP to .151. If I add the IP/subnet/ddns into Blynk.begin(auth); I disconnect from the server every couple of seconds. Where am I going wrong?


#include <avr/wdt.h>  // WatchDog Timer
#include "EmonLib.h"  // Include Emon Library
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include <BlynkSimpleEthernet.h>

#define BLYNK_PRINT Serial

char auth [] = "x";

SimpleTimer timer;

EnergyMonitor emon1;     // Create an instance
EnergyMonitor emon2;    

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF};
IPAddress ip ( 192, 168, 1, 151);


unsigned int localPort = 8888;      // local port to listen on
unsigned int remotePort = 2007;

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back


EthernetUDP Udp;

void setup()
{
  wdt_disable(); // Disable watchdog to allow setup to complete

  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  Blynk.begin(auth);
  
  emon1.current(1, 111.1);             // Current: input pin, calibration.
  emon2.current(2, 111.1);             // Current: input pin, calibration.

  Blynk.syncVirtual(V1); //This gets the virtual pin value from server. (Needed for example after power loss)
  Blynk.syncVirtual(V2);
  
  timer.setInterval(1000, SendData); // Miliseconds to seconds, every second run function
  wdt_enable(WDTO_8S); //set watchdog to 8secs (max time)

  // enable the watchdog timer. There are a finite number of timeouts allowed (see wdt.h).
  // Notes I have seen say it is unwise to go below 250ms as you may get the WDT stuck in a
  // loop rebooting.
  // The timeouts I'm most likely to use are:
  // WDTO_1S
  // WDTO_2S
  // WDTO_4S
  // WDTO_8S
}

void loop()
{
  Blynk.run();
  timer.run();
  wdt_reset(); // reset watchdog timer each loop - if program hangs watchdog will time out and reset board
}

void SendData()
{
 
  // read the packet into packetBufffer
  Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 

  double phase1 = emon1.calcIrms(1480);  // Calculate Irms only (Mains)
  double phase2 = emon2.calcIrms(1480);  // Calculate Irms only (Solar)
  phase1 = phase1/4.2; //further calibration
  phase2 = phase2/4.2;
  phase2 = phase2/10;
  
  int packetSize = Udp.parsePacket();

  Udp.beginPacket("192.168.1.146", remotePort); //Initialize packet send
  Udp.print(phase1, 1); //to 1 decimal place
  Udp.print(":"); // Token for c#/PC to split Udp output
  Udp.print(phase2, 2);
  Udp.endPacket();


  Blynk.virtualWrite(V1, phase1);
  Blynk.virtualWrite(V2, phase2);
  
 }
  


Did you post the wrong version of your sketch?

Also, I assume that “ddns” is a typo?

Pete.

Hi,

Sorry yes, ddns was a typo. The code I have linked above, blynk is working on but not being assigned IP ending in 151 so no udp data being sent.
If I change to the below, Blynk then crashes and I still dont get IP .151

byte arduino_mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
IPAddress arduino_ip ( 192,   168,   1,  151);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,   168,   1,   254);
IPAddress subnet_mask(255, 255, 255,   0);

Blynk.begin(auth, "blynk-cloud.com", 80, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
//Ethernet.begin(mac, ip);

Do you have any other devices on your network where you are specifying the MAC address?

The MAC address needs to be unique, and your router wont be happy if it sees another device with this MAC:

Pete.

Nope and I tried changing the mac address. All blynk taken out this is working:

#include <avr/wdt.h>  // WatchDog Timer
#include "EmonLib.h"  // Include Emon Library
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include <BlynkSimpleEthernet.h>

#define BLYNK_PRINT Serial

char auth [] = "x";

SimpleTimer timer;

EnergyMonitor emon1;     // Create an instance
EnergyMonitor emon2;    

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF};
IPAddress ip ( 192, 168, 1, 151);


unsigned int localPort = 8888;      // local port to listen on
unsigned int remotePort = 2007;

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back


EthernetUDP Udp;

void setup()
{
  wdt_disable(); // Disable watchdog to allow setup to complete

  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  //Blynk.begin(auth);
  
  emon1.current(1, 111.1);             // Current: input pin, calibration.
  emon2.current(2, 111.1);             // Current: input pin, calibration.

 // Blynk.syncVirtual(V1); //This gets the virtual pin value from server. (Needed for example after power loss)
 // Blynk.syncVirtual(V2);
  
  timer.setInterval(1000, SendData); // Miliseconds to seconds, every second run function
  wdt_enable(WDTO_8S); //set watchdog to 8secs (max time)

  // enable the watchdog timer. There are a finite number of timeouts allowed (see wdt.h).
  // Notes I have seen say it is unwise to go below 250ms as you may get the WDT stuck in a
  // loop rebooting.
  // The timeouts I'm most likely to use are:
  // WDTO_1S
  // WDTO_2S
  // WDTO_4S
  // WDTO_8S
}

void loop()
{
  //Blynk.run();
  timer.run();
  wdt_reset(); // reset watchdog timer each loop - if program hangs watchdog will time out and reset board
}

void SendData()
{
 
  // read the packet into packetBufffer
  Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 

  double phase1 = emon1.calcIrms(1480);  // Calculate Irms only (Mains)
  double phase2 = emon2.calcIrms(1480);  // Calculate Irms only (Solar)
  phase1 = phase1/4.2; //further calibration
  phase2 = phase2/4.2;
  phase2 = phase2/10;
  
  int packetSize = Udp.parsePacket();

  Udp.beginPacket("192.168.1.146", remotePort); //Initialize packet send
  Udp.print(phase1, 1); //to 1 decimal place
  Udp.print(":"); // Token for c#/PC to split Udp output
  Udp.print(phase2, 2);
  Udp.endPacket();

  Udp.beginPacket("192.168.1.145", remotePort); //Initialize packet send
  Udp.print(phase1, 1); //to 1 decimal place
  Udp.print(":"); // Token for c#/PC to split Udp output
  Udp.print(phase2, 1);
  Udp.endPacket();

 // Blynk.virtualWrite(V1, phase1);
 // Blynk.virtualWrite(V2, phase2);
  
 }
  

So both versions are working, just not together

So what happens if you assign a static IP in your router and use your DHCP version of the code?

Pete.

No option to do that sadly.

Should the first paste of code work then?

So can you use a DHCP assigned IP address and edit the script on the PC to match that, to allow you to prove that the Blynk and UDP connections can work together?

Pete.

Yes, that works

Is your router allowing an IP address of 151 ?

Have you tried a lower number?

Pete.

Yes .151 works and the script works using .151 when not adding blynk. Once I try to incorporate blynk I get issues

Can you post your full code, with static IP, Blynk etc that you are using, but which isn’t working.

Also, the serial monitor output that this code generates.

Pete.

command prompt:
ping 192.168.1.151
Reply from 192.168.1.146: Destination host unreachable.

#include <avr/wdt.h>  // WatchDog Timer
#include "EmonLib.h"  // Include Emon Library
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include <BlynkSimpleEthernet.h>

#define BLYNK_PRINT Serial

char auth [] = "x";

SimpleTimer timer;

EnergyMonitor emon1;     // Create an instance
EnergyMonitor emon2;    

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xCE, 0xED};
IPAddress ip ( 192, 168, 1, 151);


unsigned int localPort = 8888;      // local port to listen on
unsigned int remotePort = 2007;

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back


EthernetUDP Udp;

void setup()
{
  wdt_disable(); // Disable watchdog to allow setup to complete

  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  Blynk.begin(auth);
  
  emon1.current(1, 111.1);             // Current: input pin, calibration.
  emon2.current(2, 111.1);             // Current: input pin, calibration.

  Blynk.syncVirtual(V1); //This gets the virtual pin value from server. (Needed for example after power loss)
  Blynk.syncVirtual(V2);
  
  timer.setInterval(1000, SendData); // Miliseconds to seconds, every second run function
  wdt_enable(WDTO_8S); //set watchdog to 8secs (max time)

  // enable the watchdog timer. There are a finite number of timeouts allowed (see wdt.h).
  // Notes I have seen say it is unwise to go below 250ms as you may get the WDT stuck in a
  // loop rebooting.
  // The timeouts I'm most likely to use are:
  // WDTO_1S
  // WDTO_2S
  // WDTO_4S
  // WDTO_8S
}

void loop()
{
  Blynk.run();
  timer.run();
  wdt_reset(); // reset watchdog timer each loop - if program hangs watchdog will time out and reset board
}

void SendData()
{
 
  // read the packet into packetBufffer
  Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 

  double phase1 = emon1.calcIrms(1480);  // Calculate Irms only (Mains)
  double phase2 = emon2.calcIrms(1480);  // Calculate Irms only (Solar)
  phase1 = phase1/4.2; //further calibration
  phase2 = phase2/4.2;
  phase2 = phase2/10;
  
  int packetSize = Udp.parsePacket();

  Udp.beginPacket("192.168.1.146", remotePort); //Initialize packet send
  Udp.print(phase1, 1); //to 1 decimal place
  Udp.print(":"); // Token for c#/PC to split Udp output
  Udp.print(phase2, 2);
  Udp.endPacket();

  Udp.beginPacket("10.20.10.14", remotePort); //Initialize packet send
  Udp.print(phase1, 1); //to 1 decimal place
  Udp.print(":"); // Token for c#/PC to split Udp output
  Udp.print(phase2, 1);
  Udp.endPacket();

  Blynk.virtualWrite(V1, phase1);
  Blynk.virtualWrite(V2, phase2);
  
 }
  

Interestingly I get no serial data, but blynk is working, but unknown what IP address it has so udp data not working.

This is the same code as my first post just with the blynk lines now not commented out. If I comment them back out static IP works, obviously blynk doesn’t

So, just to clarify once more, if you use Blynk.begin(auth) and allow the router to assign a DHCP IP address, and edit your script on the PC to listen to the DHCP assigned IP address, both Blynk and your UDP comms are working perfectly?

In this scenario, do you see anything in your serial monitor?

How does this work, with an IP address outside the range used by your local network?

No, it’s not. Your first sketch didn’t have the 10.20.10.14 lines of code in it.

You can find the IP address using tools like:

I assumed that you were doing this when I asked this question:

and you replied:

Pete.

Sorry yes, I did add another IP address Udp.beginPacket("10.20.10.14", remotePort); I was testing on another network with no luck.

If I remove this line:
Ethernet.begin(mac, ip);
Blynk will connect to servers and keep disconnecting almost straight away and no UDP connection

If I add this line back in Ethernet.begin(mac, ip); Blynk will connect and hold a connection but no UDP

With both examples I am unable to locate an IP address searching via the mac.

I am convinced earlier on it did work via DHCP but now doubting myself.

If I remove the Blynk code, it connects straight away to .151 and connects to UDP

I’m more confused now than I was before, and I think you are too.

I’d suggest that you look at the sketch builder examples for your board (presumably an Uno) with your Ethernet adapter and see how the code is structured, and how the Blynk connection is established.
Maybe try one of these examples to ensure that it works okay.

Then, I’d add your UDP code into that working sketch and see if that works, using a DHCP allocated IP, and making changes to your PC script.

The serial monitor output is extremely useful, so work on getting this functioning correctly.
Make notes on what you’ve done, and what you see in the serial monitor, and keep each version of your code.

If you still need help then report back with your findings.

Also, as you’re using Blynk :Legacy, which will be retired at some point, you may want to think about migrating to Blynk IoT.
Normally, I’d suggest that you do that after you’ve resolved this issue, but as Blynk IoT has a web-based desktop which can be viewed via a PC, maybe this will remove the need for your UDP interface?
(I don’t know, because you’ve nor explained the functionality that this gives).

Pete.

#include <avr/wdt.h>  // WatchDog Timer
#include "EmonLib.h"  // Include Emon Library
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include <BlynkSimpleEthernet.h>

#define BLYNK_PRINT Serial

char auth [] = "pGOi_3KmcGgi9SHQanJDMaPI3JdveAE1";

SimpleTimer timer;

EnergyMonitor emon1;     // Create an instance
EnergyMonitor emon2;    

byte mac[] = {0xBE, 0xFC, 0xCE, 0xEF, 0xCE, 0xED};
IPAddress arduino_ip ( 192, 168, 1, 154);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,   168,   1,   254);
IPAddress subnet_mask(255, 255, 255,   0);

unsigned int localPort = 8888;      // local port to listen on
unsigned int remotePort = 2007;

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back


EthernetUDP Udp;

void setup()
{

  Udp.begin(localPort);
  Serial.begin(9600);
  Blynk.begin(auth, "blynk-cloud.com", 80, arduino_ip, dns_ip, gateway_ip, subnet_mask, mac);

  Blynk.syncVirtual(V1); //This gets the virtual pin value from server. (Needed for example after power loss)
  Blynk.syncVirtual(V2);

  emon1.current(1, 111.1);             // Current: input pin, calibration.
  emon2.current(2, 111.1);             // Current: input pin, calibration.
  
  timer.setInterval(1000, SendData);
}

void loop()
{
  Blynk.run();
  timer.run();

}

void SendData()
{

  double phase1 = emon1.calcIrms(1480);  // Calculate Irms only (Mains)
  double phase2 = emon2.calcIrms(1480);  // Calculate Irms only (Solar)
  phase1 = phase1/4.2; //further calibration
  phase2 = phase2/4.2;
  phase2 = phase2/10;
  
  Blynk.virtualWrite(V1, phase1);
  Blynk.virtualWrite(V2, phase2);  
 }

Above code is working with static IP using blynk to set IP

If I try to add in any udp blynk crashes and I never recieve data

  int packetSize = Udp.parsePacket();

  Udp.beginPacket("192.168.1.146", remotePort); //Initialize packet send
  Udp.print(phase1, 1); //to 1 decimal place
  Udp.print(":"); // Token for c#/PC to split Udp output
  Udp.print(phase2, 2);
  Udp.endPacket();

Is Blynk just not compatible then?

I don’t know, I’ve never tried to do what you’re doing.

Pete.

Thanks for your help Pete

@dmitriy Do you think this should work?