Hello all back at it once again with another problem and the lack of my programming skills. I was searching a way online how to post a temperature sensor to a mysql and display it on a web page. I found a lot of stuff out there but nothing I can either get to work or was other sensors. So i decided to piece stuff together and see what happens. I mange to get the arduino and Ethernet to send the dht22 information to mysql and have it display on a web page that database information. But with my arduino lack of skills I'm trying to also send a text displaying a name. So i can identify that this sensor has a dht22 on it or maybe a light sensor. I'm working on a home automation project.
So my problem is I don't know how to send a text saying this "Sensor 1" from the arduino to the php page. I'm below adding my sketches and code. Can someone please help me?
my arduino sketch
#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;
#define DHTPIN 6 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;
void setup() {
Serial.begin(115200);
pinMode(7, OUTPUT); // sets the digital pin as output
pinMode(5, OUTPUT); // sets the digital pin as output
digitalWrite(7, HIGH); // sets +5v for the sensor
digitalWrite(5, LOW); // sets gnd for the sensor
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
dht.begin();
delay(10000); // GIVE THE SENSOR SOME TIME TO START
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
data = "";
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}
data = "temp1=";
data.concat(t);
data.concat("&hum1=");
data.concat(h);
if (client.connect("mysite.com",80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /add.php HTTP/1.1"); // path to the add.php file
client.println("Host: mysite.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
Serial.print(t);
Serial.print("\n");
Serial.print(h);
Serial.print("\n");
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
delay(30000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
Here is the php code that interfaces from the arduino to the mysql
<?php
include("connect.php");
$link=Connection();
$temp1=$_POST["temp1"];
$press1=$_POST["press1"];
$hum1=$_POST["hum1"];
$sensorname1=$_POST["sensorname"];
$query="update tempLog set temperature='$temp1',humidity='$hum1',pressure='$press1',user='$sensorname1'";
mysql_query($query,$link);
mysql_close($link);
header("Location: index.php");
?>
I called it sensorname that is what is to the mysql.
and this is the index page that displays all the information from the mysql
<?php
include("connect.php");
$link=Connection();
$result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);
?>
<html>
<head>
<title>Sensor Data</title>
<meta http-equiv="refresh" content="15">
</head>
<body>
<h1>Temperature / Humidity / Barometric Pressure Sensor Readings</h1>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<td> Timestamp </td>
<td> Temperature 1 </td>
<td> Humidity 1 </td>
<td> Pressure </td>
<td> Sensor name </td>
</tr>
<?php
if($result!==FALSE){
while($row = mysql_fetch_array($result)) {
printf("<tr><td> %s </td><td> %s </td><td> %s </td><td> %s </td><td> %s </td></tr>",
$row["timeStamp"], $row["temperature"], $row["humidity"], $row["pressure"], $row["sensorname"]);
}
mysql_free_result($result);
mysql_close();
}
?>
</table>
</body>
</html>
Please someone help me. To add this one part, I'm so lost.