Hi,
I`m new here and I have one problem with my project.
I want to create web server using Arduino Wifi Shield and Arduino Mega 2560.
I use code like this (find on the Internet) and i have the same problem like someone here:
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "belkin.E33"; // your network SSID (name)
char pass[] = "abc123cde456"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(9, OUTPUT); // set the LED pin mode
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on
");
client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off
");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(9, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(9, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
When I try to ping this server, everything is ok, but when i want to use my browser to check this there is no result (browser could`t find server)
Are you using IDE v1.0.5 or better? Have you upgraded your wifi shield firmware? Here is a way to check the firmware version. It should be v1.1.0 if current.
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while(true);
}
// check firmware version
Serial.print(F("Firmware version: "));
Serial.println(WiFi.firmwareVersion());
edit: If you want to see one working on the internet, here is mine the shield PaulS loaned me to debug . All pages except the test form are on a SD card. http://68.99.58.119
The server library/firmware has been a bit unstable. Users report getting wrong or corrupted files when the wifi shield is under a "heavy" request load. I am working on getting that corrected.
The shield only uses one socket for the server, and has a rather nasty delay (a second or two repeated a few times during the image downloads) so the download is a little slow. Working on that also.
Arduino Wifi Shields with firmware lower than v1.1.0 seems to have issues. Please upgrade your firmware and re-upload the code. It should work just fine. !!
SurferTim:
Are you using IDE v1.0.5 or better? Have you upgraded your wifi shield firmware? Here is a way to check the firmware version. It should be v1.1.0 if current.
I try to use every possible ver IDE. Now I`m using 1.0.5 but i try also to use 1.0.2 and naightly build.
I had firmware version 1.00 but i upgrade it as you say to version 1.1.0 but i now i have new problem. When i try to use example of ConnectWithWPA serial monitor said that connection is ok but when I try to use example SimpleExampleWebServer serial monitor shows something like this:
Attempting to connect to Network named: Android
Attempting to connect to Network named: Android
SSID:
IP Address: 0.0.0.0
signal strength (RSSI):0 dBm
To see this page in action, open a browser to http://0.0.0.0
When I use example from IDE to connect to WPA network
like this one:
/*
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <WiFi.h>
char ssid[] = "CyfrowyPolsat"; // your network SSID (name)
char pass[] = "szumacher3"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
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 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();
}
the result is:
Attempting to connect to WPA SSID: CyfrowyPolsat
Attempting to connect to WPA SSID: CyfrowyPolsat
You're connected to the networkSSID: CyfrowyPolsat
BSSID: 0:0:0:0:0:0
signal strength (RSSI):0
Encryption Type:4
IP Address: 192.168.2.101
192.168.2.101
MAC address: 78:C4:E:1:97:F7
SSID: CyfrowyPolsat
BSSID: 0:0:0:0:0:0
signal strength (RSSI):0
Encryption Type:0
SSID: CyfrowyPolsat
BSSID: 0:0:0:0:0:0
signal strength (RSSI):0
Encryption Type:0
I used the upgrade files included with IDE v1.0.5. I use IDE v1.0.5 and recently IDE v1.5.5, and both work with the upgraded firmware. It is still online. It isn't fancy, but it has the stuff that I use to test with. A form, a couple pictures, a couple css files, and a favicon.ico. http://68.99.58.119
Insure you use IDE v1.0.5 or later with the upgrade, and IDE v1.0.4 or earlier without the upgrade. I understand from others they do not mix well. I haven't tried it.
I can get the server to send the incorrect files after experimenting this morning. I believe the firmware is not stopping the listen part of the server and it accepts another connection before it should. It seems to work almost like udp. When you read the packet (empty the wifi shield rx buffer), it accepts another packet (connection). That has nothing to do with your problem tho.
stefan6:
I delete evry wire but ofcourse it doesnt help ;/
But... i see realy strange things.
When I load firmwire v. 1.0.0 result of trying start WifiWebServer is:
Attempting to connect to SSID: CyfrowyPolsat
SSID: CyfrowyPolsat
IP Address: 192.168.2.101
signal strength (RSSI):-46 dBm
so its looks like good connection (browser doesnt work)
but when I load firmware v 1.1.0 the result is:
Attempting to connect to SSID: CyfrowyPolsat
Attempting to connect to SSID: CyfrowyPolsat
SSID:
IP Address: 0.0.0.0
signal strength (RSSI):0 dBm
I have no idea why... Maybe you know any other, new version of firmware?
I tried the WifiWebServer example file that came with Arduino IDE 1.5.5. I have firmware version 1.1.0 on my wifi shield.
I think from your earlier post, it does seems that you have the right version on your WiFiShield. With version 1.0, I was able to scan the networks, get an ipAddress, but I wasn't able to run the webServer example succesfully.
Might be dump to ask, but you are connecting to the same network right?
Also you have already tried pinging your server on your computer command prompt?
/*
WiFi Web Server
A simple web server that shows the value of the analog input pins.
using a WiFi shield.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* Analog inputs attached to pins A0 through A5 (optional)
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "2WIRE464"; // your network SSID (name)
char pass[] = "80040xese"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(2000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
I am in the same situation as the OP, and in that case rather than make a new thread, I'll post here perhaps it will help us both. The Arduino connects to the network but does not host the webserver properly.
I've taken my debugging a small bit further. I can ping my device and see it on the router page. So the device exists and is connected to the network. I brought my setup to my home (typically working on the device in my office) and it worked perfectly. Brought it back, under the office network and same issue reappears.
I've tried multiple networks, even a local (no internet access) router with just about ever security measure disabled and all ports open, with absolutely no effect. I've used an ethernet shield plugged into the network and it hosts fine, and I've used other Arduino's. I feel this has narrowed the problem down to the Router setup more than any other source, or atleast a Router/WiFi Shield comms issue.
Rokmonkey:
I've tried multiple networks, even a local (no internet access) router with just about ever security measure disabled and all ports open, with absolutely no effect.
...
I've used an ethernet shield plugged into the network and it hosts fine, and I've used other Arduino's. I feel this has narrowed the problem down to the Router setup more than any other source, or atleast a Router/WiFi Shield comms issue.
Are you saying, it works at home but not at the office, even after trying multiple routers and multiple security settings at the office? I.e. The problem appears to be location specific.
If so, could you have a local interference problem? For example, some non-WiFi device using the 2.4Ghz ISM band. Keep in mind the RSSI number only tells you the signal strength and says nothing of noise or transient interference.
Wireless (analogue) CCTV cameras are a bugger, as each camera tramples on 1/4 of the band (~3 WiFi channels) .
I briefly considered that, and switched WiFi channels. There are many wireless routers around since the office is in an industrial park. I have only issues with the WiFi Shield though, no other devices. Is the WiFi shield unique in that regard where it can be overpowered by all other devices?
If it is an interference problem what solutions may exist to solve it? Any advice in that regard?