Hi
I have a arduino mega 2560 and Ethernet shield 5100. I want to send a message to a server eg http://luk2009.dyndns.org, in which there is a program that listens to a port and archive messages.
The problem I have is that not how do you send a dyndns address, because it only allows me to enter byte addresses.
I want to know how to change: DestinationIP IPAddress (192,168,69,122); by: DestinationIP IPAddress (luk2009.dyndns.biz);
I used this code:
#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
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 69, 200);
//IPAddress remip(10, 0, 0, 2);
unsigned int localPort = 5015; // 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;
int pinSensor[]= {7,8};
const int pinled = 3;
const int sirena = 4;
int n=0;
int sonando; // para estado del pin que monitorea la sirena de la alarma
char alarma[] ="30120001BA0"; //codigo a ser enviado, solo dejando espacio para la zona
void setup(){
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
// Udp.remoteIP=(remip);
// Udp.remotePort(rport);
Serial.begin(9600);
for (int i=0; i<2; i++){
pinMode(pinSensor[i],INPUT);
}
pinMode(pinled,OUTPUT);
pinMode(sirena,INPUT);
digitalWrite(pinled,LOW);
}
void loop(){
IPAddress destinationIP(192,168,69,122); // Address of target machine // I want to change that address for this address http://luk2009.dyndns.biz
unsigned int destinationPort = 5003; // puerto udp al que se envia el dato
sonando = digitalRead(sirena);
if (sonando==LOW){
for (n=0;n<2;n++){
if (digitalRead(pinSensor[n])==LOW) {
digitalWrite(pinled, HIGH);// este led es solo para pruebas y saber que el sistema esta detectando los cambios
Udp.beginPacket(destinationIP, destinationPort);
//Udp.write(alarma);
Udp.print(alarma);
Udp.print(pinSensor[n]); // esto seria el equivalente a la zona del sistema de alarmas
Udp.endPacket();
delay (30000); // para controlar que no este enviando señales durante 4 minutos continuos, la idea es que revise todos los pines antes de hacer la pausa
}
else{
digitalWrite(pinled, LOW); //led para pruebas solamente
}
}
}
}
Here is thd DhcpAddressPrinter example sketch modified to resolve dns requests. I used Google because neither of the domain names you listed resolve to an ip.
Basic client test code you can modify with your dyndns url to see if a connection can be made.
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
client.println("Host: web.comporium.net");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
This is what I get when using the zoomkat's sketch:
Better client test 9/22/12
Send an e in serial monitor to test
connected
HTTP/1.1 404 Not Found
Date: Sat, 01 Mar 2014 08:39:06 GMT
Server: Apache/2.2.8 (Win32) PHP/5.2.6
Content-Length: 311
Connection: close
Content-Type: text/html; charset=iso-8859-1
404 Not Found
Not Found
The requested URL /~shb/arduino.txt was not found on this server.
Apache/2.2.8 (Win32) PHP/5.2.6 Server at ~~luccasa~~.dyndns.biz Port 80
disconnecting.
but I really do not know how to use the sketch that I get to send a string over internet to my ip address.
luk2009:
thank you SurferTim, the trick was to use dnsClient.getHostByName
I like that best, but FYI you can use this:
Udp.beginPacket("www.google.com",80)
However, this resolves that domain name every call to beginPacket(), and requires significantly longer to perform. If this is a requirement due to a changing destination IP, this could be your best approach as zoomkat's tcp client code demonstrates.