Arduino post to local emoncms (raspberry with emonsd)

Hi all,

I have an arduino configured and posting well some humidity and temperature data to emoncms.org with this sketch: EmonESP/esp_dht22_emoncms.ino at master · openenergymonitor/EmonESP · GitHub

Now I have installed emonsd on a raspberry pi, and i have a local emoncms running, but i can’t got the arduino post to the local server.

I have updated host and api in the sketch but don’t work. I have tried:
host: 192.168.1.xxx
host: http://www.192.168.1.xx
host: 192.168.1.xxx/emoncms

If I try with the manual JSON post: http://192.168.1.xxx/emoncms/input/post.json?node=1&json={power1:100,power2:200,power3:300}
It works.

Thanks,

This should be correct. Please post the exact sketch you are trying to use. I will test.

Is the Arduino connected to the same network as the Raspi? Can you ping the Arduino / ESP from the Raspi?

Hi Glyn,

Yes arduino and raspi are in the same network. I have tried and I can ping from raspi to arduino with no problems.

This is the sketch:

//simple sketch reading from DHT22 and posting up to emoncms.org using the ESP8266 wifi module.
// This file is part of OpenEnergyMonitor project.



#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 12    


#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321


DHT dht(DHTPIN, DHTTYPE);


const char* ssid = "XXXXXXX";
const char* password = "XXXXXX";

const char* host = "192.168.1.xxx/emoncms";


void setup() {
  Serial.begin(9600);//Baud rate
  delay(10);

  

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  
  dht.begin();
}

int value = 0;

void loop() {
  delay(2000);
  ++value;
//  DHT22_ERROR_t errorCode;


  
  Serial.print("Requesting data...");
//  errorCode = DHT.readData();
//  switch(errorCode)
  {
   

  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);
  Serial.print("\n");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\n");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C\n ");

  // We now create a URI for the request
  String url = "/input/post.json?csv="+String(h)+","+String(t)+"&apikey=XXXXXXXXXXXXXXXXX"; //Enter api key here

  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
  }
}

What is the https response when you try and post? I’m assuming you have also copied the new R/W API key from the RasPi emonSD?

Yes, I updated the api with the new one. How I see http response? I will check tomorrow.

Does it make a difference if you add the “emoncms” sub-directory to the front of the url rather than the end of the host ?

Thanks pb66, I have tried but the same result. Is there another arduino sketch that I can try?