Lab_interaccio/2011/Intact/Send_Receive/Send_Receive.pde

358 lines
7.2 KiB
Plaintext
Raw Normal View History

2025-02-25 21:29:42 +01:00
#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[] = {
192, 168, 2, 3 };
int destPort = 9889;
int serverPort = 8998;
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 serverOSC;
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);
serverOSC.sockOpen(serverPort);
DHCP_begin();
DNS_resolve();
}
void loop(){
EthernetDHCP.maintain();
if(serverOSC.available()){
rcvMes=serverOSC.getMessage();
logMessage();
}
sendProcess();
Z_OSCMessage mes;
mes.setAddress(destIp, destPort);
mes.setZ_OSCMessage(oscAdr2 ,"s" ,"test test");
client.send(&mes);
mes.flush();
}
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;
}
void logMessage(){
uint16_t i;
byte *ip=rcvMes->getIpAddress();
long int intValue;
float floatValue;
char *stringValue;
Serial.print(ip[0],DEC);
Serial.print(".");
Serial.print(ip[1],DEC);
Serial.print(".");
Serial.print(ip[2],DEC);
Serial.print(".");
Serial.print(ip[3],DEC);
Serial.print(":");
Serial.print(rcvMes->getPortNumber());
Serial.print(" ");
Serial.print(rcvMes->getZ_OSCAddress());
Serial.print(" ");
Serial.print(rcvMes->getTypeTags());
Serial.print("--");
for(i=0 ; i<rcvMes->getArgsNum(); i++){
switch( rcvMes->getTypeTag(i) ){
case 'i':
intValue = rcvMes->getInteger32(i);
Serial.print(intValue);
Serial.print(" ");
break;
case 'f':
floatValue = rcvMes->getFloat(i);
Serial.print(floatValue);
Serial.print(" ");
break;
case 's':
stringValue = rcvMes->getString(i);
Serial.print(stringValue);
Serial.print(" ");
break;
}
}
Serial.println("");
}
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(".");
}
}