Talk2 Whisper node & ESP8266 base station code

8266

I have my basic whisper code ready and working, I used part of the talk2 library code from their bitbucket repository and changed it to my needs.

It still needs some refactoring and tweaking battery saving functionality, for instance I would like to:

  • Have the sensor nodes sleep for 60 minutes, before they re-read the distance (the maximum sleeptime is 8 seconds, but they can go to sleep again immediately to save batterylife.
  • Only read voltage every 6 hours or so, reading the voltage every hour will switch a mosfet to high for a moment, which will cost some batterylife.

As mentioned in my previous post the ESP8266 will receive serial messages, and send them to the internet.
I already posted most of the esp8266 code on github, that code is without the serial connection part, I will post a basic serial receiving code here, you can merge it with your own code, or take the code from my github and combine the 2 to receive a serial message on the ESP8266 and send it to a webserver.

I use the $ character on the esp to catch a message, and the # to catch the end of the message, this way I know when the message is complete and I can send the data to my webserver for further parsing and saving.

 

This code is’nt completely optimized, for instance in my project I send the amount of times the node needs to go to sleep as a reply from the “base station”, and I removed any delays to save energy, but I hope it helps you get on the way with sending and receiving data.

Starter code ESP8266 Serial connection:

 

My whisper server code:

Whisper Client sourcecode:

sensor.h code for the whisper_client:

 

  • Pingback: Talk2 Whisper node first client server setup – Thomas Lang()

  • Farmer

    Thanks for sharing your adventures! I’m thinking about a similar
    setup myself, but did not start so far – Your code will be very useful 🙂

    I saw that the LowPower lib can only power down for 8secs. Since you are measuring the temperature anyway, wouldn’t the RTC Kit be the logical addon? The DS3231M’s integrated temperature sensor can be used to measure the temperature. I might be wrong, but then you could use the RTC to power down for a custom amount of time (eg 1 full minute) using RTC alarms. I guess that would extend the battery life even further (?)

    • Thomas Lang

      Hi there, thanks for your comment 🙂

      I’m measuring the distance of an object, not the temperature at the moment.

      I added a piece of code which instantly puts the whisper node into sleep mode again, if a certain time hasn’t been reached, in this example it will go to sleep 10 times in a row:

      for the millisecond it will be on every 8 seconds the power consumption will be insignificant, since the radio etc will still be off.

      I guess the RTC will provide some benefits, but it also requires me to solder it on, and will use some extra power (although not significant).

      I will be doing a power usage test this week, and add a thingspeak graph on my blog, so I can keep track of the CR2032 battery draining, I’m hoping to get at least 8 weeks out of it, the measurements will be done every 2 hours for this test.

      • Farmer

        Oh, my bad. I too the image in the previous post as a hint, one node was named “temperature sensor”.
        But nonetheless I’m looking forward to your follow-up posts 🙂

        • Thomas Lang

          I have removed the delay(1); in the code, and removed the millis() counter, it’s better to just compare the amount of sleeps with a variable and go into sleep again, it will only take microseconds, instead of 1 millisecond, which will further reduce drain.

          Edit: I use the base station to send the amount of sleepcycles the node needs to make before resending new sensor data, it will log maximum of once per hour, this should give me at least 12 months of power on a single battery according to Talk2, hope to setup my batterytest tomorrow, otherwise beginning of next week.

    • Thomas Lang

      Hi there Mike, thanks for all the extra information, In the currently running battery test I already removed all println and reduced the delays greatly. I will just run this test out, I’m guessing my current code will keep it up for about 6 days with reading sensor data every minute, since my project will only read every hour I should be able to get 60x that amount (or let’s say 50x for margin).

      I will add the current source code to the post when I have time 🙂

      Thanks for some more info about spi flash, reducing CPU speed and waking up on receive!

  • Wisen Talk²

    Hi Thomas, the code looks great! You might be able to save some extra microamps if you add the following line after you copy the SPI Flash ID:

    myFlash.powerDown(); //You should save at least 10uA

    I also see that you wait up to 500ms to receive a message back. This can be a pitfall in case your base is off-line or it takes while to reply the message. You have a few options here:

    1. Reduce the time-out;

    2. Slow down the CPU while waiting for a message, by basically doing:
    #include
    (…)
    clock_prescale_set(clock_div_16); //Slow down the MCU
    if (rf69.waitAvailableTimeout(32)) //used to be 500, but as the MCU runs at 1MHz now, 1 milli represents 16 millis on this section of the code
    {
    clock_prescale_set(clock_div_1); //Restore full speed
    (…)
    }
    clock_prescale_set(clock_div_1); //Restore full speed

    Note that at 1MHz the MCU should consume less than 1mA, where it normally consumes over 8mA at 16MHz. This technique can be done at any point in your code, just keep in mind that functions using the hardcoded “F_CPU” will need to be compensated – mostly timing functions. You might be able to reduce the whole sketch speed depending on your application (http://www.atmel.com/Images/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_datasheet.pdf page 379)

    3. Put the Radio into receive mode, remove the “rf69.waitAvailableTimeout(500)” and set the MCU sleep for 500ms, so if the Radio receives something, it’ll generate an interrupt, which will wake up the MCU and it’ll be ready to receive the message from the buffer. Otherwise, the MCU will wake up and continue the code normally.

    Finally, removing all “Serial.print” when you have your application deployed will also save some extra energy.

    Cheers,
    Mike M.

    • Thomas Lang

      Hi there Mike,

      thanks for all the extra information, In the currently running battery test I already removed all println and reduced the delays greatly. I will just run this test out, I’m guessing my current code will keep it up for about 6 days with reading sensor data every minute, since my project will only read every hour I should be able to get 60x that amount (or let’s say 50x for margin).

      I will add the current source code to the post when I have time this week.

      Thanks for some more info about spi flash, reducing CPU speed and waking up on receive!