source: trunk/Arduino/GSM/doms/doms.ino @ 17970

Last change on this file since 17970 was 17970, checked in by dneise, 6 years ago
variant of arduinoGSMFACT that can handle flodding via ethernet while still checking for SMS
File size: 5.7 KB
Line 
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
52EthernetServer 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
63char  message[18];      // Array to hold SMS message
64char  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#endif
72
73// -------------- Light sensor stuff -----------------------------------
74int   iLight     =  9999;  // light level
75
76
77// -------------- Compass stuff ----------------------------------------
78#ifdef USE_LSM
79LSM303 compass;
80#endif
81int   heading    = -9999;  // Continual heading reading
82float pitch      = -9999.; // Pitch relative to horizon
83
84
85
86void setup()
87{
88    // this is to begin (ethz) ethernet
89    //Ethernet.begin(mac, ip, dnserver, gatew, smask);
90    // initialize serial communications and wait for port to open:
91    Serial.begin(9600);
92
93
94#ifdef USE_ETH
95    //ETH
96    byte mac[]   = { 0x90, 0xA2, 0xDA, 0x0D, 0xB5, 0xE3 };
97    byte ip[]    = { 192, 33, 103, 241 };
98    //LA PALMA
99    //byte mac[]   = { 0xFA, 0xC7, 0xBB, 0xFF, 0x33, 0x33 };
100    //byte ip[]    = { 10, 0, 100, 202 };
101
102    Ethernet.begin(mac, ip);
103    server.begin();
104    Serial.print("Chat server address:");
105    Serial.println(Ethernet.localIP());
106#else
107    Serial.println("No ethernet this time :-(");
108#endif
109
110
111
112    // Start the Wire communication
113#ifdef USE_WIRE
114    Wire.begin();
115#endif
116
117    // Initialize the Compass
118#ifdef USE_LSM
119    compass.init();
120    compass.enableDefault();
121
122    // Calibration values. Use the Calibrate example program to get the values for your compass.
123    // ETH Calibration values
124    //  compass.m_min.x = -700; compass.m_min.y = -695; compass.m_min.z = -635;
125    //  compass.m_max.x = +293; compass.m_max.y = +551; compass.m_max.z = 606;
126    // La Palma calibration values
127    compass.m_min.x = -389; compass.m_min.y = -423; compass.m_min.z = -420;
128    compass.m_max.x = +510; compass.m_max.y = +420; compass.m_max.z = +315;
129#endif
130}
131
132
133void loop()
134{
135#ifdef USE_GSM
136    if( gsmStatus==GSM_READY )
137        checkForSms();
138    else
139    {
140        // try to connect to DSM network again.
141        gsmStatus = gsmAccess.begin(PINNUMBER);
142    }
143#else
144    checkForSms();
145#endif
146
147#ifdef USE_ETH
148    checkForEthernetClient();
149#endif
150
151}
152
153
154void checkForEthernetClient()
155{
156#ifdef USE_ETH
157    EthernetClient client = server.available();
158    if( !client ){
159        return;
160    }
161
162    if( client.available() )
163    {
164        //if(!sentHeader)
165        //{
166            //printHelp(client);
167            //sentHeader = true;
168        //}
169
170        int c = client.read();
171        switch( c )
172        {
173            case 'q':
174                //sentHeader = false;
175                delay(1); // give the web browser time to receive the data
176                client.stop(); // close the connection:
177                break;
178            case 'h':
179                printHelp(client);
180                break;
181            case 'd':
182                generateMessage();
183                client.println(message);
184                break;
185          //case 'p':
186              //digitalWrite(9, HIGH);
187              //server.println("Park");
188              //break;
189        }
190    }
191#endif
192}
193
194
195void printHelp(
196#ifdef USE_ETH
197    EthernetClient &c
198#endif
199    )
200{
201#ifdef USE_ETH
202    c.println();
203    c.println("FACT GSM");
204    c.println("h help");
205    c.println("q quit");
206    c.println();
207    c.println("d direction");
208    //c.println("p Park Tel");
209    c.println();
210#endif
211}
212
213void checkForSms()
214{
215#ifdef USE_GSM
216    if( sms.available() )
217    {
218        // Get remote number
219        sms.remoteNumber(senderNumber, NUMLEN);
220        if( sms.peek()=='P' )
221        {
222            Serial.println("I P");
223            generateMessage();
224            sendSMS();
225        }
226        sms.flush();
227    }
228#else
229    Serial.println("I *would* now check for SMS.");
230#endif
231}
232
233void sendSMS()
234{
235#ifdef USE_GSM
236    sms.beginSMS(senderNumber);
237    sms.print(message);
238    sms.endSMS();
239#endif
240}
241
242void get_heading_pitch()
243{
244#ifdef USE_LSM
245    compass.read();
246    heading = compass.heading((LSM303::vector<int>){0,-1,0}) - 90 - 24;
247    if( heading < 0 ) heading = heading + 360;
248
249    pitch = atan(compass.a.x/sqrt(pow(compass.a.y,2.)+pow(compass.a.z,2.)));
250    pitch *= 180.0/3.1415;
251    pitch += 13.;
252    pitch  = 90. - pitch;
253#endif
254}
255
256
257// Generate SMS
258void generateMessage()
259{
260  get_heading_pitch();
261  // Read light level
262  iLight = analogRead(0);
263  // Setup message
264  sprintf(message,"%d %d %d\n",heading,(int)pitch,iLight);
265}
Note: See TracBrowser for help on using the repository browser.