Skip to content

Commit 22c0610

Browse files
committed
add one example
1 parent 703865b commit 22c0610

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include "LoRaWan_APP.h"
2+
#include "Arduino.h"
3+
4+
/* OTAA para*/
5+
uint8_t devEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A };
6+
uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
7+
uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 };
8+
9+
/* ABP para*/
10+
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };
11+
uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };
12+
uint32_t devAddr = ( uint32_t )0x007e6ae1;
13+
14+
/*LoraWan channelsmask, default channels 0-7*/
15+
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };
16+
17+
/*LoraWan region, select in arduino IDE tools*/
18+
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
19+
20+
/*LoraWan Class, Class A and Class C are supported*/
21+
DeviceClass_t loraWanClass = LORAWAN_CLASS;
22+
23+
/*the application data transmission duty cycle. value in [ms].*/
24+
uint32_t appTxDutyCycle = 15000;
25+
26+
/*OTAA or ABP*/
27+
bool overTheAirActivation = LORAWAN_NETMODE;
28+
29+
/*ADR enable*/
30+
bool loraWanAdr = LORAWAN_ADR;
31+
32+
/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
33+
bool keepNet = LORAWAN_NET_RESERVE;
34+
35+
/* Indicates if the node is sending confirmed or unconfirmed messages */
36+
bool isTxConfirmed = LORAWAN_UPLINKMODE;
37+
38+
/* Application port */
39+
uint8_t appPort = 2;
40+
41+
uint8_t confirmedNbTrials = 4;
42+
43+
const int soilMoisturePin = 1;
44+
45+
/* Prepares the payload of the frame */
46+
static void prepareTxFrame( uint8_t port )
47+
{
48+
int sensorValue = analogRead(soilMoisturePin);
49+
Serial.print("ADC Value: ");
50+
Serial.println(sensorValue);
51+
52+
appDataSize = 0;
53+
unsigned char*puc;
54+
appData[appDataSize++] = 0x00; // parent ID
55+
appData[appDataSize++] = 0x00; // parent ID
56+
appData[appDataSize++] = 0x05; // sensor length
57+
appData[appDataSize++] = 0x00;
58+
59+
puc = (unsigned char *)(&sensorValue);
60+
appData[appDataSize++] = puc[3];
61+
appData[appDataSize++] = puc[2];
62+
appData[appDataSize++] = puc[1];
63+
appData[appDataSize++] = puc[0];
64+
65+
Serial.print("Sending data: ");
66+
for (int i = 0; i < appDataSize; i++) {
67+
Serial.print(appData[i], HEX);
68+
Serial.print(" ");
69+
}
70+
71+
if (sensorValue < 300) {
72+
Serial.println("🌱 The soil is too dry, water it quickly!");
73+
}
74+
else if (sensorValue >= 300 && sensorValue <= 700) {
75+
Serial.println("🌼 The soil moisture is just right, no watering is needed");
76+
}
77+
else {
78+
Serial.println("🚫 The soil is too wet, be careful of rotting the roots");
79+
}
80+
81+
82+
}
83+
84+
bool waterPumpState = false;
85+
uint8_t command = 0; // To store received command
86+
void downLinkDataHandle(McpsIndication_t *mcpsIndication) {
87+
if (mcpsIndication->BufferSize > 0) {
88+
// Print the received downlink data
89+
Serial.print("+Received Buffer: ");
90+
for (uint8_t i = 0; i < mcpsIndication->BufferSize; i++) {
91+
Serial.printf("0x%02X ", mcpsIndication->Buffer[i]);
92+
}
93+
Serial.println();
94+
95+
// Extract the first byte as a command
96+
uint8_t command = mcpsIndication->Buffer[0];
97+
Serial.print("Received command: 0x");
98+
Serial.println(command, HEX);
99+
100+
// Process according to commands
101+
switch (command) {
102+
case 0x01:
103+
Serial.println("Command 0x01: Turn on the water pump...");
104+
waterPumpState = true;
105+
break;
106+
107+
case 0x02:
108+
Serial.println("Command 0x02: Turn off the water pump");
109+
waterPumpState = false;
110+
break;
111+
112+
default:
113+
Serial.println("Unknown command received.");
114+
break;
115+
}
116+
} else {
117+
Serial.println("No data received.");
118+
}
119+
}
120+
121+
void setup() {
122+
Serial.begin(115200);
123+
pinMode(P0_1, OUTPUT);
124+
digitalWrite(P0_1, LOW);
125+
}
126+
127+
void loop()
128+
{
129+
switch( deviceState )
130+
{
131+
case DEVICE_STATE_INIT:
132+
{
133+
#if(LORAWAN_DEVEUI_AUTO)
134+
LoRaWAN.generateDeveuiByChipID();
135+
#endif
136+
#if(AT_SUPPORT)
137+
getDevParam();
138+
#endif
139+
printDevParam();
140+
LoRaWAN.init(loraWanClass,loraWanRegion);
141+
deviceState = DEVICE_STATE_JOIN;
142+
break;
143+
}
144+
case DEVICE_STATE_JOIN:
145+
{
146+
LoRaWAN.join();
147+
break;
148+
}
149+
case DEVICE_STATE_SEND:
150+
{
151+
prepareTxFrame( appPort );
152+
pinMode(P0_1, OUTPUT);
153+
if(waterPumpState) {
154+
digitalWrite(P0_1, HIGH); // Turn on the water pump
155+
Serial.println("Water pump is ON.");
156+
} else {
157+
digitalWrite(P0_1, LOW); // Turn off the water pump
158+
Serial.println("Water pump is OFF.");
159+
}
160+
LoRaWAN.send();
161+
deviceState = DEVICE_STATE_CYCLE;
162+
break;
163+
}
164+
case DEVICE_STATE_CYCLE:
165+
{
166+
// Schedule next packet transmission
167+
txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );
168+
LoRaWAN.cycle(txDutyCycleTime);
169+
deviceState = DEVICE_STATE_SLEEP;
170+
break;
171+
}
172+
case DEVICE_STATE_SLEEP:
173+
{
174+
LoRaWAN.sleep();
175+
break;
176+
}
177+
default:
178+
{
179+
deviceState = DEVICE_STATE_INIT;
180+
break;
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)