297 lines
5.9 KiB
Plaintext
297 lines
5.9 KiB
Plaintext
|
|
#include <SPI.h>
|
|
#include <Ethernet.h> // version IDE 0022
|
|
#include <EthernetDHCP.h>
|
|
#include <EthernetDNS.h>
|
|
|
|
#include <Z_OSC.h>
|
|
|
|
byte mac[] = {
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
|
//byte myIp[] = { 192, 168, 0, 255 };
|
|
|
|
byte server[] = {
|
|
0,0,0,0 }; // TEMPORAL SERVER
|
|
|
|
|
|
byte destIp[] = {
|
|
172, 26, 0, 46 };
|
|
int destPort = 8889;
|
|
int serverPort = 8888;
|
|
|
|
char oscAdr[] = "/z-osc/test/123";
|
|
char oscAdr2[] = "/z-osc/test2/abc";
|
|
|
|
int iCount = 0;
|
|
long int liCount = 0;
|
|
float fCount = 0.0;
|
|
char str[] = "abcd";
|
|
|
|
Z_OSCClient client;
|
|
|
|
Z_OSCServer server;
|
|
|
|
Z_OSCMessage *rcvMes;
|
|
|
|
const char* ip_to_str(const uint8_t*);
|
|
boolean dhcpReady = 0;
|
|
boolean flagConnect = 0;
|
|
char serverHostname[] = "kn.sofiethic.org";
|
|
|
|
|
|
void setup(){
|
|
|
|
Serial.begin(9600);
|
|
|
|
//Ethernet.begin(myMac ,myIp);
|
|
Serial.println("Attempting to obtain a DHCP lease...");
|
|
|
|
EthernetDHCP.begin(mac);
|
|
|
|
DHCP_begin();
|
|
|
|
DNS_resolve();
|
|
|
|
}
|
|
|
|
|
|
void loop(){
|
|
|
|
EthernetDHCP.maintain();
|
|
|
|
sendProcess();
|
|
|
|
Z_OSCMessage mes;
|
|
|
|
mes.setAddress(destIp, destPort);
|
|
mes.setZ_OSCMessage(oscAdr2 ,"s" ,"test test");
|
|
|
|
client.send(&mes);
|
|
|
|
mes.flush();
|
|
|
|
delay(100);
|
|
}
|
|
|
|
|
|
void sendProcess(){
|
|
|
|
|
|
long int tmp=(long int)iCount;
|
|
Z_OSCMessage message;
|
|
|
|
message.setAddress(destIp,destPort);
|
|
message.setZ_OSCMessage(oscAdr ,"iifs" ,&tmp ,&liCount ,&fCount ,str);
|
|
|
|
client.send(&message);
|
|
|
|
if(iCount++ > 1000) iCount =0;
|
|
if(liCount++ > 1000) liCount=0;
|
|
fCount += 0.1;
|
|
if(fCount > 100.0) fCount =0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* ip_to_str(const uint8_t* ipAddr)
|
|
{
|
|
static char buf[16];
|
|
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
|
|
return buf;
|
|
}
|
|
|
|
|
|
void dhcp()
|
|
{
|
|
|
|
static DhcpState prevState = DhcpStateNone;
|
|
static unsigned long prevTime = 0;
|
|
|
|
DhcpState state = EthernetDHCP.poll();
|
|
|
|
if (prevState != state) {
|
|
Serial.println();
|
|
|
|
switch (state) {
|
|
case DhcpStateDiscovering:
|
|
Serial.print("Discovering servers.");
|
|
dhcpReady=0;
|
|
break;
|
|
case DhcpStateRequesting:
|
|
Serial.print("Requesting lease.");
|
|
dhcpReady=0;
|
|
break;
|
|
case DhcpStateRenewing:
|
|
Serial.print("Renewing lease.");
|
|
dhcpReady=0;
|
|
break;
|
|
case DhcpStateLeased:
|
|
{
|
|
Serial.println("Obtained lease!");
|
|
|
|
const byte* ipAddr = EthernetDHCP.ipAddress();
|
|
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
|
|
const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
|
|
|
|
Serial.print("My IP address is ");
|
|
Serial.println(ip_to_str(ipAddr));
|
|
|
|
Serial.print("Gateway IP address is ");
|
|
Serial.println(ip_to_str(gatewayAddr));
|
|
|
|
Serial.print("DNS IP address is ");
|
|
Serial.println(ip_to_str(dnsAddr));
|
|
|
|
Serial.println('\n');
|
|
|
|
EthernetDNS.setDNSServer(dnsAddr);
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
// DETECCION DE LA IP DEL SERVIDOR
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
Serial.print("Resolving ");
|
|
Serial.print(serverHostname);
|
|
Serial.print("...");
|
|
|
|
//byte ServerIpAddr[4];
|
|
|
|
//DNSError err = EthernetDNS.sendDNSQuery("kn.sofiethic.org");
|
|
DNSError err = EthernetDNS.sendDNSQuery(serverHostname);
|
|
|
|
if (DNSSuccess == err) {
|
|
do {
|
|
err = EthernetDNS.pollDNSReply(server);
|
|
|
|
if (DNSTryLater == err) {
|
|
delay(20);
|
|
Serial.print(".");
|
|
}
|
|
}
|
|
while (DNSTryLater == err);
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
if (DNSSuccess == err) {
|
|
Serial.print("The IP address is ");
|
|
Serial.print(ip_to_str(server));
|
|
Serial.println(".");
|
|
dhcpReady=1;
|
|
}
|
|
else if (DNSTimedOut == err) {
|
|
Serial.println("Timed out.");
|
|
}
|
|
else if (DNSNotFound == err) {
|
|
Serial.println("Does not exist.");
|
|
}
|
|
else {
|
|
Serial.print("Failed with error code ");
|
|
Serial.print((int)err, DEC);
|
|
Serial.println(".");
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (state != DhcpStateLeased && millis() - prevTime > 300) {
|
|
prevTime = millis();
|
|
Serial.print('.');
|
|
}
|
|
else if (state == DhcpStateLeased) {
|
|
|
|
dhcpReady = 1;
|
|
|
|
}
|
|
|
|
prevState = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DHCP_begin()
|
|
{
|
|
|
|
|
|
// Since we're here, it means that we now have a DHCP lease, so we print
|
|
// out some information.
|
|
const byte* ipAddr = EthernetDHCP.ipAddress();
|
|
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
|
|
const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
|
|
|
|
Serial.println("A DHCP lease has been obtained.");
|
|
|
|
Serial.print("My IP address is ");
|
|
Serial.println(ip_to_str(ipAddr));
|
|
|
|
Serial.print("Gateway IP address is ");
|
|
Serial.println(ip_to_str(gatewayAddr));
|
|
|
|
Serial.print("DNS IP address is ");
|
|
Serial.println(ip_to_str(dnsAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DNS_resolve()
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
// DETECCION DE LA IP DEL SERVIDOR
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
Serial.print("Resolving ");
|
|
Serial.print(serverHostname);
|
|
Serial.print("...");
|
|
|
|
//byte ServerIpAddr[4];
|
|
|
|
//DNSError err = EthernetDNS.sendDNSQuery("kn.sofiethic.org");
|
|
DNSError err = EthernetDNS.sendDNSQuery(serverHostname);
|
|
|
|
if (DNSSuccess == err) {
|
|
do {
|
|
err = EthernetDNS.pollDNSReply(server);
|
|
|
|
if (DNSTryLater == err) {
|
|
delay(20);
|
|
Serial.print(".");
|
|
}
|
|
}
|
|
while (DNSTryLater == err);
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
if (DNSSuccess == err) {
|
|
Serial.print("The IP address is ");
|
|
Serial.print(ip_to_str(server));
|
|
Serial.println(".");
|
|
dhcpReady=1;
|
|
}
|
|
else if (DNSTimedOut == err) {
|
|
Serial.println("Timed out.");
|
|
}
|
|
else if (DNSNotFound == err) {
|
|
Serial.println("Does not exist.");
|
|
}
|
|
else {
|
|
Serial.print("Failed with error code ");
|
|
Serial.print((int)err, DEC);
|
|
Serial.println(".");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|