Go Down

Topic: Help needed, on Arduino to Arduino over ethernet UDP (Read 2104 times) previous topic - next topic

Monstro30

May 31, 2016, 05:00 pm Last Edit: May 31, 2016, 07:51 pm by Monstro30
Hey whats up all.
Hope that its all good. :)

I need help with a project.

I want to help a farmer get water to his dam

So when the dam is empty the Arduino main unit should receive a input high for empty and low for full and forward the commands on to the second unit.

The units are connected over Ethernet to wireless antennas

The second unit receives the command switch on or off, but with this unit for safety reasons,so that the motor don't burn out, when switched on and connection was lost or main unit lost power, to switch off automatically after 1 min or if before the 1 min is over the command off is received to switch off

I do have a receiving code without the timer

Code: [Select]

#include <SPI.h>                                             // needed for Arduino versions later than 0018
#include <Ethernet.h>                                        // Ethernet board connected
#include <EthernetUdp.h>                                     // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };         // MAC Address
IPAddress ip(192, 168, 0, 10);                               // IP Address
unsigned int localPort = 8888;                               // Local IP port to listen on
byte gateway[] = { 192, 168, 0, 1 };                         // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                        // Subnet Address

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];                   // buffer to hold incoming packet
EthernetUDP Udp;                                             // An EthernetUDP instance to let us send and receive packets over UDP

int Relay = A0;                                              // Relay Pin A0
                                               
                                                             

void setup() {                                               // run setup only once on boot

  Ethernet.begin(mac,ip);                                    // Enable Ethernet
  Udp.begin(localPort);                                      // Start UDP Connection

  pinMode(Relay, OUTPUT);                                    // Pin Mode



  Serial.begin(9600);                                        // Start Serial Output
  Serial.write("SYSTEM BOOT");                               // Send Power on to serial

  digitalWrite(Relay, LOW);                                  // tell the pin to turn off
                                                             
             }                                               // end of setup

void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if(packetSize)
  {
  int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);  // read the packet into packetBufffer
    packetBuffer[read] = 0;
    char ch = packetBuffer[0];

switch(ch)
{
case '0':                                                     // if 0 is sent via udp then do this -->>
  digitalWrite(Relay, LOW);                                   // Turn Relay Off
break;                                                        // end of case 0
case '1':                                                     // if 1 is sent via udp then do this -->>
  digitalWrite(Relay, HIGH);                                  // Turn Relay On
break;                                                        // end of case 1
default :                                                     // if recived info via udp and not found in the case do this -->>
    Serial.print("No Case Found for: ");                      // if case not found send info via serial
    Serial.print(ch);                                         // send udp info back to serial
break;                                                        // end of case not found
}
}

  delay(15);                                                 // waits 15ms for udp input wait ...
}                                                            // end system run, but now loop and start system run again



PaulS

Some comments. PinA and PinB are lousy names. The comments say that they are switch pins. But, then they are declared as OUTPUT. Last time I connected a switch it was an input device. I can't imagine how you are going to write to a switch.

Make the names reflect what is ACTUALLY connected to the pin.

The packet currently contains '0', '1', '2', '3', or some other value that means nothing.

If the packet contains '0' or '1', you diddle with the pin defined in PinA. If it is '2' or '3', you diddle with the pin defined in PinB. What you need to do is, when you turn the pin on, record when that happened, using millis().

You also need to add code to loop() that will, independent of getting data in a packet, determine if the pin defined in PinA is on (by reading it OR by remembering what you wrote to it) and has been on too long (now minus then exceeds the time needed to pump the tank dry). If it is on and has been on too long, turn it off.
The art of getting good answers lies in asking good questions.

Monstro30

Hey thank you PaulS for the input, sure it will help a lot.

Okay I hope I renamed everything accordingly LoL

The Code was pulled from a different post, I only tested that it works with KMP UDP tester and worked so I'am in need of the sending side, I'am really not good with writing code and really would appreciate the help, like a lazy basted  but I will install the units for this farmer for free as a helping hand.

I walked up to this farmer and asked how could we as town people help as its really dry and farmers get no help these days and he said he needs something to help him pump water automatically to his dam.

I looked into this and the solution over such a far distance is Arduinos and looked fairly simple to use and once I started I realized that I was not skilled or trained to do this.

I'am willing to pay for this code as long as it is reasonable I'am not rich but its for a good course.


Thanks for all the help so far  :)

PaulS

Quote
Okay I hope I renamed everything accordingly LoL
That is better, except that you overwrote the code in the previous post. That is frowned upon here. It makes posters look like idiots, commenting on code that looks perfectly OK by later readers.

Always post new code in a new reply.

You now turn the relay on or off in response to a UDP message. But, the whole idea behind UDP is that the sender sends them and doesn't care if anyone is listening, or not. So, you should not rely on getting a "Hey, turn the pump off, the well is dry" message.

You need to record when you turn the pump on. Are you sure that LOW turns the relay off? Most relay shields use LOW for on and HIGH for off.

Code: [Select]
unsigned long pumpOn = 0;
unsigned long pumpTime = 15000; // Run pump for 15 seconds

void loop()
{
    // Get the packet
    switch(ch)
    {
       case '0':                                                     // if 0 is sent via udp then do this -->>
         digitalWrite(Relay, LOW);                                   // Turn Relay Off
         break;
      case '1':
         digitalWrite(Relay, HIGH);
         pumpOn = millis();
         break;
    }

    // Don't let the pump run forever
    unsigned long now = millis();
    if(pumpOn > 0)  // Is pump on?
    {
        if(now - pumpOn > pumpTime)
        {
           // Turn the pump off
           pumpOn = 0;
        }
    }
}
The art of getting good answers lies in asking good questions.

PaulS

It looks like your code came from the UDP receiver example. There is a UDP sender example that came with the UDP library that would be a good starting point for the sender side.
The art of getting good answers lies in asking good questions.

Monstro30

Haha sorry rookie mistake, I understand will do so from now on.


Yes low is the off for the relay when no power is given to the relay the connection is normally open when 5v is applied the connection is closed 

Monstro30

Okay I don't know if I placed the code in correctly.

It will switch on the relay but not switch off automatically when no on is being send.

Thank you Paul, The time you put in is appreciated ;D 


Code: [Select]

#include <SPI.h>                                             // needed for Arduino versions later than 0018
#include <Ethernet.h>                                        // Ethernet board connected
#include <EthernetUdp.h>                                     // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };         // MAC Address
IPAddress ip(192, 168, 0, 10);                               // IP Address
unsigned int localPort = 8888;                               // Local IP port to listen on
byte gateway[] = { 192, 168, 0, 1 };                         // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                        // Subnet Address

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];                   // buffer to hold incoming packet
EthernetUDP Udp;                                             // An EthernetUDP instance to let us send and receive packets over UDP

int Relay = A0;                                              // Relay Pin A0
                                               
unsigned long pumpOn = 0;
unsigned long pumpTime = 15000;                              // Run pump for 15 seconds                                                             

void setup() {                                               // run setup only once on boot

  Ethernet.begin(mac,ip);                                    // Enable Ethernet
  Udp.begin(localPort);                                      // Start UDP Connection

  pinMode(Relay, OUTPUT);                                    // Pin Mode



  Serial.begin(9600);                                        // Start Serial Output
  Serial.write("SYSTEM BOOT");                               // Send Power on to serial

  digitalWrite(Relay, LOW);                                  // tell the pin to turn off
                                                             
             }   
             


void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if(packetSize)
  {
  int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);  // read the packet into packetBufffer
    packetBuffer[read] = 0;
    char ch = packetBuffer[0];

{
    // Get the packet
    switch(ch)
    {
       case '0':                                              // if 0 is sent via udp then do this -->>
         digitalWrite(Relay, LOW);                            // Turn Relay Off
         break;
      case '1':
         digitalWrite(Relay, HIGH);
         pumpOn = millis();
         break;
    }

    // Don't let the pump run forever
    unsigned long now = millis();
    if(pumpOn > 0)  // Is pump on?
    {
    if(now - pumpOn > pumpTime)
    {
    // Turn the pump off
    pumpOn = 0;
    }
    }

}
}

PaulS

Code: [Select]
    if(now - pumpOn > pumpTime)
    {
    // Turn the pump off
    pumpOn = 0;
    }

I'm nearly certain that your pump is not reading the comments. You need to put code there to actually turn the pump off.
The art of getting good answers lies in asking good questions.

Monstro30

Sorry I think I'am missing something, the code works the only problem is the automatically off not working.

The code is inserted or is the placement wrong?

Code: [Select]
   
if(now - pumpOn > pumpTime)
    {
    // Turn the pump off
    pumpOn = 0;
    }

PaulS

Quote
The code is inserted or is the placement wrong?
It is in the right place. But, I expected you to add the code, after the comment, to actually turn the pump off.
The art of getting good answers lies in asking good questions.

Monstro30

Okay I applied the off code, But it is still not switching off .

Code: [Select]

void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if(packetSize)
  {
  int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);  // read the packet into packetBufffer
    packetBuffer[read] = 0;
    char ch = packetBuffer[0];

{
    // Get the packet
    switch(ch)
    {
       case '0':                                              // if 0 is sent via udp then do this -->>
         digitalWrite(Relay, LOW);                            // Turn Relay Off
         break;
      case '1':
         digitalWrite(Relay, HIGH);
         pumpOn = millis();
         break;
    }

    // Don't let the pump run forever
    unsigned long now = millis();
    if(pumpOn > 0)  // Is pump on?
    {
    if(now - pumpOn > pumpTime)
    {
    digitalWrite(Relay, LOW); 
    pumpOn = 0;
    }
    }

}
}

PaulS

Quote
Okay I applied the off code
And f**ked up the indenting again. Is it SO difficult to use Tools + Auto Format?

Code: [Select]
    char ch = packetBuffer[0];

{

Is it SO difficult to not use useless curly braces?

The code for not letting the pump run too long does NOT belong inside the if there is UDP data block. It belongs at the end of loop() outside of any blocks.
The art of getting good answers lies in asking good questions.

Monstro30

Thank you works perfectly,

I'am going to look into the sensor side :smiley-confuse: 

Monstro30

#13
Jun 03, 2016, 09:19 pm Last Edit: Jun 04, 2016, 09:48 am by Monstro30
Proud to reply and say the hole setup is working.


and going to share the code with all, to use for future projects.

I do not take credit and many thanks to Paul for all the help, LoL trough the hard times.

I'am sure this project is going to grow fast to help farmers in South Africa.

here is the input high or low (ON or OFF) sending side code.

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>   // UDP library from: bjoern@cs.stanford.edu
#define Button A0

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0xA1, 0xAE};
IPAddress ip(192, 168, 0, 11);
unsigned int localPort = 7788;      // local port to listen on

IPAddress RemIP(192, 168, 0, 10);
unsigned int RemPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,



// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;


void setup()
{
  // Button
  pinMode(Button, INPUT);
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

}

void loop() {

  if (digitalRead(Button) == LOW)
  {
    Udp.beginPacket(RemIP, RemPort);
    Udp.write("0");
    Udp.endPacket();
  }
  if (digitalRead(Button) == HIGH)
  {
    Udp.beginPacket(RemIP, RemPort);
    Udp.write("1");
    Udp.endPacket();
  }
  delay(400);
}



Receiving side

Code: [Select]


#include <SPI.h>                                             // needed for Arduino versions later than 0018
#include <Ethernet.h>                                        // Ethernet board connected
#include <EthernetUdp.h>                                     // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };         // MAC Address
IPAddress ip(192, 168, 0, 10);                               // IP Address
unsigned int localPort = 8888;                               // Local IP port to listen on
byte gateway[] = { 192, 168, 0, 1 };                         // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                        // Subnet Address

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];                   // buffer to hold incoming packet
EthernetUDP Udp;                                             // An EthernetUDP instance to let us send and receive packets over UDP

int Relay = A0;                                              // Relay Pin A0

unsigned long pumpOn = 0;
unsigned long pumpTime = 15000;                              // Run pump for 15 seconds

void setup() {                                               // run setup only once on boot

  Ethernet.begin(mac, ip);                                   // Enable Ethernet
  Udp.begin(localPort);                                      // Start UDP Connection

  pinMode(Relay, OUTPUT);                                    // Pin Mode



  Serial.begin(9600);                                        // Start Serial Output
  Serial.write("SYSTEM BOOT");                               // Send Power on to serial

  digitalWrite(Relay, LOW);                                  // tell the pin to turn off

}



void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if (packetSize)
  {
    int read = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer
    packetBuffer[read] = 0;
    char ch = packetBuffer[0];


    // Get the packet
    switch (ch)
    {
      case '0':                                              // if 0 is sent via udp then do this -->>
        digitalWrite(Relay, LOW);                            // Turn Relay Off
        break;
      case '1':
        digitalWrite(Relay, HIGH);
        pumpOn = millis();
        break;
    }
  }

  // Don't let the pump run forever
  unsigned long now = millis();
  if (pumpOn > 0) // Is pump on?
  {
    if (now - pumpOn > pumpTime)
    {
      digitalWrite(Relay, LOW);
      pumpOn = 0;
    }
  }
}


Go Up