Go Down

Topic: How to read post headers? (Read 9839 times) previous topic - next topic

knowthebird

Aug 01, 2012, 06:39 pm Last Edit: Aug 01, 2012, 07:00 pm by knowthebird Reason: 1
Hello there,

been having alot of trouble getting arduino to read some data getting posted from a php script.

Right now I have a script sending it this...
POST / HTTP/1.0
Host: 192.168.20.2
Content-Length: 11
?a=255&b=111
Content-Type: application/x-www-form-urlencoded

And I would like arduino to read a and b into variables to be used later.

Any and all help would be appreciated.

P.S.  This is getting sent to an ethernet board, not a serial connection

SurferTim

What is wrong with a GET request? Most example code uses that.
Code: [Select]
GET /?a=255&b=111 HTTP/1.0
Host: 192.168.20.2

Here is a web server example that has two form fields.
http://arduino.cc/forum/index.php/topic,114461.msg867258.html#msg867258

I can retrieve the POST data, but it is not as widely used. That data is after the blank line (end of header).

knowthebird

My php get function was not working for some reason, but when a modified a post one i got the headers i posted.  so if i can just read that part of the header it would work easier for me, maybe.

SurferTim

The POST data is after the blank line. In the form sever example I posted the link above, you will find this first line. Change the second line to this display the POST data.
Code: [Select]
        if (c == '\n' && currentLineIsBlank) {
          // Here is the POST data. This displays the data on the serial monitor
          while(client.available()) Serial.write(client.read());


knowthebird

Could you show me how to modify my code for this, I tried changing it myself and just started getting confused.

Code: [Select]

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 25

byte mac[] = {0x1c, 0x26, 0x0c, 0x98, 0x22, 0x85 }; 
byte ip[]  = {192, 168, 1 , 20 };             
byte gateway[] = {192, 168, 1, 1 }; 
byte subnet[]  = {255, 255, 255, 0 };

EthernetServer server(80);

const int photocellPin = A0;
int photocellReading;         
const float Res=10.0;

String inString = String(maxLength);

const int Pin9 = 9;
const int Pin8 = 8;
const int Pin7 = 7;

int lux = 0;
int val;
int lightlux = 0;
int lightvol = 0;

char luxBuff[6];

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
 
  pinMode(Pin9, OUTPUT);
  pinMode(Pin8, OUTPUT);
  pinMode(Pin7, OUTPUT);
 
  pinMode(photocellPin, INPUT);
}

void loop()
{
  int bufLength;
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          if (inString.indexOf("?") > -1) {
            int Pos_a = inString.indexOf("a");
            int Pos_b = inString.indexOf("b", Pos_a);
            int Pos_c = inString.indexOf("c", Pos_c);
            int End = inString.indexOf("H", Pos_b);
            if(End < 0){
              End =  inString.length() + 1;
            }
            bufLength = ((Pos_c) - (Pos_a+2));
            if(bufLength > 6){ 
              bufLength = 6;
            }   
            inString.substring((Pos_a+2), (Pos_c-1)).toCharArray(luxBuff, bufLength); 
            lightlux = atoi(luxBuff);
            lightvol = 51*(lightlux/1333);

            bufLength = ((Pos_b) - (Pos_c+2));
            if(bufLength > 6){ 
              bufLength = 6;
            }     
            inString.substring((Pos_c+2), (Pos_b-1)).toCharArray(luxBuff, bufLength);
            val = atoi(luxBuff);
           
            adjustlight();
           
            photocellReading = analogRead(photocellPin); 
            float Vout=photocellReading*0.0048828125;
            lux=500/(Res*((5-Vout)/Vout));
           
            while (lightlux < lux){
              lightvol = lightvol + .1;
              adjustlight();
            }
            while (lightlux > lux){
              lightvol = lightvol - .1;
              adjustlight();
            }
           
          }
         
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
          photocellReading = analogRead(photocellPin);
          float Vout=photocellReading*0.0048828125;
          lux=500/(Res*((5-Vout)/Vout));
         
            client.print("Lux Value is : ");
            client.print(lux);
            client.print(" Current Voltage is: ");
            client.print(lightvol);
            client.print(" END");
            client.println("<br />");
         
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }
  photocellReading = analogRead(photocellPin); 
  float Vout=photocellReading*0.0048828125;
  lux=500/(Res*((5-Vout)/Vout));
           
  while (lightlux < lux){
    lightvol = lightvol + .1;
    adjustlight();
  }
  while (lightlux > lux){
    lightvol = lightvol - .1;
    adjustlight();
  }
}

void adjustlight() {
            if (val == '000') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
            if (val == '111') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
            if (val == '100') { analogWrite(Pin9, lightvol);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
            if (val == '010') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
            if (val == '001') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, lightvol);}
            if (val == '110') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
            if (val == '011') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
}


SurferTim

Same place.
Code: [Select]
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          // This is after the header. The POST data is in the body.
          // so read the POST data
           while(client.available()) Serial.write(client.read());
        }


knowthebird

It looks like thats where my code is at, so it must be a problem with how im processing it right?

SurferTim

Here is server code with a POST form. It sends the POST data to the serial monitor. You will need to save it and parse the stuff from it.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

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

EthernetServer server(80);

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

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    int r,t;
    char *pch;
   
    Serial.print("Client request: ");
   
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;         
        }

        if (c == '\n' && currentLineIsBlank) {
          Serial.println(tBuf);
          // retrieve the POST data
          Serial.print("POST data: ");
          while(client.available()) Serial.write(client.read());
          Serial.println();
         
          pch = strtok(tBuf,"?");

          while(pch != NULL)
          {
            if(strncmp(pch,"t=",2) == 0)
            {
              t = atoi(pch+2);
              Serial.print("t=");
              Serial.println(t,DEC);             
            }

            if(strncmp(pch,"r=",2) == 0)
            {
              r = atoi(pch+2);
              Serial.print("r=");             
              Serial.println(r,DEC);
            }


            pch = strtok(NULL,"& ");
          }
          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>");
         
          client.write("<body><H1>TEST</H1>");

          client.write("<form method=POST>T: <input type=text name=t><br>");
          client.write("R: <input type=text name=r><br><input type=submit></form>");

         
          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("done");
  }
}

My apology in advance for any typos.

knowthebird

#8
Aug 02, 2012, 04:40 am Last Edit: Aug 02, 2012, 04:49 am by knowthebird Reason: 1
I changed the code the way I understood u mean it, but still not recieving post data...
I would debug it more myself, but dont actually have the arduino board, im sending this stuff to someone else to check...

This is what I get in the serial monitor...

Ready
Client request: GET /favicon.ico HTTP/1.1
POST data:
Sending response
Returning Information to server...
adjusting lights...
adjust light function starting with val:0

and that "GET /favicon.ico HTTP/1.1"  makes me think they are using the wrong script on the server now, but any how, would appreciate a code check for me.


Code: [Select]

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

byte mac[] = {0x1c, 0x26, 0x0c, 0x98, 0x22, 0x85 };  
byte ip[]  = {192, 168, 1 , 20 };            
byte gateway[] = {192, 168, 1, 1 };  
byte subnet[]  = {255, 255, 255, 0 };

EthernetServer server(80);

const int photocellPin = A0;
int photocellReading;          
const float Res=10.0;

const int Pin9 = 9;
const int Pin8 = 8;
const int Pin7 = 7;

int lux = 0;
int val;
int lightlux = 0;
int lightvol = 0;

void setup()
{
 Serial.begin(9600);
 Ethernet.begin(mac, ip, gateway, subnet);
 
 pinMode(Pin9, OUTPUT);
 pinMode(Pin8, OUTPUT);
 pinMode(Pin7, OUTPUT);
 
 pinMode(photocellPin, INPUT);
 delay(2000);
 server.begin();
 Serial.println("Ready");
}

void loop()
{
 EthernetClient client = server.available();
 if(client) {
   boolean currentLineIsBlank = true;
   boolean currentLineIsGet = true;
   int tCount = 0;
   char tBuf[64];
   int a,b;
   char *pch;
   
   Serial.print("Client request: ");
   
   while (client.connected()) {
     while(client.available()) {
       char c = client.read();

       if(currentLineIsGet && tCount < 63)
       {
         tBuf[tCount] = c;
         tCount++;
         tBuf[tCount] = 0;          
       }

       if (c == '\n' && currentLineIsBlank) {
         Serial.println(tBuf);
         // retrieve the POST data
         Serial.print("POST data: ");
         while(client.available()) Serial.write(client.read());
         Serial.println();
         
         pch = strtok(tBuf,"?");

         while(pch != NULL)
         {
           if(strncmp(pch,"a=",2) == 0)
           {
             a = atoi(pch+2);
             lightlux = a;
             lightvol = 51*(lightlux/1333);
             Serial.print("Lux a=");
             Serial.println(a,DEC);
             Serial.print("Lightlux:");
             Serial.print(lightvol);            
           }

           if(strncmp(pch,"b=",2) == 0)
           {
             b = atoi(pch+2);
             val = b;
             Serial.print("Lights b=");              
             Serial.println(b,DEC);
           }


           pch = strtok(NULL,"& ");
         }
         Serial.println("Sending response");
         Serial.print("Returning Information to server...");
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
         
         photocellReading = analogRead(photocellPin);
         float Vout=photocellReading*0.0048828125;
         lux=500/(Res*((5-Vout)/Vout));
         
         client.print("Lux Value is : ");
         client.print(lux);
         client.print(" Current Voltage is: ");
         client.print(lightvol);
         client.print(" END");
         client.println("<br />");
         client.stop();
       }
       else if (c == '\n') {
         currentLineIsBlank = true;
         currentLineIsGet = false;
       }
       else if (c != '\r') {
         currentLineIsBlank = false;
       }
     }
   }
   if (val != 0) {
   Serial.print("adjusting lights...");
   while (lightlux < lux){
     lightvol = lightvol + .1;
     adjustlight();
   }
     while (lightlux > lux){
     lightvol = lightvol - .1;
     adjustlight();
   }
   }
   Serial.println("done");
 }
}

void adjustlight() {
           Serial.print("adjust light function starting with val: ");
           Serial.print(val);
           if (val == '000') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
           if (val == '111') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
           if (val == '100') { analogWrite(Pin9, lightvol);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
           if (val == '010') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
           if (val == '001') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, lightvol);}
           if (val == '110') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
           if (val == '011') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
           photocellReading = analogRead(photocellPin);  
           float Vout=photocellReading*0.0048828125;
           lux=500/(Res*((5-Vout)/Vout));
}

zoomkat

Post server code.

Code: [Select]

/*
A simple web server using an Arduino Wiznet Ethernet shield.
For Arduino IDE V1.0 only. Previous IDE versions require mods to this code.

Original code created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
modified 18 Jan 2012
by Tim Dicus
*/

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,1,102 );
IPAddress gateway( 192,168,1,1 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns( 192,168,1,1 );

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(84);

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

  // set SPI SS pins on w5100 and SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // start the SD interface here if you want.
  // Add the SD.h library above
  // SD.begin(4);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  // disable w5100 SPI so SD SPI can work with it
  digitalWrite(10,HIGH);
  delay(2000);
  server.begin();

  Serial.println("setup finished");
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {

// Here is where the POST data is. 
          while(client.available())
          {
             Serial.write(client.read());
          }
          Serial.println();

          Serial.println("Sending response");
          // send a standard http response header
          client.println("HTTP/1.0 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML><BODY>TEST OK!");
         
//form added to send data from browser and view received data in serial monitor         
client.println("<FORM ACTION=\"http://192.168.1.102:84\" METHOD=\"post\" target=\"inlineframe\">");
client.println("Name: <INPUT TYPE=\"TEXT\" NAME=\"Name\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");
client.println("Email: <INPUT TYPE=\"TEXT\" NAME=\"Email\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");
client.println("<INPUT TYPE=\"SUBMIT\" NAME=\"submit\" VALUE=\"Sign Me Up!\">");
client.println("</FORM>");
client.println("<BR>");

client.println("<IFRAME name=inlineframe");
client.println("src=\"res://D:/WINDOWS/System32/shdoclc.dll/dnserror.htm\" width=1");
client.println("height=1> this will hold the html answer of the moves");
client.println("</IFRAME>");
         
          client.stop();
        }
        else if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("Disconnected");
  }
}
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

knowthebird

#10
Aug 02, 2012, 01:40 pm Last Edit: Aug 02, 2012, 01:43 pm by knowthebird Reason: 1
this is the server code im using for my script...

Code: [Select]


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Lux Level Controller</title>
<style type="text/css">
<!--
body {
background-color: #FFFFFF;
background-image: url(background.jpg);
background-repeat: repeat;
}
body,td,th {
color: #000000;
}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" onsubmit="return validateForm()" action="index.php">
<div align="center">
<p><strong>Lux Controller</strong></p>
<p>
  <?php
include "libchart/libchart/classes/libchart.php";

error_reporting (E_ALL E_NOTICE);

$ArduinoIp '192.168.1.20';                       // IP address of Arduino Board

$luxValue file_get_contents($ArduinoIp);      
$findme 'Lux Value is : ';                        // Cleaning up the extra text the board will send
$findmet ' Current Voltage is : ';
$findend ' END';

$pos = (strpos($luxValue$findme) + 15);
$post = (strpos($luxValue$findmet));
$postr $post 22;
$posfr = (strpos($luxValue$findend));

$dif $post - ($pos);
$curdif $posfr - ($postr);
$luxval substr($luxValue$pos$dif);           //Lux value after cleaned
$curval substr($luxValue$postr$curdif);      //Current value after cleaned

$myFile "ValueLog.txt";                          //Where you would like the log file saved
$fh fopen($myFile'a') or die("can't open file");
$stringData date("m/d") . "  " date("H:i") . " - " $luxval " - " $curval;  //Recored in "month/date hour:minute - lux - current" format
fwrite($fh$stringData);                          //Record the new Lux and Current values to log
fwrite($fh"\n");
fclose($fh);

$chart = new VerticalBarChart(750400); // Change to $chart = new LineChart(750, 400); for a line chart

$serie1 = new XYDataSet();
$serie2 = new XYDataSet();

$lines=array();
$file_handle fopen($myFile"rb");

while (!
feof($file_handle) ) {
$line_of_text fgets($file_handle);
$parts explode('-'$line_of_text);
$serie1->addPoint(new Point($parts[0], $parts[2]));   //Create new point on graph for Lux
$serie2->addPoint(new Point($parts[0], $parts[1]));   //Create new point on graph for Current
}

fclose($file_handle);

$dataSet = new XYSeriesDataSet();
$dataSet->addSerie("Voltage"$serie1);               //Description of bars on graph
$dataSet->addSerie("Lux"$serie2);
$chart->setDataSet($dataSet);

$chart->setTitle("Voltage Versus LUX");              //Your Graph Title
$chart->getPlot()->setGraphCaptionRatio(0.75);
$chart->render("graph.png");



if (
$_POST["b"] == "") {                                 //See if any new lux values were posted
} else {       
if (
$_POST["s1"] == "0" && $_POST["s2"] == "0" && $_POST["s3"] == "0" ) {$vals "000";}
if (
$_POST["s1"] == "1" && $_POST["s2"] == "1" && $_POST["s3"] == "1" ) {$vals "111";}
if (
$_POST["s1"] == "1" && $_POST["s2"] == "0" && $_POST["s3"] == "0" ) {$vals "100";}
if (
$_POST["s1"] == "0" && $_POST["s2"] == "1" && $_POST["s3"] == "0" ) {$vals "010";}
if (
$_POST["s1"] == "0" && $_POST["s2"] == "0" && $_POST["s3"] == "1" ) {$vals "001";}
if (
$_POST["s1"] == "1" && $_POST["s2"] == "1" && $_POST["s3"] == "0" ) {$vals "110";}
if (
$_POST["s1"] == "0" && $_POST["s2"] == "1" && $_POST["s3"] == "1" ) {$vals "011";}

$postdata http_build_query(
    array(
        
'a' => $_POST["b"],
'b' => $vals
    
)
);
$opts = array('http' =>
    array(
        
'method'  => 'POST',
        
'header'  => "?a=" $_POST["b"] . "&b=" $vals,
        
'content' => $postdata
    
)
);
$context  stream_context_create($opts);
$result file_get_contents($ArduinoIpfalse$context);     //They will be converted to current on the arduino board

echo "<p>Lux Value : " $_POST["b"] ." Sent</p>";
}

?>

</p>
<div align="center">
  <p>Current LUX Level : <?php echo $luxval?></p>
  <?php
  
if ($luxval 10) {echo "<p>Current Light Conditions: Dark </p>";}
  else if (
$luxval 35) {echo "<p>Current Light Conditions: Dim </p>";}
  else if ((
$luxval >= 35) && ($luxval 120)) {echo "<p>Current Light Conditions: Light </p>";}
  else if ((
$luxval >= 150) && ($luxval 250)) {echo "<p>Current Light Conditions: Bright </p>";}
  else if (
$luxval >= 250) {echo "<p>Current Light Conditions: Very Bright </p>";}
  
?>

  <p>Light Controls</p>
  <label>Enter Lux Level For Lights
  <input type="text" name="b" />
  </label>
  <label></label>
  <table width="60%" border="1">
    <tr>
      <td width="50%"><div align="center"><strong>Turn On </strong></div></td>
      <td width="50%"><p align="center"><strong>Turn Off </strong></p>        </td>
    </tr>
    <tr>
      <td><input name="s1" type="radio" value="1" checked="checked" />
Section 1</td>
      <td><input name="s1" type="radio" value="0" />
Section 1</td>
    </tr>
    <tr>
      <td><input name="s2" type="radio" value="1" checked="checked" />
Section 2</td>
      <td><input name="s2" type="radio" value="0" />
Section 2</td>
    </tr>
    <tr>
      <td><label>
        <input name="s3" type="radio" value="1" checked="checked" />
Section 3 </label></td>
      <td><label>
        <input name="s3" type="radio" value="0" />
Section 3 </label></td>
    </tr>
  </table>
  <input type="submit" name="h" value="Submit" />
  <p><a href="ValueLog.txt" title="Logs" target="_self">View Logs </a></p>
  <p><img alt="Line Graph" src="graph.png" style="border: 1px solid gray;"/> </p>
</div>
</div>

</label>
</form>
<script language="JavaScript" type="text/javascript">
function validateForm()
{
var x=document.forms["form1"]["b"].value;
if (x==null || x=="")
  {
  alert("Must give lux value for lights.");
  return false;
  }
}
</script>

<p align="center">&nbsp;</p>
</body>
</html>

SurferTim

#11
Aug 02, 2012, 01:44 pm Last Edit: Aug 02, 2012, 02:06 pm by SurferTim Reason: 1
Quote
and that "GET /favicon.ico HTTP/1.1"  makes me think they are using the wrong script on the server now, but any how, would appreciate a code check for me.

That is the web browser (not the server) trying to get the little icon for the address bar. Mine does the same.

edit: Actually, that is a really good sign. If the web browser is asking for the site icon, everything went smooth with the request and response.

knowthebird

Thats good its working well, but I still dont understand why its not reading my variables or the post data at all.  Maybe the way my script changes the headers its not actually post data, but it is sending them in some format (how i posted first) so why dont the variables a or b show up at all?  I dont think its a connection problem between the server and client, I guess now its a data parsing problem.

SurferTim

The code I posted above is working well here, including the site icon request. If you want to recover those values sent by the web browser, then you must store the characters after the blank line (end of header) in a character array, and modify the parse routine to use that array.

I want to leave some of the "fun" for you!  :)

knowthebird

So Im thinking I just needed to move what I was using to parse the data before?...

Code: [Select]

while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;         
        }

        if (c == '\n' && currentLineIsBlank) {
          Serial.println(tBuf);
          // retrieve the POST data
          Serial.print("POST data: ");
           pch = strtok(tBuf,"?");

          while(pch != NULL)
          {
            if(strncmp(pch,"a=",2) == 0)
            {
              a = atoi(pch+2);
              lightlux = a;
              lightvol = 51*(lightlux/1333);
              Serial.print("Lux a=");
              Serial.println(a,DEC);
              Serial.print("Lightlux:");
              Serial.print(lightvol);           
            }

            if(strncmp(pch,"b=",2) == 0)
            {
              b = atoi(pch+2);
              val = b;
              Serial.print("Lights b=");             
              Serial.println(b,DEC);
            }


            pch = strtok(NULL,"& ");
          }
         
          while(client.available()) Serial.write(client.read());
          Serial.println();

Go Up