Question

This code sends data to my server, where it is being get with php and sent to mysql. The main problem is, that I use cycle void loop(), but it does its task only once. If I will reset the Arduino, then it sends data twice and so on.. I have tried to to send multiple data with delays without a cycle, but it repeatedly sends data, that was send first.. Help :)

#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>        //Including libraries

int moi_int = 12;
int temp_int = 13;                         //doing some convertion                                   
int moi_int1 = moi_int %10;                                                                                   
int moi_int2 = moi_int - moi_int1;
int moi_int3 = moi_int2/10;
char moi1 = (char)(((int)'0')+moi_int1);
char moi2 = (char)(((int)'0')+moi_int3);
 int temp_int1 = temp_int %10;                                                                                   
int temp_int2 = temp_int - temp_int1;
int temp_int3 = temp_int2/10;
char temp1 = (char)(((int)'0')+temp_int1);     //convertion
char temp2 = (char)(((int)'0')+temp_int3);     

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   
byte server[] = { my server };    
IPAddress ip(my ip);
EthernetClient client;


void setup() {
  Serial.begin(9600);  
}




void SendInfo()     //Info
{
     char string[29];        //Here is displayed by char some adress (data) I need to send    

 string[0] = 'G';  string[1] = 'E'; string[2] = 'T';     string[3] = ' ';     string[4] = '/';     string[5] = 'a';    string[6] = 'd';     string[7] = 'd';
     string[8] = '.';     string[9] = 'p';     string[10] = 'h';     string[11] = 'p';     string[12] = '?';     string[13] = 't';     string[14] = 'e';     string[15] = 'm';
     string[16] = 'p';     string[17] = '1';     string[18] = '=';     string[19] = temp2;     string[20] = temp1;     string[21] = '&';     string[22] = '&';     
     string[23] = 'm';     string[24] = 'o';     string[25] = 'i';     string[26] = '1';     string[27] = '=';     string[28] = moi2;     string[29] = moi1; 

     String adresas(string);    
     client.println(adresas);        //   Sending Data 
     client.print(" HTTP/1.1");
     client.println( "Host: my server" );
     client.println("Connection: close");
     client.println();  
}



void Connect()              //Connecting
{
  Ethernet.begin(mac, ip);
  delay(1000);
  if (client.connect(server, 80)) {                      
    delay(1000);
    SendInfo();
    delay(1000);
  }
  else  Serial.println("connection failed");  
}


void loop()              //Main Loop
{ 
Connect();                
}
Was it helpful?

Solution

You should call begin() only once in the setup:

void setup() {
  ...
  Ethernet.begin() 

You call

client.connect(server, 80)

but never a matching disconnect. You need to add one of these to cleanup the connection:

if (client.connect(server, 80)) {                      
  ...
  client.stop();
}

An alternate approach is to re-user the previously opened connection on each loop.

// open the connection if needed
if(!client.connected()) {
  client.connect(server, 80);
}

// ensure connection is available before trying to write
if(client.connected()) {
  // do something with connection
}

If you are going to be transmitting very frequent small data, then this second approach is better because less time is wasted to the open/close of the connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top