Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Arduino - Trouble g...
 
Share:
Notifications
Clear all

Arduino - Trouble getting temperature data to my webserver


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

This is a continuation of a question at the Push Arduino Data to webserver article ...

Hi there I manage my project to work on wamp server locally (localhost) correctly, but when I try to move online on my godaddy server the problem starts… In serial monitor I get message that I have connection with the server and I get the readings from my sensors, but when I go online to see if the data are in my database on my godaddy server I don’t get any data. I cant find what is the problem . First I export already made database from localhost, than I imported it online on mysql. 

here is the code for dbconnect.php

<?php
$MyUsername = “*****”; // enter your username for mysql
$MyPassword = “
****”; // enter your password for mysql
$MyHostname = “tanjamayaarduino.db.5049499.hostedresource.com”; // this is usually “localhost” unless your database resides on a different server
$dbh = mysql_pconnect($MyHostname , $MyUsername, $MyPassword);
$selected = mysql_select_db(“tanjamayaarduino”,$dbh);
?>

here is the code for add_data.php

<?php
    // Connect to MySQL
    include(“dbconnect.php”);
    // Prepare the SQL statement
    $SQL = “INSERT INTO tanjamayaarduino.temperature (sensor ,celsius, light, moisture1, moisture2) VALUES (‘”.$_GET[“serial”].”‘, ‘”.$_GET[“temperature”].”‘,'”.$_GET[“light”].”‘,'”.$_GET[“moisture1″].”‘,'”.$_GET[“moisture2″].”‘)”;     
    // Execute SQL statement
// mysql_query($SQL);
$result1 = mysql_query($sql) or die(mysql_error());
    // Go to the review_data.php (optional)
    header(“Location: review_data.php”);
?>

here is Arduino code

#include<Ethernet.h>
#include<SPI.h>
const int temperaturePin = A3; //LM35 Temperature sensor
const int lightPin = A2; //LDR photoresistor sensor
const int moisture1Pin = A4; //Moisture1 sensor
const int moisture2Pin = A5; //Moisture2 sensor
// * ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address – must be unique on your network – MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x3A, 0xDC };
// For the rest we use DHCP (IP address and such)
IPAddress ip(192, 168, 2, 121);
EthernetClient client;
//IPAddress server(192, 168, 2, 100); // IP Adres (or name) of server to dump data to  
// IP Adres (or name) of server to dump data to (godaddy baza server 37.148.204.140) (User: tanjamayaarduino@188.121.42.33) (goddady glavna adresa na hostingot 188.121.46.1) 
//IPAddress server(188, 121, 46, 1); 
char server[] = “188.121.46.1”;
int interval = 5000; // Wait between dumps
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  if (Ethernet.begin(mac) == 0) {
    Serial.println(“Failed to configure Ethernet using DHCP”);
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  Serial.println(“Tweaking4All.com – Temperature Drone – v2.0”);
  Serial.println(“-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-n”);
  Serial.print(“IP Address : “);
  Serial.println(Ethernet.localIP());
  Serial.print(“Subnet Mask : “);
  Serial.println(Ethernet.subnetMask());
  Serial.print(“Default Gateway IP: “);
  Serial.println(Ethernet.gatewayIP());
  Serial.print(“DNS Server IP : “);
  Serial.println(Ethernet.dnsServerIP());
}
void loop() {
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)==1) {
    float tem = getTemp();
    Serial.println( tem );
    float lig = getLight();
    Serial.println( lig );
    float mois1 = getMoisture1();
    Serial.println( mois1 );
    float mois2 = getMoisture2();
    Serial.println( mois2 );
    Serial.println(“-> Connected”);
    if (client.connected()) {
      // Make a HTTP request:
      client.print( “GET /advertacs/test/arproekt/add_data.php?”);
            client.print(“serial=”);
      client.print( “TempSensor” );
      client.print(“&”);
      client.print(“temperature=”);
      client.print( tem);
      client.print(“&”);
      client.print(“light=”);
      client.print( lig);
       client.print(“&”);
      client.print(“moisture1=”);
      client.print( mois1);
     client.print(“&”);
      client.print(“moisture2=”);
      client.print( mois2);
      client.println( ” HTTP/1.1″);
      client.println(“Host: http://www.adverta.com.mk“);
     // client.println(server);
      client.println( “Connection: close” );
      client.println();
      client.println();
      client.stop();
    }
  }
  else {
    // you didn’t get a connection to the server:
    Serial.println(“–> connection failed/n”);
  }
  delay(interval);
}
float getTemp() {
  float temperatureC = (5.0 * analogRead(temperaturePin) * 100.0) / 1024; //converting from a 0 to 1023 digital range to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
  return temperatureC;
}
float getLight() {
  float light;
  light = analogRead(lightPin) ;
  return light;
}
float getMoisture1() {
  float moisture1;
  moisture1 = analogRead(moisture1Pin) ;
  return moisture1;
}
float getMoisture2() {
  float moisture2;
  moisture2 = analogRead(moisture2Pin) ;
  return moisture2;
}

please help me… I can't find my error.. where I’m making mistake

I’m very new in coding

Thanks


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

Hi Tanja,

first question is: did this work with a local setup? ie. a local webserver.

If so, then we can narrow it down to how to connect to the webserver.

Also: does the IP address of your webserver belong to you? Or is it on a shared setup where other domains share the same IP address.
If so, then the problem would be that it doesn't know where the data/link should go.

The line:

char server[] = “188.121.46.1”;

might have to be replaced with the domain (I have not tested this, so I cannot confirm if this would even work, but it's worth testing):

char server[] = “www.mydomain.com”;

Or you need to get a dedicated IP address for your domain.


ReplyQuote
(@tanjamaya)
Active Member
Joined: 6 years ago
Posts: 6
 

Dear Hans, thanks for your reply... Yes it did worked on local server wamp server with localhost, I also tried with the my domain www.adverta.com.mk here is the link http://adverta.com.mk/test/arproekt/review_data.php ...btw those data are imported from my localhost project

also I want to ask you... the IP 188.121.46.1 is on my server host where are hosted a lot of my web pages... but on the picture from mysql database phpadmin there are other numbers for server, like 37.148.204.140. What is this number, becouse when I put it in my code I cant make a connection

so any other idea what can be wrong?


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

Hi Tanja,

As for the difference in IP addresses:

It is not uncommon, on shared websites, that the MySQL database run a different server. In your case on 37.148.204.140. Apache, the webserver, and PHP seem to be running on the server with the IP address 188.121.46.1.

Another common setup is when webserver and MySQL are on the same server, in those cases MySQL is typically referred to with 127.0.0.1 (or: localhost).

In either case, the code should work, you just have to make sure that in dbconnect.php, the following line is set correctly:

$MyHostname = “tanjamayaarduino.db.5049499.hostedresource.com”;

I'm not sure if "tanjamayaarduino.db.5049499.hostedresource.com" refers to "37.148.204.140" or not. Maybe you copied it from an example or info you've received from your webhost?

When you enter the link manually, does it add the data then as well?

http://adverta.com.mk/test/arproekt/add_data.php?serial=123&temperature=20&light=12&moisture1=34&moisture2=56

I just tested the link and got an error message "The query is empty". Looking a little closer, this might be the problem in add_data.php:

$result1 = mysql_query($sql) or die(mysql_error());

which probably should be

$result1 = mysql_query($SQL) or die(mysql_error());

The variable names in PHP are case sensitive. You created the query string in $SQL but you passed $sql to $mysql_query.


ReplyQuote
(@tanjamaya)
Active Member
Joined: 6 years ago
Posts: 6
 

Dear Hans, sorry I was away for 2 days... thanks for your replay... I change the line in add_data.php: to:

 $result1 = mysql_query($SQL) or die(mysql_error());

now you link 

http://adverta.com.mk/test/arproekt/add_data.php?serial=123&temperature=20&light=12&moisture1=34&moisture2=56

is working, but still my code from arduino is not working...

also I try this Hello World example that I find on line just to see if I can make a connection to my server online and it worked

here is the code for Hello World example:

/*
  Web client
  This sketch connects to a website ( http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
  Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
  created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 
 */
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x3A, 0xDC };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
IPAddress ip(192, 168, 2, 121); // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
//IPAddress ip(192,168,2,8);
char server[] = "www.adverta.com.mk";

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
   //client.print("GET /rec.php HTTP/1.1n");
    client.println("GET /test/arproekt/rec.php HTTP/1.1");
    client.println("Host: www.adverta.com.mk");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing forevermore:
    while(true);
  } }

I changed the first code ti something like this to try to put some data directly to database but still no luck

#include<Ethernet.h>
#include<SPI.h>
const int temperaturePin = A3; //LM35 Temperature sensor
const int lightPin = A2; //LDR photoresistor sensor
const int moisture1Pin = A4; //Moisture1 sensor
const int moisture2Pin = A5; //Moisture2 sensor
// * ETHERNET SETTING *
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x3A, 0xDC };
// For the rest we use DHCP (IP address and such)
IPAddress ip(192, 168, 2, 121);
//IPAddress server(192, 168, 2, 100); // IP Adres (or name) of server to dump data to
// IP Adres (or name) of server to dump data to (godaddy baza server 37.148.204.140) (User: tanjamayaarduino@188.121.42.33) (goddady glavna adresa na hostingot 188.121.46.1)
//IPAddress server(188, 121, 46, 1);
char server[] = "www.adverta.com.mk";
int interval = 5000; // Wait between dumps
EthernetClient client;
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }

  Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-n");
  Serial.print("IP Address : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP : ");
  Serial.println(Ethernet.dnsServerIP());
}
void loop() {
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    float tem = getTemp();
    Serial.println( tem );
    float lig = getLight();
    Serial.println( lig );
    float mois1 = getMoisture1();
    Serial.println( mois1 );
    float mois2 = getMoisture2();
    Serial.println( mois2 );
    Serial.println("-> Connected");
    if (client.connected()) {
      // Make a HTTP request:
   // client.println("GET /test/arproekt/rec.php HTTP/1.1");
      client.print("GET /test/arproekt/add_data.php?");
      client.print("serial=");
      client.print("100" );
      client.print("&");
      client.print("temperature=");
      client.print("100");
      client.print("&");
      client.print("light=");
      client.print("100");
      client.print("&");
      client.print("moisture1=");
      client.print("100");
      client.print("&");
      client.print("moisture2=");
      client.print("100");
      client.println( "HTTP/1.1");
      client.print("Host: www.adverta.com.mk");
      //client.println(server);
      client.println("Connection: close");
      client.println();
      client.println();
      client.stop();
    }
  }
  else {
    // you didn't get a connection to the server:
    Serial.println("--> connection failed/n");
  }
  delay(interval);
}
float getTemp() {
  float temperature = (5.0 * analogRead(temperaturePin) * 100.0) / 1024; //converting from a 0 to 1023 digital range to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
  return temperature;
}
float getLight() {
  float light;
  light = analogRead(lightPin) ;
  return light;
}
float getMoisture1() {
  float moisture1;
  moisture1 = analogRead(moisture1Pin) ;
  return moisture1;
}
float getMoisture2() {
  float moisture2;
  moisture2 = analogRead(moisture2Pin) ;
  return moisture2;
}

please do you have some Idea what it would be wrong?

thanks


ReplyQuote


(@tanjamaya)
Active Member
Joined: 6 years ago
Posts: 6
 

about this line

$MyHostname = “tanjamayaarduino.db.5049499.hostedresource.com”;

I got it from my srever database detils when I created my database... here are the pics


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

I suspect that you're running on a virtual host, and that's why this link will work:

 http://adverta.com.mk/test/arproekt/add_data.php?serial=123&temperature=20&light=12&moisture1=34&moisture2=56 

and this link will not work:

 http://188.121.46.1/test/arproekt/add_data.php?serial=123&temperature=20&light=12&moisture1=34&moisture2=56 

What happens with a Virtual Host is that a server is using one IP address for multiple websites.

Since the Arduino probably can't resolve http://adverta.com.mk properly, you might want to try the actual path on the server.
Now ... this can be hard to find and I cannot even guarantee that this will work.
It might look something like this (I made this example up, so it will not work):

 http://188.121.46.1/some/path/to/username/www/test/arproekt/add_data.php?serial=123&temperature=20&light=12&moisture1=34&moisture2=56 

Best thing to do is to contact the webhost and ask them how to reach your website with the use of an IP address. Either they will know the full path, or the can assign your website a dedicated IP address,


ReplyQuote
(@tanjamaya)
Active Member
Joined: 6 years ago
Posts: 6
 

Dear Hans, 

thank you for your reply and for you effort to help me... I really think that I'm bordering you, you ware so kind to me But please I really need this to work, I just bought new hosting space specially for this project, I have just one wish please, please, please  :) could you be so kind to send me your email to mine tanjahiljadnikova@yahoo.co.uk so I can send you info about new hosting and database, I think that that way it would be easier for you and me, to set every thing up :) it doesn't  have to have so many sensors.. with one it would be enough for me... just time and temperature.... I'll do the rest... on the server you can upload the sketches, php. and everything. If you don't have time I'll understand

Thank you


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

Hi Tanja,

I'd rather not work on somebody else's server - I'd hate things to go wrong.

Did you ask your webhost how to approach your website with an IP address?
That's highly likely why things do not work.


ReplyQuote
(@tanjamaya)
Active Member
Joined: 6 years ago
Posts: 6
 

Dear Hans its not a problem ... its a new temporary server that I just bought for this project... there is nothing else on it, so there is nothing to go wrong its. please help me :)


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

Even with the new host, we'd really need to know how to access the server directly with an IP address,... 
Please ask your provider/webhost about this. Having access to your webserver will not provide that info either ... 
The new link you provided appears to be a virtual server as well (just looked up the IP address, but couldn't use it either).

Some webhosts offer a dedicated IP address, but usually you have to request this with them.


ReplyQuote
(@tanjamaya)
Active Member
Joined: 6 years ago
Posts: 6
 

Dear Hans, 

Thank you for your effort and patience to help me, I did it as you told me... I bought dedicated IP adrress on my server, and 

FINALLY I DID IT!! :)

If it was't for you I would still be lost... Thank you.

Now I'm going to the next phase of my project, to try to put all the data in to a chart

If you have any idea where to start I'm open

B.R


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2012
Topic starter  

Great to hear it's working now ...

Graphing is yet another challenge, but might not be as easy.

I have limited experience graphing like that ... I have used Flot for this website, but even that took me a bit to figure out.

Alternatives are Google Charts, Chart.js, and EmberCharts. Google Charts has the downside that you depend on them hosting the needed JavaScript library.

You'll have to spend some time reading up on these. 

I have not tested this, but EmberCharts seems relatively easy.


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: