{"id":3428,"date":"2021-04-10T10:52:19","date_gmt":"2021-04-10T15:52:19","guid":{"rendered":"http:\/\/blog.espol.edu.ec\/girni\/?p=3428"},"modified":"2026-08-04T22:26:14","modified_gmt":"2026-08-05T03:26:14","slug":"lorawan-bateria-carga-panel-solar-archivo-ino","status":"publish","type":"post","link":"https:\/\/blog.espol.edu.ec\/girni\/lorawan-bateria-carga-panel-solar-archivo-ino\/","title":{"rendered":"2. LoRaWan - Bateria y carga. Archivo.ino"},"content":{"rendered":"\n<p>El m\u00f3dulo de desarrollo Heltec Wireless Stick Lite tiene el circuito de sensor y alimentaci\u00f3n por bater\u00edas. El diagrama de pines muestra que el pin 13 se encuentra conectado como detector de energ\u00eda, que se incorpora a las instrucciones como PowerDetection. En los nuevos modelos, el detector de energ\u00eda es el pin 37.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparaci\u00f3n de trama<\/h2>\n\n\n\n<p>El valor del sensor ADC para bateria es de tipo entero, entrega la cuantificaci\u00f3n en el rango [0, 4096], por lo que un entero de 2 bytes es suficiente para los datos. El proceso de conversi\u00f3n a voltaje se realiza en el decodificador en Application-Server del Broker ChirpStack.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code alignwide\"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n  uint16_t BateriaV = SensorBateria();\n  \n  appDataSize = 4 ;\n  appData&#x5B;3] = 0 ;\n  appData&#x5B;2] = 0 ;\n  appData&#x5B;1] = highByte(BateriaV);\n  appData&#x5B;0] = lowByte(BateriaV);\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Intrucciones Principales<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code alignwide\"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n\/* Adaptado a partir de:\n * HelTec Automation(TM) LoRaWAN 1.0.2 OTAA example use OTAA, CLASS A\n * Solo ESP32+LoRa series boards con licencia http:\/\/www.heltec.cn\/search\/);\n * https:\/\/github.com\/HelTecAutomation\/ESP32_LoRaWAN\/tree\/master\/examples\/OTAA_Battery_power\n*\/\n#include &lt;ESP32_LoRaWAN.h&gt;\n#include &quot;Arduino.h&quot;\n\n\/*licencia Heltec ESP32 LoRaWan http:\/\/resource.heltec.cn\/search *\/\nuint32_t  license&#x5B;4] = { 0xC254CA22, 0xFB5646A9, 0xA23B184F, 0x8F613844};\n\n\/* OTAA parametros*\/\nuint8_t DevEui&#x5B;] = { 0xc5, 0xdb, 0x5e, 0x2b, 0x64, 0xee, 0xfc, 0xae };\nuint8_t AppEui&#x5B;] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };\nuint8_t AppKey&#x5B;] = { 0xf2, 0x1f, 0xff, 0x5c, 0x6f, 0xf2, 0x4c, 0xa0, 0x74, 0x73, 0xf5, 0xef, 0xf7, 0x39, 0x39, 0x10 };\n\n\/* ABP parametros*\/\nuint8_t NwkSKey&#x5B;] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };\nuint8_t AppSKey&#x5B;] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };\nuint32_t DevAddr =  ( uint32_t )0x007e6ae1;\n\n\/*LoraWan channelsmask, default channels 0-7*\/ \nuint16_t userChannelsMask&#x5B;6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };\n\nDeviceClass_t  loraWanClass = CLASS_A; \/*Soporte de A and C*\/\nuint32_t appTxDutyCycle = 15000;       \/*15000; en &#x5B;ms]*\/\nbool overTheAirActivation = true;      \/*OTAA or ABP*\/\nbool loraWanAdr = true;                \/*ADR enable*\/\nbool isTxConfirmed = true;             \/*confirmed or unconfirmed messages *\/\nuint8_t appPort = 2;                   \/* Application port *\/\n\n\/* reintentos de transmisi\u00f3n, en caso de no recibir ack *\/\nuint8_t confirmedNbTrials = 8;\n\n\/* Seleccionado de Arduino IDE tools *\/\nuint8_t debugLevel = LoRaWAN_DEBUG_LEVEL;\nLoRaMacRegion_t loraWanRegion = ACTIVE_REGION;\n\n\/\/ Mensajes por Puerto Serial\nvolatile boolean serial_msg = true;\n\n\/\/ variables de sensor\/actuador\n\/\/ Sensor de estado de bateria, revisar modelo de m\u00f3dulo\n\/\/ PowerDetection 13 en Wireless Stick lite, otros 37\n#define Vext 21\n#define PowerDetection 13\n\nvoid setup()\n{\n  Serial.begin(115200);\n  while (!Serial);\n  SPI.begin(SCK,MISO,MOSI,SS);\n  Mcu.init(SS,RST_LoRa,DIO0,DIO1,license);\n\n  \/\/sensor de bateria\n  adcAttachPin(PowerDetection);\n  analogSetClockDiv(255); \/\/ 1338mS\n    pinMode(Vext, OUTPUT);\n    \n  deviceState = DEVICE_STATE_INIT;\n}\n\nvoid loop() {\n  \n  switch( deviceState )  {\n    case DEVICE_STATE_INIT:    {\n      LoRaWAN.init(loraWanClass,loraWanRegion);\n      break;\n    }\n    case DEVICE_STATE_JOIN:    {\n      LoRaWAN.join();\n      break;\n    }\n    case DEVICE_STATE_SEND:    { \n      prepareTxFrame( appPort );\n      LoRaWAN.send(loraWanClass);\n      deviceState = DEVICE_STATE_CYCLE;\n      break;\n    }\n    case DEVICE_STATE_CYCLE:    {\n      \/\/ Schedule next packet transmission\n      txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND,\n                                                APP_TX_DUTYCYCLE_RND );\n      LoRaWAN.cycle(txDutyCycleTime);\n      deviceState = DEVICE_STATE_SLEEP;\n      break;\n    }\n    case DEVICE_STATE_SLEEP:    {\n      LoRaWAN.sleep(loraWanClass,debugLevel);\n      break;\n    }\n    default:    {\n      deviceState = DEVICE_STATE_INIT;\n      break;\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Preparaci\u00f3n de trama<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code alignwide\"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n\/\/ Trama - integra datos\nstatic void prepareTxFrame( uint8_t port ){\n  \n  uint16_t BateriaV = SensorBateria();\n  \n  appDataSize = 4 ;\n  appData&#x5B;3] = 0 ;\n  appData&#x5B;2] = 0 ;\n  appData&#x5B;1] = highByte(BateriaV);\n  appData&#x5B;0] = lowByte(BateriaV);\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Lectura de sensor<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code alignwide\"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nuint16_t SensorBateria(){\n  \n  \/\/ lectura ADC Voltaje de bateria \n  digitalWrite(Vext, LOW);\n  delay(10);\n  uint16_t ADC_voltage = analogRead(PowerDetection);\n  digitalWrite(Vext, HIGH);\n\n  Serial.print(&quot;Voltaje Bater\u00eda (V): &quot;);\n  Serial.println(ADC_voltage);\n\n  return ADC_voltage;\n}\n<\/pre><\/div>\n\n\n<p><em><strong>Referencia<\/strong><\/em>: Heltec LoRa ESP32, https:\/\/github.com\/HelTecAutomation\/ESP32_LoRaWAN\/blob\/master\/examples\/OTAA_Battery_power\/OTAA_Battery_power.ino<\/p>\n\n\n\n<p>Heltec LoRa ESP32 Wireless Stick Lite, diagrama de pintes- https:\/\/resource.heltec.cn\/download\/Wireless_Stick_Lite\/Wireless_Stick_Lite.pdf<\/p>\n","protected":false},"excerpt":{"rendered":"<p>El m\u00f3dulo de desarrollo Heltec Wireless Stick Lite tiene el circuito de sensor y alimentaci\u00f3n por bater\u00edas. El diagrama de pines muestra que el pin 13 se encuentra conectado como detector de energ\u00eda, que se incorpora a las instrucciones como PowerDetection. En los nuevos modelos, el detector de energ\u00eda es el pin 37. Preparaci\u00f3n de [&hellip;]<\/p>\n","protected":false},"author":8043,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1465118],"tags":[],"class_list":["post-3428","post","type-post","status-publish","format-standard","hentry","category-lorawan-bateria-panel-solar"],"_links":{"self":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts\/3428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/users\/8043"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/comments?post=3428"}],"version-history":[{"count":6,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts\/3428\/revisions"}],"predecessor-version":[{"id":4896,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts\/3428\/revisions\/4896"}],"wp:attachment":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/media?parent=3428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/categories?post=3428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/tags?post=3428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}