Arduino project - connecting to server followed by immediate disconnect

Discussion in 'Programmer's Corner' started by Elektrishun, Feb 5, 2017.

  1. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    I have been attempting to get this Arduino Wifi project working:

    https://openhomeautomation.net/monitor-your-home-remotely-using-the-arduino-wifi-shield/

    I am new to programming micro-controllers but I feel this project is close to being successfull. The gist of it is that I can use my Arduino Uno (Rev 3) and Wifi Shield to monitor a door alarm. With the Arduino connected to my wireless network I should be able to use the board's IP address with a web browser (I have mainly used Chrome but tried Firefox, too) and see whether the door is opened or closed. The main issue is that I have only successfully done this once in numerous attempts (I seen the message "Alert! The door has been opened" for a brief second or two using Chrome web browser).

    I believe that the main issue is that I am connecting and then disconnecting almost immediately from the server. But why? The one time I had that momentary success I had followed advice on a website that suggested that I go to Programs -> Java -> Temporary Internet Files -> Settings -> Restore Defaults -> Delete Files. That hasn't yielded the same positive results since.

    I am using Windows 7 Professional with Arduino IDE 1.0.2 (I have read in numerous threads that this version works best with the Wifi Shield and I've already gone the other route with updating the IDE and Wifi firmware with overall less connection success despite a successful updating of the firmware).

    Any suggestions would be greatly appreciated.
     
  2. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    I used this Arduino Wifi sample code which is similar to the other code in what it attempts to do:

    https://www.arduino.cc/en/Tutorial/WiFiWebServer

    The code works fine. In the Programmer's Serial Monitor I see the following:

    Attempting to connect to SSID: "my network name"
    SSID: "my network name"
    IP Address: ###.###.#.##
    signal strength (RSSI):-83 dBm


    But when I use the Arduino Wifi Shield's IP address in Chrome the Programmer's Serial monitor it continues with:

    new client
    yclient disconnected
    new client
    GETclient disconnected
    new client
    client disconnected

    Edit: Fixed above link
     
    Last edited: Feb 5, 2017
  3. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,582
    518
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    Ethernet is not a stateful protocol. What you are seeing is exactly what is expected, depending on what packets the code communicates.

    Why your message disappears depends on your code. Instead of posting what the code is based on, post your code

    (use CODE tags,
    "[ CODE]" and "[ /CODE]".
    No spaces)
     
  5. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    This is the code for the WiFi Web Server from the Arduino WiFi Shield examples page:

    Code (C):
    1. /*
    2. WiFi Web Server
    3.  
    4. A simple web server that shows the value of the analog input pins.
    5. using a WiFi shield.
    6.  
    7. This example is written for a network using WPA encryption. For
    8. WEP or WPA, change the Wifi.begin() call accordingly.
    9.  
    10. Circuit:
    11. * WiFi shield attached
    12. * Analog inputs attached to pins A0 through A5 (optional)
    13.  
    14. created 13 July 2010
    15. by dlf (Metodo2 srl)
    16. modified 31 May 2012
    17. by Tom Igoe
    18.  
    19. */
    20.  
    21. #include <SPI.h>
    22. #include <WiFi.h>
    23.  
    24.  
    25. char ssid[] = "yourNetwork"; // your network SSID (name)
    26. char pass[] = "secretPassword"; // your network password
    27. int keyIndex = 0; // your network key Index number (needed only for WEP)
    28.  
    29. int status = WL_IDLE_STATUS;
    30.  
    31. WiFiServer server(80);
    32.  
    33. void setup() {
    34. //Initialize serial and wait for port to open:
    35. Serial.begin(9600);
    36. while (!Serial) {
    37. ; // wait for serial port to connect. Needed for Leonardo only
    38. }
    39.  
    40. // check for the presence of the shield:
    41. if (WiFi.status() == WL_NO_SHIELD) {
    42. Serial.println("WiFi shield not present");
    43. // don't continue:
    44. while(true);
    45. }
    46.  
    47. // attempt to connect to Wifi network:
    48. while ( status != WL_CONNECTED) {
    49. Serial.print("Attempting to connect to SSID: ");
    50. Serial.println(ssid);
    51. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    52. status = WiFi.begin(ssid, pass);
    53.  
    54. // wait 10 seconds for connection:
    55. delay(10000);
    56. }
    57. server.begin();
    58. // you're connected now, so print out the status:
    59. printWifiStatus();
    60. }
    61.  
    62.  
    63. void loop() {
    64. // listen for incoming clients
    65. WiFiClient client = server.available();
    66. if (client) {
    67. Serial.println("new client");
    68. // an http request ends with a blank line
    69. boolean currentLineIsBlank = true;
    70. while (client.connected()) {
    71. if (client.available()) {
    72. char c = client.read();
    73. Serial.write(c);
    74. // if you've gotten to the end of the line (received a newline
    75. // character) and the line is blank, the http request has ended,
    76. // so you can send a reply
    77. if (c == '\n' && currentLineIsBlank) {
    78. // send a standard http response header
    79. client.println("HTTP/1.1 200 OK");
    80. client.println("Content-Type: text/html");
    81. client.println("Connection: close"); // the connection will be closed after completion of the response
    82. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
    83. client.println();
    84. client.println("<!DOCTYPE HTML>");
    85. client.println("<html>");
    86. // output the value of each analog input pin
    87. for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    88. int sensorReading = analogRead(analogChannel);
    89. client.print("analog input ");
    90. client.print(analogChannel);
    91. client.print(" is ");
    92. client.print(sensorReading);
    93. client.println("<br />");
    94. }
    95. client.println("</html>");
    96. break;
    97. }
    98. if (c == '\n') {
    99. // you're starting a new line
    100. currentLineIsBlank = true;
    101. }
    102. else if (c != '\r') {
    103. // you've gotten a character on the current line
    104. currentLineIsBlank = false;
    105. }
    106. }
    107. }
    108. // give the web browser time to receive the data
    109. delay(1);
    110.  
    111. // close the connection:
    112. client.stop();
    113. Serial.println("client disonnected");
    114. }
    115. }
    116.  
    117.  
    118. void printWifiStatus() {
    119. // print the SSID of the network you're attached to:
    120. Serial.print("SSID: ");
    121. Serial.println(WiFi.SSID());
    122.  
    123. // print your WiFi shield's IP address:
    124. IPAddress ip = WiFi.localIP();
    125. Serial.print("IP Address: ");
    126. Serial.println(ip);
    127.  
    128. // print the received signal strength:
    129. long rssi = WiFi.RSSI();
    130. Serial.print("signal strength (RSSI):");
    131. Serial.print(rssi);
    132. Serial.println(" dBm");
    133. }
     
    Last edited: Feb 5, 2017
  6. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    Sigh. It is hard to help when you cannot read a post.

    First, the code tags have NO SPACES.

    Second, example code is useless. We need to see the code that you modified.

    Note: TS fixed code tags
     
    Last edited: Feb 5, 2017
  7. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    The example code is what I used to demonstrate what was happening. There was no modification other than inserting my network name and password where it was required.
     
  8. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    This is the code from the project that I linked to in post #1:

    Code (C):
    1.  
    2. #include <SPI.h>
    3. #include <WiFi.h>
    4.  
    5. char ssid[] = "yourSSID"; // your network SSID (name)
    6. char pass[] = "yourPass"; // your network password
    7.  
    8. int status = WL_IDLE_STATUS;
    9.  
    10. boolean door_status = false;
    11.  
    12. WiFiServer server(80);
    13.  
    14. void setup() {
    15. // start serial port for debugging purposes
    16. Serial.begin(9600);
    17.  
    18. // Attach interrupt to pin 2
    19. attachInterrupt(0, setDoorStatus, RISING);
    20.  
    21. // attempt to connect to Wifi network:
    22. while ( status != WL_CONNECTED) {
    23. Serial.print("Attempting to connect to SSID: ");
    24. Serial.println(ssid);
    25. status = WiFi.begin(ssid, pass);
    26. // wait 10 seconds for connection:
    27. delay(10000);
    28. }
    29. server.begin();
    30. // you're connected now, so print out the status:
    31. printWifiStatus();
    32. }
    33.  
    34.  
    35. void loop() {
    36.  
    37. // listen for incoming clients
    38. WiFiClient client = server.available();
    39. if (client) {
    40. Serial.println("new client");
    41. // an http request ends with a blank line
    42. boolean currentLineIsBlank = true;
    43. while (client.connected()) {
    44. if (client.available()) {
    45. char c = client.read();
    46. Serial.write(c);
    47. // if you've gotten to the end of the line (received a newline
    48. // character) and the line is blank, the http request has ended,
    49. // so you can send a reply
    50. if (c == '\n' && currentLineIsBlank) {
    51. // send a standard http response header
    52. client.println("HTTP/1.1 200 OK");
    53. client.println("Content-Type: text/html");
    54. client.println("Connnection: close");
    55. client.println();
    56. client.println("<!DOCTYPE HTML>");
    57. client.println("<html>");
    58. client.println("<meta http-equiv=\"refresh\" content=\"5\">");
    59.  
    60. if (door_status == false){
    61. client.print("Everything is ok");
    62. }
    63. else {
    64. client.print("Alert ! The door has been opened");
    65. }
    66. client.println("<br />");
    67. client.println("</html>");
    68. break;
    69. }
    70. if (c == '\n') {
    71. // you're starting a new line
    72. currentLineIsBlank = true;
    73. }
    74. else if (c != '\r') {
    75. // you've gotten a character on the current line
    76. currentLineIsBlank = false;
    77. }
    78. }
    79. }
    80. // give the web browser time to receive the data
    81. delay(1);
    82. // close the connection:
    83. client.stop();
    84. Serial.println("client disonnected");
    85. }
    86. }
    87.  
    88.  
    89. void printWifiStatus() {
    90. // print the SSID of the network you're attached to:
    91. Serial.print("SSID: ");
    92. Serial.println(WiFi.SSID());
    93.  
    94. // print your WiFi shield's IP address:
    95. IPAddress ip = WiFi.localIP();
    96. Serial.print("IP Address: ");
    97. Serial.println(ip);
    98.  
    99. // print the received signal strength:
    100. long rssi = WiFi.RSSI();
    101. Serial.print("signal strength (RSSI):");
    102. Serial.print(rssi);
    103. Serial.println(" dBm");
    104. }
    105.  
    106. void setDoorStatus() {
    107. door_status = true;
    108. }
    109.  
     
  9. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    Ok, I see where you are setting the status of the door. The only place where this occurs is in your interrupt handler. Now, we need to verify that the interrupt routine is being triggered. To do that, for a moment we have to look at the hardware.

    Please post a schematic of everything connected to pin 2. You may think this is an irrelevant request, but please humor me.

    Thanks.
     
  10. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    This is the schematic I used which is posted on the page with the project:

    [​IMG]

    Edit: This is probably insignificant but the project designer shows the Uno board in his schematic. I have the Wifi Shield connected atop the Uno board when I connect this circuit to the Wifi Shield.
     
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    Ok. At this point (or before), I would write a test sketch to verify that the interrupt is working. In setup, configure you serial output and setup the interrupt routine.

    In the void function, I would print out a simple message to confirm that you entered the function.

    Then, go into a do nothing loop.

    Your interrupt routine should print out a message when executed.

    Make sure that this works.
     
  12. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    Yes, in the project steps the author has this exact step which I completed successfully before moving on:

    "Finally, let’s test the push button. We want to detect the state of the button, so we will use the function digitalRead(). Here is the sketch to test the button:"

    Code (Text):
    1.  
    2. // Push button pin
    3. int pushButton = 2;
    4.  
    5. // Setup
    6. void setup() {
    7. Serial.begin(9600);
    8. }
    9.  
    10. // Main loop
    11. void loop() {
    12.  
    13. // read the pin number 2
    14. int buttonState = digitalRead(pushButton);
    15.  
    16. // print out the state of the button
    17. Serial.println(buttonState);
    18. delay(1);
    19. }
    20.  
    "Now upload the sketch, and open the serial monitor. You should see a lot of zeros, which is logical because the pin number 2 is connected to the ground. Then, just push the button, and if you see ones like on the following screenshot, it is all good."
     
  13. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    I think I have noticed something. You haven't initialized pin 2. Before your statement to attach the interrupt, you need to initialize the pin. Use this line of code:
    pinMode(2, INPUT);​
    Try that.
     
  14. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    I must be doing something wrong - getting error "stray '\' in program".

    Where exactly was I supposed to insert that line of code?
     
  15. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    Insert the line before you call the attachInterrupt function.

    Are you using the Arduino IDE? If so in the bottom window, where I expect you are getting that error message, should indicate the line with the "stray" error. There is a button in the IDE that will copy all errors to the clipboard. Then you can paste them into an editor to get a better look. I'll be honest, never saw that "stray" error.
     
  16. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    Not sure why it never compiled without error before but this time it did so without error.

    Code (Text):
    1.  
    2. #include <SPI.h>
    3. #include <WiFi.h>
    4.  
    5. char ssid[] = "yourNetwork";     //  your network SSID (name)
    6. char pass[] = "secretPassword";    // your network password
    7.  
    8. int status = WL_IDLE_STATUS;
    9.  
    10. boolean door_status = false;
    11.  
    12. WiFiServer server(80);
    13.  
    14. void setup() {
    15.   // start serial port for debugging purposes
    16.   Serial.begin(9600);
    17.   pinMode(2, INPUT);
    18.  
    19.   // Attach interrupt to pin 2
    20.   attachInterrupt(0, setDoorStatus, RISING);
    21.  
    22.   // attempt to connect to Wifi network:
    23.   while ( status != WL_CONNECTED) {
    24.     Serial.print("Attempting to connect to SSID: ");
    25.     Serial.println(ssid);
    26.     status = WiFi.begin(ssid, pass);
    27.     // wait 10 seconds for connection:
    28.     delay(10000);
    29.   }
    30.   server.begin();
    31.   // you're connected now, so print out the status:
    32.   printWifiStatus();
    33. }
    34.  
    35.  
    36. void loop() {
    37.        
    38.   // listen for incoming clients
    39.   WiFiClient client = server.available();
    40.   if (client) {
    41.     Serial.println("new client");
    42.     // an http request ends with a blank line
    43.     boolean currentLineIsBlank = true;
    44.     while (client.connected()) {
    45.       if (client.available()) {
    46.         char c = client.read();
    47.         Serial.write(c);
    48.         // if you've gotten to the end of the line (received a newline
    49.         // character) and the line is blank, the http request has ended,
    50.         // so you can send a reply
    51.         if (c == '\n' && currentLineIsBlank) {
    52.           // send a standard http response header
    53.           client.println("HTTP/1.1 200 OK");
    54.           client.println("Content-Type: text/html");
    55.           client.println("Connnection: close");
    56.           client.println();
    57.           client.println("<!DOCTYPE HTML>");
    58.           client.println("<html>");
    59.           client.println("<meta http-equiv=\"refresh\" content=\"5\">");
    60.      
    61.             if (door_status == false){
    62.               client.print("Everything is ok");
    63.             }
    64.             else {
    65.                client.print("Alert ! The door has been opened");
    66.             }  
    67.           client.println("<br />");
    68.           client.println("</html>");
    69.            break;
    70.         }
    71.         if (c == '\n') {
    72.           // you're starting a new line
    73.           currentLineIsBlank = true;
    74.         }
    75.         else if (c != '\r') {
    76.           // you've gotten a character on the current line
    77.           currentLineIsBlank = false;
    78.         }
    79.       }
    80.     }
    81.     // give the web browser time to receive the data
    82.     delay(1);
    83.       // close the connection:
    84.       client.stop();
    85.       Serial.println("client disonnected");
    86.   }
    87. }
    88.  
    89.  
    90. void printWifiStatus() {
    91.   // print the SSID of the network you're attached to:
    92.   Serial.print("SSID: ");
    93.   Serial.println(WiFi.SSID());
    94.  
    95.   // print your WiFi shield's IP address:
    96.   IPAddress ip = WiFi.localIP();
    97.   Serial.print("IP Address: ");
    98.   Serial.println(ip);
    99.  
    100.   // print the received signal strength:
    101.   long rssi = WiFi.RSSI();
    102.   Serial.print("signal strength (RSSI):");
    103.   Serial.print(rssi);
    104.   Serial.println(" dBm");
    105. }
    106.  
    107. void setDoorStatus() {
    108.   door_status = true;
    109. }
    110.  
    After uploading the output in the Arduino IDE serial monitor was consistent. Once I tried connecting by entering the Sheild's IP address in Chrome's web browser it did the same as before:
    "new client"
    "client disconnected"
     
    Last edited: Feb 6, 2017
  17. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    I have told you in post #4 why that happens. Sigh.

    Does the alarm code work regardless?
     
  18. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    No.
    The serial monitor shows that I tried connecting when entering the IP address in Chrome but the page itself shows following:

    [​IMG]

    There was only one occasion where I got a brief glimpse of the alarm code before seeing the above result directly after.
     
  19. Elektrishun

    Thread Starter Member

    Dec 27, 2016
    47
    5
    Last edited: Feb 5, 2017
  20. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,233
    1,055
    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!
     
Loading...