sensorlogger icon indicating copy to clipboard operation
sensorlogger copied to clipboard

[USAGE]

Open stockreign opened this issue 3 years ago • 7 comments

Platform you are using

nextcloud

Sensors/Hardware

NodeMCU DHT11

Your Goals

LogData from NodeMCu to nextcloud serve

Your Question

In arduino serial monitor i keep getting ERROR HTTP Response Code -11, i think i have problem with creatting device and perform a createLog request. What could be the problem? Many thanks

stockreign avatar Mar 17 '21 06:03 stockreign

@stockreign Thanx for reporting.

Could you post the code you are using? My best guess for the moment: http vs https

alexstocker avatar Mar 17 '21 18:03 alexstocker

#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <ESP8266HTTPClient.h> #include <ArduinoJson.h>

const int led = LED_BUILTIN; const char* ssid = "xxxxxx"; // network ssid const char* password = "xxxxxx"; // network passphrase const char* apiUrl = "http://xxxxxxxxxxxx/index.php/apps/sensorlogger/api/v1/createlog/"; const char* username = "xxxxxx"; // username const char* token = "xxxxxxx"; // token or password const char* host = "128.138.141.172"; // NTP Pool or Host

String dateTime = "";

ESP8266WebServer server(80);

void handleRoot() { digitalWrite(led, 1); server.send(200, "text/plain", "hello from Hell"); digitalWrite(led, 0); }

void getDateFromNtp() { WiFiClient client; const int httpPort = 13; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); delay(1000); } client.print("HEAD / HTTP/1.1\r\nAccept: /\r\nUser-Agent: Mozilla/4.0 (compatible; ESP8266 NodeMcu Lua;)\r\n\r\n"); delay(100); char buffer[12]; while(client.available()) { String line = client.readStringUntil('\r'); if (line.indexOf("Date") != -1) {

} else {
  dateTime = line.substring(7, 24);
  Serial.println(dateTime);
}

} }

void handleNotFound(){ digitalWrite(led, 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(led, 0); }

void setup(void){ pinMode(led, OUTPUT); digitalWrite(led, 0); Serial.begin(115200); WiFi.begin(ssid, password); Serial.println("");

// Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); }

server.on("/", handleRoot);

server.onNotFound(handleNotFound);

server.begin(); Serial.println("HTTP server started"); }

void loop(void){ server.handleClient(); if(WiFi.status() == WL_CONNECTED) { DynamicJsonBuffer jsonBuffer;

getDateFromNtp();

int rTemp = rand() % 20; // random int
int rHum = rand() % 99; // random int

digitalWrite(led, 0);

HTTPClient http;

http.begin(apiUrl);

http.setAuthorization(username,token);
http.addHeader("Content-Type", "application/json");

JsonObject& root = jsonBuffer.createObject();
root["deviceId"] = "0100110-001100110-1101";
root["temperature"] = rTemp;
root["humidity"] = rHum;
root["date"] = "20"+dateTime;

String jsonStr;
root.printTo(jsonStr);
int httpCode = http.POST(jsonStr);

http.end();

if(httpCode != 200) {
  Serial.print("ERROR HTTP Response Code ");
  Serial.println(httpCode);
      digitalWrite(led,1);
      delay(250);
      digitalWrite(led,0);
      delay(250);
      digitalWrite(led,1);
      delay(250);
      digitalWrite(led,0);
      delay(1000);
} else {
  digitalWrite(led, 1);
  delay(10000);
}

}else{ digitalWrite(led,0); delay(250); digitalWrite(led,1); delay(5000); }

}

stockreign avatar Mar 17 '21 19:03 stockreign

@stockreign Thanx. I see. Your using https://github.com/alexstocker/sensordata/blob/master/sensordata.ino Is your Nextcloud instance you are sending the request to located at your local network or out in the wild? Domain names propably not resolved by your NodeMCU, so use the IP address of your Nextcloud Instance.

alexstocker avatar Mar 18 '21 15:03 alexstocker

Thank you for reply. I had it with IP address, i got to have device ID name on the device list in nextcloud, but no data is showing and i am still getting HTTTP response error.

stockreign avatar Mar 19 '21 17:03 stockreign

@stockreign you should try to send some random data from your PC first using a cron job / PHP script / curl in a loop. Once you are able to log data to the sensorlogger it should be easier to find and fix the issue on the ESP8266 and debug the code there.

I had to configure https on my side for which I am using the WiFiClientBearSSL library on ESP8266 / ESP32 from Earle F. Philhower, III.

There are various ways to verify the certificate chain of your Let's Encrypt certificate chain too. I verified Fingerprint, Certificate, Chain Cert, Root Cert and will probably stick with both the Cert and Chain Cert in the end. However I recall that setting the clock via the NTP code (port 13) above did not work for me right out-of-the-box, hence I used the time returned via a simple HEAD request to my server instead.

@alexstocker are there plans to provide an example for getting LoRaWAN messages from The Things Network / The Things Stack into sensorlogger too ?

stefan123t avatar Oct 20 '21 01:10 stefan123t

Here is the routine I use from Earle's library https://github.com/earlephilhower/bearssl-esp8266

#include <time.h>

// Set time via NTP, as required for x.509 validation
void setClock() {
  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  Serial.print("Waiting for NTP time sync: ");
  time_t now = time(nullptr);
  while (now < 8 * 3600 * 2) {
    delay(500);
    Serial.print(".");
    now = time(nullptr);
  }
  Serial.println("");
  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.print("Current time: ");
  Serial.print(asctime(&timeinfo));
}

You may need or want to adjust for the timezone setting depending on your location and the ntp servers used.

stefan123t avatar Oct 20 '21 01:10 stefan123t

@stockreign You may also look into the example https://github.com/alexstocker/sensordata/ and the proposed fix by @christoschronopoulos in https://github.com/alexstocker/sensordata/issues/6

stefan123t avatar Oct 20 '21 09:10 stefan123t