1+ /* Heltec Automation LoRaWAN communication example
2+ *
3+ * Function:
4+ * 1. Drive GXHTV3 temperature and humidity sensor;
5+ * 2. Display current temperature and humidity on the TFT display;
6+ * 3. Upload temperature and humidity sensor via LoRaWAN protocol.
7+ *
8+ * Description:
9+ * 1. This example requires connecting the GXHTV3 sensor to the SH2.0-4P interface;
10+ * 2. To complete LoRaWAN communication, you need a LoRaWAN gateway and LoRaWAN server;
11+ * 3. Recommend LoRaWAN Gateway: https://heltec.org/project/ht-m7603/
12+ * 4. Recommend LoRaWAN Server: https://snapemu.com/
13+ *
14+ * HelTec AutoMation, Chengdu, China
15+ * 成都惠利特自动化科技有限公司
16+ * www.heltec.org
17+ *
18+ * */
19+
20+ #include " LoRaWan_APP.h"
21+ #include " HT_ST7789spi.h"
22+ #include < Adafruit_GFX.h> // Core graphics library
23+ #include " img.h"
24+ #include " Wire.h"
25+ #include " GXHTC.h"
26+ #define st7789_CS_Pin 39
27+ #define st7789_REST_Pin 40
28+ #define st7789_DC_Pin 47
29+ #define st7789_SCLK_Pin 38
30+ #define st7789_MOSI_Pin 48
31+ #define st7789_LED_K_Pin 17
32+ #define st7789_VTFT_CTRL_Pin 7
33+ static HT_ST7789 *st7789 = NULL ; // lcd object pointer, it's a 240x135 lcd display, Adafruit dependcy
34+ static SPIClass *gspi_lcd = NULL ;
35+
36+ GXHTC gxhtc;
37+ char buffer[256 ];
38+
39+ /* OTAA para*/
40+ uint8_t devEui[] = { 0x22 , 0x32 , 0x33 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
41+ uint8_t appEui[] = { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
42+ uint8_t appKey[] = { 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 , 0x88 };
43+
44+ /* ABP para*/
45+ uint8_t nwkSKey[] = { 0x15 , 0xb1 , 0xd0 , 0xef , 0xa4 , 0x63 , 0xdf , 0xbe , 0x3d , 0x11 , 0x18 , 0x1e , 0x1e , 0xc7 , 0xda , 0x85 };
46+ uint8_t appSKey[] = { 0xd7 , 0x2c , 0x78 , 0x75 , 0x8c , 0xdc , 0xca , 0xbf , 0x55 , 0xee , 0x4a , 0x77 , 0x8d , 0x16 , 0xef , 0x67 };
47+ uint32_t devAddr = (uint32_t )0x007e6ae1 ;
48+
49+ /* LoraWan channelsmask, default channels 0-7*/
50+ uint16_t userChannelsMask[6 ] = { 0x00FF , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 };
51+
52+ /* LoraWan region, select in arduino IDE tools*/
53+ LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
54+
55+ /* LoraWan Class, Class A and Class C are supported*/
56+ DeviceClass_t loraWanClass = CLASS_A;
57+
58+ /* the application data transmission duty cycle. value in [ms].*/
59+ uint32_t appTxDutyCycle = 15000 ;
60+
61+ /* OTAA or ABP*/
62+ bool overTheAirActivation = 0 ;
63+
64+ /* ADR enable*/
65+ bool loraWanAdr = true ;
66+
67+ /* Indicates if the node is sending confirmed or unconfirmed messages */
68+ bool isTxConfirmed = true ;
69+
70+ /* Application port */
71+ uint8_t appPort = 2 ;
72+ /* !
73+ * Number of trials to transmit the frame, if the LoRaMAC layer did not
74+ * receive an acknowledgment. The MAC performs a datarate adaptation,
75+ * according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
76+ * to the following table:
77+ *
78+ * Transmission nb | Data Rate
79+ * ----------------|-----------
80+ * 1 (first) | DR
81+ * 2 | DR
82+ * 3 | max(DR-1,0)
83+ * 4 | max(DR-1,0)
84+ * 5 | max(DR-2,0)
85+ * 6 | max(DR-2,0)
86+ * 7 | max(DR-3,0)
87+ * 8 | max(DR-3,0)
88+ *
89+ * Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
90+ * the datarate, in case the LoRaMAC layer did not receive an acknowledgment
91+ */
92+ uint8_t confirmedNbTrials = 4 ;
93+
94+ /* Prepares the payload of the frame */
95+ static void prepareTxFrame (uint8_t port) {
96+ /* appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in "commissioning.h".
97+ *appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
98+ *if enabled AT, don't modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
99+ *if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
100+ *for example, if use REGION_CN470,
101+ *the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h".
102+ */
103+ gxhtc.begin (2 , 1 );
104+ gxhtc.read_data ();
105+ Serial.print (" Temperature:" );
106+ Serial.print (gxhtc.g_temperature );
107+ Serial.print (" Humidity:" );
108+ Serial.println (gxhtc.g_humidity );
109+ st7789->drawRGBBitmap (0 , 50 , temp_72_0, 72 , 72 );
110+ sprintf (buffer, " %.2f" , gxhtc.g_temperature );
111+ st7789->setTextSize (3 );
112+ st7789->fillRect (70 , 73 , 100 , 30 , ST7789_BLACK);
113+ testdrawtext (70 , 73 , (char *)buffer, ST7789_RED);
114+ // testdrawtext(130, 100, "C", ST7789_WHITE);
115+ st7789->drawRGBBitmap (160 , 50 , Humidity_72_0, 72 , 72 );
116+ sprintf (buffer, " %.2f" , gxhtc.g_humidity );
117+ st7789->fillRect (220 , 73 , 100 , 30 , ST7789_BLACK);
118+ testdrawtext (220 , 73 , (char *)buffer, ST7789_BLUE);
119+ // testdrawtext(280, 100, "%", ST7789_WHITE);
120+ appDataSize = 4 ;
121+ unsigned char *puc;
122+ appDataSize = 0 ;
123+ appData[appDataSize++] = 0x04 ;
124+ appData[appDataSize++] = 0x00 ;
125+ appData[appDataSize++] = 0x0A ;
126+ appData[appDataSize++] = 0x02 ;
127+ puc = (unsigned char *)(&gxhtc.g_temperature );
128+
129+ appData[appDataSize++] = puc[0 ];
130+ appData[appDataSize++] = puc[1 ];
131+ appData[appDataSize++] = puc[2 ];
132+ appData[appDataSize++] = puc[3 ];
133+ appData[appDataSize++] = 0x12 ;
134+
135+ puc = (unsigned char *)(&gxhtc.g_humidity );
136+ appData[appDataSize++] = puc[0 ];
137+ appData[appDataSize++] = puc[1 ];
138+ appData[appDataSize++] = puc[2 ];
139+ appData[appDataSize++] = puc[3 ];
140+
141+ Wire.end ();
142+
143+ appData[0 ] = 0x00 ;
144+ appData[1 ] = 0x01 ;
145+ appData[2 ] = 0x02 ;
146+ appData[3 ] = 0x03 ;
147+ }
148+ void testdrawtext (uint16_t x, uint16_t y, char *text, uint16_t color) {
149+ st7789->setCursor (x, y);
150+ st7789->setTextColor (color);
151+ st7789->setTextWrap (true );
152+ st7789->print (text);
153+ }
154+ // if true, next uplink will add MOTE_MAC_DEVICE_TIME_REQ
155+
156+ void setup () {
157+ Serial.begin (115200 );
158+ pinMode (7 , OUTPUT);
159+ digitalWrite (7 , LOW);
160+ delay (20 );
161+ gspi_lcd = new SPIClass (HSPI);
162+ st7789 = new HT_ST7789 (240 , 320 , gspi_lcd, st7789_CS_Pin, st7789_DC_Pin, st7789_REST_Pin);
163+ gspi_lcd->begin (st7789_SCLK_Pin, -1 , st7789_MOSI_Pin, st7789_CS_Pin);
164+ // set up slave select pins as outputs as the Arduino API
165+ pinMode (gspi_lcd->pinSS (), OUTPUT);
166+ st7789->init (170 , 320 );
167+
168+ Serial.printf (" Ready!\r\n " );
169+ st7789->setRotation (1 );
170+ st7789->fillScreen (ST7789_BLACK);
171+ testdrawtext (0 , 0 , " init >>> " , ST7789_WHITE);
172+
173+ pinMode (17 , OUTPUT);
174+ digitalWrite (17 , HIGH);
175+ pinMode (5 , OUTPUT);
176+ digitalWrite (5 , HIGH);
177+
178+
179+ st7789->fillScreen (ST7789_BLACK);
180+ st7789->drawFastHLine (0 , 15 , 320 , ST7789_WHITE);
181+ sprintf (buffer, " %.s" , devEui);
182+ // st7789->setTextSize(1);
183+ // testdrawtext(0, 0, "deveui : 22233200000000", ST7789_WHITE);
184+ // st7789->drawRGBBitmap(270, 0, battery32_0, 32, 32);
185+ Mcu.begin (HELTEC_BOARD, SLOW_CLK_TPYE);
186+ }
187+
188+ void loop () {
189+ switch (deviceState) {
190+ case DEVICE_STATE_INIT:
191+ {
192+ #if (LORAWAN_DEVEUI_AUTO)
193+ LoRaWAN.generateDeveuiByChipID ();
194+ #endif
195+ LoRaWAN.init (loraWanClass, loraWanRegion);
196+ // both set join DR and DR when ADR off
197+ LoRaWAN.setDefaultDR (3 );
198+ break ;
199+ }
200+ case DEVICE_STATE_JOIN:
201+ {
202+ LoRaWAN.join ();
203+ break ;
204+ }
205+ case DEVICE_STATE_SEND:
206+ {
207+ prepareTxFrame (appPort);
208+ LoRaWAN.send ();
209+ deviceState = DEVICE_STATE_CYCLE;
210+ break ;
211+ }
212+ case DEVICE_STATE_CYCLE:
213+ {
214+ // Schedule next packet transmission
215+ txDutyCycleTime = appTxDutyCycle + randr (-APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND);
216+ LoRaWAN.cycle (txDutyCycleTime);
217+ deviceState = DEVICE_STATE_SLEEP;
218+ break ;
219+ }
220+ case DEVICE_STATE_SLEEP:
221+ {
222+ LoRaWAN.sleep (loraWanClass);
223+ break ;
224+ }
225+ default :
226+ {
227+ deviceState = DEVICE_STATE_INIT;
228+ break ;
229+ }
230+ }
231+ }
0 commit comments