Rock Monitoring  V 1.0
Gateway.cpp
Go to the documentation of this file.
1 #include "Gateway.h"
2 
4  static State runState=dataRequest;
5 
6 Gateway::Gateway():myLora(&mySensorStruct){
10 }
11 uint8_t Gateway::init(void){
16  int8_t error=0;
17  PWR.setSensorPower(SENS_3V3,SENS_ON); // Sets the 3,3V switch ON
18  myLeds.init();
19  pinMode(DIGITAL4 ,INPUT); //Fault (LOW-Active)
20  pinMode(DIGITAL5 ,INPUT); //Charge (LOW-Active)
21  pinMode(DIGITAL6 ,OUTPUT); //Shutdown
23  my3G.init();
24  error = myLora.init();
25  return error;
26 }
31  uint8_t level=0;
32  USB.OFF();
33  digitalWrite(DIGITAL6, HIGH);//Shutdown pin activate
34  delay(200);
35  level=PWR.getBatteryLevel();
36  digitalWrite(DIGITAL6, LOW);//Shutdown pin deactivate
37  USB.ON();
38  return level;
39 }
40 void Gateway::showStructContent(uint8_t node){
43  USB.println(F("\t\-----------------------------------"));
44  USB.print(F("\t\ Show Struct from Node:"));
45  USB.println(mySensorStructArray[node].sensorNodeId, DEC);
46  USB.print(F("\t\ BatteryLevel: "));
47  USB.println(mySensorStructArray[node].batteryLevel, DEC);
48  USB.print(F("\t\ extension: "));
49  USB.println(mySensorStructArray[node].extension, DEC);
50  USB.print(F("\t\ pitch1: "));
51  USB.println(mySensorStructArray[node].sensor1.pitch, DEC);
52  USB.print(F("\t\ roll1: "));
53  USB.println(mySensorStructArray[node].sensor1.roll, DEC);
54  USB.print(F("\t\ pitch2: "));
55  USB.println(mySensorStructArray[node].sensor2.pitch, DEC);
56  USB.print(F("\t\ roll2: "));
57  USB.println(mySensorStructArray[node].sensor2.roll, DEC);
58  USB.print(F("\t\ pitch3: "));
59  USB.println(mySensorStructArray[node].sensor3.pitch, DEC);
60  USB.print(F("\t\ roll3: "));
61  USB.println(mySensorStructArray[node].sensor3.roll, DEC);
62  USB.print(F("\t\ temp1: "));
63  USB.println(mySensorStructArray[node].sensTemp1,DEC);
64  USB.print(F("\t\ temp2: "));
65  USB.println(mySensorStructArray[node].sensTemp2, DEC);
66  USB.print(F("\t\ temp3: "));
67  USB.println(mySensorStructArray[node].sensTemp3, DEC);
68  USB.println(F("\t\-----------------------------------"));
69 }
70 uint8_t Gateway::requestData(uint8_t ID){
75  return myLora.dataRequest(ID);
76 }
82  return myLora.receiveData();//Information are stored in mySensorStruct
83 }
89  my3G.sendData(dataToSend);
90 }
91 uint8_t Gateway::sendSleep(uint8_t ID){
96  return myLora.sendDeepSleep(ID);
97 }
98 void Gateway::deepSleep(void){
101  Utils.setLED(LED0, LED_OFF);
102  Utils.setLED(LED1, LED_OFF);
103  PWR.deepSleep("00:00:15:20",RTC_OFFSET,RTC_ALM1_MODE1,SOCKET0_OFF);
104  if( intFlag & RTC_INT ){
105  intFlag &= ~(RTC_INT);
106  }
107 }
108 int8_t Gateway::wakeUp(void){
113 PWR.setSensorPower(SENS_3V3,SENS_ON); // Sets the 3,3V switch ON
114  return myLora.wakeUp();
115 }
116 void Gateway::run(void){
119  switch(runState){
120  case dataRequest:{
121  USB.println();
122  USB.println("runstate = SendDataRequest");
123  USB.println();
124  if(Gateway::requestData(sensorID[currentNode])==RETURN_OK){//checks if the currentNode has received the data request
125  USB.print("\t\ datarequest has been ack from sensorstab ");
126  USB.println(sensorID[currentNode],DEC);
127  runState=receive;
128  }
129  else{
130  USB.println("\t\ datarequest has NOT been ack from sensorstab ");
131  if(notReceivedCnt>=1){//reached maximum number of tries --> sensor-node is not responding
132  USB.println("\t\ Reached maximum number of tries -> not responding -> overjump");
133  notReceivedCnt=0;
134  mySensorStructArray[currentNode].sensorNodeId=0; //sensorNodeId equal 0 means that the sensor could not be reached
135  if(currentNode != sizeof(sensorID)-1){//checks if it isn't the last node
136  USB.println("\t\ Not last sensor-node -> send data request to next sensor-node");
137  currentNode++;//jump to next node for data request, current node is not responding
138  }
139  else{
140  USB.println("\t\ Last Node -> change directly to runstate=sendServer");
141  runState=sendServer;
142  }
143  }
144  else{
145  USB.println("\t\ Is not responding");
146  notReceivedCnt++;
147  }
148  }
149  }
150  break;
151 
152  case receive:{
153 
154  USB.println();
155  USB.println("runstate = receive");
156  USB.println();
157  if(receiveNodeData()==RETURN_OK){
158  USB.println("\t\ data has been received");
159  myLora.convertStructBack(&mySensorStructArray[currentNode]);//Saves the measurement of the different Sensor-nodes separately
160  showStructContent(currentNode);
161  if(currentNode == sizeof(sensorID)-1){//Check if all the Sensor-nodes have send there data
162  currentNode=0;//start again with the first sensor-node
163  myLeds.LedGreenOn();
164  runState=sendServer;
165  }
166  else{
167  currentNode++;
168  runState=dataRequest; //request data from next sensor-node
169  }
170  }
171  else{
172  if(nothingReceived==3){
173  nothingReceived=0;
174  mySensorStructArray[currentNode].sensorNodeId=0; //sensorNodeId equal 0 means that the sensor hasn't sent new data
175  if(currentNode != sizeof(sensorID)-1){//checks if it isn't the last node
176  currentNode++;//jump to next node for data request, current node is not responding
177  runState=dataRequest; //error handling
178  USB.println("\t\ received ACK for Datarequest but no new data");
179  }
180  else{
181  runState=sendServer;
182  }
183  }
184  else{
185  nothingReceived++;
186  }
187  }
188  }
189  break;
190 
191  case sendServer:{
192  USB.println();
193  USB.println("runstate = SendServer");
194  USB.println();
196  runState=sendSleepCommand;
197  }
198  }
199  break;
200 
201  case sendSleepCommand:{
202  USB.println();
203  USB.println("runstate = SendSleepCommand");
204  USB.println();
205  myLeds.LedGreenBlink4seconds();//all data have been sent
207  if(currentNode == sizeof(sensorID)-1){//Check if all the Sensor-nodes have send there data
208  runState=sleep;
209  currentNode=0;//start again with the first sensor-node
210  }
211  else{
212  currentNode++;
213  }
214  }
215  else{
216  if(notSleepCnt>=4){
217  notSleepCnt=0;
218  if(currentNode != sizeof(sensorID)-1){//checks if it isn't the last node
219  currentNode++;//jump to next node for sleep command
220  }
221  else{
222  currentNode=0;
223  runState=sleep;
224  }
225  }
226  else{
227  notSleepCnt++;
228  }
229  }
230  }
231  break;
232 
233  case sleep:{
234  USB.println();
235  USB.println("runstate = Sleep");
236  USB.println();
237  deepSleep();
238  runState=wakingUP;
239  }
240  break;
241 
242  case wakingUP:{
243  USB.println();
244  USB.println("runstate = WakingUp");
245  USB.println();
246  if(wakeUp()==RETURN_OK){
247  runState=dataRequest;
248  }
249  }
250  break;
251  }
252 }
uint8_t sendData(Sensors::structSensors *sensors)
This method sends all the sensor information from the struct to the webserver.
uint8_t sensorNodeId
Definition: Sensors.h:39
void BlinkAkku(uint8_t batLevel)
This method sets the button led on or in blink state for 5 seconds (depending on battery level) (bloc...
Definition: Leds.cpp:75
uint8_t measureBattery(void)
Definition: Gateway.cpp:27
Sensors::structSensors mySensorStructArray[numberOfNodes]
Definition: Gateway.h:26
uint8_t dataRequest(uint8_t ID)
Definition: lora_class.cpp:182
uint8_t sendToServer(Sensors::structSensors *dataToSend)
Definition: Gateway.cpp:84
uint8_t notSleepCnt
Definition: Gateway.h:33
uint8_t init(void)
Definition: Gateway.cpp:11
#define RETURN_OK
Definition: Constants.h:9
int8_t wakeUp(void)
Definition: lora_class.cpp:258
Definition: Gateway.cpp:3
void init()
This method initializes the output pins for the leds and clears both led.
Definition: Leds.cpp:9
uint8_t sendDeepSleep(uint8_t ID)
Definition: lora_class.cpp:311
uint8_t sensorID[numberOfNodes]
Definition: Gateway.h:29
int8_t wakeUp(void)
Definition: Gateway.cpp:108
uint8_t currentNode
Definition: Gateway.h:35
void LedGreenOn()
This method turns the green led on.
Definition: Leds.cpp:19
uint8_t receiveNodeData(void)
Definition: Gateway.cpp:77
void showStructContent(uint8_t node)
Definition: Gateway.cpp:40
void convertStructBack(Sensors::structSensors *dest)
Definition: lora_class.cpp:152
void LedGreenBlink4seconds()
This method lets the green led blink for 4 seconds (blocks program for 4s)
Definition: Leds.cpp:64
State
Definition: Gateway.cpp:3
Lora myLora
Definition: Gateway.h:38
Gateway()
Definition: Gateway.cpp:6
void init()
This method initializes the operator parameters that are necessary for 3G connection.
Definition: Connection3G.cpp:9
Class that handles the communication over 3G.
Definition: Connection3G.h:15
void run(void)
Definition: Gateway.cpp:116
Connection3G my3G
Definition: Gateway.h:39
uint8_t sendSleep(uint8_t ID)
Definition: Gateway.cpp:91
Leds myLeds
Definition: Gateway.h:40
uint8_t notReceivedCnt
Definition: Gateway.h:32
#define numberOfNodes
Definition: Constants.h:10
void deepSleep(void)
Definition: Gateway.cpp:98
uint8_t nothingReceived
Definition: Gateway.h:34
uint8_t receiveData(void)
Definition: lora_class.cpp:241
uint8_t requestData(uint8_t ID)
Definition: Gateway.cpp:70
int8_t init(void)
Definition: lora_class.cpp:60