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);
}