ah I see it is not implemented
Check here
Ah great, I see. Thank you! Now I can get an output for temperature. Humidity does not work, though. It only produces “nan” even though I tried converting it just like Temperature. But I hope I’ll figure it out, if not I’ll only display temp for now ^^
@rikki78 said in Using API with c++/arduino:
char urlHum[80];
That’s too small and generally it is risky to use fixed size buffers without checking sizing requirements as it may cause an overflow.
The format string “http://%s:%s@%s/api/device/%schangeTemperatureTo?temperature=%.2f” already has 61 bytes (including the terminating 0 byte) and
the format string “http://user:password@192.168.1.21/api/device/schlafzimmer-dht1changeTemperatureTo?temperature=%.2f” has 99bytes. Note, the parameters will also require space. With the second format string the buffer size will already be exceed with a one digit number as “%.2f” will fill 4 bytes in the buffer, at least.
Use char urlHum[255];
or even better use a helper function which handles the memory allocation internally. I am not sure Arduino library may fit in here but I am sure there is helper function available like “asprintf”. See https://stackoverflow.com/questions/3774417/sprintf-with-automatic-memory-allocation and https://github.com/littlstar/asprintf.c/blob/master/asprintf.c. For the latter, I am not sure this will work on Arduino
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
Thanks @mwittig
This entire project is growing over my head though. I cannot post these urls via http get, at least not figure out how to. I had used the example esp8266 http client script and modified it, but it only produces an immense error log (too long to post).
This is the code, in case somebody notices a minor fixable error I might have made ^^
#include <ESP8266WiFi.h>
#include "DHT.h";
#include <string>
#include <iostream>
// Pin für DHT11 Sensor festlegen
#define DHTPIN 4
// Typ definieren
#define DHTTYPE DHT11
const char* ssid =
const char* password =
const char* host =
const char* myDev =
const char* myUsr =
const char* myPwd =
// folgende Variabeln __nicht__ ändern
char urlHum[80];
char urlTem[80];
char str_temp[8];
char str_hum[8];
// Namen des Sensors definieren
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
delay(10);
}
void loop() {
int value = 0;
dht.begin();
delay(5000);
++value;
// Luftfeuchtigkeit auslesen und als "Luftfeuchte" speichern
float feuchte = dht.readHumidity();
dtostrf(feuchte, 2, 4, str_hum);
// Temperatur auslesen und als "Tempe" speichern
float tempe = dht.readTemperature();
dtostrf(feuchte, 2, 4, str_temp);
// Verbindung mit WiFi herstellen
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
sprintf(urlHum,"http://%s:%s@%s/api/device/%s/changeHumidityTo=humidity=%s", myUsr, myPwd, host, myDev, str_hum);
sprintf(urlTem,"http://%s:%s@%s/api/device/%s/changeTemperatureTo=temperatue=%s", myUsr, myPwd, host, myDev, str_temp);
//Serial.print("Requesting URL: ");
//Serial.println(urlHum);
// This will send the request to the server
client.print(String("GET ") + urlHum + " HTTP/1.1\r\n" +
// "Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
client.print(String("GET ") + urlTem + " 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");
}
@Ben-S. said in Using API with c++/arduino:
char urlHum[80];
char urlTem[80];
char urlHum[255];
char urlTem[255];
"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law
@mwittig I agree with you
and this one
sprintf(urlTem,"http://%s:%s@%s/api/device/%s/changeTemperatureTo=temperatue=%s", myUsr, myPwd, host, myDev, str_temp);
If you check the output above you see there is a missing / in the result, after the devicename. This should help.
Print your string as formatted from arduino and paste it in a browser to test it
sprintf(urlTem,"http://%s:%s@%s/api/device/%s//changeTemperatureTo=temperature=%s", myUsr, myPwd, host, myDev, str_temp);
Thanks @mwittig and @rikki78
I did those changes. However, that did not do the trick.
It is strange: when I tested the original file (file -> examples -> ESP8266WiFi -> WiFi Client), I could successfully compile my code.
The other example code (from funduino.de -> http://funduino.de/anleitung-dht11-dht22)
#include "DHT.h" //DHT Bibliothek laden
#define DHTPIN 2 //Der Sensor wird an PIN 2 angeschlossen
#define DHTTYPE DHT11 // Es handelt sich um den DHT11 Sensor
DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth“ angesprochen
void setup() {
Serial.begin(9600); //Serielle Verbindung starten
dht.begin(); //DHT11 Sensor starten
}
void loop() {
delay(2000); //Zwei Sekunden Vorlaufzeit bis zur Messung (der Sensor ist etwas träge)
float Luftfeuchtigkeit = dht.readHumidity(); //die Luftfeuchtigkeit auslesen und unter „Luftfeutchtigkeit“ speichern
float Temperatur = dht.readTemperature();//die Temperatur auslesen und unter „Temperatur“ speichern
Serial.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und
Serial.print(Luftfeuchtigkeit); //die Dazugehörigen Werte anzeigen
Serial.println(" %");
Serial.print("Temperatur: ");
Serial.print(Temperatur);
Serial.println(" Grad Celsius");
}
works as well. Both on their own. Adding sprintf and dtostrf was pretty much all I really changed; all the other stuff (adding variables, deleting and/or commenting out serial output) should not have caused any problems. But the final code from my last posting does not work at all.
I finally got all the annoying stuff out of the way (setting up each and every 433MHz for remote controls, figuring out which pimatic rules I need), now adding a bunch of DHT11 sensors to my room was pretty much going to be “it”. But it takes longer than all the other work combined -.-
@Ben-S why so compilcated. you can use ESPimatic instead.
there you can use your dht sensor and send the data directly to pimatic via wifi.
isn’t it that what you want
I do need to use a DHT11 sensor, though, so I tried setting it up on a nodeMCU which simply sends data to my pimatic device.
https://github.com/koffienl/ESPimatic
btw - better to use a dht 22, it’s more reliable.
pimatic v0.9 has been released!
Support Pimatic and get some free stickers
Like us on Facebook
make it so !
@leader21 ahh yeah that is exactly what i am looking for. thank you.
i recently asked for help setting up ESPimaticRF and asked if it was possible to connect a DHT sensor… The developer said no and I didn’t know there was another project I could use (by what seems like the same dev).
do ESPimatic and ESPimaticRF work together? My main device (the one set up as homeduino) is a ESPimaticRF. I also have an ESPimaticRF node running. Could I leave the setup as is and still use a ESPimatic (without RF) for my DHT sensors?
I just had a box full of DHT11s so I used them, but already am waiting for DHT22s to be delivered
sorry for the late response. both systems are separate, they do not “cooperate”.
but you can use both parallel.
@koffienl has developed both. he also wanted to come up with a newer version of ESPimatic with some kind of plugin system. but actually there has not been any further release on that.
pimatic v0.9 has been released!
Support Pimatic and get some free stickers
Like us on Facebook
make it so !