Hi
ich benutze ein Redfly um über WLAN Daten an den Arduino zu senden.
Mein erster test klappt schon ganz gut, ich kann zb per Telnet Strings ans RedFly schicken, und diese dann per serial Monitor ausgeben, klappt wunderbar.
Ich sende also über die Verbindung eine Nachricht die dann immer in eine variable, zb var1 geschrieben wird und ausgegeben werden kann.
Jetzt möchte ich aber mehrere "werte" senden.
Also soll der Arduino mehrere variablen haben, zb var1, var2, var3 und diese sollen dann jeweils mit werten gefüllt werden, die vom RedFly empfangen werden.
Kann mir hier jemand helfen, wie soetwas am besten umgesetzt wird?
Hier noch mein Quelltext, wie er momentan funktioniert, jedoch mit nur einer Variable:
#include <RedFly.h>
byte ip[] = { 192,168, 0, 30 }; //ip from shield (server)
byte netmask[] = { 255,255,255, 0 }; //netmask
#define TCP_PORT (1234) //local TCP port on the shield
#define UDP_PORT (1234) //local UDP port on the shield
uint8_t hTCP=0xFF, hUDP=0xFF; //socket handles
//serial format: 9600 Baud, 8N2
void debugout(char *s) { RedFly.disable(); Serial.print(s); RedFly.enable(); }
void debugoutln(char *s){ RedFly.disable(); Serial.println(s); RedFly.enable(); }
void setup()
{
uint8_t ret;
//init the WiFi module on the shield
ret = RedFly.init();
if(ret)
{
debugoutln("INIT ERR"); //there are problems with the communication between the Arduino and the RedFly
}
else
{
//scan for wireless networks (must be run before join command)
RedFly.scan();
//join network
ret = RedFly.join("XXX", "XXX", INFRASTRUCTURE);
if(ret)
{
debugoutln("JOIN ERR");
for(;;); //do nothing forevermore
}
else
{
//set ip config
ret = RedFly.begin(ip, 0, 0, netmask);
if(ret)
{
debugoutln("BEGIN ERR");
RedFly.disconnect();
for(;;); //do nothing forevermore
}
}
}
}
void loop()
{
int var;
char buf[512], *ptr;
uint8_t sock;
uint16_t buf_len, rd, len;
uint16_t port; //incomming UDP port
uint8_t ip[4]; //incomming UDP ip
//check if sockets are opened
if(RedFly.socketClosed(hTCP) || (hTCP == 0xFF))
{
hTCP = RedFly.socketListen(PROTO_TCP, TCP_PORT);
}
//get data
sock = 0xFF; //0xFF = return data from all open sockets
ptr = buf;
buf_len = 0;
do
{
rd = RedFly.socketRead(&sock, &len, ip, &port, (uint8_t*)ptr, sizeof(buf)-buf_len);
if((rd != 0) && (rd != 0xFFFF)) //0xFFFF = connection closed
{
ptr += rd;
buf_len += rd;
}
}while(len != 0);
//send data
if(buf_len && (sock != 0xFF))
{
if(sock == hTCP)
{
var = atoi(buf);
Serial.println(var);
}
}
}
VG
vega