1+ /* Heltec Automation LoRaWAN communication example
2+ *
3+ * Function:
4+ * 1. Upload Heltec_ESP32 node data to the server using the standard LoRaWAN protocol.
5+ *
6+ * Description:
7+ * 1. Communicate using LoRaWAN protocol.
8+ *
9+ * HelTec AutoMation, Chengdu, China
10+ * 成都惠利特自动化科技有限公司
11+ * www.heltec.org
12+ *
13+ * this project also realess in GitHub:
14+ * https://github.com/HelTecAutomation/Heltec_ESP32
15+
16+ * the development environment of this project in Arduion:
17+ * https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
18+ * */
19+
20+ #include " LoRaWan_APP.h"
21+ #include " Wire.h"
22+ #include " GXHTC.h"
23+ // #include "Arduino.h"
24+
25+ GXHTC gxhtc;
26+
27+ uint8_t devEui[] = { 0x22 , 0x32 , 0x33 , 0x00 , 0x00 , 0x88 , 0x88 , 0x03 };
28+ uint8_t appEui[] = { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
29+ uint8_t appKey[] = { 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 };
30+
31+ /* ABP para*/
32+ uint8_t nwkSKey[] = { 0x15 , 0xb1 , 0xd0 , 0xef , 0xa4 , 0x63 , 0xdf , 0xbe , 0x3d , 0x11 , 0x18 , 0x1e , 0x1e , 0xc7 , 0xda ,0x85 };
33+ uint8_t appSKey[] = { 0xd7 , 0x2c , 0x78 , 0x75 , 0x8c , 0xdc , 0xca , 0xbf , 0x55 , 0xee , 0x4a , 0x77 , 0x8d , 0x16 , 0xef ,0x67 };
34+ uint32_t devAddr = ( uint32_t )0x007e6ae1 ;
35+
36+ /* LoraWan channelsmask, default channels 0-7*/
37+ uint16_t userChannelsMask[6 ]={ 0x00FF ,0x0000 ,0x0000 ,0x0000 ,0x0000 ,0x0000 };
38+
39+ /* LoraWan region, select in arduino IDE tools*/
40+ LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
41+
42+ /* LoraWan Class, Class A and Class C are supported*/
43+ DeviceClass_t loraWanClass = CLASS_A;
44+
45+ /* the application data transmission duty cycle. value in [ms].*/
46+ uint32_t appTxDutyCycle = 15000 ;
47+
48+ /* OTAA or ABP*/
49+ bool overTheAirActivation = true ;// OTAA security is better
50+
51+ /* ADR enable*/
52+ bool loraWanAdr = true ;
53+
54+ /* Indicates if the node is sending confirmed or unconfirmed messages */
55+ bool isTxConfirmed = true ;
56+
57+ /* Application port */
58+ uint8_t appPort = 2 ;
59+ /* !
60+ * Number of trials to transmit the frame, if the LoRaMAC layer did not
61+ * receive an acknowledgment. The MAC performs a datarate adaptation,
62+ * according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
63+ * to the following table:
64+ *
65+ * Transmission nb | Data Rate
66+ * ----------------|-----------
67+ * 1 (first) | DR
68+ * 2 | DR
69+ * 3 | max(DR-1,0)
70+ * 4 | max(DR-1,0)
71+ * 5 | max(DR-2,0)
72+ * 6 | max(DR-2,0)
73+ * 7 | max(DR-3,0)
74+ * 8 | max(DR-3,0)
75+ *
76+ * Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
77+ * the datarate, in case the LoRaMAC layer did not receive an acknowledgment
78+ */
79+ uint8_t confirmedNbTrials = 4 ;//
80+
81+ /* Prepares the payload of the frame */
82+ static void prepareTxFrame ( uint8_t port )
83+ {
84+ /* appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in "commissioning.h".
85+ *appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
86+ *if enabled AT, don't modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
87+ *if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
88+ *for example, if use REGION_CN470,
89+ *the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h".
90+ */
91+
92+ /* Initialize the sensor */
93+ gxhtc.begin (20 , 21 );
94+
95+ /* Read sensor data */
96+ gxhtc.read_data ();
97+
98+ Serial.print (" Temperature:" );
99+ Serial.print (gxhtc.g_temperature );
100+ Serial.print (" Humidity:" );
101+ Serial.println (gxhtc.g_humidity );
102+
103+ // ghxtc3();
104+ unsigned char *puc;
105+ appDataSize = 0 ;
106+ appData[appDataSize++] = 0x04 ;
107+ appData[appDataSize++] = 0x00 ;
108+ appData[appDataSize++] = 0x0A ;
109+
110+ /* The upper four digits represent the subID, and the lower four digits represent significant decimal places */
111+ appData[appDataSize++] =0X02 ;
112+
113+ // print out the values you read:
114+ puc = (unsigned char *)(&gxhtc.g_temperature );
115+ appData[appDataSize++] = puc[0 ];
116+ appData[appDataSize++] = puc[1 ];
117+ appData[appDataSize++] = puc[2 ];
118+ appData[appDataSize++] = puc[3 ];
119+
120+ /* The upper four digits represent the subID, and the lower four digits represent significant decimal places */
121+ appData[appDataSize++] = 0x12 ;
122+
123+ // print out the values you read:
124+ puc = (unsigned char *)(&gxhtc.g_humidity );
125+ appData[appDataSize++] = puc[0 ];
126+ appData[appDataSize++] = puc[1 ];
127+ appData[appDataSize++] = puc[2 ];
128+ appData[appDataSize++] = puc[3 ];
129+
130+ // read the analog / millivolts value for pin 1:
131+ int analogValue = analogRead (1 );
132+ int analogVolts = analogReadMilliVolts (1 );
133+ Serial.printf (" ADC analog value = %d\n " ,analogValue);
134+ Serial.printf (" ADC millivolts value = %d\n " ,analogVolts);
135+
136+ /* Sensor parent ID and sensor length */
137+ appData[appDataSize++] = 0x00 ;
138+ appData[appDataSize++] = 0x00 ;
139+ appData[appDataSize++] = 0x02 ;
140+
141+ /* The higher 4 digits represent the subID, and the lower 4 digit represents the data type */
142+ appData[appDataSize++] = (uint8_t )0X05 ;
143+
144+ // print out the values you read:
145+ puc = (unsigned char *)(&analogValue);
146+ appData[appDataSize++] = (uint8_t )0X05 ;
147+ appData[appDataSize++] =((uint8_t )(int )analogValue / 3.7 );
148+
149+ Wire.end ();
150+ }
151+
152+ /* if true, next uplink will add MOTE_MAC_DEVICE_TIME_REQ */
153+ void VextON (void )
154+ {
155+ pinMode (Vext,OUTPUT);
156+ digitalWrite (Vext, LOW);
157+ }
158+
159+ void VextOFF (void )
160+ {
161+ pinMode (Vext,OUTPUT);
162+ digitalWrite (Vext, HIGH);
163+ }
164+
165+ void setup () {
166+ gxhtc.begin (20 , 21 );
167+ Serial.begin (115200 );
168+ analogReadResolution (12 );
169+ Mcu.begin (HELTEC_BOARD,SLOW_CLK_TPYE);
170+ deviceState = DEVICE_STATE_INIT;
171+ }
172+
173+ void loop ()
174+ {
175+ switch ( deviceState )
176+ {
177+ case DEVICE_STATE_INIT:
178+ {
179+ #if (LORAWAN_DEVEUI_AUTO)
180+ LoRaWAN.generateDeveuiByChipID ();
181+ #endif
182+ LoRaWAN.init (loraWanClass,loraWanRegion);
183+ break ;
184+ }
185+ case DEVICE_STATE_JOIN:
186+ {
187+ LoRaWAN.join ();
188+ break ;
189+ }
190+ case DEVICE_STATE_SEND:
191+ {
192+ prepareTxFrame ( appPort );
193+ LoRaWAN.send ();
194+ deviceState = DEVICE_STATE_CYCLE;
195+ break ;
196+ }
197+ case DEVICE_STATE_CYCLE:
198+ {
199+ // Schedule next packet transmission
200+ txDutyCycleTime = appTxDutyCycle + randr ( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
201+ LoRaWAN.cycle (txDutyCycleTime);
202+ deviceState = DEVICE_STATE_SLEEP;
203+ break ;
204+ }
205+ case DEVICE_STATE_SLEEP:
206+ {
207+ LoRaWAN.sleep (loraWanClass);
208+ break ;
209+ }
210+ default :
211+ {
212+ deviceState = DEVICE_STATE_INIT;
213+ break ;
214+ }
215+ }
216+ }
0 commit comments