Emoncms

good morning everybody,
i've search in more website but i can't comunicare with my account Emoncms ...
my code is:

/*
  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

  author Mirco Piccin aka pitusso

  based on
  http://arduino.cc/en/Tutorial/WebClientRepeating
*/

char foo;  //without a simple variable declaration, use of #ifdef at the top of your code raises an error!

//if using a W5100 based Ethernet shield, comment out the following line;
//leave untouched if using Arduino Wifi Shield
//#define WIFI

#include <SPI.h>

#ifdef WIFI
#include <WiFi.h>
#include <WiFiClient.h>
#else
#include <Ethernet.h>
#endif

// Include Emon Library
#include "EmonLib.h"

//network configuration, WIRED or WIFI
#ifdef WIFI
//if using WIFI
char ssid[] = "ssid"; //  your network SSID (name)
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;
int keyIndex = 0;            // your network key Index number (needed only for WEP)

//WiFiClient client;
#else
//if using WIRED
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x69, 0xD5};

// fill in an available IP address on your network here,
// for auto configuration:
IPAddress ip(10, 0, 0, 7);
IPAddress subnet(255, 0, 0, 0);
IPAddress DNS(8, 8, 8, 8);
IPAddress gw(10, 0, 0, 254);

EthernetClient client;
#endif

//Calibrations
const int volt = 220;
const float ct_calibration = 29;
const float temp_offset = -1.2;

// Sensor pins
const int tempSensorPin = A0;
const int lightSensorPin = A1;
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
//IPAddress server(213, 138, 101, 177);  // numeric IP for emoncms.org (no DNS)

String apikey = "b5be5641dfe299bcb3da3b480d50ae82";  //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
  Serial.println("Emoncms client starting...");

  emon1.current(currentSensorPin, ct_calibration);

#ifdef WIFI
  // 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 SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network.
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
#else
  /*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, dns, gw, subnet);
  }*/
#endif

  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);
    tempValue = getCelsius(analogRead(tempSensorPin));
    Irms = emon1.calcIrms(1480);

    //Print values (debug)
    Serial.println();
    Serial.print("Temp : ");
    Serial.print(tempValue);
    Serial.print(" ; Light : ");
    Serial.print(lightValue);
    Serial.print(" ; Power : ");
    Serial.println(Irms * volt);

    //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() {
#ifdef WIFI
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.print(" dBm");
#else
  // 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(".");
  }
#endif
  Serial.println();
}

float getCelsius(int sensorValue) {
  /*
    created by Federico Vanzati for TinkerKit Thermistor Library
  */
  const static float ADCres = 1023.0;
  const static int Beta = 3950;      // Beta parameter
  const static float Kelvin = 273.15; // 0°C = 273.15 K
  const static int Rb = 10000;      // 10 kOhm
  const static float Ginf = 120.6685; // Ginf = 1/Rinf

  float Rthermistor = Rb * (ADCres / sensorValue - 1);
  float _temperatureC = Beta / (log( Rthermistor * Ginf )) ;
  return _temperatureC - Kelvin;
}

and my output in the serial monitor is:

i've follow this guide:

http://playground.arduino.cc/italiano/emoncms

please help me ;(

..me again.
The SSID, Password, IP and mask are probably incorrect. They must match your network to be able to get a connection.

to determine IP/mask/gateway:
On a Windows PC: "start" - "run" - type "cmd" to open a command window.
In this windows: Type: ipconfig /all and press
Take note of IP, mask, gateway.
This may look like:

IP 192.168.1.5
mask 255.255.255.0
gateway 192.168.1.1

for arduino - select

IP 192.168.1.200 // high value here
mask 255.255.255.0
gateway 192.168.1.1

I use a ethernet shield for which the ssid and the password do not matter... and i work in a office and the responsible tell me the ip, gateway and subnet mask....

You got the mask of an A-net. very strange as most companies divide their networks into several C-nets

yes but if my office have a A-net because if they use a C-net the switchboard doesn't work

I see that status-report tells IP 255.255.255.255
You have diabled part of code that needs to be active.

Tidy up - remove the wifi-related - then U'll see the problem

/*
  Arduino & OpenEnergyMonitor

  This sketch connects to an emoncms server and makes a request using
  Arduino Ethernet shield (or other based on Wiznet W5100)
  author Mirco Piccin aka pitusso

  based on
  http://arduino.cc/en/Tutorial/WebClientRepeating
*/

char foo;  //without a simple variable declaration, use of #ifdef at the top of your code raises an error!

//if using a W5100 based Ethernet shield, comment out the following line;

#include <SPI.h>
#include <Ethernet.h>

// Include Emon Library
#include "EmonLib.h"

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x69, 0xD5};

// fill in an available IP address on your network here,
// for auto configuration:
IPAddress ip(255, 255, 255 ,255);
IPAddress subnet(255, 0, 0, 0);
IPAddress DNS(8, 8, 8, 8);
IPAddress gw(10, 0, 0, 254);

EthernetClient client;

//Calibrations
const int volt = 220;
const float ct_calibration = 29;
const float temp_offset = -1.2;

// Sensor pins
const int tempSensorPin = A0;
const int lightSensorPin = A1;
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
//IPAddress server(213, 138, 101, 177);  // numeric IP for emoncms.org (no DNS)

String apikey = "b5be5641dfe299bcb3da3b480d50ae82";  //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
  Serial.println("Emoncms client starting...");

  emon1.current(currentSensorPin, ct_calibration);
  /*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, dns, gw, subnet);
  }*/

  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);
    tempValue = getCelsius(analogRead(tempSensorPin));
    Irms = emon1.calcIrms(1480);

    //Print values (debug)
    Serial.println();
    Serial.print("Temp : ");
    Serial.print(tempValue);
    Serial.print(" ; Light : ");
    Serial.print(lightValue);
    Serial.print(" ; Power : ");
    Serial.println(Irms * volt);

    //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:
  Serial.print("arrivato");
  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.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();
}

float getCelsius(int sensorValue) {
  /*
    created by Federico Vanzati for TinkerKit Thermistor Library
  */
  const static float ADCres = 1023.0;
  const static int Beta = 3950;      // Beta parameter
  const static float Kelvin = 273.15; // 0°C = 273.15 K
  const static int Rb = 10000;      // 10 kOhm
  const static float Ginf = 120.6685; // Ginf = 1/Rinf

  float Rthermistor = Rb * (ADCres / sensorValue - 1);
  float _temperatureC = Beta / (log( Rthermistor * Ginf )) ;
  return _temperatureC - Kelvin;
}

this is my code now... but the output is the same... ;(

why is this commented out ? I think it should be active.

emon1.current(currentSensorPin, ct_calibration);
  /*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, dns, gw, subnet);
  }*/

maybe only this statment is enough:

Ethernet.begin(mac, ip, dns, gw, subnet);

(I'll be offline till monday..)

no because i read in a guide that this row means in the net witout dhcp....

..read more closely.
looks like u dont ask the DHCP-server. Therfore: no good IP

i try with every example of the library but it don't work.... i don't know why... :frowning:
in other topic some people ask me that use a banal code to veryfi that the shield work but i can't ping it in the cmd.... i assign an ip address but the shield when i use the function Ethernet.localIP(); in the serial monitor i see an address similar to: 0.0.0.0 or 0.65.65.65.... :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

Are theese number a result when running the example "DhcpAddressPrinter" ?
Are connections to pins 10..13 correct?

the pin are connect with shield

If u dont use the SD-card - set pin 4 to output and write it high.
the again: ...Are theese number a result when running the example "DhcpAddressPrinter" ?

read section 2: https://www.arduino.cc/en/Guide/ArduinoEthernetShield

/*
  DHCP-based IP printer
 
 This sketch uses the DHCP extensions to the Ethernet library
 to get an IP address via DHCP and print the address obtained.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 12 April 2011
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <EthernetV2_0.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, 0x10, 0x06, 0x2E };

// 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;
#define W5200_CS  10
#define SDCARD_CS 4
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(SDCARD_CS,OUTPUT);
 digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // this check is only needed on the Leonardo:
   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:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My 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();
}

void loop() {

}

this is the code of the DhcpAddressPrinter example but when i run it in my serial monitor there isn't nothing... :frowning:

Your code referns W5200...
I found this in other post:

This is a w5100 ethernet controller. It uses the standard ethernet library.
Ethernet Shield V1.0 | Seeed Studio Wiki

This is a w5200 ethernet controller. It uses the EthernetV2_0 library.
Ethernet Shield V2.0 | Seeed Studio Wiki

relevant ?

yes but i have a w5500 controller and i've read in other post that the library for w5500 is the same of w5200...

fun.. I cannot see whats wrong. I'd try "start from scratch". eg here: POE W5500 Ethernet Shield - ITEAD Wiki

ok halp me... what would you like that i do for you??

Read the instructions anbout the 55xx card