Cannot "POST" TO WEBSERVER

I have to post data to the webserver on WAMP using arduino wifi shield.The shield connects well to the server but cannot post the data in the sql database running on the server code is below.

#include <TinkerKit.h>
#include <WiFi.h>
#include <SPI.h>

char ssid[] = "mokete"; // your network SSID (name)
char pass[] = "59654125"; // your network password
int status = WL_IDLE_STATUS;

//WiFiServer server(80);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int sensor;
int analog_val;

String data;
String Hall;
String Temp;
WiFiClient client;
IPAddress server(192,168,43,169);
//char server[] = "google.com";
void setup() { 
Serial.begin(9600);
        
 while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}

// you're connected now, so print out the status:
printWifiStatus();
    
        
          Hall = "50";
          Temp = "50";

	data = "";
}

void loop(){

	currentMillis = millis();
	if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
		previousMillis = currentMillis;
                 Hall = String(50);
                 Temp = String(50);
                 
	}

	data = "temp1="+Hall+"&hum1="+Temp;
        client.flush();
	if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
	        Serial.println("Connected");
		client.println("POST /add.php HTTP/1.1"); 
		client.println("Host: 192.168.43.169"); // SERVER ADDRESS HERE TOO
		client.println("Content-Type: application/x-www-form-urlencode;charset=UTF-8"); 
                client.println("User-Agent: Arduino/1.0");
		client.print("Content-Length: ");
		client.println(data.length()); 
		client.println(); 
		client.print(data+"\n"); 

Serial.println("POST /add.php HTTP/1.1"); 
Serial.println("Host: 192.168.43.169"); 
Serial.println("Content-Type: application/<span class=hiddenSpellError>x-www-form-urlencoded</span>");
Serial.println("Connection: close");
Serial.print("Content-Length: ");
Serial.println(data.length());
Serial.println();
Serial.print(data);
Serial.println();
	} 

	if (client.connected()) { 
                char c = client.read();
                Serial.write(c);
		client.stop();	// DISCONNECT FROM THE SERVER
                client.flush();
	}

	delay(70000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
        Serial.println("Noo");
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

mokete:
I have to post data to the webserver on WAMP using arduino wifi shield.The shield connects well to the server but cannot post the data in the sql database running on the server code is below.

To receive useful responces:

  • Put some error message into your code
  • Capture those error message
  • attach the Captured Error message to your help request

Just saying it cannot post leaves too many possibilities.

Unless someone has used your exact software, and encountered the exact same problem, no one will be able to help you.

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.

The php code to which the data is sent to has no errors, and if data is inserted manually it is okay e.g($query = "INSERT INTO tempLog (timeStamp, temperature, humidity)
VALUES (NOW(),11, 11)";).I am using arduino uno 1.06 with a wifi shield.For server I am using WAMP.The database is okay but when i try to send thee data via arduino the entrys in database table are zeroes.BELOW IS THE PHP CODE:

<?php
  
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    $link=mysqli_connect("localhost", "root", "", "databse");
	
	if (mysqli_connect_errno()) {
    //printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }

	$temp1=$_POST["temp1"];
	$hum1=$_POST["hum1"];
	if(isset($_POST["temp1"])){ $temp1 = $_POST["temp1"];}
	if(isset($_POST["hum1"])){ $hum1 = $_POST["hum1"];}
        $safetemp1= mysqli_real_escape_string($link, $temp1);
	$safehum1= mysqli_real_escape_string($link, $hum1);

    $query = "INSERT INTO `databse`.`tempLog` (`temperature`, `humidity`) 
	VALUES ('".$safetemp1."','".$safehum1."')"; 
 
    $result = mysqli_query($link,$query) or die(mysqli_error($link));
	if (!$result) {
       die("Nothing was inserted, something went wrong.");
    }
	mysqli_close($link);
   	header("Location: indexx.php");
?>

mokete:
I have to post data to the webserver on WAMP using arduino wifi shield.The shield connects well to the server but cannot post the data in the sql database running on the server code is below.

void loop(){

currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
                Hall = String(50);
                Temp = String(50);
               
}

data = "temp1="+Hall+"&hum1="+Temp;
       client.flush();
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
       Serial.println("Connected");
client.println("POST /add.php HTTP/1.1");
client.println("Host: 192.168.43.169"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencode;charset=UTF-8");
               client.println("User-Agent: Arduino/1.0");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data+"\n");

Serial.println("POST /add.php HTTP/1.1");
Serial.println("Host: 192.168.43.169");
Serial.println("Content-Type: application/x-www-form-urlencoded");
Serial.println("Connection: close");
Serial.print("Content-Length: ");
Serial.println(data.length());
Serial.println();
Serial.print(data);
Serial.println();
}

if (client.connected()) {
               char c = client.read();
               Serial.write(c);
client.stop(); // DISCONNECT FROM THE SERVER
               client.flush();
}

delay(70000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
       Serial.println("Noo");
}

Ok, looking at your code, it has some issues.

  1. You are reading your sensors once per interval, but you are delaying 5 minutes between transactions.
    your program flow should be:
  • Initialize Wifi, connect to network
  • loop top:
  • if(interval elapsed)
    if(Connect to Server)
    Read Sensor
    Post to Server
    Wait for Post acknowledgement
    Disconnect from Server
    else
    error(connection failed)
    Set next interval
  • goto top:

your Current program is

  • loop top:
  • if(interval elapsed)
    Read Sensor
  • if(Connect to Server) //whether or not new Data?
    Post to server
  • if(Connected)
    don't wait for characters, just read one character
    close connection
    flush connection
  • delay(5minutes)
  • goto top:
unsigned long timeout=millis();

void loop(){
if(millis()>timeout){
  if(WIFI.status()==WL_CONNECTED){
    client.flush();
    if (client.connect(server,80)) { 
      read Sensors
      client.print( all of post data );
      while(client.connected()){ // connection is still open, may have status data waiting
         if(client.available()) Serial.print(client.read());
         }
      client.close();
      timeout = millis()+interval;
      }
    else {
       error("unable to connect to server");
       timeout = millis() + interval/4
       }
  else {
       error("WiFi not connected");
       WIFI.begin();
       timeout = millis() + 10000; // see if Wifi will reconnect
       }
  }
}

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.