esp8266 does not update counter

Everything about esp8266 and more.

Moderator: leecollings

Post Reply
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

esp8266 does not update counter

Post by bnwgraaf »

I would like to count the pulses of a kWh meter.
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&param=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();
}

User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: esp8266 does not update counter

Post by gizmocuz »

Just a quick thought that might solve the issue

remove the semicolon at the end of the svalue

sendDomoticz("/json.htm?type=command&param=udevice&idx=31&nvalue=0&svalue=1");

if that does not work, try the command in your browser first
(adding http://yourip:port in front of it)
Quality outlives Quantity!
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

gizmocuz wrote: Sunday 09 January 2022 6:40 Just a quick thought that might solve the issue

remove the semicolon at the end of the svalue

sendDomoticz("/json.htm?type=command&param=udevice&idx=31&nvalue=0&svalue=1");

if that does not work, try the command in your browser first
(adding http://yourip:port in front of it)
Hi, thank you for your reply!

I thought that I already had tried this code without that semicolon, but I will check that one.

As told in my first post, entering the command (with ip address and portnumber) directly in a webbrowser does work, so the problem is somewhere in the esp or its code.

I did use this same esp before for measuring temperature and send it to Domoticz, of course with some different code, but I have copied.parts of it.
In the serial monitor I can see that the esp does connect to my wifi network.
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

gizmocuz wrote: Sunday 09 January 2022 6:40 Just a quick thought that might solve the issue

remove the semicolon at the end of the svalue
Just double checked, but removing the semicolon did not work.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: esp8266 does not update counter

Post by jvdz »

Are you using Userid&Password on your Domoticz?
If so you either need to exclude this node for that or provide it in the ESP code.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

Well, I have added the one line to my other code where I send the temperature to Domoticz and there it works, so I will check the differences!
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: esp8266 does not update counter

Post by jvdz »

Not totally sure I understand the reply about the other line, but you didn't answer my question about using Userid&Password to access domoticz. ;)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

jvdz wrote: Sunday 09 January 2022 11:13 Not totally sure I understand the reply about the other line, but you didn't answer my question about using Userid&Password to access domoticz. ;)
Sorry, I was not reacting on your post and my reacting wass also not very clear, as I am reading it back! Sorry! :-)

What I ment was, that I took antoher code which I have used to measure temperature and send the values to Domoticz. This code was working well.
Than I added only this, additional to that code:

Code: Select all

sendDomoticz("/json.htm?type=command&param=udevice&idx=31&nvalue=0&svalue=1");
And now it works, so I do not need to use the Userid nor the Password.

Now I have rebuild this older code and discovered, that if I place the line

Code: Select all

sendDomoticz("/json.htm?type=command&param=udevice&idx=31&nvalue=0&svalue=1");
in the Loop, than it works, but if I place it in "interrupt-void" than it does not.....
So for some reason the void which is called when the interrupt takes place, can not execute the http.begin command?

I hope I have made it clear, English is not ny native language....
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

Okay, I have discovered that it might not be a good idea to do the http reqest within the interrupt handler.
I now do only count in this handler and check in the loop if there has been interrupts counted and than I will send the command to Domoticz.

That seems to work now, I only discover it sometimes miss a count. '

This is the code for now:

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, 200);                       // verbinden met netwerk; esp ip adres

HTTPClient http;

// interrupt pin
uint8_t _interruptPin = 14; // = pin D5

int _Count = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("gestart");

  attachInterrupt(digitalPinToInterrupt(_interruptPin), Count, RISING);

  // start wifi
  if (WiFi.status() != WL_CONNECTED)
  {
    WifiStart();
  }
}

void loop()
{
  // verzenden naar Domoticz
  if (WiFi.status() != WL_CONNECTED)
  {
    WifiStart();
  }
  
  if (_Count > 1)
  {
    sendDomoticz("/json.htm?type=command&param=udevice&idx=31&nvalue=0&svalue=1");
    _Count = 0;
  }
  yield();
}


/////////////////////////////////////////
//////////////// VOIDS //////////////////

//Counter void
ICACHE_RAM_ATTR void Count()
{
  Serial.println("Interrupt detected...");
  _Count++;
}



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();
}

User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: esp8266 does not update counter

Post by jvdz »

Something similar like this maybe?
https://www.reddit.com/r/arduino/commen ... rrupt_not/

ah I see you found out the same :)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

jvdz wrote: Sunday 09 January 2022 11:58 Something similar like this maybe?
https://www.reddit.com/r/arduino/commen ... rrupt_not/

ah I see you found out the same :)
Thank you! That is indeed something similar.
For testing I am using a push button, because I still need to buy the kWh meter module. But I have learned, one button click sometimes causes two counts, and as I did understand it well, there is the debounce part for.
I do not know if I need that also with the kWh meter though....
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

Well, even I don't have the kWh meter module yet, the test setup does work and counts every pulse I (manually) generate.

Right now I have used the Virtual Sensor "Counter Incremental".
Is it possible to make the high and low rates visible (lower rate during nighttime and in the weekends, high rate all the other times), just like with the P1 Smart meter option?

On the esp8266 I can determine if the pulse was generated during high or during low rate. Of course I can create two virtual sensors, but it would be nice if those two values are combined in one widget, just like the Smart Meter widget (P1)...
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: esp8266 does not update counter

Post by waltervl »

You can use the P1 meter for this but then you have your ESP do the counting as this meter uses a total counter (like the counter on the kWh Meters).
So you have to find a way to have the ESP remember the total count.
One way is to first get the current total count from Domoticz by http API call add the count by ESP en send it back to Domoticz.

Domoticz will do the high/low counting for you.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

waltervl wrote: Tuesday 11 January 2022 22:15 You can use the P1 meter for this but then you have your ESP do the counting as this meter uses a total counter (like the counter on the kWh Meters).
So you have to find a way to have the ESP remember the total count.
One way is to first get the current total count from Domoticz by http API call add the count by ESP en send it back to Domoticz.

Domoticz will do the high/low counting for you.
I did read your post twice, but I do understand now and I will give that also a try!

For now, I have it working with incremental counters; one for low and one for high tariff, and a third for total.
But I will also try your suggestion, because that is a nicer way if you see all the kWh usage in one Domoticz widget.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: esp8266 does not update counter

Post by waltervl »

If you have high/low tariffs you can also use the P1 smart meter device.
https://www.domoticz.com/wiki/Domoticz_ ... mart_meter
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
bnwgraaf
Posts: 28
Joined: Monday 26 December 2016 19:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: esp8266 does not update counter

Post by bnwgraaf »

waltervl wrote: Tuesday 11 January 2022 22:15 You can use the P1 meter for this but then you have your ESP do the counting as this meter uses a total counter (like the counter on the kWh Meters).
So you have to find a way to have the ESP remember the total count.
One way is to first get the current total count from Domoticz by http API call add the count by ESP en send it back to Domoticz.

Domoticz will do the high/low counting for you.
Can someone help me with receiving/reading the data from Domoticz?

Does someone have some example code?
I succeed to let the esp8266 read the values of the new made P1 meter. The retreived text looks like a json, but I do not succeed to get the wanted value from the json. So my problem is in the decoding of the retceived text.

The text/data I can read with my esp is, for example:

Code: Select all

{
	"ActTime" : 1643103244,
	"AstrTwilightEnd" : "19:14",
	"AstrTwilightStart" : "06:31",
	"CivTwilightEnd" : "17:53",
	"CivTwilightStart" : "07:51",
	"DayLength" : "08:48",
	"NautTwilightEnd" : "18:34",
	"NautTwilightStart" : "07:10",
	"ServerTime" : "2022-01-25 10:34:04",
	"SunAtSouth" : "12:52",
	"Sunrise" : "08:28",
	"Sunset" : "17:16",
	"app_version" : "2021.1",
	"result" : 
	[
		{
			"AddjMulti" : 1.0,
			"AddjMulti2" : 1.0,
			"AddjValue" : 0.0,
			"AddjValue2" : 0.0,
			"BatteryLevel" : 255,
			"Counter" : "3.400",
			"CounterDeliv" : "0.000",
			"CounterDelivToday" : "0.000 kWh",
			"CounterToday" : "0.000 kWh",
			"CustomImage" : 0,
			"Data" : "1200;2200;0;0;0;0",
			"Description" : "",
			"Favorite" : 0,
			"HardwareDisabled" : false,
			"HardwareID" : 17,
			"HardwareName" : "kWh meter",
			"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
			"HardwareTypeVal" : 15,
			"HaveTimeout" : true,
			"ID" : "14072",
			"LastUpdate" : "2022-01-24 08:17:48",
			"Name" : "test",
			"Notifications" : "false",
			"PlanID" : "0",
			"PlanIDs" : 
			[
				0
			],
			"Protected" : false,
			"ShowNotifications" : true,
			"SignalLevel" : "-",
			"SubType" : "Energy",
			"SwitchTypeVal" : 0,
			"Timers" : "false",
			"Type" : "P1 Smart Meter",
			"TypeImg" : "counter",
			"Unit" : 1,
			"Usage" : "0 Watt",
			"UsageDeliv" : "0 Watt",
			"Used" : 1,
			"XOffset" : "0",
			"YOffset" : "0",
			"idx" : "34"
		}
	],
	"status" : "OK",
	"title" : "Devices"
}
From this, I would like to extraxt the values of "Data", which in this example has the next value: "1200;2200;0;0;0;0"
If I can read this value, I can add one to it and send it back to the P1 meter in Domoticz.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests