UDP packet as input for a cat feeder

As with many here, I am new to this and although I'm flying through the hardware parts of my build I am really stuck on my programming part.

I am trying to recreate an internet controlled cat feeder I found here: http://www.klomp.de/index.php/catfeeder?id=7

I am using the code and PHP page that klomp.de provided and I have been successful in controlling my relay with a push button, but I am having trouble with the online part.

I have a website that is hosting my PHP page (see code below for PHP and arduino code) but I am having trouble getting the PHP form to send a udp to the ip address of my arduino.

Arduino code:
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>

/*
Arduino Catfeeder

This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender. Also it plays a small
melody (Pavlovian cats, Classical conditioning - Wikipedia)
and triggers one relay for the motor of the catfeeder.
It also polls a switch for manual feeding.

from Jan Klomp, 18 May 2012

The original script "UDPSendReceive.pde" was created from by Michael Margolis
on 21 Aug 2010

This code is in the public domain.
*/

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

#include "pitches.h"

// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
MAC ADDRESS }; // made up MaAC Address
IPAddress ip( xxx,xxx,x,xx); // made up IP addreess
IPAddress gateway( xx, xx, xxx, xxx); // Router IP Address
IPAddress subnet( 255, 255, 255, 0);

int Relay = 4; // Relay on port 0
int Taster = 6; // Switch on port6

unsigned int localPort = 8081; // local port to listen on

// 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

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

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

pinMode(Relay, OUTPUT); // set Pin0 as output
digitalWrite(Relay, HIGH); // Relay off
pinMode(Taster, INPUT); // set Pin6 as input

Serial.begin(9600);
}

void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("Text: ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote*, DEC);*

  • if (i < 3)*

  • {*

  • Serial.print(".");*

  • }*

  • }*

  • Serial.print(", port ");*

  • Serial.println(Udp.remotePort());*

  • // read the packet into packetBufffer*

  • Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);*

  • Serial.println("Contents:");*

  • Serial.println(packetBuffer);*

  • // send a reply, to the IP address and port that sent us the packet we received*

  • Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());*

  • Udp.write(ReplyBuffer);*

  • Udp.endPacket();*

  • for(int i=0; i<UDP_TX_PACKET_MAX_SIZE; i++)*

  • {*
    _ packetBuffer = '\0';_
    * }*
    * // feed the beasts*
    * fuettern(); *
    * }*
    * // poll the switch (pressed at least 3 seconds)*
    * delay(10);*
    * int buttonState = digitalRead(Taster);*
    * if (buttonState)*
    * {*
    * delay(3000);*
    * int buttonState = digitalRead(Taster);*
    * if (buttonState)*
    * {*
    * // feed the beasts*
    * fuettern();*
    * }*
    * } *
    }
    void fuettern(void) {
    * // iterate over the notes of the melody:*
    * for (int thisNote = 0; thisNote < 8; thisNote++) {*
    * // to calculate the note duration, take one second*
    * // divided by the note type.*
    * //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.*
    * int noteDuration = 1000/noteDurations[thisNote];*
    * tone(5, melody[thisNote],noteDuration);*
    * // to distinguish the notes, set a minimum time between them.*
    * // the note's duration + 30% seems to work well:*
    _ int pauseBetweenNotes = noteDuration * 1.30;_
    * delay(pauseBetweenNotes);*
    * // stop the tone playing:*
    * noTone(5);*
    * } *

* // set Relay on*
* digitalWrite(Relay, LOW); // Relay on*
* Serial.println("Relais an");*
* delay(3000);*
* digitalWrite(Relay, HIGH); // Relay off*
* Serial.println("Relais aus");*
}
PHP code:
<?php
if ( empty ($_GET['feed']) == TRUE )
{
* echo '*

Jack and Leos catfeeder
* *

*

Text:*
* *
*

*

* *
*

*
* *
* ';*
}
else
{
* $info = substr($GET['feed'], 0, 20);
_
$socket = fsockopen("udp://xx.xx.xxx.xxx" [#router ip address], 8081 [#port to listen on], $errno, $errstr, 30);*

* fwrite($socket, $info);*
* echo"*
Jack and Leos catfeeder
";
* echo "*
remote adress: ";
* echo $SERVER['REMOTE_ADDR'];
_
echo "*

message: ";
* echo $info;*
* echo "*
Thanks for feeding!
";
}
?>
I have tried learning some of the code examples, but even though I am a doctor, I find the code a little above my head right now without learning from scratch. I am not planning on doing a lot of coding (at this time, maybe after residency) so I don't have time to learn all the code myself. Any help would be greatly appreciated!!

I have a website that is hosting my PHP page

Nonsense. You might have a web SERVER that is hosting your PHP SCRIPTS.

find the code a little above my head right now

Especially when it is not posted correctly.

The PHP script serves up a page if it is invoked by a browser or other kind of client. The page, when rendered, contains a submit button. How are you invoking the script?

When the submit button is pressed, the script opens a socket connection to an IP address on some port. You need to make changes to the script to put YOUR IP address and port. It is not clear that you have done that (in fact it is clear that you haven't done that).

PaulS:
Nonsense. You might have a web SERVER that is hosting your PHP SCRIPTS.

Yes, I have a web server that is hosting my PHP scripts.

PaulS:
You need to make changes to the script to put YOUR IP address and port. It is not clear that you have done that (in fact it is clear that you haven't done that).

I have done that. I'm just still new to web security too, and I didn't want to post my ip address on the forum (I'm sure it doesn't really matter if I do or not, but I'm obviously a little new to this).

Right now I have a PHP script that I'm under the impression is SUPPOSED to be sending a UDP packet, and an arduino script that is SUPPOSED to be receiving it. Like I said before, the arduino script works with the push-button input, but when I press submit on the PHP form, nothing seems to happen.

The 2 questions I guess I have are:

  1. Does my PHP script look like it SHOULD be sending a UDP packet to my router and port (assuming I put my actual router number and desired port number instead of x's).

  2. Does my arduino script look like it SHOULD be receiving that packet (assuming i'm telling it to listen to the right port) and therefore running the script fuettern, thus feeding my cats.

If the scripts are correct, then I'm sure its something to do with my firewall, or hosting server, or something and Ill figure that out later.

Right now I have a PHP script that I'm under the impression is SUPPOSED to be sending a UDP packet, and an arduino script that is SUPPOSED to be receiving it.

  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("Text: ");

If the Arduino gets a packet, you print the size. Do you see that data being printed? If not, your assumption that the Arduino is supposed to receive a packet is wrong.

If you do, you print the IP address, labeled as Text: . Why?