Rock Monitoring  V 1.0
lora_class.cpp
Go to the documentation of this file.
1 #include "lora_class.h"
2 #include "WaspUSB.h"
3 #include "WaspRTC.h"
4 #include "Waspmote.h"
5 
6 //Variables
8 
9 
10 //Konstruktor
19 
20  localSensorStruct = sensorstruct;
21  mySettings.channel=CH_17_868;
22  mySettings.crc=1;
24  mySettings.mode=3;
25  mySettings.power='H';
26 }
27 uint8_t Lora::getID(void){
31  USB.ON();
32  USB.flush();
33  int ID[3]={0,0,0};
34  int i=0;
35  int iOld;
36  int myID=0;
37 
38  USB.print("Enter ID: ");
39  while((i<3)&&(ID[iOld]!=13))
40  {
41  if (USB.available() > 0)
42  {
43  // USB.println("Byte received: ");
44  ID[i] = USB.read();
45  USB.print(ID[i],BYTE);
46  iOld = i;
47  i++;
48  }
49  }
50  char ID2[3];
51  for(i=0;i<3;i++){
52  ID2[i]=(char)ID[i];
53  }
54  myID=atoi((const char*)ID2); //convert the String to
55  //USB.println(F("Choosen ID is:"));
56  USB.print(myID);
57  USB.println();
58  return (uint8_t)myID;
59 }
60 int8_t Lora::init(void){
66 
67 //Config LoRa
68  int8_t error=0; //variable used to show the errors
69  USB.ON();
70  // init SX1272 module
71  sx1272.ON();
72  // Select channel
73  error = error || sx1272.setChannel(CH_17_868);
74  mySettings.channel = CH_17_868; //Safe the Settings
75  USB.print(F("Setting Channel to\t'17'\t\t state "));
76  USB.println(error);
77  // Set Header mode: ON
78  error = error || sx1272.setHeaderON();
79  mySettings.header=1; //Safe the Setting
80  USB.print(F("Setting Header to\t'1' \t\t state "));
81  USB.println(error);
82 
83  // Set Mode
84  error = error || sx1272.setMode(3);//slowest mode with highest range
85  mySettings.mode=3;
86  USB.print(F("Setting Mode to\t'3'\t\t state "));
87  USB.println(error);
88 
89  // Set CRC mode
90  error = error || sx1272.setCRC_ON();
91  mySettings.crc=1; //Safe the Settings
92  USB.print(F("Setting CRC to\t'1' \t\t state "));
93  USB.println(error);
94 
95  // Set packet lengt
96  error = error || sx1272.setPacketLength(19);
97  mySettings.packetLength=19; //Safe the Settings
98  USB.print(F("Setting Packet to '19'.\t\t state "));
99  USB.println(error);
100 
101  // Select output power (Max, High or Low)
102  error = error || sx1272.setPower('M');
103  mySettings.power='M'; //Safe the Settings
104  USB.print(F("Setting Power to\t'L' \t\t state "));
105  USB.println(error);
106 
107  // mySettings.nodeId = getID();//get the ID from Terminal
108  mySettings.nodeId=Utils.readEEPROM(1024);
109  error = error || sx1272.setNodeAddress(mySettings.nodeId);
110  USB.print(F("Setting ID to\t"));
111  USB.print(mySettings.nodeId,DEC);
112  USB.print(F("\t\t state "));
113  //set retries
114  sx1272.setRetries(5);
115  //RTC config
116  RTC.ON();
117  RTC.setTime("00:00:00:00:00:00:0");
118  USB.println(error);
119  if(error==0){
120  USB.println("settings succesful");
121  Utils.setLED(LED0, LED_ON);
122  return RETURN_OK;
123  }
124  else{
125  USB.println("settings unsuccesful");
126  return RETURN_ERROR;
127  }
128 }
132  Lora::stringData[0]=(uint8_t)((localSensorStruct->sensor1.pitch>>8)&0xFF); //high byte
133  Lora::stringData[1]=(uint8_t)((localSensorStruct->sensor1.pitch)&0xFF); //low byte
134  Lora::stringData[2]=(uint8_t)((localSensorStruct->sensor2.pitch>>8)&0xFF); //high byte
135  Lora::stringData[3]=(uint8_t)((localSensorStruct->sensor2.pitch)&0xFF); //low byte
136  Lora::stringData[4]=(uint8_t)((localSensorStruct->sensor3.pitch>>8)&0xFF); //high byte
137  Lora::stringData[5]=(uint8_t)((localSensorStruct->sensor3.pitch)&0xFF); //low byte
138  Lora::stringData[6]=(uint8_t)((localSensorStruct->sensor1.roll>>8)&0xFF); //high byte
139  Lora::stringData[7]=(uint8_t)((localSensorStruct->sensor1.roll)&0xFF); //low byte
140  Lora::stringData[8]=(uint8_t)((localSensorStruct->sensor2.roll>>8)&0xFF); //high byte
141  Lora::stringData[9]=(uint8_t)((localSensorStruct->sensor2.roll)&0xFF); //low byte
142  Lora::stringData[10]=(uint8_t)((localSensorStruct->sensor3.roll>>8)&0xFF); //high byte
143  Lora::stringData[11]=(uint8_t)((localSensorStruct->sensor3.roll)&0xFF); //low byte
144  Lora::stringData[12]=(uint8_t)((localSensorStruct->extension>>8)&0xFF); //high byt
145  Lora::stringData[13]=(uint8_t)((localSensorStruct->extension)&0xFF); //low byte
151 }
156  dest->sensor1.pitch=(int16_t)((int16_t)(sx1272.packet_received.data[0]<<8)|(sx1272.packet_received.data[1]));
157  dest->sensor2.pitch=(int16_t)((int16_t)(sx1272.packet_received.data[2]<<8)|(sx1272.packet_received.data[3]));
158  dest->sensor3.pitch=(int16_t)((int16_t)(sx1272.packet_received.data[4]<<8)|(sx1272.packet_received.data[5]));
159  dest->sensor1.roll=(int16_t)((int16_t)(sx1272.packet_received.data[6]<<8)|(sx1272.packet_received.data[7]));
160  dest->sensor2.roll=(int16_t)((int16_t)(sx1272.packet_received.data[8]<<8)|(sx1272.packet_received.data[9]));
161  dest->sensor3.roll=(int16_t)((int16_t)(sx1272.packet_received.data[10]<<8)|(sx1272.packet_received.data[11]));
162  dest->extension=(int16_t)((int16_t)(sx1272.packet_received.data[12]<<8)|(sx1272.packet_received.data[13]));
163  dest->batteryLevel=(uint8_t)sx1272.packet_received.data[14];
164  dest->sensTemp1=(uint8_t)sx1272.packet_received.data[15];
165  dest->sensTemp2=(uint8_t)sx1272.packet_received.data[16];
166  dest->sensTemp3=(uint8_t)sx1272.packet_received.data[17];
167  dest->sensorNodeId=(uint8_t)sx1272.packet_received.data[18];
168 }
174  if(sx1272.cadDetected()){
175  USB.println(F("\t\t\ LoRa: Channel Activity Detected"));
176  return RETURN_OK;
177  }
178  else{
179  return RETURN_ERROR;
180  }
181 }
182 uint8_t Lora::dataRequest(uint8_t ID){
188  USB.println(F("\t\t\ LoRa:dataRequest"));
189  USB.print(F("\t\t\ LoRa:sending Request to note"));
190  USB.println(ID,DEC);
191  txFlag=sx1272.sendPacketTimeoutACKRetries(ID,&sendDataCommand,1);
192 
193  if(txFlag==0){//sensornode has received the Data Request and sent ACK
194  USB.println(F("\t\t LoRa: Request sent"));
195  return RETURN_OK;
196  }
197  else{
198  USB.print(F("\t\t\ LoRa: SEND state"));
199  USB.println(txFlag, DEC);
200  return RETURN_ERROR;
201  }
202  //delay(5000);
203 }
209  uint8_t rxFlag=0;
210  USB.println(F("\t\t\ LoRa: dataRequest"));
211  rxFlag=sx1272.receivePacketTimeoutACK(10000);
212  if(sx1272.packet_received.data[0]==sendDataCommand){//checks if the received Packet is really a data request
213  if(rxFlag==0){//sensornode has received the Data Request and send ACK
214  USB.println(F("Packet Reiceived "));
215  return RETURN_OK;
216  }
217  else{
218  USB.print(F("\t\t\ LoRa: RECEIVE state "));
219  USB.println(rxFlag, DEC);
220  return RETURN_ERROR;
221  }
222  }
223 }
224 uint8_t Lora::sendData(void){
229  USB.println(F("\t\t\ LoRa: sendData"));
230  txFlag=sx1272.sendPacketTimeoutACKRetries(gatewayID ,stringData, sizeof(stringData));
231  if(txFlag==0){
232  USB.println(F("\t\t\ LoRa: Packet sent"));
233  return RETURN_OK;
234  }
235  else{
236  USB.print(F("\t\t\ LoRa: Sending failed: "));
237  USB.println(txFlag, DEC);
238  return RETURN_ERROR;
239  }
240 }
241 uint8_t Lora::receiveData(void){
246 
247  USB.println(F("receiveData"));
248  rxFlag=sx1272.receivePacketTimeoutACK(10000);
249  if(rxFlag == 0){ //received the packet and ACK is send
250  return RETURN_OK;
251  }
252  else{
253  USB.print(F("RECEIVE state"));
254  USB.println(rxFlag, DEC);
255  return RETURN_ERROR;
256  }
257 }
258 int8_t Lora::wakeUp(void){
263  int8_t error=0;
264  USB.ON();
265  sx1272.ON();
266  delay(1000);
267  // Select channel
268  error =error|| sx1272.setChannel(CH_17_868);
269  USB.print("set channel is ther an error: ");
270  USB.println(error);
271  // Set Header mode: ON
272  if(mySettings.header==1){
273  error =error || sx1272.setHeaderON();
274  }
275  if(mySettings.header==0){
276  error = error || sx1272.setHeaderOFF();
277  }
278  // Set Mode
279  error = error || sx1272.setMode( mySettings.mode);//slowest mode with highest range
280  // Set CRC mode
281  if(mySettings.crc==1){
282  error = error || sx1272.setCRC_ON();
283  }
284  if(mySettings.crc==0){
285  error = error || sx1272.setCRC_OFF();
286  }
287  // Set packet length
288  error = error || sx1272.setPacketLength(mySettings.packetLength);
289  // Select output power (Max, High or Low)
290  error = error || sx1272.setPower(mySettings.power);
291  //Set Node ID
292  error = error || sx1272.setNodeAddress(mySettings.nodeId);
293  //set number of retries
294  sx1272.setRetries(5);
295  //RTC config
296  RTC.ON();
297  RTC.setTime("00:00:00:00:00:00:0");
298 
299  if(error == 0){
300  Utils.setLED(LED0, LED_ON);
301  Utils.setLED(LED1, LED_ON);
302  USB.println(F("\t\t\t LoRa: Settings done"));
303  return RETURN_OK;
304  }
305  else{
306  USB.print("\t\t\ LoRa: Settings error");
307  USB.println(error);
308  return RETURN_ERROR;
309  }
310 }
311 uint8_t Lora::sendDeepSleep(uint8_t ID){
317  USB.println(F("\t\t\ LoRa: sendDeepSleep command"));
318  txSleepFlag=sx1272.sendPacketTimeoutACKRetries(ID,&sendCommandToSleep,1);
319  if(txSleepFlag==0){
320  USB.println(F("\t\t\ LoRa: deep sleep sent"));
321  return RETURN_OK;
322  }
323  else{
324  USB.print(F("\t\t\ LoRa: deep Sleep failed: "));
325  USB.println(txSleepFlag, DEC);
326  return RETURN_ERROR;
327  }
328 }
334  USB.println(F("\t\t\ LoRa: Wait For DeepSleep command"));
335  rxSleepFlag=sx1272.receivePacketTimeoutACK(10000);
336  if(sx1272.packet_received.data[0]==sendCommandToSleep){//Checks if the packet received is a sleepCommand
337  if(rxSleepFlag == 0){ //received the packet and ACK is send
338  USB.println(F("\t\t DeepSleep command received"));
339  return RETURN_OK;
340  }
341  else{
342  USB.print(F("\t\t\ LoRa: DeepSleep command NOT received"));
343  USB.println(rxSleepFlag, DEC);
344  return RETURN_ERROR;
345  }
346  }
347 }
uint8_t rxSleepFlag
Definition: lora_class.cpp:7
#define RETURN_ERROR
Definition: Constants.h:4
uint8_t sensorNodeId
Definition: Sensors.h:39
uint8_t dataRequest(uint8_t ID)
Definition: lora_class.cpp:182
void convertStruct(void)
Definition: lora_class.cpp:129
Sensors::structSensors * localSensorStruct
Definition: lora_class.h:16
uint8_t rxFlag
Definition: lora_class.cpp:7
uint8_t txFlag
Definition: lora_class.cpp:7
#define RETURN_OK
Definition: Constants.h:9
uint8_t initialize
Definition: lora_class.cpp:7
int8_t wakeUp(void)
Definition: lora_class.cpp:258
uint8_t sendDataCommand
Definition: lora_class.h:17
FXLS8471Q::structPitchRoll sensor3
Definition: Sensors.h:34
uint8_t sendDeepSleep(uint8_t ID)
Definition: lora_class.cpp:311
uint8_t packetLength
Definition: lora_class.h:25
uint8_t sendData(void)
Definition: lora_class.cpp:224
uint8_t activityDetection(void)
Definition: lora_class.cpp:169
uint8_t sendCommandToSleep
Definition: lora_class.h:18
uint8_t stringData[19]
Definition: lora_class.h:15
FXLS8471Q::structPitchRoll sensor1
Definition: Sensors.h:32
void convertStructBack(Sensors::structSensors *dest)
Definition: lora_class.cpp:152
uint8_t waitForDataRequest(void)
Definition: lora_class.cpp:204
Lora(Sensors::structSensors *sensorstruct)
Definition: lora_class.cpp:11
uint8_t waitForDeepSleep(void)
Definition: lora_class.cpp:329
struct Lora::loraSettings mySettings
uint8_t txSleepFlag
Definition: lora_class.cpp:7
uint8_t getID(void)
Definition: lora_class.cpp:27
uint8_t gatewayID
Definition: lora_class.h:19
FXLS8471Q::structPitchRoll sensor2
Definition: Sensors.h:33
uint8_t batteryLevel
Definition: Sensors.h:40
uint8_t receiveData(void)
Definition: lora_class.cpp:241
int8_t init(void)
Definition: lora_class.cpp:60