Hi all,
I have a WiFi shield on an Arduino Uno (rev 1), and I'm trying to use the wifi and the SD card in the same sketch. I can use the SD card in its own sketch, and I can use the the WiFi in a separate sketch. But as soon as I try and use them in the same sketch, things behave oddly.
Can anyone point me at an example that uses both the WiFi shield and the SD card in the same sketch, or advise where I'm going wrong here please?
The only change between working and failing is the addition of the following lines to the setup() method
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Working example code
#include <SPI.h>
#include <WiFi.h>
#include <SD.h>
#include <LiquidCrystal595.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal595 lcd(5,6,8);
// Wifi network details
char ssid[] = "MySSID"; // your network SSID (name)
char pass[] = "passwordtomynetwork"; // your network password
int wifiStatus = WL_IDLE_STATUS; // the Wifi radio's status
WiFiClient client;
boolean lastConnected = false;
/* setup
Setup various pin modes, LCD display and ensure that we are connected to the wifi network
*/
void setup() {
Serial.begin(9600);
Serial.println("Arduino in setup()");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("Temp: ");
wifiStatus = connectToNetwork();
}
/* connectToNetwork
Connect to a wifi network
- if we are able to connect to the network, we briefly display the SSID on the LCD then Net: OK
- if we fail to connect to the network, we display Net: BAD on the LCD
*/
int connectToNetwork() {
while(!Serial) ;
Serial.println("Initializing Wifi...");
printMacAddress();
Serial.print("Attempting to connect to WPA network: [");
Serial.print(ssid);
Serial.println("]");
lcd.setCursor(0, 1);
lcd.print("Connecting...");
wifiStatus = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( wifiStatus != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
lcd.setCursor(0, 1);
lcd.print("Net: BAD ");
while(true);
}
// if you are connected, print out info about the connection:
else {
Serial.println("Connected to network");
lcd.setCursor(0, 1);
String ssidString = ssid;
int padCount = 16 - ssidString.length();
if (padCount > 0) {
for (int i = 0; i < padCount; i++) {
ssidString + ssidString + " ";
}
}
lcd.print(ssidString);
delay(2000); // Change this value to define how long the SSID is displayed on the LCD before we proceed
lcd.setCursor(0,1);
lcd.print("NET: OK ");
}
return wifiStatus;
}
/* printMacAddress
Finds the WiFi shield's mac address and prints it to the serial output. This is used for debugging
*/
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);
}
Working example output
Arduino in setup()
Initializing Wifi...
MAC: 7A:C4:E:94:EA:2A
Attempting to connect to WPA network: [MySSID]
Connected to network
Failure example code
#include <SPI.h>
#include <WiFi.h>
#include <SD.h>
#include <LiquidCrystal595.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal595 lcd(5,6,8);
// Wifi network details
char ssid[] = "MySSID"; // your network SSID (name)
char pass[] = "passwordtomynetwork"; // your network password
int wifiStatus = WL_IDLE_STATUS; // the Wifi radio's status
WiFiClient client;
boolean lastConnected = false;
/* setup
Setup various pin modes, LCD display and ensure that we are connected to the wifi network
*/
void setup() {
Serial.begin(9600);
Serial.println("Arduino in setup()");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("Temp: ");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
wifiStatus = connectToNetwork();
}
/* connectToNetwork
Connect to a wifi network
- if we are able to connect to the network, we briefly display the SSID on the LCD then Net: OK
- if we fail to connect to the network, we display Net: BAD on the LCD
*/
int connectToNetwork() {
while(!Serial) ;
Serial.println("Initializing Wifi...");
printMacAddress();
Serial.print("Attempting to connect to WPA network: [");
Serial.print(ssid);
Serial.println("]");
lcd.setCursor(0, 1);
lcd.print("Connecting...");
wifiStatus = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( wifiStatus != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
lcd.setCursor(0, 1);
lcd.print("Net: BAD ");
while(true);
}
// if you are connected, print out info about the connection:
else {
Serial.println("Connected to network");
lcd.setCursor(0, 1);
String ssidString = ssid;
int padCount = 16 - ssidString.length();
if (padCount > 0) {
for (int i = 0; i < padCount; i++) {
ssidString + ssidString + " ";
}
}
lcd.print(ssidString);
delay(2000); // Change this value to define how long the SSID is displayed on the LCD before we proceed
lcd.setCursor(0,1);
lcd.print("NET: OK ");
}
return wifiStatus;
}
/* printMacAddress
Finds the WiFi shield's mac address and prints it to the serial output. This is used for debugging
*/
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);
}
Failure example output
Arduino in setup()
inMAC: