1 | /*
|
---|
2 |
|
---|
3 | Arduino for FACT
|
---|
4 |
|
---|
5 | Gareth Hughes 2013-10-30
|
---|
6 |
|
---|
7 | Using: Arduino Ethernet
|
---|
8 | GSM Overlay
|
---|
9 | LSM303: Magnetic field and accelerometer
|
---|
10 | YL-38: Light Sensor
|
---|
11 |
|
---|
12 | Function: Will reply to an SMS stating the position, movement and light level.
|
---|
13 | If lighlevel gets above a certain threshold will send a warning SMS every 1 min.
|
---|
14 |
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <SPI.h>
|
---|
18 | #include <Ethernet.h>
|
---|
19 |
|
---|
20 | // include libraries
|
---|
21 | #include <GSM.h>
|
---|
22 | #include <Wire.h>
|
---|
23 | #include <LSM303.h>
|
---|
24 |
|
---|
25 | //ETH
|
---|
26 | //byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xB5, 0xE3 };
|
---|
27 | //byte ip[] = { 192, 33, 103, 241 };
|
---|
28 | //LA PALMA
|
---|
29 | byte mac[] = { 0xFA, 0xC7, 0xBB, 0xFF, 0x33, 0x33 };
|
---|
30 | byte ip[] = { 10, 0, 100, 202 };
|
---|
31 |
|
---|
32 | EthernetServer server = EthernetServer(23);
|
---|
33 | EthernetClient client;
|
---|
34 | //boolean sentHeader = false;
|
---|
35 | bool sentHeader = false;
|
---|
36 |
|
---|
37 | // PIN Number for the SIM
|
---|
38 | //#define PINNUMBER "0839"
|
---|
39 | #define PINNUMBER ""
|
---|
40 | //#define PINNUMBER "7143"
|
---|
41 | #define NUMLEN 15
|
---|
42 |
|
---|
43 | // initialize the library instances
|
---|
44 | GSM gsmAccess;
|
---|
45 | GSM_SMS sms;
|
---|
46 | LSM303 compass;
|
---|
47 |
|
---|
48 | char message[18]; // Array to hold SMS message
|
---|
49 | char senderNumber[NUMLEN]; // Array to hold the number a SMS is retreived from
|
---|
50 |
|
---|
51 | //unsigned int counter = 0; // loop counter
|
---|
52 | int heading = -9999; // Continual heading reading
|
---|
53 | //int heading2 = -9999; // Continual heading reading
|
---|
54 | float pitch = -9999.; // Pitch relative to horizon
|
---|
55 | int iLight = 9999; // light level
|
---|
56 | int iDiff = -9999; // difference in degrees
|
---|
57 | char c;
|
---|
58 |
|
---|
59 | void setup()
|
---|
60 | {
|
---|
61 |
|
---|
62 |
|
---|
63 | //begin the ethernet connections
|
---|
64 | //Ethernet.begin(mac, ip, dnserver, gatew, smask); // this is to begin (ethz) ethernet
|
---|
65 | Ethernet.begin(mac, ip);
|
---|
66 | server.begin();
|
---|
67 |
|
---|
68 | // initialize serial communications and wait for port to open:
|
---|
69 | //Serial.begin(9600);
|
---|
70 |
|
---|
71 | // Start the Wire communication
|
---|
72 | Wire.begin();
|
---|
73 |
|
---|
74 | // Initialize the Compass
|
---|
75 | compass.init();
|
---|
76 | compass.enableDefault();
|
---|
77 |
|
---|
78 | // Calibration values. Use the Calibrate example program to get the values for your compass.
|
---|
79 | // ETH Calibration values
|
---|
80 | // compass.m_min.x = -700; compass.m_min.y = -695; compass.m_min.z = -635;
|
---|
81 | // compass.m_max.x = +293; compass.m_max.y = +551; compass.m_max.z = 606;
|
---|
82 | // La Palma calibration values
|
---|
83 | compass.m_min.x = -389; compass.m_min.y = -423; compass.m_min.z = -420;
|
---|
84 | compass.m_max.x = +510; compass.m_max.y = +420; compass.m_max.z = +315;
|
---|
85 |
|
---|
86 | // connection state
|
---|
87 | boolean notConnected = true;
|
---|
88 |
|
---|
89 | // Start GSM connection
|
---|
90 | while(notConnected)
|
---|
91 | {
|
---|
92 | if(gsmAccess.begin(PINNUMBER)==GSM_READY)
|
---|
93 | notConnected = false;
|
---|
94 | else
|
---|
95 | {
|
---|
96 | delay(1000);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | // use pin 9 to mean parked!
|
---|
101 | //pinMode(9, OUTPUT);
|
---|
102 |
|
---|
103 | }
|
---|
104 |
|
---|
105 | void loop()
|
---|
106 | {
|
---|
107 |
|
---|
108 | // If there are any SMSs available()
|
---|
109 | if (sms.available())
|
---|
110 | {
|
---|
111 |
|
---|
112 | // Get remote number
|
---|
113 | sms.remoteNumber(senderNumber, NUMLEN);
|
---|
114 |
|
---|
115 | //if( sms.peek()=='#' )
|
---|
116 | //{
|
---|
117 | //sms.flush();
|
---|
118 | //} else if( sms.peek()=='P' || sms.peek()=='p' )
|
---|
119 | //} else
|
---|
120 | if( sms.peek()=='P' )
|
---|
121 | {
|
---|
122 |
|
---|
123 | //Serial.println("I P");
|
---|
124 |
|
---|
125 | // Generate message
|
---|
126 | generateMessage();
|
---|
127 | // reply to the message
|
---|
128 | sendSMS();
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | // flush sms memory
|
---|
133 | sms.flush();
|
---|
134 |
|
---|
135 | }
|
---|
136 |
|
---|
137 | // listen for incoming clients, and process qequest.
|
---|
138 | checkForClient();
|
---|
139 |
|
---|
140 | // Main loop
|
---|
141 | delay(1000);
|
---|
142 |
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Send the message.
|
---|
147 | // iType = 0: Sends reply to an SMS with the heading, movement and light level
|
---|
148 | // iType = 1: Sends an alert to predefined number warning that the light has gone above threshold
|
---|
149 | void sendSMS()
|
---|
150 | {
|
---|
151 |
|
---|
152 | sms.beginSMS(senderNumber);
|
---|
153 | sms.print(message);
|
---|
154 | sms.endSMS();
|
---|
155 |
|
---|
156 | }
|
---|
157 |
|
---|
158 | void checkForClient()
|
---|
159 | {
|
---|
160 |
|
---|
161 | EthernetClient client = server.available();
|
---|
162 |
|
---|
163 | if( client )
|
---|
164 | {
|
---|
165 |
|
---|
166 | // an http request ends with a blank line
|
---|
167 | boolean currentLineIsBlank = true;
|
---|
168 |
|
---|
169 | while( client.connected() )
|
---|
170 | {
|
---|
171 | if( client.available() )
|
---|
172 | {
|
---|
173 |
|
---|
174 | if(!sentHeader)
|
---|
175 | {
|
---|
176 | printHelp();
|
---|
177 | sentHeader = true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | c = client.read();
|
---|
181 |
|
---|
182 | switch( c )
|
---|
183 | {
|
---|
184 |
|
---|
185 | case 'q':
|
---|
186 | sentHeader = false;
|
---|
187 | delay(1); // give the web browser time to receive the data
|
---|
188 | client.stop(); // close the connection:
|
---|
189 | break;
|
---|
190 | case 'h':
|
---|
191 | printHelp();
|
---|
192 | break;
|
---|
193 | case 'd':
|
---|
194 | generateMessage();
|
---|
195 | server.println(message);
|
---|
196 | break;
|
---|
197 | // case 'p':
|
---|
198 | // digitalWrite(9, HIGH);
|
---|
199 | // server.println("Park");
|
---|
200 | // break;
|
---|
201 | }
|
---|
202 |
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | }
|
---|
207 |
|
---|
208 | }
|
---|
209 |
|
---|
210 | // errr Print the Help Stuff
|
---|
211 | void printHelp()
|
---|
212 | {
|
---|
213 |
|
---|
214 | server.println();
|
---|
215 | //server.println("FACT GSM");
|
---|
216 | //server.println("h help");
|
---|
217 | server.println("q quit");
|
---|
218 | server.println();
|
---|
219 | server.println("d direction");
|
---|
220 | // server.println("p Park Tel");
|
---|
221 | server.println();
|
---|
222 |
|
---|
223 | }
|
---|
224 |
|
---|
225 | // Get the pointing difference
|
---|
226 | void getDiff()
|
---|
227 | {
|
---|
228 |
|
---|
229 | compass.read();
|
---|
230 | heading = compass.heading((LSM303::vector<int>){0,-1,0}) - 90 - 24;
|
---|
231 | if( heading < 0 ) heading = heading + 360;
|
---|
232 | //server.println(heading);
|
---|
233 |
|
---|
234 | // delay(5000);
|
---|
235 | // compass.read();
|
---|
236 | // heading2 = compass.heading((LSM303::vector){0,-1,0}) - 90;
|
---|
237 | // if( heading2 < 0 ) heading2 = heading2 + 360;
|
---|
238 | // iDiff = heading - heading2;
|
---|
239 | // //server.println(iDiff);
|
---|
240 |
|
---|
241 | pitch = atan(compass.a.x/sqrt(pow(compass.a.y,2.)+pow(compass.a.z,2.)));
|
---|
242 | pitch *= (180.0)/3.1415;
|
---|
243 | pitch += 13.;
|
---|
244 | pitch = 90. - pitch;
|
---|
245 |
|
---|
246 | }
|
---|
247 |
|
---|
248 | // Generate SMS
|
---|
249 | void generateMessage()
|
---|
250 | {
|
---|
251 |
|
---|
252 | // Get pointing difference
|
---|
253 | getDiff();
|
---|
254 | // Read light level
|
---|
255 | iLight = analogRead(0);
|
---|
256 | // Setup message
|
---|
257 | sprintf(message,"%d %d %d\n",heading,(int)pitch,iLight);
|
---|
258 |
|
---|
259 | }
|
---|