Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. OpenHardware.io
  3. 💬 MySensors NRF5 Platform

💬 MySensors NRF5 Platform

Scheduled Pinned Locked Moved OpenHardware.io
contest2017nrf52mysensorsnrf5nrf51
210 Posts 20 Posters 42.9k Views 18 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • d00616D d00616

    @NeverDie said in 💬 MySensors NRF5 Platform:

    I refreshed all the libraries. I'm able to compile and upload my sketches, but now it complains a lot about "invalid libraries."

    At the moment, I cannot reproduce this with Linux and Arduino 1.8.2. I try to find out what's wrong.

    d00616D Offline
    d00616D Offline
    d00616
    Contest Winner
    wrote on last edited by d00616
    #72

    @d00616 said in 💬 MySensors NRF5 Platform:

    @NeverDie said in 💬 MySensors NRF5 Platform:

    I refreshed all the libraries. I'm able to compile and upload my sketches, but now it complains a lot about "invalid libraries."

    At the moment, I cannot reproduce this with Linux and Arduino 1.8.2. I try to find out what's wrong.

    Can you try to remove the .ci and .mystools folders?

    NeverDieN 1 Reply Last reply
    0
    • d00616D d00616

      @d00616 said in 💬 MySensors NRF5 Platform:

      @NeverDie said in 💬 MySensors NRF5 Platform:

      I refreshed all the libraries. I'm able to compile and upload my sketches, but now it complains a lot about "invalid libraries."

      At the moment, I cannot reproduce this with Linux and Arduino 1.8.2. I try to find out what's wrong.

      Can you try to remove the .ci and .mystools folders?

      NeverDieN Offline
      NeverDieN Offline
      NeverDie
      Hero Member
      wrote on last edited by
      #73

      @d00616 said in 💬 MySensors NRF5 Platform:

      Can you try to remove the .ci and .mystools folders?

      Done. So, here it is now:

      Archiving built core (caching) in: C:\Users\David\AppData\Local\Temp\arduino_cache_16539\core\core_MySensors_nRF5_MyBoard_nRF52832_bootcode_none,lfclk_lfxo,reset_notenable_2d457965a40837233e636c052c58b359.a
      Sketch uses 3872 bytes (0%) of program storage space. Maximum is 524288 bytes.
      Open On-Chip Debugger 0.10.0-dev-00254-g696fc0a (2016-04-10-10:13)
      Licensed under GNU GPL v2
      For bug reports, read
      	http://openocd.org/doc/doxygen/bugs.html
      debug_level: 0
      0
      adapter speed: 10000 kHz
      cortex_m reset_config sysresetreq
      nrf52.cpu: target state: halted
      target halted due to debug-request, current mode: Thread 
      xPSR: 0x01000000 pc: 0x0000070c msp: 0x20010000
      ** Programming Started **
      auto erase enabled
      wrote 4096 bytes from file C:\Users\David\AppData\Local\Temp\arduino_build_610023/Interrupt_button_test_v006.ino.hex in 0.800001s (5.000 KiB/s)
      ** Programming Finished **
      ** Verify Started **
      verified 4000 bytes in 0.060000s (65.104 KiB/s)
      ** Verified OK **
      ** Resetting Target **
      shutdown command invoked
      Invalid library found in C:\Users\David\Documents\Arduino\libraries\arduino-nRF5: C:\Users\David\Documents\Arduino\libraries\arduino-nRF5
      Invalid library found in C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5: C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5
      Invalid library found in C:\Users\David\Documents\Arduino\libraries\arduino-nRF5: C:\Users\David\Documents\Arduino\libraries\arduino-nRF5
      Invalid library found in C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5: C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5
      

      BTW, I'm using the Windows version of Arduino IDE 1.8.5, which is the most current.

      d00616D 1 Reply Last reply
      0
      • NeverDieN NeverDie

        @d00616 said in 💬 MySensors NRF5 Platform:

        Can you try to remove the .ci and .mystools folders?

        Done. So, here it is now:

        Archiving built core (caching) in: C:\Users\David\AppData\Local\Temp\arduino_cache_16539\core\core_MySensors_nRF5_MyBoard_nRF52832_bootcode_none,lfclk_lfxo,reset_notenable_2d457965a40837233e636c052c58b359.a
        Sketch uses 3872 bytes (0%) of program storage space. Maximum is 524288 bytes.
        Open On-Chip Debugger 0.10.0-dev-00254-g696fc0a (2016-04-10-10:13)
        Licensed under GNU GPL v2
        For bug reports, read
        	http://openocd.org/doc/doxygen/bugs.html
        debug_level: 0
        0
        adapter speed: 10000 kHz
        cortex_m reset_config sysresetreq
        nrf52.cpu: target state: halted
        target halted due to debug-request, current mode: Thread 
        xPSR: 0x01000000 pc: 0x0000070c msp: 0x20010000
        ** Programming Started **
        auto erase enabled
        wrote 4096 bytes from file C:\Users\David\AppData\Local\Temp\arduino_build_610023/Interrupt_button_test_v006.ino.hex in 0.800001s (5.000 KiB/s)
        ** Programming Finished **
        ** Verify Started **
        verified 4000 bytes in 0.060000s (65.104 KiB/s)
        ** Verified OK **
        ** Resetting Target **
        shutdown command invoked
        Invalid library found in C:\Users\David\Documents\Arduino\libraries\arduino-nRF5: C:\Users\David\Documents\Arduino\libraries\arduino-nRF5
        Invalid library found in C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5: C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5
        Invalid library found in C:\Users\David\Documents\Arduino\libraries\arduino-nRF5: C:\Users\David\Documents\Arduino\libraries\arduino-nRF5
        Invalid library found in C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5: C:\Users\David\Documents\Arduino\libraries\ArduinoHwNRF5
        

        BTW, I'm using the Windows version of Arduino IDE 1.8.5, which is the most current.

        d00616D Offline
        d00616D Offline
        d00616
        Contest Winner
        wrote on last edited by
        #74

        @NeverDie said in 💬 MySensors NRF5 Platform:

        BTW, I'm using the Windows version of Arduino IDE 1.8.5, which is the most current.

        I don't know the correct path on an Windows system. On my system the Sketches are in the ~/Arduino folder in my home directory (~). The arduino-nrf5 is in ~/.arduino15/packages/sandeepmistry/hardware/nRF5/0.4.0 and the ArduinoHwNRF5 files are in ~/.arduino15/packages/MySensors/hardware/nRF5/0.2.1

        1 Reply Last reply
        1
        • mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #75

          on my Windows machine, all boards are located in c:\Users<username>\AppData\Local\Arduino15\packages
          Strange that they've been installed to C:\Users\David\Documents\Arduino\libraries\ on your machine.

          Maybe just delete the folders and re-install using the board manager?

          NeverDieN 1 Reply Last reply
          0
          • mfalkviddM mfalkvidd

            on my Windows machine, all boards are located in c:\Users<username>\AppData\Local\Arduino15\packages
            Strange that they've been installed to C:\Users\David\Documents\Arduino\libraries\ on your machine.

            Maybe just delete the folders and re-install using the board manager?

            NeverDieN Offline
            NeverDieN Offline
            NeverDie
            Hero Member
            wrote on last edited by
            #76

            @mfalkvidd All I know is that if I put a library in the folder: C:\Users\David\Documents\Arduino\libraries
            it always gets detected and used. It also means you can completely uninstall/re-install the arduino IDE, and nothing gets lost. I'm not sure about other locations.

            Am I the only one doing it this way?

            mfalkviddM 1 Reply Last reply
            0
            • NeverDieN NeverDie

              @mfalkvidd All I know is that if I put a library in the folder: C:\Users\David\Documents\Arduino\libraries
              it always gets detected and used. It also means you can completely uninstall/re-install the arduino IDE, and nothing gets lost. I'm not sure about other locations.

              Am I the only one doing it this way?

              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by
              #77

              @NeverDie for libraries, doing what you do should result in the same behavior as when using the Library Manager.
              But I'm not sure it works with boards (especially given that the Arduino IDE complains when you put boards in the libraries folder).

              1 Reply Last reply
              0
              • NeverDieN Offline
                NeverDieN Offline
                NeverDie
                Hero Member
                wrote on last edited by
                #78

                I use the board manager to manage the boards. I believe it connects directly into github.

                Anyhow, I stripped out all the non-library stuff, and now Windows IDE works without complaining.

                1 Reply Last reply
                1
                • NeverDieN Offline
                  NeverDieN Offline
                  NeverDie
                  Hero Member
                  wrote on last edited by NeverDie
                  #79

                  Has anyone here yet figured out how to have more than one type of interrupt at a time wake-up the MCU from sleep? Based on the examples so far, it seems as though only one type at a time can be active. I'm sure there must be some way to do it. For instance, it would be desirable if the MCU could wake up not just from a timer event every, say, 5 minutes, to take a temperature reading, but also immediately if there is a leak detected. Right now it's just one or the other.

                  rmtuckerR 1 Reply Last reply
                  0
                  • NeverDieN NeverDie

                    Has anyone here yet figured out how to have more than one type of interrupt at a time wake-up the MCU from sleep? Based on the examples so far, it seems as though only one type at a time can be active. I'm sure there must be some way to do it. For instance, it would be desirable if the MCU could wake up not just from a timer event every, say, 5 minutes, to take a temperature reading, but also immediately if there is a leak detected. Right now it's just one or the other.

                    rmtuckerR Offline
                    rmtuckerR Offline
                    rmtucker
                    wrote on last edited by
                    #80

                    @NeverDie

                    So we can not use the normal statement

                    int8_t sleep(int interrupt, int mode, unsigned long ms=0);

                    I did not realize that was the case as i have never tried it.

                    1 Reply Last reply
                    0
                    • NeverDieN Offline
                      NeverDieN Offline
                      NeverDie
                      Hero Member
                      wrote on last edited by NeverDie
                      #81

                      I believe hwSleep(..) is the preferred incantation, but beyond that I'm not sure of anything. Maybe @d00616 can comment?

                      I should be receiving a number of different PCB projects tomorrow for final assembly and test, and if I can't resolve this soon, I'm simply going to release them without full demo code.

                      scalzS 1 Reply Last reply
                      0
                      • NeverDieN NeverDie

                        I believe hwSleep(..) is the preferred incantation, but beyond that I'm not sure of anything. Maybe @d00616 can comment?

                        I should be receiving a number of different PCB projects tomorrow for final assembly and test, and if I can't resolve this soon, I'm simply going to release them without full demo code.

                        scalzS Offline
                        scalzS Offline
                        scalz
                        Hardware Contributor
                        wrote on last edited by scalz
                        #82

                        @NeverDie said in 💬 MySensors NRF5 Platform:

                        I believe hwSleep(..) is the preferred incantation, but beyond that I'm not sure of anything.

                        In MySensors, the regular way to use sleep mode is by using the sleep() functions:
                        https://www.mysensors.org/download/sensor_api_20#sleeping

                        But like you said, you can also use the raw hwSleep() from the hw abstraction layer. Or you could also rewrite it!
                        Which means at each abstraction layer, there are additional logics. sleep() functions have more logics (regarding MySensors states and features) than the raw hwSleep for example (like testing if there is an ongoing ota, smartsleep, heartbeat etc..). I have nodes where i use raw or not, but it's good to know what's behind.

                        rmtuckerR 1 Reply Last reply
                        1
                        • scalzS scalz

                          @NeverDie said in 💬 MySensors NRF5 Platform:

                          I believe hwSleep(..) is the preferred incantation, but beyond that I'm not sure of anything.

                          In MySensors, the regular way to use sleep mode is by using the sleep() functions:
                          https://www.mysensors.org/download/sensor_api_20#sleeping

                          But like you said, you can also use the raw hwSleep() from the hw abstraction layer. Or you could also rewrite it!
                          Which means at each abstraction layer, there are additional logics. sleep() functions have more logics (regarding MySensors states and features) than the raw hwSleep for example (like testing if there is an ongoing ota, smartsleep, heartbeat etc..). I have nodes where i use raw or not, but it's good to know what's behind.

                          rmtuckerR Offline
                          rmtuckerR Offline
                          rmtucker
                          wrote on last edited by
                          #83

                          @scalz
                          So the statement
                          int8_t sleep(int interrupt, int mode, unsigned long ms=0);
                          Can be used for timer and pin interrupt at he same time on the NRF5 platform just like the arduino?

                          1 Reply Last reply
                          0
                          • NeverDieN Offline
                            NeverDieN Offline
                            NeverDie
                            Hero Member
                            wrote on last edited by
                            #84

                            I'll try it, but I have strong doubts that it's going to work.

                            FWIW, here's the pin mapping currently being supplied by digitalPinToInterrupt(..) for the nRF5:
                            Digital pin 0 = interrupt pin 0
                            Digital pin 1 = interrupt pin 1
                            Digital pin 2 = interrupt pin 2
                            Digital pin 3 = interrupt pin 3
                            Digital pin 4 = interrupt pin 4
                            Digital pin 5 = interrupt pin 5
                            Digital pin 6 = interrupt pin 6
                            Digital pin 7 = interrupt pin 7
                            Digital pin 8 = interrupt pin 8
                            Digital pin 9 = interrupt pin 9
                            Digital pin 10 = interrupt pin 10
                            Digital pin 11 = interrupt pin 11
                            Digital pin 12 = interrupt pin 12
                            Digital pin 13 = interrupt pin 13
                            Digital pin 14 = interrupt pin 14
                            Digital pin 15 = interrupt pin 15
                            Digital pin 16 = interrupt pin 16
                            Digital pin 17 = interrupt pin 17
                            Digital pin 18 = interrupt pin 18
                            Digital pin 19 = interrupt pin 19
                            Digital pin 20 = interrupt pin 20
                            Digital pin 21 = interrupt pin 21
                            Digital pin 22 = interrupt pin 22
                            Digital pin 23 = interrupt pin 23
                            Digital pin 24 = interrupt pin 24
                            Digital pin 25 = interrupt pin 25
                            Digital pin 26 = interrupt pin 26
                            Digital pin 27 = interrupt pin 27
                            Digital pin 28 = interrupt pin 28
                            Digital pin 29 = interrupt pin 29
                            Digital pin 30 = interrupt pin 30

                            1 Reply Last reply
                            0
                            • NeverDieN Offline
                              NeverDieN Offline
                              NeverDie
                              Hero Member
                              wrote on last edited by NeverDie
                              #85

                              The last I heard, from @d00616 , we had to supply code such as:

                                  // Enable interrupt
                                NVIC_SetPriority(RTC0_IRQn, 15);
                                NVIC_ClearPendingIRQ(RTC0_IRQn);
                                NVIC_EnableIRQ(RTC0_IRQn);
                              
                              

                              and

                              // This must be in one line
                              extern "C" { void RTC0_IRQHandler(void) {rtcInterruptCounter++; NRF5_RESET_EVENT(NRF_RTC0->EVENTS_OVRFLW); NRF_RTC0->EVENTS_OVRFLW=0; }}
                              

                              to get interrupts to work. Even with that approach, though, I haven't gotten it to support any interrupts in addition to a timed sleeping, though I have gotten it to support one interrupt that's separate from a timed sleeping (basically, sleeps indefinitely until the interrupt happens).

                              1 Reply Last reply
                              0
                              • NeverDieN Offline
                                NeverDieN Offline
                                NeverDie
                                Hero Member
                                wrote on last edited by NeverDie
                                #86

                                OK, I just now tried:

                                  sleep(3,CHANGE,3000);
                                

                                and, as I suspected, it does nothing but sleep for 3 seconds. It's not responsive to any changes on pin P03 on an nRF52.

                                @scalz Are you getting a different result? It seems like there's a strong disconnect somewhere between what you're recommending and what I am experiencing.

                                scalzS 1 Reply Last reply
                                0
                                • NeverDieN NeverDie

                                  OK, I just now tried:

                                    sleep(3,CHANGE,3000);
                                  

                                  and, as I suspected, it does nothing but sleep for 3 seconds. It's not responsive to any changes on pin P03 on an nRF52.

                                  @scalz Are you getting a different result? It seems like there's a strong disconnect somewhere between what you're recommending and what I am experiencing.

                                  scalzS Offline
                                  scalzS Offline
                                  scalz
                                  Hardware Contributor
                                  wrote on last edited by scalz
                                  #87

                                  @NeverDie said in 💬 MySensors NRF5 Platform:

                                  @scalz Are you getting a different result? It seems like there's a strong disconnect somewhere between what you're recommending and what I am experiencing.

                                  I didn't give any recommandation, you misread. I just said the regular way to use sleep feature in mysensors, for users, is with sleep(..). But I agree, I misread you too! when you were asking for the specific nrf52 case I guess :)

                                  That said, I got this working when I made my recessed node for door (accelerometer and hall effect sensor with sleeping). Maybe things have changed in the lib?? or not. I'm struggling between different version of the lib, and some are different from the dev branch.. I'll recheck this asap (not sure for this evening).

                                  1 Reply Last reply
                                  0
                                  • NeverDieN Offline
                                    NeverDieN Offline
                                    NeverDie
                                    Hero Member
                                    wrote on last edited by NeverDie
                                    #88

                                    I tried toying around with it a bit, and I got a useful result:

                                    #define MY_CORE_ONLY
                                    
                                    #include <nrf.h>
                                    #include <MySensors.h>
                                    
                                    
                                    const byte ledPin = LED_BUILTIN;
                                    const byte interruptPin = 3;
                                    volatile byte state = LOW;
                                    
                                    void blinkityBlink(uint8_t pulses, uint8_t repetitions) {
                                      for (int x=0;x<repetitions;x++) {
                                        for (int i=0;i<pulses;i++) {
                                          digitalWrite(LED_BUILTIN,HIGH);
                                          wait(20);
                                          digitalWrite(LED_BUILTIN,LOW);
                                          wait(100);
                                        }    
                                          wait(500);
                                      }
                                    }
                                    
                                    void setup() {
                                      hwPinMode(LED_BUILTIN,OUTPUT_D0H1);
                                      hwPinMode(interruptPin, INPUT);
                                      attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
                                      blinkityBlink(2,1);  //signify power-on and start of main loop
                                    }
                                    
                                    volatile bool buttonPressed=false;
                                    void loop() {
                                      state = !state;
                                      digitalWrite(ledPin, state);
                                      sleep(digitalPinToInterrupt(interruptPin), RISING, 3000);
                                      wait(20);  //wait 20 milliseconds for button to debounce if it was pressed
                                      if (digitalRead(interruptPin)) { //if button is pressed
                                        blinkityBlink(2,1);
                                      }
                                      if (buttonPressed) {
                                        buttonPressed=false;
                                        blinkityBlink(20,1);
                                      }
                                    }
                                    
                                    void blink() {
                                      buttonPressed=true;
                                    }
                                    

                                    So, with this approach, pushing the button on pin 3 does wake up the nRF52 from sleep, whereupon the button press can still be detected and serviced, but it also demonstrates that the ISR per se isn't working.

                                    Anyhow, with this I'm able to get the PIR or leak sensor or magnet sensor or light sensor doing useful things in a timely manner, even if it isn't ideal. That puts me further ahead than I was before. :)

                                    rmtuckerR 1 Reply Last reply
                                    0
                                    • NeverDieN NeverDie

                                      I tried toying around with it a bit, and I got a useful result:

                                      #define MY_CORE_ONLY
                                      
                                      #include <nrf.h>
                                      #include <MySensors.h>
                                      
                                      
                                      const byte ledPin = LED_BUILTIN;
                                      const byte interruptPin = 3;
                                      volatile byte state = LOW;
                                      
                                      void blinkityBlink(uint8_t pulses, uint8_t repetitions) {
                                        for (int x=0;x<repetitions;x++) {
                                          for (int i=0;i<pulses;i++) {
                                            digitalWrite(LED_BUILTIN,HIGH);
                                            wait(20);
                                            digitalWrite(LED_BUILTIN,LOW);
                                            wait(100);
                                          }    
                                            wait(500);
                                        }
                                      }
                                      
                                      void setup() {
                                        hwPinMode(LED_BUILTIN,OUTPUT_D0H1);
                                        hwPinMode(interruptPin, INPUT);
                                        attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
                                        blinkityBlink(2,1);  //signify power-on and start of main loop
                                      }
                                      
                                      volatile bool buttonPressed=false;
                                      void loop() {
                                        state = !state;
                                        digitalWrite(ledPin, state);
                                        sleep(digitalPinToInterrupt(interruptPin), RISING, 3000);
                                        wait(20);  //wait 20 milliseconds for button to debounce if it was pressed
                                        if (digitalRead(interruptPin)) { //if button is pressed
                                          blinkityBlink(2,1);
                                        }
                                        if (buttonPressed) {
                                          buttonPressed=false;
                                          blinkityBlink(20,1);
                                        }
                                      }
                                      
                                      void blink() {
                                        buttonPressed=true;
                                      }
                                      

                                      So, with this approach, pushing the button on pin 3 does wake up the nRF52 from sleep, whereupon the button press can still be detected and serviced, but it also demonstrates that the ISR per se isn't working.

                                      Anyhow, with this I'm able to get the PIR or leak sensor or magnet sensor or light sensor doing useful things in a timely manner, even if it isn't ideal. That puts me further ahead than I was before. :)

                                      rmtuckerR Offline
                                      rmtuckerR Offline
                                      rmtucker
                                      wrote on last edited by
                                      #89

                                      @NeverDie
                                      This is a test program that wakes up from either that i was using some time ago.

                                      /**
                                         The MySensors Arduino library handles the wireless radio link and protocol
                                         between your home built sensors/actuators and HA controller of choice.
                                         The sensors forms a self healing radio network with optional repeaters. Each
                                         repeater and gateway builds a routing tables in EEPROM which keeps track of the
                                         network topology allowing messages to be routed to nodes.
                                      
                                         Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                                         Copyright (C) 2013-2015 Sensnology AB
                                         Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                                      
                                         Documentation: http://www.mysensors.org
                                         Support Forum: http://forum.mysensors.org
                                      
                                         This program is free software; you can redistribute it and/or
                                         modify it under the terms of the GNU General Public License
                                         version 2 as published by the Free Software Foundation.
                                      
                                       *******************************
                                      
                                         REVISION HISTORY
                                         Version 1.0 - Henrik EKblad
                                      
                                         DESCRIPTION
                                         This sketch provides an example how to implement a distance sensor using HC-SR04
                                         Use this sensor to measure KWH and Watt of your house meeter
                                         You need to set the correct pulsefactor of your meeter (blinks per KWH).
                                         The sensor starts by fetching current KWH value from gateway.
                                         Reports both KWH and Watt back to gateway.
                                      
                                         Unfortunately millis() won't increment when the Arduino is in
                                         sleepmode. So we cannot make this sensor sleep if we also want
                                         to calculate/report watt-number.
                                         http://www.mysensors.org/build/pulse_power
                                      */
                                      
                                      // Enable debug prints
                                      #define MY_DEBUG
                                      
                                      // Enable and select radio type attached
                                      //#define MY_RADIO_NRF24
                                      #define MY_RADIO_NRF5_ESB
                                      //#define MY_RADIO_RFM69
                                      //#define MY_RADIO_RFM95
                                      
                                      #include <MySensors.h>
                                      #include <Wire.h> // must be included here so that Arduino library object file references work
                                      #include <RtcDS3231.h>
                                      RtcDS3231<TwoWire> Rtc(Wire);
                                      
                                      #define DIGITAL_INPUT_SENSOR 2  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
                                      #define PULSE_FACTOR 1000       // 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 1              // 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 pulseCount = 0;
                                      volatile unsigned long lastBlink = 0;
                                      volatile unsigned long watt = 0;
                                      volatile unsigned long kwh = 0;
                                      unsigned long oldWatt = 0;
                                      double oldKwh;
                                      unsigned long lastSend;
                                      MyMessage wattMsg(CHILD_ID, V_WATT);
                                      MyMessage kwhMsg(CHILD_ID, V_KWH);
                                      
                                      
                                      void setup()
                                      {
                                        Serial.begin(115200);
                                        Rtc.Begin();
                                        Rtc.Enable32kHzPin(false);
                                        Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz);
                                        Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock);
                                      
                                        // 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
                                        hwPinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
                                      
                                        attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
                                        //pcReceived = true;
                                        lastSend = millis();
                                      }
                                      
                                      void presentation()
                                      {
                                        // Send the sketch version information to the gateway and Controller
                                        sendSketchInfo("Energy Meter", "1.0");
                                      
                                        // Register this device as power sensor
                                        present(CHILD_ID, S_POWER);
                                      }
                                      
                                      void loop()
                                      {
                                        unsigned long now = millis();
                                        // Only send values at a maximum frequency or woken up from sleep
                                        bool sendTime = now - lastSend > SEND_FREQUENCY;
                                        if (sendTime) {
                                          // New watt value has been calculated
                                          if (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;
                                          }
                                      
                                          // Pulse count has changed
                                          //kwh = pulseCount;
                                          //double kwh = ((double)pulseCount / ((double)PULSE_FACTOR));
                                          send(kwhMsg.set(pulseCount));  // Send kwh value to gw
                                          Serial.print("Wh = ");
                                          Serial.println(pulseCount);
                                          pulseCount = 0;
                                          lastSend = now;
                                        }
                                        sleep(SEND_FREQUENCY);
                                      }
                                      
                                      void receive(const MyMessage &message)
                                      {
                                      }
                                      
                                      void onPulse()
                                      {
                                        unsigned long newBlink = micros();
                                        unsigned long interval = newBlink - lastBlink;
                                        if (interval < 50000L) { // Sometimes we get interrupt on RISING
                                          return;
                                        }
                                        watt = (3600000000.0 / interval) / ppwh;
                                        lastBlink = newBlink;
                                        pulseCount++;
                                      }
                                      
                                      1 Reply Last reply
                                      1
                                      • NeverDieN Offline
                                        NeverDieN Offline
                                        NeverDie
                                        Hero Member
                                        wrote on last edited by NeverDie
                                        #90

                                        Well, that's interesting. In the sketch I posted, I invoked sleep with:

                                          sleep(digitalPinToInterrupt(interruptPin), RISING, 3000);
                                        

                                        which caused the mcu to wake up immediately after I press the button, but it didn't process the ISR.

                                        Using an invocation like yours in the sketch you just posted above:

                                          sleep(3000);
                                        

                                        the ISR executes and then terminates when I press the button, but the MCU doesn't wake and continue with the loop as it did with the prior incantation. Instead, it has to wait for the timer cycle to finish.

                                        What I want is for it to wake up, do the ISR, and continue with the loop where it left off until it is explicitly put back to sleep again. How do I do that?

                                        1 Reply Last reply
                                        0
                                        • d00616D Offline
                                          d00616D Offline
                                          d00616
                                          Contest Winner
                                          wrote on last edited by
                                          #91

                                          @NeverDie I have checked the sleep routine in all three variants. It's working with my setup.

                                          There is no API stopping the sleep() by another ISR. Sleep only ends at one of the given conditions.

                                          When you use the MY_CORE_ONLY define, please add "hwInit();" into the setup() routine.

                                          NeverDieN 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          11

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular