{"id":35,"date":"2016-12-13T13:40:32","date_gmt":"2016-12-13T12:40:32","guid":{"rendered":"http:\/\/emariete.com\/?p=35"},"modified":"2023-12-28T11:27:22","modified_gmt":"2023-12-28T10:27:22","slug":"measuring-gas-consumption","status":"publish","type":"post","link":"https:\/\/emariete.com\/en\/medir-consumo-gas-arduino\/","title":{"rendered":"Measuring gas consumption with Arduino"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Those of you who are regulars of the blog already know that I like to measure everything, and gas could not be less. I've been measuring gas consumption for years with Arduino, but the important thing is not that, the important thing is that this Arduino carries <strong>almost six years sending data wirelessly with only two AA batteries<\/strong>and it still works...<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>One thing that not many people know is that it is often very easy to measure because the meters have a \"magnetic output\" that can be easily read.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The difficulty, more than anything else, is in <strong>how to send data from the meter<\/strong>which is often inaccessible and has no nearby power sockets.<\/p>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Jeenode.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"207\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Jeenode-300x207.jpg\" class=\"wp-image-111 size-full\" alt=\"Electronic circuit with components and connections for DIY projects.\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Jeenode-300x207.jpg 300w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Jeenode-768x531.jpg 768w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Jeenode.jpg 846w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/> <\/a><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">For a while I was reading the gas consumption using a Jeenode (a kind of Arduino with a radio frequency module for low consumption applications). The system worked very well for a few months until, well, until it was stolen from my meter box!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One fine day the readings simply stopped coming in, I went to see what was going on, and my beloved little device had disappeared.<\/p>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">I haven't read the gas for almost a year now, and with the changes I have made this year to the boiler and the heating control system, it would be good to have real-time data again so that I can optimise its operation and know if the changes I am implementing are resulting in gas savings.<\/p>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/ContadorGas.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"222\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/ContadorGas-300x222.jpg\" alt=\"\" class=\"wp-image-113 size-full\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/ContadorGas-300x222.jpg 300w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/ContadorGas-768x568.jpg 768w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/ContadorGas.jpg 850w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">Measuring gas consumption is not difficult, in most cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fortunately, gas meters usually have a magnet that moves with the little numbers that go round and round, so we can detect each turn of the number and calculate consumption that way.<\/p>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In principle I wanted to make the metering system using an ESP8266 and MQTT (and I prepared it). The problem is that I don't have an electrical socket near the gas meter and the ESP8266 is a bit too energy hungry to be powered by a battery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the other hand, JeeNodes are a bit expensive, for what is out there nowadays.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since I already have a JeeNode receiver installed and integrated through Nodered (my power consumption reading system was an EmonTX, until I replaced it with an IoTaWatt, which is based on and compatible with JeeNode), and all the components to make a home-made JeeNode for little money, I have decided to do it like this.<\/p>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/P61215-215905-1-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"222\" height=\"300\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/P61215-215905-1-1-222x300.jpg\" alt=\"\" class=\"wp-image-153 size-full\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/P61215-215905-1-1-222x300.jpg 222w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/P61215-215905-1-1-759x1024.jpg 759w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/P61215-215905-1-1-768x1036.jpg 768w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/P61215-215905-1-1.jpg 850w\" sizes=\"auto, (max-width: 222px) 100vw, 222px\" \/><\/a><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">With an Arduino Pro Mini, one of those that you can get in AliExpress for less than 2\u20ac, duly modified to improve its power consumption (removing the power led, which consumes a lot for this application and eliminating the voltage regulator, since I'm going to use it with batteries and I hope they last several months), and a RFM12B radio module that I already had, I have built a homemade JeeNode in a couple of hours.<\/p>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The biggest problem is the sketch that has to be loaded in the Arduino, and after a couple of years since I wrote the old one, I don't know where it is, I can't find it, neither alive nor dead, so I had to write a new one. The sketch itself is very simple, the difficulty comes because you have to optimise it so that it consumes as little as possible, which means some additional headaches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As in the previous version, I have used the great Arduino Jeelib library. This library is great for implementing low-power modules with radio communication. It's a bit old, but it's just what I need and I'm familiar with it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The truth is that optimising its consumption has taken me much longer than I thought it would. In the end I spent a week testing, measuring and changing the code until I got what I wanted.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is the finished device:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/GasPulseCounterTransp-min.png\"><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"570\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/GasPulseCounterTransp-min.png\" alt=\"Arduino Gas Meter\" class=\"wp-image-235520\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/GasPulseCounterTransp-min.png 737w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/GasPulseCounterTransp-min-300x232.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/a><\/figure>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGas.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"519\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGas-1024x519.jpg\" alt=\"\" class=\"wp-image-154 size-full\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGas-1024x519.jpg 1024w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGas-300x152.jpg 300w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGas-768x389.jpg 768w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGas.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\"><em><strong>Edito:<\/strong> <\/em>I include this graph one year later (February 2018), where you can see that the invention is working spectacularly well. After a year of uninterrupted use, the battery (two 1.5 Volt AA batteries) is still above 3 Volts, so many more months of operation are expected before the batteries need to be replaced.<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2021\/02\/BateriaGasFebrero2021.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"265\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2021\/02\/BateriaGasFebrero2021-1024x265.png\" alt=\"\" class=\"wp-image-237533 size-full\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2021\/02\/BateriaGasFebrero2021-1024x265.png 1024w, https:\/\/emariete.com\/wp-content\/uploads\/2021\/02\/BateriaGasFebrero2021-300x78.png 300w, https:\/\/emariete.com\/wp-content\/uploads\/2021\/02\/BateriaGasFebrero2021-768x199.png 768w, https:\/\/emariete.com\/wp-content\/uploads\/2021\/02\/BateriaGasFebrero2021.png 1146w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\"><em><strong>Edito 2:<\/strong> <\/em>I include this graph three years later (February 2021).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The system <strong>is still in operation after more than <span style=\"text-decoration: underline;\">four years of uninterrupted operation<\/span> with the same batteries (which were not even new when I put them in to test them for the first time)<\/strong>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the voltage of the batteries is still over the <strong>2.92V<\/strong>. Not bad...<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile is-vertically-aligned-center has-global-color-8-background-color has-background\"><figure class=\"wp-block-media-text__media\"><a href=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGasDiciembre2023.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"233\" src=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGasDiciembre2023-1024x233.png\" alt=\"\" class=\"wp-image-241907 size-full\" srcset=\"https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGasDiciembre2023-1024x233.png 1024w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGasDiciembre2023-300x68.png 300w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGasDiciembre2023-768x175.png 768w, https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/BateriaGasDiciembre2023.png 1205w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\"><em><strong>Edito 3:<\/strong> <\/em>I include this graphic others <strong>almost three years later (December 2023)<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The system <strong>IT STILL WORKS AFTER MORE THAN <span style=\"text-decoration: underline;\">SIX YEARS of uninterrupted operation<\/span> with the same batteries (which were not even new when I put them in to test them for the first time)<\/strong>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The battery voltage is still above the <strong>2.8V<\/strong>and therefore <strong>there is still plenty of battery life left<\/strong>How far will it go?<\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-el-codigo-para-arduino\">The code for Arduino<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Although I'm not sure if this is the latest version of the code (I lost it years ago and just found it), it might be a good starting point if you're curious about how it works or if you want to replicate it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code\"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n\/\/ (c) eMariete 2016-2023 https:\/\/emariete.com\/medir-consumo-gas-arduino\/\n\/\/ Este sketch para Arduino se llama ContadorReedDelGas.ino:\n\/\/ Se utiliza para contar los pulsos de un sensor de gas de tipo reed y enviar los datos a trav\u00e9s de RFM12B a un nodo de registro.\n\/\/ El c\u00f3digo se basa en el c\u00f3digo de ejemplo de JeeLib y se ha modificado para adaptarse a las necesidades de este proyecto.\n\/\/ Tambi\u00e9n est\u00e1 basado en c\u00f3digo encontrado en el foro de Open Energy Monitor pero han pasado a\u00f1os y no localizo nombre ni autor (lo siento)\n\n\/* Asignaci\u00f3n recomendada de ID de nodo\n------------------------------------------------------------------------------------------------------------\n-ID-  - Tipo de Nodo\n0     - Asignaci\u00f3n especial en el controlador JeeLib RFM12: reservado para uso OOK\n1-4   - Nodos de control\n5-10  - Nodos de monitoreo de energ\u00eda\n11-14 - No asignado--\n15-16 - Estaci\u00f3n base y nodos de registro\n17-30 - Nodos de detecci\u00f3n ambiental (temperatura, humedad, etc.)\n31    - Asignaci\u00f3n especial en el controlador JeeLib RFM12: el Nodo31 puede comunicarse con nodos en cualquier grupo de red\n-------------------------------------------------------------------------------------------------------------\n*\/\n\n#define RF_freq RF12_868MHZ                                                \/\/ Frecuencia del m\u00f3dulo RF12B, puede ser RF12_433MHZ, RF12_868MHZ o RF12_915MHZ.\nconst int nodeID = 18;                                                     \/\/ ID del nodo Jeenode\nconst int networkGroup = 212;                                              \/\/ Grupo de red inal\u00e1mbrica Jeenode RFM12B\nconst int UNO = 1;                                                         \/\/ Establecer en 0 si no se est\u00e1 utilizando el cargador de arranque UNO (es decir, utilizando Duemilanove)\n\n#include &lt;avr\/wdt.h&gt;                                                       \/\/ El cargador de arranque UNO\n#include &lt;JeeLib.h&gt;                                                        \/\/ Descargar JeeLib: http:\/\/github.com\/jcw\/jeelib\n\nISR(WDT_vect) {\n  Sleepy::watchdogEvent();\n}\n#include &lt;avr\/power.h&gt;\n#include &lt;avr\/sleep.h&gt;\n#include &lt;PinChangeInt.h&gt;                                                  \/\/ Interrupciones de cambio de pin.\n\ntypedef struct {\n  unsigned int pulse, totalpulse, battery;\n} PayloadGas;\nPayloadGas gas;                                                          \/\/ Forma ordenada de empaquetar datos para la comunicaci\u00f3n RF\n\nconst  unsigned long WDT_PERIOD = 1000;                                    \/\/ milisegundos.\nconst  unsigned long WDT_MAX_NUMBER = 10;                                  \/\/ Datos enviados despu\u00e9s de WDT_MAX_NUMBER periodos de  WDT_PERIOD ms sin pulsos\n\/\/                              O\nconst  unsigned long PULSE_MAX_NUMBER = 5;                                 \/\/ Datos enviados despu\u00e9s de PULSE_MAX_NUMBER pulsos\nconst  unsigned long PULSE_MAX_DURATION = 10;                              \/\/ El sensor se apaga durante PULSE_MAX_DURATION milisegundos despu\u00e9s de un pulso para desactivar el rebote.\n\nconst int BATT_ADC = A0;\nconst int magPin = 3;                                                      \/\/ Sensor magn\u00e9tico\nconst int ledPin = 13;                                                     \/\/ LED de depuraci\u00f3n.\n\n\/\/ Objetos\nPCintPort PCintPort();\n\nvolatile unsigned long pulseCount ;\nvolatile unsigned long elapsed_time;\nunsigned long WDT_number;\n\nboolean  debug = true;\nboolean  p;                                                                \/\/ Bandera para nuevo pulso\n\nvoid setup()\n{\n  Serial.begin(57600);\n  Serial.println(&quot;ContadorReedDelGas.ino V1.0c&quot;);\n  Serial.print(&quot;Grupo Id: &quot;);\n  Serial.println(networkGroup);\n  Serial.print(&quot;ID de nodo: &quot;);\n  Serial.println(nodeID);\n  Serial.print(&quot;Enviando datos despu\u00e9s de &quot;);\n  Serial.print(PULSE_MAX_NUMBER);\n  Serial.println(&quot; pulsos&quot;);\n  Serial.print(&quot;  o  &quot;);\n  Serial.print(WDT_MAX_NUMBER);\n  Serial.print(&quot; periodos de &quot;);\n  Serial.print(WDT_PERIOD);\n  Serial.println(&quot; ms sin ning\u00fan pulso&quot;);\n  Serial.println(&quot;Inicializando RF..&quot;);\n  delay(100);\n  rf12_initialize(nodeID, RF_freq, networkGroup);                          \/\/ Inicializar RF\n  rf12_sleep(RF12_SLEEP);\n  Serial.println(&quot;RF listo&quot;);\n  delay(50);\n\n  gas.pulse = 0;\n  gas.totalpulse = 0;\n  gas.battery = 0;\n  pulseCount = 0;\n  analogReference(INTERNAL);\n  delay(50);\n\n  WDT_number = 0;\n\n  if (Serial) debug = 1; else debug = 0;                                   \/\/ Si la UART serie a USB est\u00e1 conectada, mostrar la salida de depuraci\u00f3n. Si no, deshabilitar la serie.\n  debug = true;\n  p = 0;\n\n  pinMode(magPin, INPUT);                                                  \/\/ Establecer el pin como entrada\n  digitalWrite(magPin, HIGH);                                              \/\/ No utilizar la resistencia pull-up interna\n  pinMode(ledPin, OUTPUT);\n  digitalWrite(ledPin, !0);\n\n  \/\/ attachInterrupt(1, onPulse, RISING);                                  \/\/ Interrupci\u00f3n de kWh adjunta a IRQ 0  = Digita 2 - conectado directamente al bloque de terminales\n  PCintPort::attachInterrupt(magPin, onPulse, FALLING);\n\n  gas.battery = (unsigned int)(analogRead(BATT_ADC) *   0.542);            \/\/ La primera lectura de DAC es inexacta\n}\n\n\/**\n * The main loop function that runs repeatedly in the program.\n * It checks for incoming pulses, performs fading, and sends RF data.\n *\/\nvoid loop()\n{\n  \/\/ Si acaba de llegar un pulso, esperar PULSE_MAX_DURATION ms para el desvanecimiento\n  if (p) {\n    Serial.flush;\n    Sleepy::loseSomeTime(PULSE_MAX_DURATION);\n    if (debug == debug) {\n      flashLed(25);\n    }\n    p = 0;\n  }\n\n  Serial.flush;\n  if (Sleepy::loseSomeTime(WDT_PERIOD) == 1) {                            \/\/ Verdadero si no ocurri\u00f3 ning\u00fan pulso durante el WDT\n    WDT_number++;                                                         \/\/ N\u00famero de WDT finalizados normalmente desde el \u00faltimo env\u00edo de RF\n    if (debug) {\n      Serial.print(&quot;.&quot;);\n      delay(3);\n    }\n  }\n  else if (debug)\n  {\n    Serial.print(&quot;INT &quot;);\n    delay(5);\n    Serial.println(pulseCount);\n    delay(5);\n  }\n\n  if (WDT_number &gt;= WDT_MAX_NUMBER || pulseCount &gt;= PULSE_MAX_NUMBER) {\n    cli(); \/\/ Desactivar la interrupci\u00f3n en caso de que llegue un pulso mientras estamos actualizando el conteo\n    gas.totalpulse += (unsigned int) pulseCount;\n    gas.pulse = (unsigned int) pulseCount;\n    pulseCount = 0;\n    sei();\n\n    gas.battery = (unsigned int)(readVcc());\n\n    if (debug == 1) {\n      Serial.print(&quot;ADC: &quot;); delay(5); Serial.println(readVcc()); delay(5);\n      Serial.print(&quot;Bater\u00eda: &quot;); delay(5); Serial.println(gas.battery); delay(5);\n    }\n\n    if (debug == 1) {\n      Serial.println(&quot;&quot;); delay(5);\n      Serial.print(&quot;Enviando &quot;); delay(5);\n      Serial.print(gas.pulse); delay(5);\n      Serial.print(&quot;\/&quot;); delay(5);\n      Serial.print(gas.totalpulse); delay(5);\n      Serial.print(&quot;\/&quot;); delay(5);\n      Serial.print(gas.battery); delay(5);\n      Serial.println(&quot;&quot;); delay(5);\n    }\n\n    send_rf_data();                          \/\/ *ENVIAR DATOS RF* - \n    WDT_number = 0;\n  }\n}\n\n\/\/ La rutina de interrupci\u00f3n - se ejecuta cada vez que se detecta un flanco descendente de un pulso\nvoid onPulse()\n{\n  p = 1;                                     \/\/ Bandera para nuevo pulso establecida a verdadero\n  pulseCount++;                              \/\/ N\u00famero de pulsos desde el \u00faltimo env\u00edo de RF\n}\n\n\/\/ Calcular el voltaje de la MCU (mV).\nlong readVcc() {\n  long result;\n  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);\n  Sleepy::loseSomeTime(2);\n  ADCSRA |= _BV(ADSC);\n  while (bit_is_set(ADCSRA, ADSC));\n  result = ADCL;\n  result |= ADCH &lt;&lt; 8;\n  result = 1126400L \/ result;\n  return result;\n}\n\n\/\/ Enviar datos a trav\u00e9s del m\u00f3dulo RF\nvoid send_rf_data()\n{\n  power_spi_enable();\n  rf12_sleep(RF12_WAKEUP);\n  rf12_sendNow(0, &amp;gas, sizeof gas);   \/\/ Enviar datos de temperatura a trav\u00e9s de RFM12B utilizando el wraper rf12_sendNow\n  rf12_sendWait(2);\n  rf12_sleep(RF12_SLEEP);\n  power_spi_disable();\n}\n\n\/\/ Parpadear un LED con fines de depuraci\u00f3n\nvoid flashLed (int i) {\n  digitalWrite(ledPin, !1);\n  Sleepy::loseSomeTime(i);\n  digitalWrite(ledPin, !0);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This Arduino code is used to count pulses from a reed-type gas sensor and send the data through an RFM12B module to a logging node. Here is an overview of the code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Parameter settings:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The frequency of the RF12B module is defined (<code>RF_freq<\/code>) and other parameters such as the node ID (<code>nodeID<\/code>), the wireless network group (<code>networkGroup<\/code>), and the configuration for the UNO bootloader (<code>UNO<\/code>).<\/li>\n\n\n\n<li>Constants are specified for the duration and maximum number of pulses (<code>PULSE_MAX_DURATION<\/code> y <code>PULSE_MAX_NUMBER<\/code>) and the maximum number of time periods without pulses (<code>WDT_MAX_NUMBER<\/code> y <code>WDT_PERIOD<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Libraries and structures:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Necessary libraries are included such as <code>avr\/wdt.h<\/code>, <code>JeeLib.h<\/code>, y <code>PinChangeInt.h<\/code>.<\/li>\n\n\n\n<li>A structure is defined (<code>PayloadGas<\/code>) to package the data to be sent by RF.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Initialisation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Variables and objects are initialised, such as the object <code>PCintPort<\/code> to manage pin change interruptions.<\/li>\n\n\n\n<li>Input and output pins are configured, and the internal ADC reference is set.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Interrupt routine and auxiliary functions:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The interrupt routine (<code>onPulse()<\/code>) is executed when a falling edge of a pulse is detected on the magnetic sensor pin.<\/li>\n\n\n\n<li>The function <code>readVcc()<\/code> is used to measure the MCU voltage.<\/li>\n\n\n\n<li><code>send_rf_data()<\/code> sends the data via the RFM12B module.<\/li>\n\n\n\n<li><code>flashLed()<\/code> flashes an LED for debugging purposes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main loop:<\/strong>\n<ul class=\"wp-block-list\">\n<li>It runs continuously.<\/li>\n\n\n\n<li>Wait for pulses and time out after each pulse to avoid bouncing.<\/li>\n\n\n\n<li>Counts the number of pulses and sends data through the RFM12B module after a specified number of pulses or pulse-free periods.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Debugging:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The variable is used <code>debug<\/code> to control the debug output through the serial interface.<\/li>\n\n\n\n<li>An LED flashes when a new pulse is detected.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">It is important to note that the code uses the library <code>JeeLib<\/code>so you should make sure you have it installed for the code to work properly. Also, if you plan to use this code, make sure you understand how it works and adapt the parameters to your specific needs.<\/p>","protected":false},"excerpt":{"rendered":"<p>Those who are regulars of the blog, already know that I like to measure everything, and gas could not be less. I have been measuring gas consumption for years with Arduino, but the important thing is not that, the important thing is that this Arduino has been sending data wirelessly for almost six years with only two AA batteries, and ... <a title=\"Measuring gas consumption with Arduino\" class=\"read-more\" href=\"https:\/\/emariete.com\/en\/medir-consumo-gas-arduino\/\" aria-label=\"Read more about Measuring gas consumption with the Arduino\">Read more<\/a><\/p>","protected":false},"author":1,"featured_media":237534,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[14,17],"tags":[20,21,43,50],"class_list":["post-35","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-domotica","category-proyectos-arduino","tag-arduino","tag-bajo-consumo","tag-proyectos-arduino","tag-sensores"],"modified_by":"Mariete","uagb_featured_image_src":{"full":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas.png",1080,608,false],"thumbnail":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas-150x150.png",150,150,true],"medium":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas-300x169.png",300,169,true],"medium_large":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas-768x432.png",768,432,true],"large":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas-1024x576.png",1024,576,true],"1536x1536":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas.png",1080,608,false],"2048x2048":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas.png",1080,608,false],"trp-custom-language-flag":["https:\/\/emariete.com\/wp-content\/uploads\/2016\/12\/Midiendo-el-consumo-del-gas.png",18,10,false]},"uagb_author_info":{"display_name":"Mariete","author_link":"https:\/\/emariete.com\/en\/author\/admin_xlyz052h\/"},"uagb_comment_info":7,"uagb_excerpt":"Los que son habituales del blog, ya saben que me gusta medirlo todo, y el gas no pod\u00eda ser menos. Llevo a\u00f1os midiendo el consumo del gas con Arduino, pero lo importante no es eso, lo importante es que ese Arduino lleva casi seis a\u00f1os enviando los datos inal\u00e1mbricamente con solo dos pilas AA, y&hellip;","_links":{"self":[{"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":8,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":241987,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/posts\/35\/revisions\/241987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/media\/237534"}],"wp:attachment":[{"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emariete.com\/en\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}