very Strange problem

Hi everyone,
I got Arduino UNO and WIFI shield this week

Now i use 1.0.6 as my IDE

When i started program, i tried basic example about serial and WIFi, they all work.

But when i wants to make them together and add some new in it. problem come,and very strange , i don’t why .

My program would search all avaliable wifi networks and try to connect the “PreferSSID”.

and then show a menu to do different function.

Here is my Code:

#include <SPI.h>
#include <WiFi.h>
#include<WiFiudp.h>

const byte BUF_SIZE=64;

const unsigned long SERIAL_RATE=9600;

const char PreferSSID[] = "MySSID";    
const char PreferSSIDPSW[] = "MYSSIDPSW";  

char CurrentSSID[BUF_SIZE / 2 ];
char CurrentPSW[BUF_SIZE / 2 ];



char bufFromSerial[BUF_SIZE];
char lenOfBuf=0;

char tem1[BUF_SIZE];
char tem1Len=0;
char tem2[BUF_SIZE];
char tem2Len=0;

int WIFIstatus = WL_IDLE_STATUS; 

char numSsid;

IPAddress ServerIP;

WiFiUDP Udp;

void SearchWIFINetwork()
{
	Serial.println("Scanning available networks...");
	numSsid= WiFi.scanNetworks();
	if (numSsid == -1)
	{
		Serial.println("not wifi SSID is found");
	}
	else
	{
		listNetworks(numSsid);
		connWIFI((char*)PreferSSID,(char *)PreferSSIDPSW);
	}
}

void printMacAddress() 
{
	// the MAC address of your Wifi shield
	byte mac[6];

	// print your MAC address:
	WiFi.macAddress(mac);
	Serial.print("MAC: ");
	Serial.print(mac[5], HEX);
	Serial.print(":");
	Serial.print(mac[4], HEX);
	Serial.print(":");
	Serial.print(mac[3], HEX);
	Serial.print(":");
	Serial.print(mac[2], HEX);
	Serial.print(":");
	Serial.print(mac[1], HEX);
	Serial.print(":");
	Serial.println(mac[0], HEX);
}

void listNetworks(int numSsid) {
	Serial.println("========Avaliable Wifi networks:========");
	// print the list of networks seen:
	Serial.print("number of available networks:");
	Serial.println(numSsid);

	for (int thisNet = 0; thisNet < numSsid; thisNet++)
	{
		Serial.print(thisNet);
		Serial.print(") ");
		Serial.print(WiFi.SSID(thisNet));
		Serial.print("\tSignal: ");
		Serial.print(WiFi.RSSI(thisNet));
		Serial.print(" dBm");
		Serial.print("\tEncryption: ");
		printEncryptionType(WiFi.encryptionType(thisNet));
	}
}

void printEncryptionType(int thisType) {
	// read the encryption type and print out the name:
	switch (thisType) {
	case ENC_TYPE_WEP:
		Serial.println("WEP");
		break;
	case ENC_TYPE_TKIP:
		Serial.println("WPA");
		break;
	case ENC_TYPE_CCMP:
		Serial.println("WPA2");
		break;
	case ENC_TYPE_NONE:
		Serial.println("None");
		break;
	case ENC_TYPE_AUTO:
		Serial.println("Auto");
		break;
	}
}

void setup()
{
	Serial.begin(SERIAL_RATE);
	while (!Serial) {
		; // wait for serial port to connect. Needed for Leonardo only
	}
	Serial.println("Serial connect OK");
	Serial.println("Start Init...");

	// check for the presence of the shield:
	if (WiFi.status() == WL_NO_SHIELD) {
		Serial.println("WiFi shield not present");
		// don't continue:
		while (true);
	}

	char * buf = WiFi.firmwareVersion();
	Serial.print("WIFI shield fireware version:");
	Serial.println(buf);

	// Print WiFi MAC address:
	printMacAddress();

	// scan for existing networks:
	SearchWIFINetwork();
}


void printCurrentNet() {
	// print the SSID of the network you're attached to:
	Serial.print("SSID: ");
	Serial.println(WiFi.SSID());

	// print the MAC address of the router you're attached to:
	byte bssid[6];
	WiFi.BSSID(bssid);
	Serial.print("BSSID: ");
	Serial.print(bssid[5], HEX);
	Serial.print(":");
	Serial.print(bssid[4], HEX);
	Serial.print(":");
	Serial.print(bssid[3], HEX);
	Serial.print(":");
	Serial.print(bssid[2], HEX);
	Serial.print(":");
	Serial.print(bssid[1], HEX);
	Serial.print(":");
	Serial.println(bssid[0], HEX);

	// print the received signal strength:
	long rssi = WiFi.RSSI();
	Serial.print("signal strength (RSSI):");
	Serial.println(rssi);

	// print the encryption type:
	byte encryption = WiFi.encryptionType();
	Serial.print("Encryption Type:");
	Serial.println(encryption, HEX);
	Serial.println();
}

void printWifiData() {
	// print your WiFi shield's IP address:
	IPAddress ip = WiFi.localIP();
	Serial.print("IP Address: ");
	Serial.println(ip);

	// print your MAC address:
	byte mac[6];
	WiFi.macAddress(mac);
	Serial.print("MAC address: ");
	Serial.print(mac[5], HEX);
	Serial.print(":");
	Serial.print(mac[4], HEX);
	Serial.print(":");
	Serial.print(mac[3], HEX);
	Serial.print(":");
	Serial.print(mac[2], HEX);
	Serial.print(":");
	Serial.print(mac[1], HEX);
	Serial.print(":");
	Serial.println(mac[0], HEX);

}

void connWIFI(char* aSSID,char* PSW )
{
	if( WIFIstatus != WL_CONNECTED) 
	{
		Serial.print("Attempting to connect to WPA SSID: ");
		Serial.println(aSSID);
		WIFIstatus = WiFi.begin(aSSID, PSW);
		delay(500);
		if (WIFIstatus==WL_CONNECTED)
		{
			Serial.println("Connected OK");
			
			printCurrentNet();
			printWifiData();

                        Udp.begin(1234);
		}
		else
			Serial.print("Connected failed");  
	}
	else
	{
		Serial.println("WIFI already connected.");
		printCurrentNet();
		printWifiData();
	}
}

void DisConn()
{
	if( WIFIstatus == WL_CONNECTED) 
	{ 
		WiFi.disconnect(); 
		WIFIstatus=WL_IDLE_STATUS;
		Serial.print("WIFI Disconnect OK");  
	}
	else
	{
		Serial.print("WIFI dose not connected");  
	}
}



void CharArrayCopy(char * src,char * dest, char length)
{
	if (length>0)
	{
		for(char i=0;i<length;i++)
		{
			dest[i]=src[i];
		}
	}
}

void ClearSerialBuf()
{
	Serial.flush();
	lenOfBuf=0;
}

void WaitforInputAndRead()
{
	while(lenOfBuf<=0)
	{
		ReadFromSerial();
		delay(300);
	}
}

void ReadFromSerial()
{
	while(Serial.available()>0)
	{
		bufFromSerial[lenOfBuf++] = Serial.read();
	}

	if(lenOfBuf>0)
	{
		bufFromSerial[++lenOfBuf]='\0';
	}
}

void JoininNewNetwork()
{
	ClearSerialBuf();
	Serial.print("Please Input the WIFI network name(SSID):");
	WaitforInputAndRead();

	if (lenOfBuf>0)
	{
		CharArrayCopy(bufFromSerial,tem1,lenOfBuf);
		tem1Len=lenOfBuf;
		Serial.println(bufFromSerial);
	}
	else
	{
		Serial.println("");
		Serial.print("Invalid SSID name.");
		return;
	}

	ClearSerialBuf();
	Serial.print("Please Input the network password:");
	WaitforInputAndRead();

	if (lenOfBuf>0)
	{
		CharArrayCopy(bufFromSerial,tem2,lenOfBuf);
		tem2Len=lenOfBuf;
		Serial.println(bufFromSerial);
	}
	else
	{
		Serial.println("");
		Serial.print("Invalid password.");
		return;
	}
	connWIFI(tem1,tem2);
}

void SetServerIP()
{
	Serial.print("Current server address: ");
	Serial.println(ServerIP);
	ClearSerialBuf();
	Serial.print("Please Input new ip address:");
	WaitforInputAndRead();
	if (lenOfBuf>0)
	{
		tem1Len=0;
		tem2Len=0;

		Serial.println(bufFromSerial);
		bufFromSerial[lenOfBuf-1]='.';
		for(int i=0;i<lenOfBuf;i++)
		{
			if ((bufFromSerial[i]>='0') && (bufFromSerial[i]<='9'))
			{
				if(tem1Len<=2)
					tem1[tem1Len++]=bufFromSerial[i];
				else
				{
					Serial.println("Invalid IP address");
					return;
				}
	
			}
			else
				if ((bufFromSerial[i]=='.'))
			{
				if(tem2Len<=3)
				{
					tem2[tem2Len]=0;
					for(char j=0;j<tem1Len;j++)
					{
						tem2[tem2Len]+=((unsigned char)(tem1[j]-'0'))*pow(10,tem1Len-j-1);					
					}
					if (tem1Len==3)
						tem2[tem2Len]+=1;
					
					tem2Len++;
					tem1Len=0;
				}
				else
				{
					Serial.println("Invalid IP address");
					return;
				}
			}
				else
				{
					Serial.println("Invalid IP address");
					return;
				}
		}

		for(int i=0;i<4;i++)
		{
			ServerIP[i]=tem2[i];
			
		}

		Serial.println("Server updates OK");
		Serial.print("New Server Ip is:");
		Serial.println(ServerIP);
	}
}

void DisplayMenu()
{
	while(true)
	{
		ClearSerialBuf();
                Serial.println("");
		Serial.println("**************************************************************************");
		Serial.println("1.list all avaliable WIFI network.");
		Serial.println("2.Join in a Network.");
		Serial.println("3.Disconnect current network.");
		Serial.println("4.Set Server IP Address.");
		Serial.println("5.Send message to Server");
		Serial.println("**************************************************************************");
                Serial.println("");
		WaitforInputAndRead();

		switch(bufFromSerial[0])
		{
		case '1':
			SearchWIFINetwork();
			break;
		case '2':
			JoininNewNetwork();
			break;
		case '3':
			DisConn();
			break;
		case '4':
			SetServerIP();
			break;
		case '5':
			break;
		}
	}
}

void loop()
{
	DisplayMenu();
}

!. if copy code and run ,the program would be repeat do :output some message and reboot
like this:
Serial connect OK
Start Init…
WIFI shield fireware version:1.0.0
MAC: 78:C4:E:2:18:A0
Scanning available networks…
========Avaliable Wifi networks:========
number of available networks:5
0) NETGEAR29 Signal: -28 dBm Encryption: WPA2

  1. EnGenius1 Signal: -33 dBm Encryption: WPA2
  2. Adherent Signal: -44 dBm Encryption: WPA2
  3. AP SERVER Signal: -45 dBm Encryption: WPA2
  4. belkin.e4c Signal: -75 dBm Encryption: WPA2
    Attempting to connect to WPA SSID: NETGEAR29
    Connected OK
    SSIDžª–¥…±connect OK
    Start Init…
    WIFI shield fireware version:1.0.0
    MAC: 78:C4:E:2:18:A0
    Scanning available networks…
    ========Avaliable Wifi networks:========
    number of available networks:5
  5. NETGEAR29 Signal: -30 dBm Encryption: WPA2
  6. EnGenius1 Signal: -32 dBm Encryption: WPA2
  7. Adherent Signal: -42 dBm Encryption: WPA2
  8. AP SERVER Signal: -50 dBm Encryption: WPA2
  9. belkin.e4c Signal: -84 dBm Encryption: WPA2
    Attempting to connect to WPA SSID: NETGEAR29
    Connected OK
    SSIDžª–¥…±connect OK
    Start Init…
    WIFI shield fireware version:1.0.0

2.If you change all serial print message “OK” to “Successful”
The output would be like this:
Serial connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succ¥ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Succe³ª–¥…±connect Successful
Start Init…
WIFI shield fireware version:1.0.0
MAC:

3.if you delete the variable “UDP” in very beginning and in function connwifi, the program would be run correct.

I really don’t understand why i change those would have those very strange impact for program.

hope someone could help me.

Thanks very much

Hi jacen

Sounds like you may be running out of memory, specifically SRAM.

You can reduce how much SRAM you use by adding the F() macro to store literal strings in PROGMEM. In your Serial.print() or Serial.println() statements, add the F() macro like this:

Serial.println(F("Scanning available networks..."));

More information on optimising memory use here:

Regards

Ray

Hi Hackscribble
Thank you so much .It is this problem.
I solved the problem by used F().
This problem made me crazy during last three days, you saved me.

Thank you very much .

Best Regard

Jacen