Adding Control to a Webclient GET request to Server.

I am now trying to plan out the addition of a switch control routine to the GET command I am currently using to send data to my server. I am trying to use the data returned from the GET command as shown in serial output.

HTTP/1.1 200 OK
Date: Tue, 11 Mar 2014 21:34:29 GMT
Server: Apache
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

xxx,1,0,1,0,1

Insert Ok: Temp1=24, Level1=5, Volt1=0, Volt2=1, Humid1=36, Spare2=0.

The "xxx,1,0,1,0,1" text is just what I typed on the first line of the php file that the GET is referenced to. My plan is to use the data to set digital pins. I do not know if or how I would set the data string on that first line without interfering with the code that moves my monitoring data into mysql. So I am looking for a little help. Is this a reasonable approach or is there a better way.

I do not know if or how I would set the data string on that first line without interfering with the code that moves my monitoring data into mysql.

I can't imagine why the Arduino reading the response from the PHP script would have ANY impact on what the PHP script is doing on some other computer.

Is this a reasonable approach

Is what a reasonable approach?

I can't imagine why the Arduino reading the response from the PHP script would have ANY impact on what the PHP script is doing on some other computer.

Hi Paul. Guess I wasn't clear. What I was trying to say was that I do not know yet what process to use to change the short data string at the top of the php file, without affecting the rest of the php script in that file. I have made some progress towards moving the data returned from server into an array. Here is the code I'm working on. For now I set it to read 10 bytes and store it.

int inData[10];  // Allocate some space for the Bytes
byte index = 0;   // Index into array; where to store the Bytes

 while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();  // read byte
      inData[index] = inChar; // Store it
      index++; 
      Serial.write(inChar);

However when I use this routine in my sketch, the output seems strange.

HTTP/1.1 200 OK
Date: Wed, 12 Mar 2014 15:00:24 GMT
Server: Apache
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

xxx,1,0,1,0,1

Insert O

That 'insert O is the output from the serial.write.

So I really have ( at least ) two issues I need to reslove. I need to fix the read routine and I need to figure out how to update the string of data in the php file targeted by the GET.

What I was trying to say was that I do not know yet what process to use to change the short data string at the top of the php file, without affecting the rest of the php script in that file.

I guess you weren't. There is nothing that the Arduino can do to change the PHP script. What the PHP script should return is something only you can decide.

Once you do, though, you can post the PHP code, and we'll help you make the PHP script return what you want.

Here is the code I'm working on.

I think maybe the fine folks at http://snippets-r-us.com could help you with your snippets.

Point Taken. Thank You for your offer. Here then is everything... Here is the sketch

#include <SPI.h>
#include <Ethernet.h>

#include <DHT.h>
#define DHTPIN 37     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 
DHT dht(DHTPIN, DHTTYPE);

#include <NewPing.h>
#define TRIGGER_PIN  35  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     33  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


const int ledPin =  8; 
int ledState = LOW;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   // this must be unique

IPAddress gateway(192, 168, 209, 1);
IPAddress subnet(255, 255, 255, 0);

IPAddress server(xxx,xxx,xxx,xxx); // "myserver.com"

//Change to your domain name for virtual servers
char serverName[] = "myserver.com";

int serverPort = 80;
int inData[16];  // Allocate some space for the Bytes
byte index = 0;   // Index into array; where to store the Bytes

EthernetClient client;
char pageAdd[128];

#define delayMillis 120000UL   // 2 minute delay

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;



void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  dht.begin();

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.println(F("Starting ethernet..."));
  // Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above 
  // and uncomment the next 2 lines

  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(F("ok"));
  digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}



void loop()
{
   thisMillis = millis();

  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;
    
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  int volt1 = digitalRead(31);
  int volt2 = analogRead(12);
  volt2 = (volt2 / 68.12 ,1);
  int humid1 = dht.readHumidity();
  int level1 = (uS / US_ROUNDTRIP_CM);
  int temp1 = dht.readTemperature();
  int spare2 = digitalRead (38);
 
  if (ledState == LOW)
      ledState = HIGH;
     else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    
    
   sprintf(pageAdd,"/Firstdata2.php?temp1=%d&level1=%d&volt1=%d&volt2=%d&humid1=%d&spare2=%d",temp1,level1,volt1,volt2,humid1,spare2);

 
    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));


 }    
}

  
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
 
 
  
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.0",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }


  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      inData[index] = inChar; // Store it
      index++; 
      
      
    
      
    //  Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
Serial.print(inChar);
    connectLoop++;

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

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

This is Output from Serial Monitor

Starting ethernet...
ok
192.168.0.177
Ready
connecting...connected
20483204832048320483204832048320483204832048320483.....

And here is the php code as it currently sits

xxx,1,0,1,0,1

<?

 $temp1 = $_GET['temp1'];
 $level1 = $_GET['level1']; 
 $volt1 = $_GET ['volt1'];
 $volt2 = $_GET ['volt2'];
 $humid1 = $_GET ['humid1'];
 $spare2 = $_GET ['spare2'];
 

  $dbcnx = mysql_connect("localhost","xxxxxx","xxxxxxxx");

  if(!$dbcnx)
  {
    echo( "<P>Unable to connect to the database server at this time.</P></body></html>" );
    exit();
  }

  if(!mysql_select_db("xxxxxxxxxxxx") )
  {
    echo( "<P>Unable to locate the test database at this time.</P></body></html>" );
    exit();
  }

  $result = mysql_query("INSERT INTO Table1 ( Temp1,Level1,Volt1,Volt2,Humid1,Spare2 ) VALUES ('$temp1', '$level1', '$volt1', '$volt2', '$humid1', '$spare2' )");
  
  if(!$result) echo("<P>Insert failed</P>");
  else echo("Insert Ok: ");

  mysql_close($dbcnx);
   echo('Temp1=' . $temp1 . ', ');   
   echo('Level1=' . $level1 . ', ');
   echo('Volt1=' . $volt1 . ', ');   
   echo('Volt2=' . $volt2 . ', ');
   echo('Humid1=' . $humid1 . ', ');
   echo('Spare2=' . $spare2 . '. ');
        
?>

This is where I am now.

  volt2 = (volt2 / 68.12 ,1);

I don't know what you think this code is doing, but I can assure you that it isn't.

  if (ledState == LOW)
      ledState = HIGH;
     else
      ledState = LOW;

or

    ledState = !ledState;
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      inData[index] = inChar; // Store it
      index++; 
    //  Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }
Serial.print(inChar);

After collecting all the response data from the server, print the last character read, anonymously. OK. I'll bite. Why?

xxx,1,0,1,0,1

<?

That first line is outside the portion of the file that the PHP engine cares about. What are you expecting it to do?

If you want the line sent back to the client, you need to use echo("xxx,1,0,1,0,1"); somewhere AFTER the <? line.

Thanks for taking the time to look at this. You are absolutely correct on all issues. I have made the 'led' improvement now and the volt2 issue I have just removed for now. I have moved the first line of the php file inside the <? and I was hoping to read this data as the response data from the server.

After collecting all the response data from the server, print the last character read, anonymously. OK. I'll bite. Why?

I was trying to use the serial.print for debugging but it did not help. From what you say I have at least managed to collect the response data. What I think I need to figure out now is how to use it.

I have at least managed to collect the response data.

Collect it where? That makes a big difference on how you go about trying to parse and use the data.

I thought I was storing the response in 'inData'. which is a 16 byte array.

inChar = client.read();
      inData[index] = inChar; // Store it
      index++;

Have I got this wrong too?