Problems about Redfly WiFi-Shield

Hello everyone,

I am using Arduino Mega 2560 and Redfly Wifi-Shield. I want to send data by serial.write() when i access to the wlan, to communicate with the slave device i have to initialize the serial port, Serial1.begin(38400,0x36), that means 1 startbit, 8 databits, 1 stopbit, odd, but the wifi-shield cant work under this set.

here is my code

#include <RedFly.h>
#include <Serial.h>

uint8_t PLAY[3]={0x20,0x01,0x21};
uint8_t STOP[3]={0x20,0x00,0x20};

byte ip[]      = { 192,168,  1,110 }; //ip from shield (server)
byte netmask[] = { 255,255,255,  0 }; //netmask

uint8_t http=INVALID_SOCKET; //socket handle
uint16_t http_len=0; //receive len
char http_buf[64];   //receive buffer

//debugout and debugoutIn will be used to test the communication between Arduino and PC

void debug(char *s)
{
  RedFly.disable();
  Serial.print(s);
  RedFly.enable();
}

void setup()
{
  uint8_t ret;
  Serial1.begin(38400,0x36);
  Serial.begin(9600);
  //init the WiFi module on the shield
  // ret = RedFly.init(br, pwr) //br=9600|19200|38400|57600|115200|200000|230400, pwr=LOW_POWER|MED_POWER|HIGH_POWER
  // ret = RedFly.init(pwr) //9600 baud, pwr=LOW_POWER|MED_POWER|HIGH_POWER
  // ret = RedFly.init() //9600 baud, HIGH_POWER
  ret = RedFly.init();
  if(ret)
  {
    debug("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();
    ret = RedFly.join("redfly","AB12345678",IBSS_CREATOR,2);

    if(ret)
    {
      debug("JOIN ERR");
      for(;;); //do nothing forevermore
    }
    else

    {
      ret = RedFly.begin(ip,0,0,netmask);
      if(ret)
      {
        Serial.println(ret);
        RedFly.disconnect();
        for(;;); //do nothing forevermore
      }
      else
      {
        RedFly.getlocalip(ip); //receive shield IP in case of DHCP/Auto-IP
        http = RedFly.socketListen(PROTO_TCP, 80); //start listening on port 80
        if(http == 0xFF)
        {
          debug("SOCKET ERR");
          RedFly.disconnect();
          for(;;); //do nothing forevermore
        }
      }
    }
  }
}

void loop()
{
  
  uint8_t sock;
  char buf[512], *ptr;
  uint16_t buf_len, rd, len;

  //check if socket is opened
  if(RedFly.socketClosed(http)) //no socket open
  {
    http = RedFly.socketListen(PROTO_TCP, 80); //start listening on port 80
  }

  //get data
  sock    = 0xFF; //0xFF = return data from all open sockets
  ptr     = buf;
  buf_len = 0;
  do
  {
    rd = RedFly.socketRead(&sock, &len, (uint8_t*)ptr, sizeof(buf)-buf_len);
    if((rd != 0) && (rd != 0xFFFF)) //0xFFFF = connection closed
    {
      ptr     += rd;
      buf_len += rd;
    }
  }while((len != 0) && (buf_len < sizeof(buf)));

  //progress the received data
  if(buf_len && (sock == http) && (sock != INVALID_SOCKET))
  {
    if(strncmp_P(buf, PSTR("GET / HTTP"), 10) == 0) //main site´  
    {
      //send HTTP 200 header
      RedFly.socketSendPGM(sock, PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")); //Content-Length: xx\r\n
      RedFly.socketSendPGM(sock, PSTR("<h1>Guten Tag! <h1>

"));
      RedFly.socketSendPGM(sock, PSTR(" <a href='/<h1>ledon<h1>'>play</a> - <a href='/<h1>ledoff<h1>'>stop</a>"));
    }
    else if(strncmp_P(buf, PSTR("GET /ledon HTTP"), 15) == 0) //led on
    {
      
      //send HTTP 200 header
      RedFly.socketSendPGM(sock, PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"));
      //send HTML data
      RedFly.socketSendPGM(sock, PSTR("<h1>Hello <h1>

"));
      RedFly.socketSendPGM(sock, PSTR(" <a href='/<h1>ledon<h1>'>play</a> - <a href='/<h1>ledoff<h1>'>stop</a> 

"));
      
      for(;;){
        Serial1.write(PLAY,sizeof(PLAY));
        if(Serial1.available()>0)
        {
          Serial.println("**********************");
          Serial.println(Serial1.read(),HEX);
          Serial.println("**********************");
          break;
        }
      }
    }
    else if(strncmp_P(buf, PSTR("GET /ledoff HTTP"), 16) == 0) //led off
    {
      //send HTTP 200 header
      RedFly.socketSendPGM(sock, PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"));
      //send HTML data
      RedFly.socketSendPGM(sock, PSTR("<h1>Hello <h1>

"));
      RedFly.socketSendPGM(sock, PSTR(" <a href='/<h1>ledon<h1>'>play</a> - <a href='/<h1>ledoff<h1>'>stop</a> 

"));
        for(;;){
        Serial.write(STOP,sizeof(STOP));
        
        /*if(Serial1.available()>0)
        {
          Serial.println("**********************");
          Serial.println(Serial1.read(),HEX);
          Serial.println("**********************");
          break;
        }
        */
        }
    }
    else
    {
      //send HTTP 404 header
      RedFly.socketSendPGM(sock, PSTR("HTTP/1.1 404 Not Found\r\n\r\n"));
    }

    //close connection
    RedFly.socketClose(sock);
  }
}

could anyone tell me what’s the problem?

thanks

The first hardware serial port is used for communicating with the RedFly. If you want to use this port for other things then you can either re-wire the RedFly Rx+Tx pin to another serial port or you can disable the RedFly and use the serial port with your settings.

...
  RedFly.disable();
  Serial.begin(38400);
  Serial.print(s);
  RedFly.enable(); //serial port will be configurated for RedFly
...

Rgds Andreas