• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeArduinoShieldsArduino Sample Code for Anaconda WiFI Shield
Previous Next

Arduino Sample Code for Anaconda WiFI Shield

Posted by: admin , February 20, 2014

Anaconda WiFi Shield  has built-in TCP/IP stack and all the WiFi related code. Arduino just needs to configure the WiFi parameters using AT command through the UART, or, user can simply treat the WiFi as a transparent serial port after some offline configuration.

LinkSprite_WIFI_Shield_V1_1__78161.1381457638.1280.1280

In this post, we provide the Arduino Sample code for Anaconda WiFi Shield.

unsigned char const __crc8_tbl[256]=
{	
  0x00,0x91,0xe3,0x72,0x07,0x96,0xe4,0x75,0x0e,0x9f,0xed,0x7c,0x09,0x98,0xea,0x7b,
  0x1c,0x8d,0xff,0x6e,0x1b,0x8a,0xf8,0x69,0x12,0x83,0xf1,0x60,0x15,0x84,0xf6,0x67,
  0x38,0xa9,0xdb,0x4a,0x3f,0xae,0xdc,0x4d,0x36,0xa7,0xd5,0x44,0x31,0xa0,0xd2,0x43,
  0x24,0xb5,0xc7,0x56,0x23,0xb2,0xc0,0x51,0x2a,0xbb,0xc9,0x58,0x2d,0xbc,0xce,0x5f,
  0x70,0xe1,0x93,0x02,0x77,0xe6,0x94,0x05,0x7e,0xef,0x9d,0x0c,0x79,0xe8,0x9a,0x0b,
  0x6c,0xfd,0x8f,0x1e,0x6b,0xfa,0x88,0x19,0x62,0xf3,0x81,0x10,0x65,0xf4,0x86,0x17,
  0x48,0xd9,0xab,0x3a,0x4f,0xde,0xac,0x3d,0x46,0xd7,0xa5,0x34,0x41,0xd0,0xa2,0x33,
  0x54,0xc5,0xb7,0x26,0x53,0xc2,0xb0,0x21,0x5a,0xcb,0xb9,0x28,0x5d,0xcc,0xbe,0x2f,
  0xe0,0x71,0x03,0x92,0xe7,0x76,0x04,0x95,0xee,0x7f,0x0d,0x9c,0xe9,0x78,0x0a,0x9b,
  0xfc,0x6d,0x1f,0x8e,0xfb,0x6a,0x18,0x89,0xf2,0x63,0x11,0x80,0xf5,0x64,0x16,0x87,
  0xd8,0x49,0x3b,0xaa,0xdf,0x4e,0x3c,0xad,0xd6,0x47,0x35,0xa4,0xd1,0x40,0x32,0xa3,
  0xc4,0x55,0x27,0xb6,0xc3,0x52,0x20,0xb1,0xca,0x5b,0x29,0xb8,0xcd,0x5c,0x2e,0xbf,
  0x90,0x01,0x73,0xe2,0x97,0x06,0x74,0xe5,0x9e,0x0f,0x7d,0xec,0x99,0x08,0x7a,0xeb,
  0x8c,0x1d,0x6f,0xfe,0x8b,0x1a,0x68,0xf9,0x82,0x13,0x61,0xf0,0x85,0x14,0x66,0xf7,
  0xa8,0x39,0x4b,0xda,0xaf,0x3e,0x4c,0xdd,0xa6,0x37,0x45,0xd4,0xa1,0x30,0x42,0xd3,
  0xb4,0x25,0x57,0xc6,0xb3,0x22,0x50,0xc1,0xba,0x2b,0x59,0xc8,0xbd,0x2c,0x5e,0xcf};

//LinkSprite WiFi  Shield Sample Code 
//Send strings to the TCP/IP server

unsigned char const Send_String[30]={
0x4c,0x69,0x6e,0x6b,0x73,0x70,0x72,0x69,0x74,0x65,
0x2d,0x77,0x69,0x66,0x69,0x2d,0x73,0x68,0x69,0x65,
0x6c,0x64,0x2d,0x74,0x65,0x73,0x74,0x21,0x21,0x21
};
#define WIFI_WAIT_SYN     0x00
#define WIFI_WAIT_CTL     0x01
#define WIFI_LENGTH1      0x02
#define WIFI_LENGTH2	  0x03
#define WIFI_CHCK	      0x04
#define WIFI_RECEDATA	  0x05
#define WIFI_WAITPADDING  0x06

// WIFI flow control character

#define WIFI_SYN          0XAA 

// parameter id type  
#define WIFI_BSSID        0X01
#define WIFI_CHANEL       0X02
#define WIFI_SSID         0x03
#define WIFI_ENCRYPTTYPE  0X04
#define WIFI_ENKEY        0X05
#define WIFI_DATATYPE     0x06
#define WIFI_TCPPORT      0x07
#define WIFI_TCPIP        0X08
#define WIFI_TCPSERVERIP  0x09
#define WIFI_TCPSERVERMAC 0x0a
#define WIFI_TCPMASET     0X0b
#define WIFI_TCPGATEWAY	  0X0C

// data type  
#define CTL_DATA   0X00
#define NOA_DATA   0X10
#define ACK_DATA   0X20

//  format of control data
#define COMAND_DATA 0x00   //arduino to wifi 
#define MESSAGE_DATA 0X40  //wifi to arduino

//  wifi control commands
#define SCANNETWORK_DATA   0x00
#define LINKNETWORK_DATA   0X01
#define CLOSENETWORK_DATA  0x02
#define SETNETPARA_DATA    0X03
#define QUERYNETPARA_DATA  0X04
#define RESTWIFI_DATA      0X05
#define TCPLINK_DATA       0x06
#define TCPSENDFAILED_DATA 0x07

typedef struct NetParaBuffer
{
   unsigned char M_id;
   unsigned char cLength;
   unsigned char cInfo[64];
}NetParaBuffer;
extern const struct  NetParaBuffer sysNetParaBuffer[4]={ 
{0xa2,0x01,0x03},			//baud rate   115200  
{0xa7,0x01,0x00},			//connection mode:  00 manual 
{0xa9,0x01,0x01},			//transparent trasmission : 01 yes   
{0xaa,0x01,0x00}			//TCP listen :00 close 
};
// WiFI Module Configuration
// according to the setting of access point
// user the TCP/IP tool to set up a UDP server, port 3000
extern const struct NetParaBuffer IPNetParaBuffer[8]={
{0x06,0x01,0x01},				//Link Layer Protocol: UDP 
{0x07,0x02,0x0b,0xb8},			//port number 3000  0x0b 0xb8      Port number: 6000 (0x17,0x70) 3000: 0x0b 0xb8
{0x08,0x04,0xc0,0xa8,0x02,0xa6},	//wifi ip  192.168.2.166  
{0x09,0x04,0xc0,0xa8,0x02,0xa5},	//server ip 192.168.2.165 
{0x0b,0x01,0x01},				//net mask  255.255.255.0 
{0x0c,0x04,0xc0,0xa8,0x02,0x1},	//gateway    192.168.2.1 
{0x04,0x01,0x01},              //encryption  mode:00 open 01 wep   
{0x05,0x36,0x0d,0x00, 	//key
	   0x6c,0x69,0x6e,0x6b,0x73,0x70,0x72,0x69,0x74,0x65,0x30,0x30,0x31, //13 bytes the first group    ASCII of "linksprite001"
	   0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30, //13 bytes, 0
	   0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,//13 bytes, 0
	   0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30},//13 bytes, 0
};
const struct NetParaBuffer IDNetParaBuffer[3]={
{0x01,0x06,0xe0,0x05,0xc5,0xbe,0x78,0x66}, //BSSID e0-05-c5-be-78-66
{0x02,0x01,0x05},                          //channel 5
{0x03,0x21,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  //Ssid 0x21 is regular, the later part is ascii of  Ssid 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},         
};

unsigned char gcSN=0x00;   //
unsigned char Send_datas[100];
///////////////////////////////////////////

unsigned char  GetCrc8(unsigned char *ptr,unsigned char len)
{
  unsigned char  crc8,cdata;
  crc8=0;
  while(len--!=0)
  {
    cdata = *ptr++;
    crc8=__crc8_tbl[crc8^cdata];
  }
  return crc8;
}

//wifi send datas
void  SendDataToWifi(unsigned char cCtl,unsigned char cCommand,unsigned char *cCommandPara,unsigned int iLength)
{
  unsigned char cBuf[100]; 

  cBuf[0]=WIFI_SYN;    // Synchronous Field 
  cBuf[1]=cCtl|gcSN;   // Control Field 
  gcSN++;              // frame number,number is form 0 to 15  
  if(gcSN>=0x10)
  {
    gcSN = 0;
  }
  cBuf[2]=(iLength+1)>>8;
  cBuf[3]=(iLength+1)&0x00ff;
  cBuf[4]=GetCrc8(&cBuf[1],3);
  cBuf[5]=cCommand;
  memcpy(&cBuf[6],cCommandPara,iLength);
  memset(&cBuf[6+iLength],0x00,6);
  Serial.write(cBuf, iLength+12);
}
////////////////////////////////////////////////////////////////////////////////////////////
void WIFIReset(void)
{
  unsigned char cBuf[2];
  cBuf[0]= 0;
  SendDataToWifi(CTL_DATA,RESTWIFI_DATA,cBuf,1);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//wifi net params set
void WIFISetIPNetPara(unsigned char cPb,                   //  wifi net params group number
                    const struct NetParaBuffer *cNetParaBuffer,//  wifi net params table 
		   unsigned char cParaNum)               //  params numbers 
{
    unsigned char cBuf[100];
	unsigned char i;
	unsigned char cLength;
	cBuf[0]= cPb;
	cLength = 0;
	for(i=0;i<cParaNum;i++)
	{
		cBuf[1+cLength]= cNetParaBuffer[i].M_id;
		cBuf[2+cLength]= cNetParaBuffer[i].cLength;
		memcpy(&cBuf[3+cLength],cNetParaBuffer[i].cInfo,cBuf[2+cLength]);
		cLength = cLength+cBuf[2+cLength]+2;
	}

  SendDataToWifi(CTL_DATA,SETNETPARA_DATA,cBuf,1+cLength);
}
//wifi set system params
void WIFISetSysNetPara(const struct NetParaBuffer *cNetParaBuffer,//   wifi net params table 
		   unsigned char cParaNum)               //  params numbers 
{ 																		
	unsigned char cBuf[100];
	unsigned char i;
	unsigned char cLength;

	cLength = 0;
	for(i=0;i<cParaNum;i++)
	{
		cBuf[cLength]= cNetParaBuffer[i].M_id;
		cBuf[1+cLength]= cNetParaBuffer[i].cLength;
		memcpy(&cBuf[2+cLength],cNetParaBuffer[i].cInfo,cBuf[1+cLength]);
		cLength = cLength+cBuf[1+cLength]+2;
	}
	SendDataToWifi(CTL_DATA,SETNETPARA_DATA,cBuf,cLength);
}

/*********************************************************************************************************
** function name WIFIJoinAp()
** function  Join the AP                         
** cIdx:wifi net params group,default is the first group

*********************************************************************************************************
*/
void WIFIJoinAp(unsigned char cIdx)
{
   unsigned char cBuf[1];
   cBuf[0]= cIdx;
   SendDataToWifi(CTL_DATA,LINKNETWORK_DATA,cBuf,1);
}
///////////////////////////////////////////////////////////////////
////////////////////////////////////////////

void setup() {                
  int i;
  // open the serial port at 115200 bps:
  Serial.begin(115200);
  pinMode(11, OUTPUT);     
    digitalWrite(11, LOW);   // set the LED on
    delay(1000);
    delay(1000);
  digitalWrite(11, HIGH);   // set the LED on

  //gcReceComm1OK=0;
  WIFIReset();
  //RecivDataFromWifi();
  //ProcessWIFIData();
  delay(1000);
  delay(1000);

  //gcReceComm1OK=0;
  WIFISetSysNetPara(sysNetParaBuffer,4);		
  // RecivDataFromWifi();
  //ProcessWIFIData();
  delay(1000);
  delay(1000);

 // gcReceComm1OK=0;
  WIFISetIPNetPara(0xb1,IPNetParaBuffer,8);
 // RecivDataFromWifi();
  //ProcessWIFIData();
  delay(1000);
  delay(1000);

  //gcReceComm1OK=0;
  WIFISetIPNetPara(0xb1,IDNetParaBuffer,3);
  //RecivDataFromWifi();
  //ProcessWIFIData();
  delay(1000);
  delay(1000);

  //gcReceComm1OK=0; 
  WIFIJoinAp(0x01);
  //RecivDataFromWifi();
  //ProcessWIFIData();
  digitalWrite(11, LOW);   // set the LED on
  delay(1000);
  delay(1000); 

  for(i=0;i<30;i++)
    Send_datas[i]=Send_String[i];
  delay(1000);

}

void loop() {
  digitalWrite(11, HIGH);   // set the LED on
  delay(1000);
  SendDataToWifi(NOA_DATA,Send_datas[0],&Send_datas[1],29);
  digitalWrite(11, LOW);   // set the LED on
  delay(1000);
}

The password is set in the structure named IPNetParaBuffer :

struct NetParaBuffer IPNetParaBuffer[8]={

{0x06,0x01,0x01},                                             //Link layer Protocol UDP

{0x07,0x02,0x0b,0xb8},                                     //port number 3000: 0x0b 0xb8;   6000 0x17,0x70

{0x08,0x04,0xc0,0xa8,0x02,0xa6},         //device ip   192.168.2.166  

  {0x09,0x04,0xc0,0xa8,0x02,0xa5},        //server ip 192.168.2.165  

{0x0b,0x01,0x01},                                             //net mask   255.255.255.0  

{0x0c,0x04,0xc0,0xa8,0x02,0x1},           //gateway     192.168.2.1

{0x04,0x01,0x01},   //encrypt   mode:

{0x05,0x36,0x0d,0x00, //key

   0x6c,0x69,0x6e,0x6b,0x73,0x70,0x72,0x69,0x74,0x65,0x30,0x30,0x31, //    0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,

   0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,   0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30},

};

Encryption mode:

  1. 0x04 protocol byte. It’s fixed. Don’t change.
  2. 0x01is the length, and followed by the mode byte. 0x01 means WEP. 0x00 means open.

Key:

  1. 0x05 protocol byte.
  2. 0x36 is the total length of payload. 0x0dis the length of each key.

WEP128BIT needs all 13 bytes. It’s needed to fill all of them.

If key is characters, for example, “linksprite001”,   then we just need to fill with the ASCII of “0x6c,0x69,0x6e,0x6b,0x73,0x70,0x72,0x69,0x74,0x65,0x30,0x30,0x31”, and fill the rest with 0.

 

Important Note:

When connect TX to MRX and RX to MTX, i.e., Wifi module is talking to MCU,  both the module and the USB are trying to taking to atmega. Please unplug your shield when uploading hex. Otherwise, you will get program timeout issue.

Picture 3

Arduino Mega

If you are using arduino mega, it’s recommended to use Arduino mega 1280. The bootloader of 2560 has some issue on writing the hex because it has large tables.

If you are using 2560, please use AVR studio and a programmer to burn the generated hex and eep file.

2560

Tags: Shields

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors