J'ai un vieux compteur électromécanique (celui avec le disque qui tourne) , et depuis quelques mois je recupère ma conso instantané, totale et le nb de tour.
Mais, mon compteur est de tarif jour/nuit, donc l'utilité de récupérer ma conso n'est pas l'idéal.
j'ai donc bricoler via un optocoupleur, un circuit permettant de m'informer si on est en HC ou HP. (ca fonctionne bien)
mais du coup je souhaiterais modifier mon code afin qu'il me renvoie les bonnes infos, car là, il me renvoie les infos sur les mêmes ID.
voici mon code:
Code : Tout sélectionner
// MySensors
#define MY_PARENT_NODE_ID 0 // define if fixed parent
#define MY_PARENT_NODE_IS_STATIC
#undef MY_REGISTRATION_FEATURE // sketch moves on if no registration
#define MY_NODE_ID 2 // fixed node number
// Enable debug prints
#define MY_DEBUG
// Enable RS485 transport layer
#define MY_RS485
// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Flash leds on rx/tx/err
#define MY_DEFAULT_ERR_LED_PIN 13 // Error led pin
#define MY_DEFAULT_RX_LED_PIN 7 // Receive led pin
#define MY_DEFAULT_TX_LED_PIN 6 // the PCB, on board LED
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <MySensors.h>
#include <SPI.h>
#include <Bounce2.h>
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your light sensor. (Only 2 and 3 generates interrupt!)
#define DIGITAL_INPUT_HCHP 5
#define PULSE_FACTOR 250 // Nummber of blinks per KWH of your meeter
#define SLEEP_MODE false // Watt-value can only be reported when sleep mode is false.
#define MAX_WATT 10000 // Max watt value to report. This filetrs outliers.
//#define CHILD_ID 2 // Id of the sensor child
unsigned long SEND_FREQUENCY =
20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
bool pcReceived = false;
volatile unsigned long pulseHpCount = 0;
volatile unsigned long pulseHcCount = 0;
volatile unsigned long lastBlink = 0;
unsigned long oldPulseHpCount = 0;
volatile unsigned long watt = 0;
unsigned long oldWatt = 0;
double oldKwhHp;
unsigned long oldPulseHcCount = 0;
double oldKwhHc;
unsigned long lastSend;
Bounce debouncer = Bounce();
int oldValue=-1;
MyMessage wattMsg(1,V_WATT);
MyMessage kwhHpMsg(3,V_KWH);
MyMessage pcHpMsg(2,V_VAR1);
MyMessage kwhHcMsg(5,V_KWH);
MyMessage pcHcMsg(6,V_VAR1);
MyMessage hchpMsg(7,V_VAR2);
const int ledPin = 13; // sortie digitale
void setup()
{
// Fetch last known pulse count value from gw
request(2, V_VAR1);
request(6, V_VAR1);
// Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
// If no pullup is used, the reported usage will be too high because of the floating pin
pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
pinMode(DIGITAL_INPUT_HCHP,INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
lastSend=millis();
// After setting up the button, setup debouncer
debouncer.attach(DIGITAL_INPUT_HCHP);
debouncer.interval(5);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Energy Meter", "2.0");
// Register this device as power sensor
present(1, S_POWER);
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Energy Meter", "2.0");
// Register this device as power sensor
present(2, S_POWER);
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Energy Meter", "2.0");
// Register this device as power sensor
present(3, S_POWER);
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Energy Meter", "2.0");
// Register this device as power sensor
present(5, S_POWER);
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Energy Meter", "2.0");
// Register this device as power sensor
present(6, S_POWER);
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Energy Meter", "2.0");
// Register this device as power sensor
present(7, S_POWER);
}
void loop()
{
unsigned long now = millis();
if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ) {
// LED Power On
digitalWrite(ledPin, HIGH);
// LED Power Off
digitalWrite(ledPin, LOW);
}
{
debouncer.update();
// Get the update value
int value = debouncer.read();
Serial.println("");
if (value != oldValue) {
// Send in the new value
send(hchpMsg.set(value==HIGH ? 1 : 0));
oldValue = value;
}
}
// Only send values at a maximum frequency or woken up from sleep
bool sendTime = now - lastSend > SEND_FREQUENCY;
if (pcReceived && (SLEEP_MODE || sendTime)) {
// New watt value has been calculated
if (!SLEEP_MODE && watt != oldWatt) {
// Check that we dont get unresonable large watt value.
// could hapen when long wraps or false interrupt triggered
if (watt<((unsigned long)MAX_WATT)) {
send(wattMsg.set(watt)); // Send watt value to gw
}
Serial.print("Watt:");
Serial.println(watt);
oldWatt = watt;
}
/////HP
if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH ) {
// Pulse cout has changed
if (pulseHpCount != oldPulseHpCount) {
send(pcHpMsg.set(pulseHpCount)); // Send pulse count value to gw
double kwhHp = ((double)pulseHpCount/((double)PULSE_FACTOR));
oldPulseHpCount = pulseHpCount;
digitalWrite(ledPin, LOW);
if (kwhHp != oldKwhHp) {
send(kwhHpMsg.set(kwhHp, 4)); // Send kwh value to gw
oldKwhHp = kwhHp;
}
digitalWrite(ledPin, HIGH);
}
lastSend = now;
}else if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ){
if (pulseHcCount != oldPulseHcCount) {
send(pcHcMsg.set(pulseHcCount)); // Send pulse count value to gw
double kwhHc = ((double)pulseHcCount/((double)PULSE_FACTOR));
oldPulseHcCount = pulseHcCount;
digitalWrite(ledPin, LOW);
if (kwhHc != oldKwhHc) {
send(kwhHcMsg.set(kwhHc, 4)); // Send kwh value to gw
oldKwhHc = kwhHc;
}
digitalWrite(ledPin, HIGH);
}
lastSend = now;
}
}else if (sendTime && !pcReceived) {
// No count received. Try requesting it again
request(2, V_VAR1);
request(6, V_VAR1);
lastSend=now;
}
if (SLEEP_MODE) {
sleep(SEND_FREQUENCY);
}
}
void receive(const MyMessage &message)
{
/////HP
if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH ) {
if (message.type==V_VAR1) {
pulseHpCount = oldPulseHpCount = message.getLong();
Serial.print("Received last pulse count from gw:");
Serial.println(pulseHpCount);
pcReceived = true;
}
}
/////HC
if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ) {
if (message.type==V_VAR1) {
pulseHcCount = oldPulseHcCount = message.getLong();
Serial.print("Received last pulse count from gw:");
Serial.println(pulseHcCount);
pcReceived = true;
}
}
}
void onPulse()
{
/////HP
if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH ) {
if (!SLEEP_MODE) {
unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink;
if (interval<10000L) { // Sometimes we get interrupt on RISING
return;
}
watt = (3600000000.0 /interval) / ppwh;
lastBlink = newBlink;
}
pulseHpCount++;
}
/////HC
else if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ) {
if (!SLEEP_MODE) {
unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink;
if (interval<10000L) { // Sometimes we get interrupt on RISING
return;
}
watt = (3600000000.0 /interval) / ppwh;
lastBlink = newBlink;
}
pulseHcCount++;
}
}