1 | /*
|
---|
2 |
|
---|
3 | Testing of: Arduino for FACT
|
---|
4 |
|
---|
5 | Gareth Hughes 2013-10-30
|
---|
6 | Dom Neise 2014-09
|
---|
7 |
|
---|
8 | Using: Arduino Ethernet
|
---|
9 | GSM Overlay
|
---|
10 | LSM303: Magnetic field and accelerometer
|
---|
11 | YL-38: Light Sensor
|
---|
12 |
|
---|
13 | Function: Will reply to an SMS stating the position, movement and light level.
|
---|
14 | If lighlevel gets above a certain threshold will send a warning SMS every 1 min.
|
---|
15 |
|
---|
16 | Modifications by Dom:
|
---|
17 | - In order to find our why the resulting binary is soo large,
|
---|
18 | I enclosed most of the code in precompiler #ifdef-#endif constructs.
|
---|
19 | The 4 #define USE_<name> statements in the beginning can be commented
|
---|
20 | in and out in order to create a binary, that e.g. does not need
|
---|
21 | the GSM shield to be attached.
|
---|
22 |
|
---|
23 | - printHelp function can now be given a reference to the EthernetClient
|
---|
24 | that should recieve the help message, that way to clients can be connected
|
---|
25 | and will not get their mutual help messages anymore.
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | //#define USE_WIRE
|
---|
30 | #define USE_ETH
|
---|
31 | //#define USE_LSM
|
---|
32 | #define USE_GSM
|
---|
33 |
|
---|
34 | #include <SPI.h>
|
---|
35 | #include <Ethernet.h>
|
---|
36 |
|
---|
37 | #ifdef USE_GSM
|
---|
38 | #include <GSM.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #ifdef USE_WIRE
|
---|
42 | #include <Wire.h>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #ifdef USE_LSM
|
---|
46 | #include <LSM303.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | #ifdef USE_ETH
|
---|
52 | EthernetServer server = EthernetServer(23);
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | //bool sentHeader = false;
|
---|
56 |
|
---|
57 | // PIN Number for the SIM
|
---|
58 | #define PINNUMBER ""
|
---|
59 | #define NUMLEN 15
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | char message[18]; // Array to hold SMS message
|
---|
64 | char senderNumber[NUMLEN]; // Array to hold the number a SMS is retreived from
|
---|
65 |
|
---|
66 | // -------------- GSM stuff --------------------------------------------
|
---|
67 | #ifdef USE_GSM
|
---|
68 | GSM gsmAccess;
|
---|
69 | GSM_SMS sms;
|
---|
70 | byte gsmStatus = -1; // -1 : unknown
|
---|
71 | bool use_gsm = false;
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | // -------------- Light sensor stuff -----------------------------------
|
---|
75 | int iLight = 9999; // light level
|
---|
76 |
|
---|
77 |
|
---|
78 | // -------------- Compass stuff ----------------------------------------
|
---|
79 | #ifdef USE_LSM
|
---|
80 | LSM303 compass;
|
---|
81 | #endif
|
---|
82 | int heading = -9999; // Continual heading reading
|
---|
83 | float pitch = -9999.; // Pitch relative to horizon
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | void setup()
|
---|
88 | {
|
---|
89 | // this is to begin (ethz) ethernet
|
---|
90 | //Ethernet.begin(mac, ip, dnserver, gatew, smask);
|
---|
91 | // initialize serial communications and wait for port to open:
|
---|
92 | Serial.begin(9600);
|
---|
93 |
|
---|
94 |
|
---|
95 | #ifdef USE_ETH
|
---|
96 | //ETH
|
---|
97 | byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xB5, 0xE3 };
|
---|
98 | byte ip[] = { 192, 33, 103, 241 };
|
---|
99 | //LA PALMA
|
---|
100 | //byte mac[] = { 0xFA, 0xC7, 0xBB, 0xFF, 0x33, 0x33 };
|
---|
101 | //byte ip[] = { 10, 0, 100, 202 };
|
---|
102 |
|
---|
103 | Ethernet.begin(mac, ip);
|
---|
104 | server.begin();
|
---|
105 | Serial.print("Chat server address:");
|
---|
106 | Serial.println(Ethernet.localIP());
|
---|
107 | #else
|
---|
108 | Serial.println("No ethernet this time :-(");
|
---|
109 | #endif
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | // Start the Wire communication
|
---|
114 | #ifdef USE_WIRE
|
---|
115 | Wire.begin();
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | // Initialize the Compass
|
---|
119 | #ifdef USE_LSM
|
---|
120 | compass.init();
|
---|
121 | compass.enableDefault();
|
---|
122 |
|
---|
123 | // Calibration values. Use the Calibrate example program to get the values for your compass.
|
---|
124 | // ETH Calibration values
|
---|
125 | // compass.m_min.x = -700; compass.m_min.y = -695; compass.m_min.z = -635;
|
---|
126 | // compass.m_max.x = +293; compass.m_max.y = +551; compass.m_max.z = 606;
|
---|
127 | // La Palma calibration values
|
---|
128 | compass.m_min.x = -389; compass.m_min.y = -423; compass.m_min.z = -420;
|
---|
129 | compass.m_max.x = +510; compass.m_max.y = +420; compass.m_max.z = +315;
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | void loop()
|
---|
135 | {
|
---|
136 | #ifdef USE_GSM
|
---|
137 | if (use_gsm && gsmStatus==GSM_READY)
|
---|
138 | {
|
---|
139 | // user wants us to use GSM, and GSM seems to be okay.
|
---|
140 | checkForSms();
|
---|
141 | } else if (use_gsm && gsmStatus!=GSM_READY)
|
---|
142 | {
|
---|
143 | // user wants us to use GSM, but it's not yet okay,
|
---|
144 | // so we try to connect to GSM network.
|
---|
145 | gsmStatus = gsmAccess.begin(PINNUMBER);
|
---|
146 | } else if (!use_gsm && gsmStatus!=-1)
|
---|
147 | {
|
---|
148 | // user does NOT want us to use GSM, but we seem to be connected,
|
---|
149 | // so let's shut it down.
|
---|
150 | gsmAccess.shutdown();
|
---|
151 | // since there is no dedicated 'GSM_SHUTDOWN' status, we simply use -1.
|
---|
152 | gsmStatus = -1;
|
---|
153 | } else
|
---|
154 | {
|
---|
155 | // user does NOT want us to use GSM and GSM seems to be down ..
|
---|
156 | // so we don't do anything.
|
---|
157 | }
|
---|
158 | #else
|
---|
159 | checkForSms();
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | #ifdef USE_ETH
|
---|
163 | checkForEthernetClient();
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | void checkForEthernetClient()
|
---|
170 | {
|
---|
171 | #ifdef USE_ETH
|
---|
172 | EthernetClient client = server.available();
|
---|
173 | if( !client ){
|
---|
174 | return;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if( client.available() )
|
---|
178 | {
|
---|
179 | //if(!sentHeader)
|
---|
180 | //{
|
---|
181 | //printHelp(client);
|
---|
182 | //sentHeader = true;
|
---|
183 | //}
|
---|
184 |
|
---|
185 | int c = client.read();
|
---|
186 | switch( c )
|
---|
187 | {
|
---|
188 | case 'q':
|
---|
189 | //sentHeader = false;
|
---|
190 | delay(1); // give the web browser time to receive the data
|
---|
191 | client.stop(); // close the connection:
|
---|
192 | break;
|
---|
193 | case 'h':
|
---|
194 | printHelp(client);
|
---|
195 | break;
|
---|
196 | case 'd':
|
---|
197 | generateMessage();
|
---|
198 | client.println(message);
|
---|
199 | break;
|
---|
200 | case 'g':
|
---|
201 | use_gsm = true;
|
---|
202 | break;
|
---|
203 | case 'G':
|
---|
204 | use_gsm = false;
|
---|
205 | break;
|
---|
206 | case 's':
|
---|
207 | printStatus(client, true);
|
---|
208 | break;
|
---|
209 | //case 'p':
|
---|
210 | //digitalWrite(9, HIGH);
|
---|
211 | //server.println("Park");
|
---|
212 | //break;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | #endif
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | void printHelp(
|
---|
220 | #ifdef USE_ETH
|
---|
221 | EthernetClient &c
|
---|
222 | #endif
|
---|
223 | )
|
---|
224 | {
|
---|
225 | #ifdef USE_ETH
|
---|
226 | c.println();
|
---|
227 | c.println("FACT GSM");
|
---|
228 | c.println("h help");
|
---|
229 | c.println("q quit");
|
---|
230 | c.println("g/G en-/disable GSM");
|
---|
231 | c.println("s status");
|
---|
232 | c.println();
|
---|
233 | c.println("d direction");
|
---|
234 | //c.println("p Park Tel");
|
---|
235 | c.println();
|
---|
236 | #endif
|
---|
237 | }
|
---|
238 |
|
---|
239 | void printStatus(
|
---|
240 | #ifdef USE_ETH
|
---|
241 | EthernetClient &c,
|
---|
242 | #endif
|
---|
243 | bool print_via_serial
|
---|
244 | )
|
---|
245 | {
|
---|
246 | #ifdef USE_ETH
|
---|
247 | c.print("use_gsm: "); c.println(use_gsm);
|
---|
248 | c.print("gsmStatus: "); c.println(gsmStatus);
|
---|
249 | #endif
|
---|
250 | if (print_via_serial)
|
---|
251 | {
|
---|
252 | Serial.print("use_gsm: "); Serial.println(use_gsm);
|
---|
253 | Serial.print("gsmStatus: "); Serial.println(gsmStatus);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | void checkForSms()
|
---|
259 | {
|
---|
260 | #ifdef USE_GSM
|
---|
261 | if( sms.available() )
|
---|
262 | {
|
---|
263 | // Get remote number
|
---|
264 | sms.remoteNumber(senderNumber, NUMLEN);
|
---|
265 | if( sms.peek()=='P' )
|
---|
266 | {
|
---|
267 | Serial.println("I P");
|
---|
268 | generateMessage();
|
---|
269 | sendSMS();
|
---|
270 | }
|
---|
271 | sms.flush();
|
---|
272 | }
|
---|
273 | #else
|
---|
274 | Serial.println("I *would* now check for SMS.");
|
---|
275 | #endif
|
---|
276 | }
|
---|
277 |
|
---|
278 | void sendSMS()
|
---|
279 | {
|
---|
280 | #ifdef USE_GSM
|
---|
281 | sms.beginSMS(senderNumber);
|
---|
282 | sms.print(message);
|
---|
283 | sms.endSMS();
|
---|
284 | #endif
|
---|
285 | }
|
---|
286 |
|
---|
287 | void get_heading_pitch()
|
---|
288 | {
|
---|
289 | #ifdef USE_LSM
|
---|
290 | compass.read();
|
---|
291 | heading = compass.heading((LSM303::vector<int>){0,-1,0}) - 90 - 24;
|
---|
292 | if( heading < 0 ) heading = heading + 360;
|
---|
293 |
|
---|
294 | pitch = atan(compass.a.x/sqrt(pow(compass.a.y,2.)+pow(compass.a.z,2.)));
|
---|
295 | pitch *= 180.0/3.1415;
|
---|
296 | pitch += 13.;
|
---|
297 | pitch = 90. - pitch;
|
---|
298 | #endif
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | // Generate SMS
|
---|
303 | void generateMessage()
|
---|
304 | {
|
---|
305 | get_heading_pitch();
|
---|
306 | // Read light level
|
---|
307 | iLight = analogRead(0);
|
---|
308 | // Setup message
|
---|
309 | sprintf(message,"%d %d %d\n",heading,(int)pitch,iLight);
|
---|
310 | }
|
---|