ho provato ad usare il file json per memorizzare i dati con pagine php e visualizzarli ma mi scrive bene data e ora ma non mi scrive temperature e umidita probabile errore invio stringa con GET
posto tutti i file
file arduino
#include "WiFiEsp.h"
#include <avr/dtostrf.h>
#include "DHT.h"
#define DHTPIN 31
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
char ssid[] = "XXXXXXXXX";
char pass[] = "XXXXXXXX";
int status = WL_IDLE_STATUS;
char website [] PROGMEM = "www.simonreef.it";
char dati[70];
String strURL = "";
// Initialize the Ethernet client object
WiFiEspClient client;
void setup()
{
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(115200);
// initialize ESP module
WiFi.init(&Serial1);
dht.begin();
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
printWifiStatus();
float temperature = dht.readTemperature();
char s_temperature[5]; // 5 caratteri (5 char) perché il nostro numero decimale (float) è formato da: [ un intero a due cifre, un punto, un decimale, ed in fine si aggiungerà il terminatore di stringa (cioè \0) ]
dtostrf(temperature, 4, 1, s_temperature);
float humidity = dht.readHumidity();
char s_humidity[5]; // 5 caratteri (5 char) perché il nostro numero decimale (float) è formato da: [ un intero a due cifre, un punto, un decimale, ed in fine si aggiungerà il terminatore di stringa (cioè \0) ]
dtostrf(humidity, 4, 1, s_humidity);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C ");
sprintf(dati, "sensor[]=temperature&value[]=%s&sensor[]=humidity&value[]=%s", s_temperature, s_humidity);
Serial.println("striga dati");
Serial.println(dati);
strURL = "GET /prova/write_json.php?";
strURL += dati;
strURL += " HTTP/1.1";
Serial.println(strURL);
Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (client.connect(website, 80)) {
Serial.println("Connected to server");
// Make a HTTP request
client.println(strURL);
client.println("Host: www.simonreef.it");
client.println("Connection: close");
client.println();
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
// do nothing forevermore
while (true);
}
}
file php scrittura dati
<?php
( empty($_REQUEST) || empty($_REQUEST) ) ? exit("Non posso procedere al salvataggio, mancano i dati!") : true ;
$file = "data.json";
$json = file_get_contents($file);
$array = json_decode($json, true);
if (is_array($_REQUEST)) {
for ($i=0; $i < count($_REQUEST); $i++) {
$array[] = array(
'sensor'=> $_REQUEST[$i],
'value'=> $_REQUEST[$i],
'date'=> date('d M Y', time()),
'time'=> date('H:i:s', time())
);
}
}
else {
$array[] = array(
'sensor'=> $_REQUEST,
'value'=> $_REQUEST,
'date'=> date('d M Y', time()),
'time'=> date('H:i:s', time())
);
}
$json = json_encode($array);
file_put_contents($file, $json, LOCK_EX) ? print "- Salvataggio dati riuscito! " : print "- Salvataggio dati fallito!";
?>
file php lettura dati
<?php
$file = "data.json";
$json = file_get_contents($file);
$json = utf8_encode($json);
$array = json_decode($json, true);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Arduino - Enc28j60 - DHT22 | PHP -Json</title>
<style type="text/css">
@import url(http://fonts.googleapis.com/css?family=Quattrocento+Sans:700,400);
@import url(http://fonts.googleapis.com/css?family=Dosis:400,700);
* { margin: 0 auto; padding: 0; }
body { margin: 0; padding: 0; text-align: center; }
h1 { font-family: "Quattrocento Sans"; font-size: 30pt; margin: 35px 0 }
table { border-collapse: separate; }
th {
background: #ffc000;
color: #444;
padding: 5px;
text-align: center;
font-size: 15pt;
font-family: "Quattrocento Sans";
padding: 5px 25px;
}
tr:nth-child(even) { background: #C0C0C0; }
tr:nth-child(odd) { background: #D0D0D0; }
td {
padding:3px;
text-align: center;
color: #000;
font-size: 12pt;
font-family: "Dosis", "Quattrocento Sans";
}
</style>
</head>
<body>
<h1>Arduino - Enc28j60 - DHT22 & PHP - Json</h1>
<?php
if ( empty($array) ) {
print "
<h2>Il file è vuoto, in attesa di ricevere i primi dati ... </h2>
</body>
</html>";
exit();
}
?>
<table>
<tr>
<th>Sensor</th><th>Value</th><th>Date</th><th>Time</th>
</tr>
<?php
foreach ($array as $vett) {
print "<tr>";
foreach ($vett as $key => $value) {
print "<td>" . $value . "</td>";
}
print "</tr>";
}
?>
</table>
</body>
</html>
questo è il file data.json dove non mi scrive le variabili ma solo data e ora
[
{
"sensor":null,
"value":null,
"date":"26 Aug 2017",
"time":"20:39:10"
},
{
"sensor":null,
"value":null,
"date":"26 Aug 2017",
"time":"20:39:10"
}
]
potrei avere un vostro aiuto?
grazie