agostino:
Grazie per la pronta risposta, chiedo scusa per il ritardo mio.
Questo è il link del tutorial:
Arduino Playground - HomePage
E questo è il link dello sketch:
Tutorial-Wired/OpenEnergyMonitor/emon_simple_client/emon_simple_client.ino at master · OfficineArduino/Tutorial-Wired · GitHub
Grazie
Anch'io ho basato il progetto sullo stesso tutorial e sullo stesso sketch.
Ho pero "depurato" il codice dallo shield wifi in quanto utilizzo, come te, la shield ethernet.
ti copio il codice così ti fai un'idea di quello che ho tolto e magari ti può tornare utile.
/*
Arduino & OpenEnergyMonitor
This sketch connects to an emoncms server and makes a request using
Arduino Ethernet shield (or other based on Wiznet W5100) or an
Arduino Wifi Shield
re-elaboration of Mirco Piccin aka pitusso p
based on
http://arduino.cc/en/Tutorial/WebClientRepeating
*/
#include <SPI.h>
#include <Ethernet.h>
// Include Emon Library
#include "EmonLib.h"
//if using WIRED
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // fill in an available IP address on your network here,
// for auto configuration:
IPAddress ip(10, 0, 1, 188);
IPAddress subnet(255, 255, 255, 0);
//IPAddress dns(8, 8, 8, 8);
//IPAddress gw(192, 168, 1, 254);
EthernetClient client;
//Calibrations
const int volt = 220;
const float ct_calibration = 29;
const float temp_offset = 4;
// Sensor pins
const int tempSensorPin = A1;
const int lightSensorPin = A0;
const int currentSensorPin = A2;
float tempValue = 0;
float Irms = 0;
int lightValue = 0;
// Create an Emon instance
EnergyMonitor emon1;
//Emoncms configurations
char server[] = "emoncms.org"; // name address for emoncms.org
String apikey = "yourstringapikey"; //api key
int node = 0; //if 0, not used
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
void setup() {
// start serial port:
Serial.begin(9600);
// Display a welcome message
Ethernet.begin(mac, ip);
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("Failed to get an IP address using DHCP, forcing manually");
Ethernet.begin(mac, ip);
}
Serial.println("Emoncms client starting...");
emon1.current(currentSensorPin, ct_calibration);
printStatus();
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("Disconnecting...");
client.stop();
}
// if you're not connected, and at least <postingInterval> milliseconds have
// passed sinceyour last connection, then connect again and
// send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
//read sensors
lightValue = analogRead(lightSensorPin);
Irms = emon1.calcIrms(1480);
tempValue = (analogRead(tempSensorPin) * 0.4883 -50);
//Print values (debug)
Serial.println();
Serial.print("Temp : ");
Serial.print(tempValue - temp_offset);
Serial.print(" ; Light : ");
Serial.print(lightValue);
Serial.print(" ; Power : ");
Serial.println(Irms*volt);
Serial.println(analogRead(A2));
//send values
sendData();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData() {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("Connecting...");
// send the HTTP GET request:
client.print("GET /api/post?apikey=");
client.print(apikey);
if (node > 0) {
client.print("&node=");
client.print(node);
}
client.print("&json={temp");
client.print(":");
client.print(tempValue-temp_offset);
client.print(",light:");
client.print(lightValue);
client.print(",power:");
client.print(Irms*volt);
client.println("} HTTP/1.1");
client.println("Host:emoncms.org");
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting...");
client.stop();
}
}
void printStatus() {
// print your local IP address:
Serial.print("IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}