Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: Ethernet Shield V3 - still freezing (4 sockets limit? client.stop() problem?) on: January 04, 2013, 04:21:55 am
Uh, his is a good point.. and a stupid error due to first time programming for HTTP communication!
I was sure that since I was pushing some variables, the server did not have to answer.. instead the GET method requires an answer in any case.
So, adding the reading of the answer is better.. fewer or no errors.

Sometimes (some hours) it still stops working, but I think it is related to some out-of-memory issue (my real code is about 20K.. there are a lot of debugging checks and Serial.prints). I'll try to reduce it and to increase the delay between two server requests and see if the code remains stable for some days..

Thank you Surfer and other people, it was an honor to have prompt answers from all of you!
(.. and Happy New Year)
2  Using Arduino / Networking, Protocols, and Devices / Re: Bluetooth: Android->Arduino wrong characters transmitted on: January 03, 2013, 05:31:26 am
Hello, I'm not sure, but maybe the condition "\n" is not correct for Char...

Try "NULL" or "\0"...

See the example Str3 in:
http://arduino.cc/en/Reference/String
3  Using Arduino / Networking, Protocols, and Devices / Re: Ethernet Shield V3 - still freezing (4 sockets limit? client.stop() problem?) on: January 03, 2013, 04:23:42 am
This is the code, sorry it's not very elegant and can be shortened a lot.

Basically, I took the code from http://arduino.cc/en/Tutorial/WebClientRepeating.
Then I perform 2 GET:
1. the first send sensors reading
2. the second reads a TXT file containing 2 values for switching on/off 2 leds. I took the method here http://bildr.org/?s=client and improved the readPage() function with SurferTim code.
TXT file is a string like <01> (= 4 chars). Each number represents the LED status (0 or 1).
The < and > are used to locate and extract the values, because the answer from server is about 120 chars (contains also headers).

Code:
// ---------------- IMPORT ------------------------------------
#include <SPI.h>
#include <Ethernet.h>

// ----------------- VARIABLES DEFINITIONS --------------------
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// variables used by millis
unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000;   // time to wait before repeating connections

// variables for connection
char serverName[] = "myserver.example.org";
EthernetClient client;


// GET #2
String readValue = "";   // String to store the readPage() function result

// variables for sensors
const int tempPin = A0;   // pin where temp sensors is connected
const int lightPin = A1;   // pin where light sensor (photoresistor) is connected
const int ledPinR = 6;   // pin of RED led (LED 1)
const int ledPinW = 2;   // pinf of WHITE led (LED 2)

int tempRead = 0;                              // value read by temperature sensor (0-1023)
float voltage = 0.0;   // voltage of the temp sensor
float temp = 0.0;   // calculated value of the temperature (degrees °C)
int light = 0;   // value read by light sensor
int LED1 = 0;   // value to assign to LED 1
int LED2 = 0;   // value to assign to LED 2



// -----------------SETUP --------------------------------------------
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  pinMode (4, OUTPUT);                  //disable SD card slot on ethernet shield
  digitalWrite (4, HIGH);
  pinMode (10, OUTPUT);                  //explicity enable ethernet
  digitalWrite (10, LOW);
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;}
 
  // give the Ethernet shield 1 seconds to initialize, then write debug info:
  delay(1000);
  Serial.print("Ethernet begins.. ");
  Serial.println(Ethernet.localIP());
  
  // initializes LEDs
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinW, OUTPUT);
  digitalWrite(ledPinR, LOW);
  digitalWrite(ledPinW, LOW);
}

// -----------------------------LOOP---------------------
void loop() {

// HTTP REQUEST  
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {

  // Read and calculate temp
      tempRead = analogRead(tempPin);
      voltage = (tempRead/1024.0) * 5.0;
      temp = ((voltage - 0.5) * 100.0);
  // Read Light
      light = analogRead(lightPin);
      
// -------------------------- GET #1 -----------------------------------------------------
// --------------------------(sends sensors and errors status to server) -----------------
// ---------------------------------------------------------------------------------------

  if (client.connect(serverName, 80)) {
    
    Serial.println("Connected - GET #1");
  
    client.print("GET /sensors.php?temp=");client.print(temp);
    client.print("&light=");client.print(light);
    client.println(" HTTP/1.1");
    
    client.println("Host: myserver.example.org");
    client.println("Connection: close");
    client.println();
    client.stop();
    delay(50);

    Serial.println("GET #1 - disconnecting.");
    } else {
    // if you didn't get a connection to the server:
    Serial.println("GET #1 - connection failed");
}
//  ------------------------- End of GET #1
//
//      
// -------------------------- GET #2 --------------------------------------------------
// -------------------------- (reads the values to be assigned to the LEDs ------------    
// ------------------------------------------------------------------------------------
  
    if (client.connect(serverName, 80)) {
    
    Serial.println("Connected - GET #2");

    client.println("GET /led.txt HTTP/1.1");
    client.println("Host: myserver.example.org");
    client.println("Connection: close");
    client.println();
    client.stop();
    delay(50);
 
// read the TXT file
    readValue = readPage(); // Call to readPage(): get the values extracted from led.txt file

// assign the LEDs values
LED1 = readValue[0]-'0';
LED2 = readValue[1]-'0';
digitalWrite(ledPinR,LED1);
digitalWrite(ledPinW,LED2);

  } else {
    // if you didn't get a connection to the server:
    Serial.println("GET #2 - connection failed");
    client.stop();
  }
// ------------------------------- End of GET #2 cycle

// note the time that the connection was made:
  lastConnectionTime = millis();
  delay(1000);
//
// store the state of the connection for next time through
// the loop:
  lastConnected = client.connected();

  } // Close HTTP REQUEST
} // Close void LOOP


// ----------------- FUNCTIONS -----------------------------

String readPage(){
// This function
// 1. read a TXT file in the form "<xy>" and
// 2. extract xy and return them (xy can be "00", "01", "10" or "11")
// 3. or return an error code (example "88") in case reads NULL values

// connectLoop controls the hardware fail timeout
  String inString;
  int connectLoop = 0;
  int stringPos = 0;
  
  Serial.println("Read starting...");
  while(client.connected())
  {
    while(client.available())
    {
      char inChar = client.read();
      delay(1);
      inString = inString + inChar;
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();
  Serial.println("Read finished.");
  // close client
  client.stop();
  delay (1);
  
  // extract and return valid LED values ("00", "01", "10" or "11") or "Error"
  if (inString.length() != 0){ // if inString is not null
  int a = inString.indexOf('<'); // find the position of "<"
  int b = inString.indexOf('>'); // find the position of ">"
  String extracted = inString.substring(a+1,b); // extract the text between "<" and ">"
  delay(1000);

  return extracted;     // return valid values
  } else {return "88"}                     // return some error code
}
4  Using Arduino / Networking, Protocols, and Devices / Re: Making internet interaction universal on: January 01, 2013, 09:15:26 pm
Typically you have:

Code:
INTERNET
|
|
___________________________________________
|    external ip (example): 252.66.75.43   |
|                                          |
|    YOUR ROUTER                           |
|                                          |
|   manages internal ip ranges like        |
|   192.168.1.1 - 192.168.255.255          |
--------------------------------------------
|       |          |              |   
|     PC#1        PC#2      Personal Computer#3
|     
ARDUINO server
(internal/local IP: 192.168.2.2 port 80)
Then you need to:
1. From a PC, enter into the router (usually 192.168.1.1) and configure it to forward the (internal) port 80 of the address 192.168.2.2 (Arduino) to the 80 (external) port. (You can also map the internal 80 to another external port, for example 8080, 65000, ...)
2. Access Arduino from INTERNET using the external address of your router (example): 252.66.75.43:80 (or 252.66.75.43:8080, or 252.66.75.43:65000, using the port configured in point 1)

NOTE: since the (external) address of the router could change in the time, most router have a function called DDNS (Dynamic DNS.. wikipedia), where you can link your (changing) current router external address to a fixed web address. Try services like tzo, no-ip or ddns.org.
In this case, the point 2 becomes:

2.b Access your Arduino from an address like: www.youraddressname.ddns.org:80 (or :8080, or :65000, ... as in point 1)

Bye!

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
5  Using Arduino / Networking, Protocols, and Devices / Re: Ethernet Shield V3 - still freezing (4 sockets limit? client.stop() problem?) on: January 01, 2013, 08:49:17 pm
Just a quick update:
Many thanks for your examples. I'm using them to debug more and trying new things.
My general impression for now is that the shields functions, but freezes often when reading data (a text file) from the server (client.read()).
I'll post more later.. bye!
6  Using Arduino / Networking, Protocols, and Devices / Re: Cannot get Ethernet Shield to work on: January 01, 2013, 05:34:38 am
Hello, how is the setup of your router DHCP?

- Is it enabled?

- Is there a MAXIMUM number of addresses configured (and already in use)? (some router have default values like 10, 50, ..). Increase it..
   (linksys example: http://www.404techsupport.com/wp-content/uploads/2008/11/linksys2.jpg)

- Do you see you Arduino in the list of DHCP clients?
   (linksys example: http://www.flickr.com/photos/ebothy/4445697129/in/set-72157623652768126/)

Try to find the equivalent info of linksys screenshots on your 2wire router to check..
7  Using Arduino / Networking, Protocols, and Devices / Ethernet Shield V3 - still freezing (4 sockets limit? client.stop() problem?) on: January 01, 2013, 05:22:52 am
Hello, I'm new here (sorry, I didn't present myself yet.. maybe later..).

Setup:
Arduino UNO v.3 (IDE 1.0.3) + Ethenet Shield v.3 - [programmed as simple client that reads a txt file on a webserver]

Problem:
It's 2012 2013  smiley  and I still see my shield (programmed as client) functioning well but then freezing after some time while reading a txt file from a web server (GET method).
It seems that it is related to the 4 sockets limit and the client.stop() function not working, so that sockets are not closed poperly.

I tried to follow all the several methods described in the forum, but they did not seem to work:
- disable SD pin 4 (HIGH)
- delays everywhere
- flushing everywhere
- checking client.connected();
- checking client.status();
- relaunching Ethernet.begin(mac) ..

These are old 2009-2011 advices, some of them are outdated or already included in the Libraries (SurferTim patch, ...).

So my question(s):
What is the state or the art? Is the shield working for you?
Is the client.stop() - socket disconnession solved or still an issue?

Thanks to all. Maybe I'll post the code also..
Pages: [1]