Arduino nano and ethernet shield (ENC28J60) posting to local emonsd with ethercard.h library

Hi all,

I recently bought 2 arduino nanos and 2 ethernet shields for them. The ethernet shield needs to use ethercard.h library. I am trying to post to my local emoncms with this library and there is no way.

I have tried multiple sketches and googling a lot with no result.

I have this sketch that is posting well to emoncms.org, but don’t work with my emoncms local. Please,can you help me?

/*
  This is a basic web client demo sending test data to emoncms
  It sends a couple of example variables in a semi-json like format: {power:252.4,temperature:15.4}
  
  Try creating an account on emoncms.org then get the write api key and enter in line 51 replacing
  the text YOURAPIKEY.
  
  This example features both DCHP and DNS Lookup.
  
  DHCP is where we ask the router for an ip address.
  
  DNS is where we ask a Domain name server for the ip address of the server we want to send data to:
  the domain name emoncms.org is linked to the ip address 213.138.101.177
  Using DNS Lookup we can save having to remember these hard to remember strings of numbers. 
  -----------------------------------------
  Part of the openenergymonitor.org project
  Licence: GNU GPL V3
*/

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
unsigned long timer;

const char website[] PROGMEM = "192.168.1.30";

void setup () 
{
  Serial.begin(9600);
  Serial.println("03 - Basic Web Client");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
    
  // DHCP Setup
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  // DNS Setup
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if ((millis()-timer)>5000) {
    timer = millis();
    Serial.println("Request sent");
    
    // Send some test data to the server:
    ether.browseUrl(PSTR("/emoncms/input/post?apikey=xxxxxxxx&json="), "{power:252.4,temperature:15.4}", website, 0);
  }
}

I think you have not told it which server to use. "/emoncms [etc] " means local to your Nano, which is wrong.
You must use the “dotted IP Address” of the computer where emoncms is running to tell it where on your LAN to send the data. Your router will tell you what it is. It might be something like "http://192.168.1.10/emoncms/ …[etc] "

Thanks for your fast reply Robert,

I have replaced emoncms.org for my emonsd ip: 192.168.1.30 at the top of the sketch.

(I looked for that, and did not see it.) Does emoncms appear at the root on your server?

Yes Robert, my server is at 192.168.1.30/emoncms,

Another data is that via serial I get a DNS error, “DNS failed”, with emoncms.org no porblems only with my local server.

This url is working well as a test: http://192.168.1.30/emoncms/input/post.json?json={power:200}&apikey=190528xxxxxxxx

I have other arduinos posting well to the same server but they work with ethernet,h library and this posting code:

    // if there's a successful connection:
if (client.connect(server, 80)) {
    Serial.println("Connectant...");
    // send the HTTP GET request:
    client.print("GET /emoncms/input/post?apikey=");
    client.print(apikey);
    if (node > 0) {
      client.print("&node=");
      client.print(node);
    }
    client.print("&json={");
    client.print("Potència:");
    client.print(ct1.realPower);
    client.print(",Tensió:");
    client.print(ct1.Vrms);
    client.print(",Temperatura_ext:");
    client.print(temp);
    client.print(",Humitat_ext:");
    client.print(hum);  
    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("Conexió fallada");
    Serial.println("Desconnectant...");
    client.stop();
  }

I have found some sketches but nothing solved, I don’t know what mistake I have. Last sketch I have been working is this one, with stiatic ip. But nothing received at my emonsd server… This sketch is to use with DHT sensor to post H and T data. For now, post string is posting example data: {power:252.4,temperature:15.4}

I don’t understand where is the problem. How can i know how ether.browseUrl is composing the url??

This is working ok on webbrowser: 192.168.1.30/emoncms/input/post?apikey=190528xxxxxxxxxxx&node=2&json={power:252.4,temperature:15.4}

I found this topic of the archived forum talking about that problem:
https://openenergymonitor.org/forum-archive/node/319.html
But the working sketch: NanodeRF_singleCT It’s not alive today…

//LLIBRERIES
#include <EtherCard.h>
#include <DHT.h>;

//CONSTANTS DHT
#define DHTPIN 5     // pin del sensor DHT
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//VARIABLES DHT
int chk;
float hum;  //Enmagatzema el valor humitat
float temp; //Enmagatzema el valor temperatura


//ETHERNET
#define REQUEST_RATE 5000 // milliseconds


static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };  // ethernet interface mac address
static byte myip[] = { 192,168,1,32 };                    // ethernet interface ip address
static byte gwip[] = { 192,168,1,1 };                     // gateway ip address
static byte hisip[] = { 192,168,1,30 };                   // remote website ip address and port
const char website[] PROGMEM = "";            // Set this to the domain name of your hosted emoncms - leave blank if posting to IP address



byte Ethernet::buffer[300];   // a very small tcp/ip buffer is enough here
static long timer;

// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
  Serial.print("<<< reply ");
  Serial.print(millis() - timer);
  Serial.println(" ms");
  Serial.println((const char*) Ethernet::buffer + off);
}

void setup () {

  Serial.begin(9600);
  Serial.println("ARDUINO NANO MENJADOR");
  Serial.println("\n[getStaticIP]");
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");

  ether.staticSetup(myip, gwip);

  ether.copyIp(ether.hisip, hisip);
  ether.printIp("Server: ", ether.hisip);

  while (ether.clientWaitingGw())
    ether.packetLoop(ether.packetReceive());
  Serial.println("Gateway found");
  
  timer = - REQUEST_RATE; // start timing out right away


//ARRENCAR SENSOR DHT
  dht.begin();  
}

void loop () {

//LLEGIR VARIABLES HUM I TEMP DHT
    delay(2000); //2" entre lectures de sensor
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    
  // IMPRIMIR DADES PORT SERIE

  Serial.print("Temperatura: ");
  Serial.print(temp);
  Serial.print("ºC\t");
  Serial.print("Humitat: ");
  Serial.print(hum);
  Serial.println("%");


  
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer + REQUEST_RATE) {
    timer = millis();
    Serial.println("\n>>> REQ");
    ether.browseUrl(PSTR("/emoncms/input/post?apikey=190528433xxxxx&node=2&json="), "{power:252.4,temperature:15.4}", website, 0);
    }


  
}

It has been a year or two since I used the Ethercard library, I will have to leave it to someone more familiar with it to help you.

Ok, thanks you Robert, I thing I will replace arduino nanos to arduino UNO with ethernet shield to use ethernet.h shield.

That could be the best solution. I found the documentation for Ethercard not clear and confusing, and I am English. For you it must be really difficult.

I looked at the sketches I have for the NanodeRF, but they did not help.