I made an udp sender and receiving. It al works, only while installing it. I saw that when i connect power supply to power, the Arduino with the shield doesn't boot properly. And doesn't start sending or receiving udp packet. When the power supply is connected and i plug it in the arduino or press the reset button everything works fine. Any thoughts how i can fix this?
i'm using an arduino uno rev 3
W5100 ethernet shield
Maybe a solution is giving an reset command. I implemented this in my code, only didn't found a valid way of when using the reset command. Any thoughts of that?
codo:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SoftwareSerial.h>
// netwerk instellingen
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress exip(10,0,0,89); // externe ipadress
IPAddress myip(10,0,0,90);
IPAddress gatewayIP();
unsigned int localPort = 8888; // local port to listen on
// udp configuratie
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
int packetSize; // Packet grootte
int packetdata; // Waarde
const int tringPin =9; // Bel, aansuitpin
int tringState = 0;
byte tringloop = 0;
// timers gegevens
unsigned long previoustimetring = 0;
unsigned long previoustimecycles = 0;
const long intervaltring = 150;
const long intervalcycles = 3000;
unsigned long currenttimecycles;
unsigned long currenttimetring;
//Reset function
void(* resetFunc) (void) = 0;
void setup() {
Serial.begin(9600);
pinMode(tringPin,OUTPUT);
// internet opstarten
Ethernet.begin(mac, myip);
Udp.begin(localPort);
//hardwarestatus check
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found.");
}
else if (Ethernet.hardwareStatus() == EthernetW5100) {
Serial.println("W5100 Ethernet controller detected.");
}
else if (Ethernet.hardwareStatus() == EthernetW5200) {
Serial.println("W5200 Ethernet controller detected.");
}
else if (Ethernet.hardwareStatus() == EthernetW5500) {
Serial.println("W5500 Ethernet controller detected.");
}
}
void loop() {
Serial.println(Ethernet.gatewayIP());
// timers starten
unsigned long currenttimetring = millis();
unsigned long currenttimecycles = millis();
// if there's data available, read a packet
packetSize = Udp.parsePacket();
if(packetSize) {
if (Udp.remoteIP() == exip)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("Remote IP: "); // bericht vanuit welk ip
Serial.println(Udp.remoteIP());
Serial.print("remote port is "); // bericht vanuit welke poort
Serial.println(Udp.remotePort());
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); //read the packet into packetBuffer
packetdata= atoi(packetBuffer); // van char (packetBuffer) een int (packetdata) maken
Serial.print("Packetbuffer: "); // inhoud packetBuffer
Serial.println(packetBuffer);
Serial.print("packetdata: "); // inhoud packetdata
Serial.println(packetdata);
Serial.print("tringloop: ");
Serial.println(tringloop);
}
}
if (packetdata == 1) {
(tringloop = 1);
previoustimecycles = currenttimecycles;
}
else if (currenttimecycles - previoustimecycles >= intervalcycles)
{ (tringloop = 0);
}
if (tringloop == 1) {
if (currenttimetring - previoustimetring >= intervaltring) {
previoustimetring = currenttimetring;
if (tringState == 0)
{ (tringState = 1); }
else
{ (tringState = 0); }
}
}
if (tringloop == 0)
{ (tringState = 0); }
digitalWrite(tringPin, tringState);
memset(packetBuffer, 0 ,UDP_TX_PACKET_MAX_SIZE);
}