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