Rock Monitoring  V 1.0
Connection3G.cpp
Go to the documentation of this file.
1 #include "Connection3G.h"
6 {
7 }
10 {
11  USB.println("3G init");
12 
13  // sets operator parameters
14  _3G.set_APN("gprs.swisscom.ch", "", "");
15  // And shows them
16  USB.println("APN INFOS:");
17  _3G.show_APN();
18 }
19 
26 {
27  int8_t answer;
28  char request_str[200];
29  uint8_t state = 0;
30 
31  USB.println(F("Entering 3G SendData"));
32  answer = _3G.ON(); // activate the 3G module:
33  USB.println(F("turned 3G module on"));
34  if ((answer == 1) || (answer == -3))
35  {
36  USB.println("3G module ready...");
37 
38  USB.println(F("Setting PIN code...")); // set pin code:
39  if (_3G.setPIN(simPin) == 1)
40  {
41  USB.println(F("PIN code accepted"));
42  }
43  else
44  {
45  USB.println(F("PIN code incorrect"));
46  }
47 
48 
49  answer = _3G.check(180); // Wait for connection to the network
50  if (answer == 1)
51  {
52  USB.println(F("3G module connected to the network..."));
53  USB.print(F("Getting URL with GET method..."));
54  char sensorName[20];
55  sprintf (sensorName, "SensorID%d", data->sensorNodeId);
56  sprintf(request_str,"GET /LogData.php?name=%s&battery=%u&pitch1=%d&roll1=%d&pitch2=%d&roll2=%d&pitch3=%d&roll3=%d&pot=%d&temp1=%d&temp2=%d&temp3=%d HTTP/1.1\r\nHost: %s\r\nContent-Length: 0\r\n\r\n", sensorName, data->batteryLevel,data->sensor1.pitch,data->sensor1.roll,data->sensor2.pitch,data->sensor2.roll,data->sensor3.pitch,data->sensor3.roll,data->extension,data->sensTemp1,data->sensTemp2,data->sensTemp3,httpStreamAddress);
57  USB.println(request_str);
58  answer = _3G.readURL(httpStreamAddress, 80, request_str); //gets URL from the solicited URL
59 
60  if ( answer == 1) // Checks the answer
61  {
62  USB.println(F("Done"));
63  state = RETURN_OK;
64  USB.println(_3G.buffer_3G);
65  }
66  else if (answer < -14)
67  {
68  USB.print(F("Failed. Error code: "));
69  USB.println(answer, DEC);
70  USB.print(F("CME error code: "));
71  USB.println(_3G.CME_CMS_code, DEC);
72  state = RETURN_ERROR;
73  }
74  else
75  {
76  USB.print(F("Failed. Error code: "));
77  USB.println(answer, DEC);
78  state = RETURN_ERROR;
79  }
80  }
81  else
82  {
83  USB.println(F("3G module cannot connect to the network..."));
84  state = RETURN_ERROR;
85  }
86  }
87  else
88  {
89  // Problem with the communication with the 3G module
90  USB.println(F("3G module not started"));
91  state = RETURN_ERROR;
92  }
93 
94  return state;
95  _3G.OFF(); // Powers off the 3G module
96 }
97 
98 
106 uint8_t Connection3G::sendData(Sensors::structSensors * data , uint8_t size)
107 {
108  int8_t answer;
109  char request_str[200];
110  uint8_t state = 0;
111 
112  USB.println(F("Entering 3G SendData"));
113  answer = _3G.ON(); // activate the 3G module:
114  USB.println(F("turned 3G module on"));
115  if ((answer == 1) || (answer == -3))
116  {
117  USB.println("3G module ready...");
118 
119  USB.println(F("Setting PIN code...")); // set pin code:
120  if (_3G.setPIN(simPin) == 1)
121  {
122  USB.println(F("PIN code accepted"));
123  }
124  else
125  {
126  USB.println(F("PIN code incorrect"));
127  }
128 
129  answer = _3G.check(180); // Wait for connection to the network
130  if (answer == 1)
131  {
132  USB.println(F("3G module connected to the network..."));
133 
134  for(uint8_t i = 0;i<size; i++)
135  {
136  if(data[i].sensorNodeId != 0) //Sends data only if Gateway has received new Data over Lora from a sensor node
137  {
138  USB.print(F("Getting URL with GET method..."));
139  char sensorName[20];
140  sprintf (sensorName, "SensorID%d", data[i].sensorNodeId);
141  sprintf(request_str,"GET /LogData.php?name=%s&battery=%u&pitch1=%d&roll1=%d&pitch2=%d&roll2=%d&pitch3=%d&roll3=%d&pot=%d&temp1=%d&temp2=%d&temp3=%d HTTP/1.1\r\nHost: %s\r\nContent-Length: 0\r\n\r\n", sensorName, data[i].batteryLevel,data[i].sensor1.pitch,data[i].sensor1.roll,data[i].sensor2.pitch,data[i].sensor2.roll,data[i].sensor3.pitch,data[i].sensor3.roll,data[i].extension,data[i].sensTemp1,data[i].sensTemp2,data[i].sensTemp3,httpStreamAddress);
142  USB.println(request_str);
143  answer = _3G.readURL(httpStreamAddress, 80, request_str); //gets URL from the solicited URL
144 
145  if ( answer == 1) // Checks the answer
146  {
147  USB.println(F("Done"));
148  state = RETURN_OK;
149  USB.println(_3G.buffer_3G);
150  }
151  else if (answer < -14)
152  {
153  USB.print(F("Failed. Error code: "));
154  USB.println(answer, DEC);
155  USB.print(F("CME error code: "));
156  USB.println(_3G.CME_CMS_code, DEC);
157  state = RETURN_ERROR;
158  }
159  else
160  {
161  USB.print(F("Failed. Error code: "));
162  USB.println(answer, DEC);
163  state = RETURN_ERROR;
164  }
165  }
166  }
167  }
168  else
169  {
170  USB.println(F("3G module cannot connect to the network..."));
171  state = RETURN_ERROR;
172  }
173  }
174  else
175  {
176  // Problem with the communication with the 3G module
177  USB.println(F("3G module not started"));
178  state = RETURN_ERROR;
179  }
180 
181  return state;
182  _3G.OFF(); // Powers off the 3G module
183 }
189 {
190  int8_t answer;
191 
192  USB.println(F("Entering 3G CheckConnection"));
193  answer = _3G.ON(); // activate the 3G module:
194  USB.println(F("turned 3G module on"));
195  if ((answer == 1) || (answer == -3))
196  {
197  USB.println("3G module ready...");
198 
199  USB.println(F("Setting PIN code...")); // set pin code:
200  if (_3G.setPIN(simPin) == 1)
201  {
202  USB.println(F("PIN code accepted"));
203  }
204  else
205  {
206  USB.println(F("PIN code incorrect"));
207  return RETURN_ERROR;
208  }
209 
210 
211  answer = _3G.check(180); // Wait for connection to the network
212  if (answer == 1)
213  {
214  USB.println(F("3G module connected to the network..."));
215  return RETURN_OK;
216  }
217  else
218  {
219  USB.println(F("3G module cannot connect to the network..."));
220  return RETURN_ERROR;
221  }
222  }
223  else
224  {
225  // Problem with the communication with the 3G module
226  // USB.println(F("3G module not started"));
227  return RETURN_ERROR;
228  }
229 
230  _3G.OFF(); // Powers off the 3G module
231 }
uint8_t sendData(Sensors::structSensors *sensors)
This method sends all the sensor information from the struct to the webserver.
#define RETURN_ERROR
Definition: Constants.h:4
uint8_t sensorNodeId
Definition: Sensors.h:39
const char * httpStreamAddress
Definition: Connection3G.h:28
#define RETURN_OK
Definition: Constants.h:9
FXLS8471Q::structPitchRoll sensor3
Definition: Sensors.h:34
uint8_t checkConnection()
This method checks if a connection to the 3G network can be established.
FXLS8471Q::structPitchRoll sensor1
Definition: Sensors.h:32
const char * simPin
Definition: Connection3G.h:27
void init()
This method initializes the operator parameters that are necessary for 3G connection.
Definition: Connection3G.cpp:9
FXLS8471Q::structPitchRoll sensor2
Definition: Sensors.h:33
uint8_t batteryLevel
Definition: Sensors.h:40