peony3000

Gadgeteer GPS Cellular SMS

May 2nd, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.63 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.GHIElectronics;
  13. using Gadgeteer.Modules.Seeed;
  14.  
  15. namespace Gadgeteer_GPS_Cellular_Request_01
  16. {
  17.     public partial class Program
  18.     {
  19.  
  20.         GT.Timer timer = new GT.Timer(5000);
  21.         bool gpsPositionHasBeenReceived = false;
  22.  
  23.         void ProgramStarted()
  24.         {
  25.             gps.InvalidPositionReceived += new GTM.Seeed.GPS.InvalidPositionReceivedHandler(gps_InvalidPositionReceived);
  26.             gps.PositionReceived += new GTM.Seeed.GPS.PositionReceivedHandler(gps_PositionReceived);
  27.             gps.NMEASentenceReceived += new GPS.NMEASentenceReceivedHandler(gps_NMEASentenceReceived);
  28.  
  29.             // create a timer robot
  30.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  31.             timer.Start();
  32.  
  33.             // create a button watching robot to fire off button pressed instructions
  34.             //button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
  35.  
  36.             //button.ButtonPressed += new Button.ButtonEventHandler(resetButton_ButtonPressed);
  37.             button.ButtonPressed+=new Button.ButtonEventHandler(button_ButtonPressed);
  38.             button.TurnLEDOff();
  39.            
  40.             //
  41.             cellularRadio.DebugPrintEnabled = true;
  42.             cellularRadio.PowerOn(5);
  43.            
  44.            
  45.             cellularRadio.SmsReceived += new CellularRadio.SmsReceivedHandler(cellularRadio_NewSMSEvent);
  46.             cellularRadio.ModuleInitialized += new CellularRadio.ModuleInitializedHandler(cellularRadio_ModuleInitialized);
  47.            
  48.             // tell everyone that you have started
  49.             Debug.Print("Program Started");
  50.         }
  51.  
  52.         /*void button_ButtonPressed(Button sender, Button.ButtonState state)
  53.         {
  54.             Debug.Print("GPS last position " + gps.LastPosition + " age " + gps.LastValidPositionAge);
  55.         }*/
  56.  
  57.         void timer_Tick(GT.Timer timer)
  58.         {
  59.             Debug.Print("GPS last position " + gps.LastPosition + " age " + gps.LastValidPositionAge);
  60.         }
  61.  
  62.         void gps_InvalidPositionReceived(GTM.Seeed.GPS sender)
  63.         {
  64.             Debug.Print("Invalid position");
  65.         }
  66.  
  67.         void gps_PositionReceived(GTM.Seeed.GPS sender, GTM.Seeed.GPS.Position position)
  68.         {
  69.             gpsPositionHasBeenReceived = true;
  70.             Debug.Print(position);
  71.         }
  72.  
  73.         void gps_NMEASentenceReceived(GPS sender, string nmeaSentence)
  74.         {
  75.             if (!gpsPositionHasBeenReceived)
  76.             {
  77.                 Debug.Print("NMEA Sentence: " + nmeaSentence);
  78.             }
  79.         }
  80.  
  81.           void cellularRadio_NewSMSEvent(CellularRadio sender, CellularRadio.Sms message)
  82.         {
  83.             if (message != null)
  84.             {
  85.                 Debug.Print("NEW SMS");
  86.                 Debug.Print("FROM: " + message.TelephoneNumber);
  87.                 Debug.Print("MSG: " + message.TextMessage);
  88.                 Debug.Print("WHEN: " + message.Timestamp);
  89.                 Debug.Print("STATUS: " + message.Status);
  90.                 Debug.Print("POS:" + message.Index);
  91.  
  92.                
  93.                 // remove extra '+'
  94.                 message.TelephoneNumber = message.TelephoneNumber.Trim('+');
  95.  
  96.                 if (cellularRadio.SendSms(message.TelephoneNumber, gps.LastPosition) != GTM.Seeed.CellularRadio.ReturnedState.OK)
  97.                     {
  98.                         Debug.Print("Retrieve clock failed");
  99.                     }
  100.             }
  101.         }
  102.  
  103.         void button_ButtonPressed(Button sender, Button.ButtonState state)
  104.         {
  105.             if (cellularRadio.SendSms("1401***8678", "Send me a text and I'll tell you where I am! Regards, Gadgeteer") != GTM.Seeed.CellularRadio.ReturnedState.OK)
  106.             {
  107.                 Debug.Print("Retrieve clock failed");
  108.             }
  109.         }
  110.  
  111.         void cellularRadio_SMSReady(CellularRadio sender, CellularRadio.Sms message)
  112.         {
  113.             if (message != null)
  114.             {
  115.                 Debug.Print("RETRIEVED");
  116.                 Debug.Print("FROM: " + message.TelephoneNumber);
  117.                 Debug.Print("MSG: " + message.TextMessage);
  118.                 Debug.Print("WHEN: " + message.Timestamp);
  119.                 Debug.Print("STATUS: " + message.Status);
  120.                 Debug.Print("POS:" + message.Index);
  121.             }
  122.         }
  123.  
  124.         void cellularRadio_PhoneActivityRetrieved(CellularRadio sender, CellularRadio.PhoneActivityType activity)
  125.         {
  126.             Debug.Print("Activity: " + activity);
  127.         }
  128.  
  129.         void cellularRadio_ModuleInitialized(CellularRadio sender)
  130.         {
  131.             Debug.Print("MODULE INITIALIZED");
  132.             button.TurnLEDOn();
  133.         }
  134.  
  135.         void cellularRadio_NoCarrierEvent(CellularRadio sender)
  136.         {
  137.             Debug.Print("NO CARRIER EVENT");
  138.         }
  139.  
  140.         void cellularRadio_ConnectedLineEvent(CellularRadio sender, string number)
  141.         {
  142.             Debug.Print("CONNECTED: " + number);
  143.         }
  144.  
  145.         void cellularRadio_SMSListReady(CellularRadio sender, System.Collections.ArrayList smsList)
  146.         {
  147.             if (smsList != null)
  148.             {
  149.                 Debug.Print("SMS LIST");
  150.                 foreach (GTM.Seeed.CellularRadio.Sms message in smsList)
  151.                 {
  152.                     Debug.Print("FROM: " + message.TelephoneNumber);
  153.                     Debug.Print("MSG: " + message.TextMessage);
  154.                     Debug.Print("WHEN: " + message.Timestamp);
  155.                     Debug.Print("STATUS: " + message.Status + "\n");
  156.                     Debug.Print("POS:" + message.Index);
  157.                 }
  158.             }
  159.         }
  160.  
  161.         void cellularRadio_OperatorReady(CellularRadio sender, string operatorName)
  162.         {
  163.             if (operatorName != null)
  164.                 Debug.Print("OPERATOR: " + operatorName);
  165.             else
  166.                 Debug.Print("NO OPERATOR WAS FOUND");
  167.         }
  168.  
  169.         void cellularRadio_SignalStrengthReady(CellularRadio sender, CellularRadio.SignalStrengthType signalStrength)
  170.         {
  171.             Debug.Print("SIGNAL STRENGTH: " + signalStrength);
  172.         }
  173.  
  174.         void cellularRadio_ClockReady(CellularRadio sender, DateTime clock)
  175.         {
  176.             Debug.Print("CLOCK: " + clock.ToString("r"));
  177.         }
  178.  
  179.         void cellularRadio_ContactReady(CellularRadio sender, CellularRadio.Contact contact)
  180.         {
  181.             Debug.Print("NAME: " + contact.name);
  182.             Debug.Print("TEL: " + contact.telephoneNumber);
  183.         }
  184.         void cellularRadio_IncomingCall(CellularRadio sender, string caller)
  185.         {
  186.             Debug.Print("NEW CALL: " + caller);
  187.         }
  188.  
  189.         void cellularRadio_NetworkRegistrationEvent(CellularRadio sender, CellularRadio.NetworkRegistrationState networkState)
  190.         {
  191.             Debug.Print("NETWORK EVENT " + networkState);
  192.         }
  193.  
  194.         void secondButton_ButtonPressed(Button sender, Button.ButtonState state)
  195.         {
  196.             Debug.Print("BUTTON BEGIN");
  197.  
  198.             /*
  199.              * Power ON test
  200.              */
  201.             //cellularRadio.PowerOn = false;
  202.             //cellularRadio.PowerOn = false;
  203.             // cellularRadio.PowerOn = true;
  204.             // cellularRadio.PowerOn = true;
  205.  
  206.             /*
  207.              * PIN Event Test
  208.              */
  209.             cellularRadio.SendATCommand("AT+CPIN?");
  210.  
  211.             /*
  212.              * Network Event Test
  213.              */
  214.             cellularRadio.SendATCommand("AT+CREG?");
  215.  
  216.             /*
  217.              * SMS Test
  218.              */
  219.             //cellularRadio.SendSms("1591***2620", "HELLO");
  220.             //cellularRadio.SendSms("447801***396", "HELLO");
  221.  
  222.             /*
  223.              * Receive Voice Call Test
  224.              */
  225.             //if (!button.IsLedOn)
  226.             //{
  227.             //    cellularRadio.PickUp();
  228.             //    button.TurnLEDOn();
  229.             //}
  230.             //else
  231.             //{
  232.             //    cellularRadio.HangUp();
  233.             //    button.TurnLEDOff();
  234.             //}
  235.  
  236.             /*
  237.              * Make voice call Test
  238.              */
  239.  
  240.             cellularRadio.Dial("*cell*");
  241.  
  242.  
  243.             /*
  244.              * Read SMS Test
  245.              */
  246.             //cellularRadio.RetrieveSms(1,true);
  247.  
  248.             /*
  249.              * Delete SMS Test
  250.              */
  251.             //cellularRadio.DeleteSMS(1);
  252.  
  253.             /*
  254.              * Redial Test
  255.              */
  256.             //cellularRadio.Dial("159919620");
  257.             //Thread.Sleep(3000);
  258.             //cellularRadio.HangUp();
  259.             //Thread.Sleep(3000);
  260.             //cellularRadio.Redial();
  261.  
  262.             /*
  263.              * Phone Activity Test
  264.              */
  265.             //cellularRadio.RequestPhoneActivity();
  266.  
  267.             /*
  268.              * Phonebook Test
  269.              */
  270.             //cellularRadio.SaveContact( 1,"447527202098", "Eduardo");
  271.             //Thread.Sleep(1000);
  272.             //cellularRadio.RequestContact(1);
  273.  
  274.             /*
  275.              * Clock test
  276.              */
  277.             //cellularRadio.RequestClock();
  278.  
  279.             /*
  280.              * IMEI test
  281.              */
  282.             //if (cellularRadio.RetrieveImei() != GTM.Seeed.CellularRadio.ReturnedState.OK)
  283.             //{
  284.             //    Debug.Print("Retrieve IMEI failed");
  285.             //}
  286.  
  287.             /*
  288.              * Signal Strength Test
  289.              */
  290.             //cellularRadio.RequestSignalStrength();
  291.  
  292.             /*
  293.              * Operator Test
  294.              */
  295.             //cellularRadio.RequestOperator();
  296.  
  297.             /*
  298.              * Get unread test
  299.              */
  300.             //cellularRadio.RequestSMSList(GTM.Seeed.CellularRadio.SMSState.All);
  301.  
  302.             /*
  303.              * Attach GPRS Test
  304.              */
  305.             //cellularRadio.AttachGPRS("orangeinternet", "", "");
  306.             //cellularRadio.SendATCommand("AT+CIPSTATUS");
  307.  
  308.             /*
  309.              * Delete all sms test
  310.              */
  311.             //cellularRadio.RetrieveSmsList(CellularRadio.SmsState.All);
  312.             //Thread.Sleep(1000);
  313.             //cellularRadio.DeleteAllSms();
  314.             //Thread.Sleep(1000);
  315.             //cellularRadio.RetrieveSmsList(CellularRadio.SmsState.All);
  316.  
  317.  
  318.             Debug.Print("BUTTON END");
  319.         }
  320.  
  321.         void cellularRadio_PINEvent(CellularRadio sender, CellularRadio.PINState pinState)
  322.         {
  323.             Debug.Print("PIN: " + pinState);
  324.         }
  325.     }
  326.  
  327.    
  328. }
Advertisement
Add Comment
Please, Sign In to add comment