That becomes a bit of a self-fulfilling prophecy, it will be a dead end if you do not pay attention to the error messages.
If you tell us what the error is we might be able to help.
If it works for emoncms.org, it should also work for local emoncms. Don’t worry too much about getting the nodemcu to print the url right now.
If you are getting an error and unable to post an input via the api in a browser then that needs resolving first.
Once we are able to post via an api, then we can retest the nodemcu. AFAIA all you may need to do is alter the const char* host = "192.168.0.133" to const char* host = "192.168.0.133/emoncms"IF we are able to get the url with /emoncms in it to work, but not without it.
Paul, your’re right!! Yesterday was frustrating and killed my motivation
Ok, a added /emoncms in the browser: http://172.23.56.146/emoncms/input/post.json?node=Kitchen&apikey=emonpicmsreadandwriteapikey&json={temperature:22.20,humidity:23.42,pressure:957.39}
gives back in the browser window: {"success": false, "message": "Format error, json value is not numeric"}
AFAIA all you may need to do is alter the const char* host = "192.168.0.133" to const char* host = "192.168.0.133/emoncms"IF we are able to get the url with /emoncms in it to work, but not without it.
I already tried that before, but got the same results:
no inputs and enter the link from above in the browser gives the same output:
{"success": false, "message": "Format error, json value is not numeric"}
it would seem the format of the url isn’t right, unless you go fulljson and add all the quote marks, you would need to just remove .json from your url eg
As I said, this isn’t important until you get it working in a browser, BUT it is good news that it is returning the same error, that potentially indicates that all will be well once you fix the url in the browser and introduce those edits (and the /emoncms) to your firmware.
AND when I’m checking the inputs in the emoncms menu then there are the 3 inputs with the values from the link above !!
So far so good, but I have still no idea, how to get the whole thing with real values into the cms…
Sorry, but at the moment, I’m more confused than in the beginning…
So it all sounds good and hopefully the final piece is to remove the .json from the nodemcu firmware. Somewhere it will currently have something like /input/post.json? which needs changing to /input/post?, this might be part of a longer string including node= etc, but the change is the smae, whatever is there that includes .json remove the .json and it should work, if your unsure, post the section of code that needs amending or the whole sketch for us to look at.
After a reboot I’ll get my 3 inputs, but no values and it’s not updating, the cms shows:
n/a NULL
Here’s the whole code
// Including the Libraries for WiFi Connection, HTTP Send and bme280 Sensor
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Replace with your network details
const char* ssid = "mySSID";
const char* password = "RouterPasswd";
Adafruit_BME280 bme;
//emoncms settings, change node ID for every node
const char* host = "192.168.0.133/emoncms";
const char* nodeId = "Kitchen";
const char* privateKey = "readandwriteapikey";
float h = 0;
float t = 0;
float p = 0;
int vcc;
ADC_MODE(ADC_VCC);
char temperatureCString[6];
char humiditiyString[6];
char pressureString[7];
const int httpPort = 80;
//Seconds to deep sleep 300 = 5Min
const int sleepTimeInSec = 300;
//Variables to store values
// ==============================================
// only runs once on boot then go deep sleep again
// ==============================================
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(500);
//Start sensor orginal bei 0x76
bme.begin(0x76);
//Connect WiFi
//Fix https://github.com/esp8266/Arduino/issues/2186
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Sensor readings may also be up to 2 seconds 'old', so we read twice
h = bme.readHumidity();
t = bme.readTemperature();
p = bme.readPressure() / 100.0;
delay(2500);
h = bme.readHumidity();
t = bme.readTemperature();
p = bme.readPressure() / 100.0;
//vcc = ESP.getVcc();//readvdd33();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from bme280 sensor!");
return;
}
Serial.print("Luftfeuchte: ");
Serial.print(h);
Serial.print(" %\t Temperatur: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("%\t Luftdruck: ");
Serial.print(p);
//Send data to emoncms
sendData();
//go to deep sleep
ESP.deepSleep(30e6);
}
// ==============================================
// Send Data to emoncms
// ==============================================
void sendData() {
WiFiClient emonHTTPClient;
//Connect by http
const int httpPort = 80;
if (!emonHTTPClient.connect(host, httpPort)) {
return;
}
//Build the JSON for emoncms to send data
String json = "{temperature:";
json += t;
json += ",humidity:";
json += h;
json += ",pressure:";
json += p;
//json += ",vcc:";
//json += vcc;
json += "}";
//Build emoncms URL for sending data to
String url = "/input/post?node=";
url += nodeId;
url += "&apikey=";
url += privateKey;
url += "&json=";
url += json;
// Send the HTTP Request to the emoncms server
emonHTTPClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while (emonHTTPClient.available()) {
String line = emonHTTPClient.readStringUntil('\r');
}
emonHTTPClient.stop();
}
void loop(void) {
}
Sorry, it’s not formatting correctly… [no worries, I’ve formatted it by adding ``` (3x backticks) on the lines before and after the code - pb66]
If the firmware previously worked and the only changes you have made are editing the apikey, the server IP (incl adding /emoncms) and removing the .json. I don’t see why it wouldn’t work, this has been proven by the manual use of the url in the browser.
The only potential difference I can think of is if you are logged in when you try the url. Can you therefore try the url again in an incognito browser session (or just log out of emoncms) . This will also test the apikey is correct.
What reply appears in the nodemcu serial?
this bit of the firmware looks incomplete to me (but I’m no expert on esp8266)
// Read all the lines of the reply from server and print them to Serial
while (emonHTTPClient.available()) {
String line = emonHTTPClient.readStringUntil('\r');
}
to me it looks like it gets the reply, but doesn’t make any attempt to print it, try adding a serial.print line here eg
// Read all the lines of the reply from server and print them to Serial
while (emonHTTPClient.available()) {
String line = emonHTTPClient.readStringUntil('\r');
Serial.print(line);
}
but that is a guess and untested, if anyone can confirm or correct my guess, please do.
When I advised to add it to the url, we couldn’t see the whole sketch and I assumed a usual basic url formation. You are right, I have seen this before, the server actually is 172.23.56.146 not 172.23.56.146/emoncms. adding /emoncms to the server info (ie 172.23.56.146) was a lazy way of doing it that only works with full url string formation, not when the components are passed as separate args.
So @chalkmaroon both edits (adding /emoncms and removing .json) should happen in the same line ie what was originally
//Build emoncms URL for sending data to
String url = "/input/post.json?node=";
should now be
//Build emoncms URL for sending data to
String url = "/emoncms/input/post?node=";
The only potential difference I can think of is if you are logged in when you try the url. Can you therefore try the url again in an incognito browser session (or just log out of emoncms) . This will also test the apikey is correct.
This changes nothing.
I changed to const char* host = "192.168.0.133";
and
String url = "/emoncms/input/post?node=";
Still get the same result, 3 inputs, but no values…