Arduino posting Data to mySQL via php script on Remote server

I am attempting to pst data to a mySQL database on my remote server via a php script I wrote.

When I call the script via: "www.***.com/add.php?=Data1=47&Data2=49" then it posts to my database correctly.

Now I need my Arduino to do this via that script??? I've read numerous ways to do it but none have worked for me.

Below is my Setup section:

void setup() {

// Open serial communications and wait for port to open:
  Serial.begin(9600);
   Serial.println("");  //Blank Line
   Serial.println("Gaines' Arduino Chicken Coop Controler!");
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    
// Start up the Sensor library
  sensors.begin(); 
  }
// start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  
  Serial.print("Controller is at: ");
  Serial.println(Ethernet.localIP());

// Setup Outputs
  pinMode(waterPump, OUTPUT);      
  pinMode(rainBarrel, OUTPUT); 
  pinMode(ventFan, OUTPUT);
  pinMode(heatLamp, OUTPUT); 
  pinMode(motor1, OUTPUT); 
  pinMode(motor2, OUTPUT); 
  pinMode(feedSensorA, OUTPUT); 
  pinMode(feedSensorB, OUTPUT);
  
// Setup Inputs
  pinMode(watererLevel, INPUT);
  pinMode(walkDoor, INPUT);
  pinMode(chickenDoor, INPUT);
  pinMode(photoCell, INPUT);
  pinMode(barrelLevel, INPUT_PULLUP);
  
  data = "";   //Blanks out Data String
  
  Serial.println("Setup Complete");
  Serial.println("");
  
  
} //Closes Setup

Below is the ethernet section of my code:

void sendData()  {      //ETHERNET Stuff
  
  //  Set Data String to send to PHP Script 
    data ="iTemp=" + iTemp + "&oTemp=" + oTemp + "&lLevel=" + lLevel + "&wDoor=" + wDoor + "&cDoor=" + cDoor + "&wLevel=" + wLevel + "&wPump=" + wPump + "&rBarrel=" + rBarrel + "&vFan=" + vFan + "&hLamp=" + hLamp;
   
        if (client.connect(server, 80)) { //
          Serial.println("");
          Serial.println("Connected");
          Serial.println("Sending Data to mySQL");
         
          client.print("GET /coop/scripts/add.php?");
  	 
          // client.print(data);   // Tried this an didn't work.  Data string is above if statement.
      
          client.print("iTemp=");
          client.print( insideTemperature );
          client.print("oTemp=");
          client.print( outsideTemperature );
          client.print("&lLevel=");
          client.print( lightLevel );
          client.print("&wDoor=");
          client.print( wDoor );
          client.print("&cDoor=");
          client.print(cDoor);
          client.print("&wLevel=");
          client.print(wLevel);
          client.print("&wPump=");
          client.print(wPump);
          client.print("&rBarrel=");
          client.print(rBarrel);
          client.print("&vFan=");
          client.print(vFan);
          client.print("&hLamp=");
          client.print(hLamp);
                
          client.println(" HTTP/1.1");
	  client.println("Host: http://www.hscfire.com"); // SERVER ADDRESS 
          client.println( "Content-Type: application/x-www-form-urlencoded" );
          client.println( "Connection: close" );
    
          client.println();
          client.println();
      
      
          Serial.println("Data Sent..");
          Serial.println("");        
	 
     } // Closes If Client Connect
   else {
    Serial.println("Connection Failed");
   }
    
  if (client.connected()) { 
      client.stop();  // close the connection:
      Serial.println("Connection Closed");
      Serial.println("");
      
     }
}

And the short section above Setup that pertains to Ethernet:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,200);

char server[] = "hscfire.com"; // no www in front, is that OK?


// Start the Ethernet Client
EthernetClient client;

When the arduino runs it does all the correct Serial prints for debugging and says connected from this function but nothing is posted to my database. I'm at my wits end and can't figure this out.

I can post the complete code if needed but just alot of control stuff.

Any help is appreciated!

-Gaines

I can understand when you say you are at your wits end, I, myself went through this a while ago.

Have a look at these two snippets of code I use previously in my setup, where I was having the Arduino POST data to my host where a PHP script then took the data and placed it into a SQL database for retrieving by a web app later on.

The routines in the link are direct from my code and so use my variables, just modify to suit yourself.
The first routine, you call from main loop() at your defined set period of time, say every 5 or 15 seconds, whatevery you like.
It prepares the data into a buffer from the list of variables.
With this method you can define any format you like, whether that be JSON or a simple list.

The first routine then calls the second routine which does the actual sending of data to your host site.
There are sections commented out which you can un-comment to get feedback via your serial port from your host site to see how it if all went well.

Insert into your host details in the code where I think you will see I have placed tokens.

You will of course need the setup() and main loop() with your ethernet setup correctly to make all this work.
But, the code comes direct from my system which worked very well, until I took a different path.

On dropbox Dropbox - File Deleted - Simplify your life

PS, just noticing you mention POST in your title but from your code you seem to be using the GET method, which are you wanting to use?

I used POST previously to send data from my Arduino to the host site as it allows for larger amounts of nicely formatted data, such as JSON.

PS No 2, you will certainly have issues with this sort of code:data ="iTemp=" + iTemp + "&oTemp=" + oTemp + "&lLevel=" + lLevel + "&wDoor=" + wDoor + "&cDoor=" + cDoor + "&wLevel=" + wLevel + "&wPump=" + wPump + "&rBarrel=" + rBarrel + "&vFan=" + vFan + "&hLamp=" + hLamp
You can not simply add a constand string and an integer or any other variable like that.
Look at my code and see how to place formatted data into a char buffer.


Paul

You need to post ALL of your code. The problem may not be where you think it is.

The server is generating a response. Ignoring the response is not a good idea. Perhaps the response itself contains clues as to the problem.

Can you look at the logs on the server? Again, there may be clues.

Do you know that the script got executed? Or that it didn't? If not, why not?

You can not simply add a constand string and an integer or any other variable like that.

You can if data is an instance of the dreaded String class. Which, of course, has no place on an Arduino doing Ethernet communications among other things.

OK tried the code from rockwallaby and now it freezes when tying to form the "stringf" ??

Here is my entire code, sorry it is long as it does alot of other things:

Had to attach in file.

Coop_Controller_Send_Data.txt (14.3 KB)

// Strings to send text in place of digital o - 1 to PHP Server
String data;
String oTemp = "999";  // Temp values until i figure out how to convert float(temps) to String
String iTemp = "888";
String lLevel;
String wDoor;
String cDoor;
String wLevel;
String wPump;
String rBarrel;
String vFan;
String hLamp;

There is NO reason to be using Strings, and pissing away precious resources.

Serial.println(F("Gaines' Arduino Chicken Coop Controler!"));

How much free memory do you have?

PaulS please tell me a better way. I'm trying to send a "open", "closed" based on digital output state of 0 or 1. I was told string was only way to send that. I hope I was wrong as I'm sure I don't have much memory. It's running on an Uno board.

Thanks,
Gaines

Did you ever try a test sketch to insure you understand how this works? Or did you throw all that together hoping it would work first time through?

You must decide if you are using a POST or GET request. They are different.

Chris, I've copied your code and putting it through the rockwallaby blender, not sure what it will spit out :smiley:

But in that sprintf statement you are assigning a float var to a unsigned long, for example;

sprintf(postData, "id=live&iTemp=%lu&oTemp=%i&lLevel=%i&wDoor=%i&cDoor=%i&wLevel=%i&wPump=%i&rBarrel=%i&vFan=%i",
          insideTemperature,
          outsideTemperature,

In my IDE it tells me that insideTemperature is a float and your first token on the sprintf line wants a %lu, which is unsigned long.

There should be no reason to use any string class at all.
I never use it in any of my programs, ever.

Should have something after a sleep :sleeping:
Oh, and your code is not long at all.
PS, some of those delay(...) in your main loop can just go, I mean, they can just go.


Paul

I've only done a quick amount of work on it so far, it needs more work, especially to get rid of all strings.

If you are simply passing strings to the php script, think about sending an int and have the php interpret it and generate the string at that point.

Anyway, you can have a look at your code here Dropbox - File Deleted - Simplify your life
You'll notice a few changes that I hope you will be ok with, it should be somewhat of an improvement.

I don't have use any onewire or Dallas things, but with that commented out it compiles ok.


Paul