0

I'm setting up an Elegoo Uno R3 with a Seeed 5200 ethernet shield to monitor water flow and temperature. It needs to run unattended for weeks at time (being called several times a day by a remote script).

I'm normally able to send and receive UDP packets for a few minutes when I've just reset/power-cycled the Arduino. After that, it stops responding to UDP messages. By adding some serial output I can see that it hangs unpredictably at different parts of loop(). Sometimes it seems that physically shifting the device will get it to wake up; or it will wake up on its own after a minute or two. Most of the time it works until it quits, then doesn't work until I reset it.

It does respond to ping while it's otherwise unresponsive.

Is there anything wrong with my code? This is my first Arduino project. Otherwise could it be a hardware problem? If I buy another, I'll buy a genuine Arduino. Is Seeed a reliable source for the ethernet shield?

(By the way--I think it's safe to disregard the PACKETLEN thing, which is a leftover artifact. In the end it's unnecessary to read the incoming message anyway, so the packetBuffer array is superfluous but doesn't seem to change anything one way or another.)

Thanks in advance. Best wishes, Dan

#include <SPI.h>
#include <EthernetV2_0.h>

#define PACKETLEN 48


//Flow and temp globals
byte sensorInterrupt = 0;  // 0 = digital pin 2
byte flowPin       = 2;
byte tempPin = 0;

float calibrationFactor = 45; // we're going to measure for 10s intervals
float minFlow = 270;          // minimum 6 L/min, measured for 10 s
volatile byte pulseCount;  
unsigned long startTime, timeDelta;

int tempReading;
float tempVoltage;
float temperature;

//Communication globals
IPAddress ip(xxx, xxx, xxx, xxx);
byte mac[] = {  
  0xxx, 0xxx, 0xxx, 0xxx, 0xxx, 0xxx
};
EthernetUDP Udp;

unsigned int localPort = xxxx; 
char packetBuffer[PACKETLEN+1];  //buffer to hold incoming packet,

int packetSize;
byte flowOk, tempOk;
IPAddress remoteIP;
unsigned int remotePort;


void pulseCounter()
{
  pulseCount++;
}


void setup()
{

  //Serial.begin(9600);
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);


  pinMode(flowPin, INPUT);
  digitalWrite(flowPin, HIGH);

  // attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop()
{

    delay(2000);

    if(packetSize = Udp.parsePacket()) 
    {
        remoteIP = Udp.remoteIP();
        remotePort = Udp.remotePort();

        //Measure the flow for 10 seconds
        flowOk = 0;
        pulseCount = 0;
        startTime = millis();
        attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
        while((timeDelta = millis() - startTime) <= 10000) ;
        detachInterrupt(sensorInterrupt);
        if (pulseCount >= minFlow)
            flowOk = 1;

        //Check the temperature
        tempOk = 0;
        tempReading = analogRead(tempPin);
        tempVoltage = tempReading * 5.0 / 1024.0;
        temperature = (tempVoltage - 0.5) * 100;
        if (temperature < 35.0) 
            tempOk = 1;


        Udp.beginPacket(remoteIP, remotePort);
        if (flowOk && tempOk)
            Udp.write("Ok");
        else
            Udp.write("Not ok");
        Udp.endPacket();
    }

}
  • 1
    If its doing it at random places its most likely to be a memory issue, maybe a buffer overrun? – Code Gorilla Apr 12 '18 at 10:13
  • Thanks for the comment. I assume you're right about a memory issue in one of the libraries. I will post the fix below. – Daniel Paley Apr 12 '18 at 16:29
1

I don't know where my compiler was finding the UDP code in the above sketch, because it's not in EthernetV2_0.h. The problem was fixed by adding the following lines, from the example sketch UDPSendReceiveString written by Michael Margolis and included with the Ethernet_Shield_W5200 library:

#include <EthernetUdpV2_0.h>

#define SDCARD_CS 4

void setup():
    //...//
    pinMode(SDCARD_CS, OUTPUT);
    digitalWrite(SDCARD_CS, HIGH);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.