Waterpulse Sensor - ESP8266GW without Radio only as node



  • Dear experts,
    i tried to setup my Wemos D1 mini Board as a node.
    I used 2.2.0 Beta version of Mysensors API.
    But it seems that only the gateway part is starting. Sensors are not presenting (serial monitor) nothing happens here after connection to WLAN is established...
    Am i correct? These defines are for (W)LAN communication with my Gateway(192.168.0.136):

    #define MY_USE_UDP
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, 0, 136
    

    Here my whole sketch:

    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "Fritzbox7390"
    #define MY_ESP8266_PASSWORD "xxx"
    
    // Enable UDP communication
    #define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
    
    // Set the hostname for the WiFi Client. This is the hostname
    // it will pass to the DHCP server if not static.
    #define MY_ESP8266_HOSTNAME "waterpulse-sensor"
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,0,137
    
    // If using static ip you can define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,0,1
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode
    #define MY_PORT 5003
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 2
    
    // Controller ip address. Enables client mode (default is "server" mode).
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, 0, 136
    
    // Enable inclusion mode
    //#define MY_INCLUSION_MODE_FEATURE
    
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    //#define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3
    
    // Set blinking period
    //#define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Flash leds on rx/tx/err
    // Led pins used if blinking feature is enabled above
    //#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    
    #if defined(MY_USE_UDP)
    #include <WiFiUdp.h>
    #endif
    
    #define DIGITAL_INPUT_SENSOR 3                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
    #define PULSE_FACTOR 1000                       // Nummber of blinks per m3 of your meter (One rotation/liter)
    #define SLEEP_MODE false                        // flowvalue can only be reported when sleep mode is false.
    #define MAX_FLOW 40                             // Max flow (l/min) value to report. This filters outliers.
    #define MY_NODE_ID 17                           // Static Node ID
    #define CHILD_ID 1                              // Id of the sensor child
    
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    
    unsigned long SEND_FREQUENCY = 30000;           // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    
    MyMessage flowMsg(CHILD_ID,V_FLOW);
    MyMessage volumeMsg(CHILD_ID,V_VOLUME);
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
    
    double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
    
    volatile uint16_t pulseCount = 0;
    volatile uint16_t lastBlink = 0;
    volatile double flow = 0;
    bool pcReceived = false;
    unsigned long oldPulseCount = 0;
    unsigned long newBlink = 0;
    double oldflow = 0;
    double volume =0;
    double oldvolume =0;
    unsigned long lastSend =0;
    unsigned long lastPulse =0;
    
    void setup()
    {
        // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
        pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
    
        pulseCount = oldPulseCount = 0;
    
        // Fetch last known pulse count value from gw
        request(CHILD_ID, V_VAR1);
    
        lastSend = lastPulse = millis();
    
        attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Water Meter", "1.1");
    
        // Register this device as Waterflow sensor
        present(CHILD_ID, S_WATER);
    }
    
    void loop()
    {
        unsigned long currentTime = millis();
    
        // Only send values at a maximum frequency or woken up from sleep
        if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
            lastSend=currentTime;
    
            if (!pcReceived) {
                //Last Pulsecount not yet received from controller, request it again
                request(CHILD_ID, V_VAR1);
                return;
            }
    
            if (!SLEEP_MODE && flow != oldflow) {
                oldflow = flow;
    
                Serial.print("l/min:");
                Serial.println(flow);
    
                // Check that we dont get unresonable large flow value.
                // could hapen when long wraps or false interrupt triggered
                if (flow<((unsigned long)MAX_FLOW)) {
                    send(flowMsg.set(flow, 2));                   // Send flow value to gw
                }
            }
    
            // No Pulse count received in 2min
            if(currentTime - lastPulse > 120000) {
                flow = 0;
            }
    
            // Pulse count has changed
            if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
                oldPulseCount = pulseCount;
    
                Serial.print("pulsecount:");
                Serial.println(pulseCount);
    
                send(lastCounterMsg.set(pulseCount));                  // Send  pulsecount value to gw in VAR1
    
                double volume = ((double)pulseCount/((double)PULSE_FACTOR));
                if ((volume != oldvolume)||(!SLEEP_MODE)) {
                    oldvolume = volume;
    
                    Serial.print("volume:");
                    Serial.println(volume, 3);
    
                    send(volumeMsg.set(volume, 3));               // Send volume value to gw
                }
            }
        }
        if (SLEEP_MODE) {
            sleep(SEND_FREQUENCY);
        }
    }
    
    void receive(const MyMessage &message)
    {
        if (message.type==V_VAR1) {
            unsigned long gwPulseCount=message.getULong();
            pulseCount += gwPulseCount;
            flow=oldflow=0;
            Serial.print("Received last pulse count from gw:");
            Serial.println(pulseCount);
            pcReceived = true;
        }
    }
    
    void onPulse()
    {
        if (!SLEEP_MODE) {
            unsigned long newBlink = micros();
            unsigned long interval = newBlink-lastBlink;
    
            if (interval!=0) {
                lastPulse = millis();
                if (interval<500000L) {
                    // Sometimes we get interrupt on RISING,  500000 = 0.5sek debounce ( max 120 l/min)
                    return;
                }
                flow = (60000000.0 /interval) / ppl;
            }
            lastBlink = newBlink;
        }
        pulseCount++;
    }
    
    

    Hope that somebody has an idea...



  • Mhhh, nobody has an idea?
    It seems that the sensor did not "start" the "void presentation()" section of sketch... 😞


  • Mod

    @HarrySteff the code looks good to me. Could you post the full debug output?



  • Okay i will, as soon as I'am back home... maybe monday! Thank you so far


  • Mod

    What controller are you using? Because I'd comment out the define my controller address



  • Iam using Pimatic, but only for displaying Sensor values. I set my node ID's manually...



  • @mfalkvidd Here is my Serial Output:

    MCO:BGN:INIT GW,CP=R-NGE---,VER=2.2.0-beta
    scandone
    f 0, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 0 (29)
    ..reconnect
    f -180, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 0 (29)
    ..reconnect
    f r0, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 0 (29)
    ..reconnect
    f 0, .scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 0 (29)
    ..reconnect
    f -180, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 0 (29)
    ..reconnect
    f r0, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 3
    cnt 
    
    connected with Fritzbox7390, channel 10
    ip:192.168.0.137,mask:255.255.255.0,gw:192.168.0.1
    .IP: 192.168.0.137
    0;255;3;0;9;6400 MCO:BGN:STP
    0;255;3;0;9;6473 MCO:REG:NOT NEEDED
    0;255;3;0;9;6510 MCO:BGN:INIT OK,TSP=NA
    pm open,type:2 0
    
    

    And nothing more happens...
    I tried to give out some serial prints in "void presentation" section, but they did not appear...

    Strange...

    For what is the MY_CONTROLLER_IP_ADRESS ? The IP i inserted here is the IP from my other/main "Mysensors-Gateway..."


  • Mod

    @HarrySteff a controller and a gateway are different things. See https://www.mysensors.org/about/network#the-controller for information on what a controller is.

    Try removing that define as per gohan's recommendation.



  • Okay i understood this, but how does my ESP8266 node (only wifi) knows where the gateway is`?
    I think i must tell it somwhere?
    I will try to remove defined
    MY_CONTROLLER_IP_ADRESS & MY_USE_UDP

    posting serial output here...


  • Mod

    it doesn't, if you use a esp8266 as a node without nrf24/rmf69 radio it needs to be a gateway on its own



  • It is a gateway, here the serial output:
    (nothing changes)

    ⸮Hl⸮G⸮C⸮d⸮xpI⸮⸮0;255;3;0;9;338 MCO:BGN:INIT GW,CP=R-NGE---,VER=2.2.0-beta
    scandone
    f 0, scandone
    state: 0 -> 2 (b0)
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 1
    cnt 
    
    connected with Fritzbox7390, channel 1
    ip:192.168.0.137,mask:255.255.255.0,gw:192.168.0.1
    .IP: 192.168.0.137
    0;255;3;0;9;899 MCO:BGN:STP
    0;255;3;0;9;948 MCO:REG:NOT NEEDED
    0;255;3;0;9;1004 MCO:BGN:INIT OK,TSP=NA
    pm open,type:2 0
    

    No local atached Sensor is presented?!?
    I use a wemos d1 Mini...
    Sensor is connected to Port D2
    What digital-Input-Port Must i use?
    I found some Pin mappings:
    There it is Port 4


  • Mod

    On the controller, did you add the 192.168.0.137 as a mysensors gateway?



  • No, until now i did not, but do i need a controller at all?
    (In my opinion it should present values even i do not have a controller)
    So now i even disabled the define of the controller IP in my sketch...


  • Mod

    The esp8266 is a complete stand alone mysensors network, it can't interact with the other unless you use a controller in between


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 2
  • 8
  • 1
  • 5

56
Online

11.4k
Users

11.1k
Topics

112.7k
Posts