I have made a test with a button (instead of the kWh meter) to test the code first.
I have made a virtual device and added a coutner to Domoticz.
If I enter this line in my webbrowser:
http://192.168.2.127:8080/json.htm?type ... 0&svalue=1
The counter in Domoticz is updated immediately by 1 unit, so this works.
No with the code it does not work. The http.GET does not return 200, but -1
Does anybody know what goes wrong?
Here is my code:
Code: Select all
// Wifi en netwerk
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "######"; // verbinden met netwerk
const char* password = "#########"; // verbinden met netwerk
const char* host = "192.168.2.127"; // tbv HTTP send Domoticz
const int port = 8080; // tbv HTTP send Domoticz
IPAddress gateway(192, 168, 2, 254); // verbinden met netwerk
IPAddress subnet(255, 255, 255, 0); // verbinden met netwerk
IPAddress dns(213, 75, 63, 75); //DNS // verbinden met netwerk
#define HOST_NAME "kWh_teller" // verbinden met netwerk; esp host naam
IPAddress ip(192, 168, 2, 150); // verbinden met netwerk; esp ip adres
HTTPClient http;
// interrupt pin
uint8_t _interruptPin = 4; // = pin D4
void setup()
{
Serial.begin(115200);
Serial.println("gestart");
attachInterrupt(digitalPinToInterrupt(_interruptPin), Count, RISING);
// start wifi
if (WiFi.status() != WL_CONNECTED)
{
WifiStart();
}
}
void loop()
{
}
/////////////////////////////////////////
//////////////// VOIDS //////////////////
//Counter void
ICACHE_RAM_ATTR void Count()
{
// verzenden naar Domoticz
if (WiFi.status() != WL_CONNECTED)
{
WifiStart();
}
else
{
Serial.println("Wifi is nog actief...");
}
Serial.println("Verzenden naar Domoticz");
sendDomoticz("/json.htm?type=command¶m=udevice&idx=31&nvalue=0&svalue=1;");
Serial.println("Waarde verzonden...");
}
void WifiStart()
{
WiFi.forceSleepWake(); // Wifi on
//wifi
Serial.println("Connecting Wifi...");
WiFi.config(ip, dns, gateway, subnet);
WiFi.mode(WIFI_AP_STA);
WiFi.hostname(HOST_NAME);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" Connected");
}
void sendDomoticz(String url)
{
http.begin(host, port, url);
int httpCode = http.GET();
Serial.print("httpCode: ");
Serial.println(httpCode);
http.end();
}