Arduino project - connecting to server followed by immediate disconnect

djsfantasi

Joined Apr 11, 2010
9,237
I agree. But the sketch is NOT correctly performing as a web server.

I can tell where one problem lies because your browser says "192.168.0.23 did not return any data"

The sketch has several lines where it sends a "standard http response header". But your browser is saying it never gets it.

That's why I suggested inserting the serial print to indicate on the console that it is trying to send the response.

The other thing I thought of is related to the IP address. Where did you get 192.168.0.23? I haven't used the WiFi shield, but am certified in networking. If the shield gets its address with DHCP, it can change over time. I did not see a call to either set or get the IP address, hence my question.
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
This is what the author of the project wrote which is what I followed:

"These are the important parts of the code, as usual you can find the full code in our GitHub repository. Now, just upload the final code into the board, and wait until the “link” LED turns green on the board. Now, remember when I told you to write down the shield’s IP address ? This is where you will need it. Just open your favorite browser, and type down this IP into the URL field. This is what you should see:"
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
There is an earlier step where you are supposed to run a sketch for WPA networks and use that information for what follows. That is where I got my IP address for the Arduino Shield (nonetheless, everytime I run the the final code for the project I see the IP address after connection to my wireless network has been confirmed).

This is the WPA code:

C:
/*

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[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // 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();
}
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
What is your problem? I see two. One, you cannot connect to your PC. Second, your alarm code does not work. Is this correct?

I have been addressing the latter.

For the former, I will make some general comments. I can't see yet why you are not sending the HTTP response. I had a couple of thought, but couldn't support them. I might insert a serial.print immediately before you send the HTTP response.

Your screen shot indicates that the browser is not receiving the HTTP response. Let's find out why!
Yes, using the IP address as the author suggests failed every time except for one brief moment where I did in fact see alarm code message.
 

djsfantasi

Joined Apr 11, 2010
9,237
Ok, so another silly question. You HAVE replaced the ssid and password with your own values? I have assumed so, because I would not post the real values for my network. But, just checking.

Can you log into your router and find out the actual IP address assigned to the shield?
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
Yes, I have logged into my router and verified the Arduino WiFi Shield's IP address and MAC address.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
Between lines #51 and #52. Right after the following line:
Code:
 // send the standard http response header
Insert something like this:
Code:
serisl.println("sending response header")
That way, when you type in the IP address. In your browser, we can confirm that the code to send the standard HTTP header is. being executed.

If the code is being executed and the browser says there is no response, then we have to look at the functions used to send the header. But I am getting ahead of myself.
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
Between lines #51 and #52. Right after the following line:
Code:
 // send the standard http response header
Insert something like this:
Code:
serisl.println("sending response header")
That way, when you type in the IP address. In your browser, we can confirm that the code to send the standard HTTP header is. being executed.

If the code is being executed and the browser says there is no response, then we have to look at the functions used to send the header. But I am getting ahead of myself.
I am at work today but I will definitely pursue this and get back with my results A.S.A.P.

Thank-you for your help.
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
Added your code as suggested and got this error:

'serial' was not declared in this scope

Home_Door_Alarm.ino In function 'void loop()':
Home_Door_Alarm:52 error:'serial' was not declared in this scope
 

djsfantasi

Joined Apr 11, 2010
9,237
Three comments...

Try capitalizing "Serial".

Did you put a period between "Serial" and "println"?

I noticed that I also did not end the line with a semi-colon. One is required.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
An added thought. What is your programming experience? Do you have any networking experience? If so, please describe.

It is difficult troubleshooting from afar and made more so depending on the TS' programming experience. If so, please be patient with the process. Someone with experience could have noticed that this statement syntax is used elsewhere in the sketch and used that information to resolve the error.
 

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
After making the corrections the code compiled correctly. The following is where and how I inserted the code:

Code:
// send a standard http response header
          Serial.println("sending response header");
          client.println("HTTP/1.1 200 OK");
After uploading the code the Arduino IDE serial monitor showed the following:

Attempting to connect to SSID: "my network"
SSID: "secret password"
IP Address: ###.###.#.#
signal strength (RSSI):-81 dBm


Then I tried connecting using the IP address in Chrome web browser and as usual it failed to connect and showed the following:


This site can’t be reached

###.###.#.# took too long to respond.
Did you mean http://###.###.#.#.ipaddress.com/?
ERR_CONNECTION_TIMED_OUT


The difference from previous attempts was that this time the Arduino IDE serial monitor did not show any more information such as "connecting to server", "new client", "client disconnected". Remained blank after previously mentioned information.
 
Last edited:

Thread Starter

Elektrishun

Joined Dec 27, 2016
66
An added thought. What is your programming experience? Do you have any networking experience? If so, please describe.

It is difficult troubleshooting from afar and made more so depending on the TS' programming experience. If so, please be patient with the process. Someone with experience could have noticed that this statement syntax is used elsewhere in the sketch and used that information to resolve the error.
I have almost no coding experience. Years ago I played a bit with Ruby on Rails and I worked with someone like yourself to help me get a version of Linux (Ubuntu) running on an old Apple eMac. I'm interested in learning but up until recently time was never at a surplus.
BTW, the Linux/eMac project took quite some time so if nothing else I am persistent. I was working on trying to get this Arduino WiFi Shield working over a year ago using the Mac version. After recently purchasing my first PC in years (forever been using Apple products) I decided to take another shot at it using Windows 7 OS. I've spent a lot of late nights trying to get this working, scouring the internet for tips and hints, until finally I decided it was time to get help directly.
 

djsfantasi

Joined Apr 11, 2010
9,237
Ok, if the message you added did not appear in the console, it tells me a couple of things.

One, for some reason, the sketch did not recognize the data request. Without recognizing the request, the handshaking will not complete. The PC browser is expecting the output from the "client.println" statements following the debugging statement we inserted.

The simplistic logic says that it has received a request when the last character is a '\n' and the last line was a blank one. Given that it is likely the last character is a new line, I would look carefully at how and when the Boolean is set.
 
Top