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 | //GH// #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 | //GH// GSM gsmAccess;
|
---|
45 | //GH// GSM_SMS sms;
|
---|
46 | LSM303 compass;
|
---|
47 |
|
---|
48 | char message[18]; // Array to hold SMS message
|
---|
49 | //GH// 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 | /* //GH//
|
---|
87 | // connection state
|
---|
88 | boolean notConnected = true;
|
---|
89 |
|
---|
90 | // Start GSM connection
|
---|
91 | while(notConnected)
|
---|
92 | {
|
---|
93 | if(gsmAccess.begin(PINNUMBER)==GSM_READY)
|
---|
94 | notConnected = false;
|
---|
95 | else
|
---|
96 | {
|
---|
97 | delay(1000);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | */
|
---|
101 |
|
---|
102 | // use pin 9 to mean parked!
|
---|
103 | //pinMode(9, OUTPUT);
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | void loop()
|
---|
108 | {
|
---|
109 |
|
---|
110 | /* //GH//
|
---|
111 | // If there are any SMSs available()
|
---|
112 | if (sms.available())
|
---|
113 | {
|
---|
114 |
|
---|
115 | // Get remote number
|
---|
116 | sms.remoteNumber(senderNumber, NUMLEN);
|
---|
117 |
|
---|
118 | //if( sms.peek()=='#' )
|
---|
119 | //{
|
---|
120 | //sms.flush();
|
---|
121 | //} else if( sms.peek()=='P' || sms.peek()=='p' )
|
---|
122 | //} else
|
---|
123 | if( sms.peek()=='P' )
|
---|
124 | {
|
---|
125 |
|
---|
126 | //Serial.println("I P");
|
---|
127 |
|
---|
128 | // Generate message
|
---|
129 | generateMessage();
|
---|
130 | // reply to the message
|
---|
131 | sendSMS();
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | // flush sms memory
|
---|
136 | sms.flush();
|
---|
137 |
|
---|
138 | }
|
---|
139 | */
|
---|
140 |
|
---|
141 | // listen for incoming clients, and process qequest.
|
---|
142 | checkForClient();
|
---|
143 |
|
---|
144 | // Main loop
|
---|
145 | delay(1000);
|
---|
146 |
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* //GH//
|
---|
151 | // Send the message.
|
---|
152 | // iType = 0: Sends reply to an SMS with the heading, movement and light level
|
---|
153 | // iType = 1: Sends an alert to predefined number warning that the light has gone above threshold
|
---|
154 | void sendSMS()
|
---|
155 | {
|
---|
156 |
|
---|
157 | sms.beginSMS(senderNumber);
|
---|
158 | sms.print(message);
|
---|
159 | sms.endSMS();
|
---|
160 |
|
---|
161 | }
|
---|
162 | */
|
---|
163 |
|
---|
164 | void checkForClient()
|
---|
165 | {
|
---|
166 |
|
---|
167 | EthernetClient client = server.available();
|
---|
168 |
|
---|
169 | if( client )
|
---|
170 | {
|
---|
171 |
|
---|
172 | // an http request ends with a blank line
|
---|
173 | boolean currentLineIsBlank = true;
|
---|
174 |
|
---|
175 | while( client.connected() )
|
---|
176 | {
|
---|
177 | if( client.available() )
|
---|
178 | {
|
---|
179 |
|
---|
180 | if(!sentHeader)
|
---|
181 | {
|
---|
182 | printHelp();
|
---|
183 | sentHeader = true;
|
---|
184 | }
|
---|
185 |
|
---|
186 | c = client.read();
|
---|
187 |
|
---|
188 | switch( c )
|
---|
189 | {
|
---|
190 |
|
---|
191 | case 'q':
|
---|
192 | sentHeader = false;
|
---|
193 | delay(1); // give the web browser time to receive the data
|
---|
194 | client.stop(); // close the connection:
|
---|
195 | break;
|
---|
196 | case 'h':
|
---|
197 | printHelp();
|
---|
198 | break;
|
---|
199 | case 'd':
|
---|
200 | generateMessage();
|
---|
201 | server.println(message);
|
---|
202 | break;
|
---|
203 | // case 'p':
|
---|
204 | // digitalWrite(9, HIGH);
|
---|
205 | // server.println("Park");
|
---|
206 | // break;
|
---|
207 | }
|
---|
208 |
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|
215 |
|
---|
216 | // errr Print the Help Stuff
|
---|
217 | void printHelp()
|
---|
218 | {
|
---|
219 |
|
---|
220 | server.println();
|
---|
221 | //server.println("FACT GSM");
|
---|
222 | //server.println("h help");
|
---|
223 | server.println("q quit");
|
---|
224 | server.println();
|
---|
225 | server.println("d direction");
|
---|
226 | // server.println("p Park Tel");
|
---|
227 | server.println();
|
---|
228 |
|
---|
229 | }
|
---|
230 |
|
---|
231 | // Get the pointing difference
|
---|
232 | void getDiff()
|
---|
233 | {
|
---|
234 |
|
---|
235 | compass.read();
|
---|
236 | heading = compass.heading((LSM303::vector<int>){0,-1,0}) - 90 - 24;
|
---|
237 | if( heading < 0 ) heading = heading + 360;
|
---|
238 | //server.println(heading);
|
---|
239 |
|
---|
240 | // delay(5000);
|
---|
241 | // compass.read();
|
---|
242 | // heading2 = compass.heading((LSM303::vector){0,-1,0}) - 90;
|
---|
243 | // if( heading2 < 0 ) heading2 = heading2 + 360;
|
---|
244 | // iDiff = heading - heading2;
|
---|
245 | // //server.println(iDiff);
|
---|
246 |
|
---|
247 | pitch = atan(compass.a.x/sqrt(pow(compass.a.y,2.)+pow(compass.a.z,2.)));
|
---|
248 | pitch *= (180.0)/3.1415;
|
---|
249 | pitch += 13.;
|
---|
250 | pitch = 90. - pitch;
|
---|
251 |
|
---|
252 |
|
---|
253 | }
|
---|
254 |
|
---|
255 | // Generate SMS
|
---|
256 | void generateMessage()
|
---|
257 | {
|
---|
258 |
|
---|
259 | // Get pointing difference
|
---|
260 | getDiff();
|
---|
261 | // Read light level
|
---|
262 | iLight = analogRead(0);
|
---|
263 | // Setup message
|
---|
264 | sprintf(message,"%d %d %d\n",heading,(int)pitch,iLight);
|
---|
265 |
|
---|
266 | }
|
---|