1

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 [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.

Router ip configuration LAN MAC Address: 60-E3-27-E7-70-7A IP Address: 192.168.0.1 Subnet Mask: 255.255.255.0

Wireless Wireless Radio: Enable Name (SSID): TP-LINK_707A Mode: 11bgn mixed Channel Width: Automatic Channel: Auto (Current channel 3) MAC Address: 60-E3-27-E7-70-7A WDS Status: Disable

WAN MAC Address: 60-E3-27-E7-70-7B IP Address: 100.66.166.2 Dynamic IP Subnet Mask: 255.255.255.192
Default Gateway: 100.66.166.1

DNS Server: 77.244.128.44 , 77.244.128.45

Windows ip configuration

  • Since the php script isn't doing anything you can detect it might already be working. Could you post the serial output of the Arduino? Can you check the access log of the web server you are running on your PC? – Gerben Aug 18 '16 at 17:59
  • 1
    It is working! I had to make a PHP script to store the values to a database. One change that I needed to do is to make the Arduino send the values to 192.168.0.100 – vchan Aug 19 '16 at 1:27
  • If you have found a solution to your question, then please answer the question and accept it. – TisteAndii Aug 19 '16 at 2:14
  • Great to hear. Best of luck. – Gerben Aug 19 '16 at 9:20
0

The ip that I have Arduino send data to is the router's and thus, I should have used the 192.168.0.100 ip which is the one of my computer.

I also needed to save the value to a database to notice the changes because echo $temp1; did not seem to work, probably I should have it included into HTML label tags. Anyway, I just loaded the value from the database into a PHP variable and I was able to accomplish the task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.