Ciao a tutti, sto cercando di fare un o sketch che invia con metodo post dati json al server, ma non riesco a capire come inserire l'ObjectJson nel post
#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>
#include <utility/w5100.h>
/* Libreria sensore corrente ---- */
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
/* settaggio ethernet shield */
byte mac[] = {0x2E, 0x9B, 0x92, 0x55, 0x98, 0xDE};
IPAddress ip(x.x.x.x);
IPAddress myDns(8, 8, 8, 8);
EthernetClient client;
char server[] = "x.x.x.x";
/* variabili */
String ComandoJson ="";
unsigned long Tuc = 0; // tempo ultima connessione
const unsigned long Intervallo = 2L * 999L;
void setup() {
StaticJsonBuffer <200> jsonBuffer;
Serial.begin(9600);
delay(1000);
// inizializzo la ethernet shield
Ethernet.begin(mac, ip, myDns);
W5100.setRetransmissionCount(1);
Serial.println("***** ARDUINO SENSORI *****");
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
emon1.current(A0, 1);
delay(1000);
}
void loop() {
if (millis() - Tuc > Intervallo) {
SensoreCorrente();
Tuc = millis();
}
inviodb();
}
void inviodb() {
client.stop();
// Se la stringa json è vuota non invio niente
if(ComandoJson != ""){
if (client.connect(server, 80)) {
Serial.println("connecting...");
client.println("POST http://x.x.x.x/energia.php HTTP/1.1");
client.print("Host: ");
client.println(server);
client.print("Content-Length: ");
client.println(String(ComandoJson.length()));
client.println("Content-Type: application/json;charset=utf-8");
client.println("Connection: close");
client.println();
client.println(ComandoJson);
client.println();
Serial.println(ComandoJson());
} else {
Serial.println("connection failed");
}
ComandoJson = "";
}
}
void SensoreCorrente(){
JsonObject & root = jsonBuffer. createObject ();
double Irms = emon1.calcIrms(1480); // ampere in rms
root [ " sensor " ] = " Energia " ;
root [ " value " ] = String(Irms) ;
root.printTo(Serial);
root.prettyPrintTo(Serial);
}
Chi sa darmi qualche dritta?
grazie in anticipo