POST data from Arduino to website

Hi guys, I am working on a project where I am trying to send data using HTTP and POST method from my Arduino Mega2560 to a website which I host on localhost. I am living to a students’ accommodation and thus, I do not have access to the router. However, I have bought a router and have connected it to the wall via the WAN. Also, the Arduino is connected to one of the ethernet plugs of the router via an Ethernet shield and my laptop to another ethernet plug. I am trying to retrieve the data from the website, but I cannot seem to load them.

Arduino Code

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

dht DHT1;
int temperatureSensorPin1 = 25;

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

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000;
String data;

long lastUpdate = 0;

void setup() {

  Serial.begin(9600);
  if(Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  data = "";

  pinMode(temperatureSensorPin1, INPUT);
}

void loop() {


int temp1 = DHT1.read11(temperatureSensorPin1);

switch (temp1)
  {
    case DHTLIB_OK:  
        Serial.print("OK,\t"); 
        break;
    case DHTLIB_ERROR_CHECKSUM: 
        Serial.print("Checksum error,\t"); 
        break;
    case DHTLIB_ERROR_TIMEOUT: 
        Serial.print("Time out error,\t"); 
        break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default: 
        Serial.print("Unknown error,\t"); 
        break;
  }

  currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
  }
  String data="temp1="+String(DHT1.temperature, 0)+"&temp2="+String(DHT2.temperature, 0)+"&temp3="+String(DHT3.temperature, 0)+"&temp4="+String(DHT4.temperature, 0)+"&temp5="+String(DHT5.temperature, 0)
                  +"&hum1="+String(DHT1.humidity, 0)+"&hum2="+String(DHT2.humidity, 0)+"&hum3="+String(DHT3.humidity, 0)+"&hum4="+String(DHT4.humidity, 0)+"&hum5="+String(DHT5.humidity, 0)+"&gas="+gasValue;

  Serial.println(Ethernet.localIP());
  if(client.connect("192.168.0.1", 80)) {
    client.println("POST /home/visualisation.php HTTP/1.1");
    client.println("Host:  192.168.0.1");
    //client.println("User-Agent: Arduino/1.0");
    //client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    //Serial.println(data);
    Serial.println("We have connection");
  }

  if(client.connected()) {    
    Serial.println("We have abc");
    client.stop();
  }

  delay(4000);
}

PHP Code

<?php
    $temp1 = "";
    if(isset($_POST['temp1'])) {
     $temp1 = $_POST['temp1'];
    }
    echo $temp1;
    ?>

[Windows ip configuration][1]
[Router ip configuration][2]

Is the connection that I have made the appropriate one? How should I do it in order to work?

P.S.: I would like to clarify that I do not have an extended knowledge regarding networking.

Vasileios:
Hi guys, I am working on a project where I am trying to send data using HTTP and POST method from my Arduino Mega2560 to a website which I host on localhost. I am living to a students’ accommodation and thus, I do not have access to the router. However, I have bought a router and have connected it to the wall via the WAN. Also, the Arduino is connected to one of the ethernet plugs of the router via an Ethernet shield and my laptop to another ethernet plug. I am trying to retrieve the data from the website, but I cannot seem to load them.

Arduino Code

#include <dht.h>

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

dht DHT1;
int temperatureSensorPin1 = 25;

byte mac = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
EthernetClient client;
EthernetServer server(80);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000;
String data;

long lastUpdate = 0;

void setup() {

Serial.begin(9600);
 if(Ethernet.begin(mac) == 0) {
   Serial.println(“Failed to configure Ethernet using DHCP”);
 }

data = “”;

pinMode(temperatureSensorPin1, INPUT);
}

void loop() {

int temp1 = DHT1.read11(temperatureSensorPin1);

switch (temp1)
 {
   case DHTLIB_OK:  
       Serial.print(“OK,\t”);
       break;
   case DHTLIB_ERROR_CHECKSUM:
       Serial.print(“Checksum error,\t”);
       break;
   case DHTLIB_ERROR_TIMEOUT:
       Serial.print(“Time out error,\t”);
       break;
   case DHTLIB_ERROR_CONNECT:
       Serial.print(“Connect error,\t”);
       break;
   case DHTLIB_ERROR_ACK_L:
       Serial.print(“Ack Low error,\t”);
       break;
   case DHTLIB_ERROR_ACK_H:
       Serial.print(“Ack High error,\t”);
       break;
   default:
       Serial.print(“Unknown error,\t”);
       break;
 }

currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;
 }
 String data=“temp1=”+String(DHT1.temperature, 0)+"&temp2="+String(DHT2.temperature, 0)+"&temp3="+String(DHT3.temperature, 0)+"&temp4="+String(DHT4.temperature, 0)+"&temp5="+String(DHT5.temperature, 0)
                 +"&hum1="+String(DHT1.humidity, 0)+"&hum2="+String(DHT2.humidity, 0)+"&hum3="+String(DHT3.humidity, 0)+"&hum4="+String(DHT4.humidity, 0)+"&hum5="+String(DHT5.humidity, 0)+"&gas="+gasValue;

Serial.println(Ethernet.localIP());
 if(client.connect(“192.168.0.1”, 80)) {
   client.println(“POST /home/visualisation.php HTTP/1.1”);
   client.println(“Host:  192.168.0.1”);
   //client.println(“User-Agent: Arduino/1.0”);
   //client.println(“Connection: close”);
   client.println(“Content-Type: application/x-www-form-urlencoded;”);
   client.print("Content-Length: ");
   client.println(data.length());
   client.println();
   client.print(data);
   //Serial.println(data);
   Serial.println(“We have connection”);
 }

if(client.connected()) {    
   Serial.println(“We have abc”);
   client.stop();
 }

delay(4000);
}




**PHP Code**




<?php

$temp1 = “”;
   if(isset($_POST[‘temp1’])) {
    $temp1 = $_POST[‘temp1’];
   }
   echo $temp1;
   ?>



[Windows ip configuration][1]
[Router ip configuration][2]

Is the connection that I have made the appropriate one? How should I do it in order to work?

P.S.: I would like to clarify that I do not have an extended knowledge regarding networking.

Why are you sending the POST data to the router? (192.168.0.1)

I would expect you would want to send it to your PC? (192.168.0.100)

Chuck.

You are right. I changed it and now it is working. However the php script needs some changes cause it was not displaying the data.

For example:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label>
<?php echo temp1; ?>
</label>
</body>
</html>

Vasileios:
You are right. I changed it and now it is working. However the php script needs some changes cause it was not displaying the data.

For example:

<!DOCTYPE html>
<?php echo temp1; ?> > ```

It will not because you have assigned the value of $temp1 as a label of something.
Further, variables in php require the ‘$’ sign.

echo $temp1;

You are also missing your entire code for catching the POST data in the php script in the first place!
Why have you gone from:

<?php
    $temp1 = "";
    if(isset($_POST['temp1'])) {
     $temp1 = $_POST['temp1'];
    }
    echo $temp1;
    ?>

This would have worked perfectly well…

Yeah, I just forgot the $ sign as I was writing the post. I also just mentioned how to view the data, not how the whole .php file should be.