peony3000

Gadgeteer Weather Crier Xml 01

Nov 7th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.56 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. //using Microsoft.SPOT.Presentation;
  6. //using Microsoft.SPOT.Presentation.Controls;
  7. //using Microsoft.SPOT.Presentation.Media;
  8. //using Microsoft.SPOT.Touch;
  9.  
  10. using Gadgeteer.Networking;
  11. using GT = Gadgeteer;
  12. using GTM = Gadgeteer.Modules;
  13. using Gadgeteer.Modules.GHIElectronics;
  14. using System.Xml;
  15. using System.IO;
  16. using Gadgeteer.Modules.Seeed;
  17.  
  18. namespace Gadgeteer_Weather_Crier_xml_01
  19.  
  20. {
  21.  
  22.     public partial class Program
  23.     {
  24.  
  25.         GT.StorageDevice usbStorage;
  26.         bool messageComplete = true;
  27.         FileStream stream;
  28.         ArrayList musicFiles = new ArrayList();
  29.         // create a time based event robot to trigger repeated tasks
  30.         //GT.Timer timer = new GT.Timer(60000, GT.Timer.BehaviorType.RunContinuously);
  31.  
  32.         // This method is run when the mainboard is powered up or reset.  
  33.         void ProgramStarted()
  34.         {
  35.             Debug.Print("Program Started");
  36.  
  37.             // Initialize event handling robots
  38.             usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
  39.             usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
  40.  
  41.             //accelerometer threshold bump method
  42.             accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded);
  43.             accelerometer.MeasurementRange = GTM.Seeed.Accelerometer.Range.TwoG;
  44.             // set the calibration point (the device shouldn't be moving!)
  45.             accelerometer.Calibrate();
  46.             accelerometer.EnableThresholdDetection(-3.0, false, false, true, true, false, false);
  47.  
  48.             //start Wifi
  49.             StartWiFi();
  50.  
  51.             // music finished listening robot
  52.             music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished);
  53.             music.SetVolume(255);
  54.  
  55.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  56.             Debug.Print("Program Running");
  57.         }
  58.  
  59.         void accelerometer_ThresholdExceeded(Accelerometer sender)
  60.         {
  61.             Debug.Print("Bump detected!");
  62.             GetXmlFromWeb();
  63.             // check that the USB is connected and that we're not in the middle of playing the whole message
  64.             //if (usbStorage != null && messageComplete)
  65.             //{
  66.             //    messageComplete = false;
  67.             //    Debug.Print("Starting the first audio track");
  68.             //    playTracks();
  69.             //}
  70.         }
  71.  
  72.         void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
  73.         {
  74.             Debug.Print("USB Storage connected");
  75.             usbStorage = storageDevice;
  76.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 151953.mp3");//Good Morning
  77.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152009.mp3");//It looks like
  78.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152027.mp3");//It going to be mostly sunny
  79.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152211.mp3");//Tomorrow
  80.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152044.mp3");//There's a chance of a storm
  81.         }
  82.  
  83.         void playTracks()
  84.         {
  85.  
  86.             for (int currentFile = 0; currentFile < musicFiles.Count; currentFile++)
  87.             {
  88.                 Debug.Print("Moving to next audio track: currentFile = " + currentFile);
  89.                 stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
  90.                 music.SetVolume(255);
  91.                 Debug.Print("Starting to play: " + (String)musicFiles[currentFile]);
  92.                 music.Play(stream);
  93.                 while (music.IsBusy)
  94.                 {
  95.                     // You can't call music.StopPlaying or set volume with this loop running.    
  96.                     Thread.Sleep(10);
  97.                     // Check for a condition to break the music
  98.                     //if (false)
  99.                     //{
  100.                     //    break;
  101.                     //}
  102.                 }
  103.             }
  104.             Debug.Print("Resetting bump and audio");
  105.             accelerometer.ResetThresholdDetection();
  106.             messageComplete = true;
  107.         }
  108.  
  109.         void music_MusicFinished(Music sender)
  110.         {
  111.             Debug.Print("Track finished");
  112.         }
  113.  
  114.         void usbHost_USBDriveDisconnected(UsbHost sender)
  115.         {
  116.             Debug.Print("USB Storage Disconnected");
  117.             musicFiles.Clear();
  118.             usbStorage = null;
  119.         }
  120.  
  121.         void GetXmlFromWeb()
  122.         {
  123.             WebClient.GetFromWeb("http://www.mysite.com/weather.xml").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
  124.         }
  125.  
  126.  
  127.         //void timer_Tick(GT.Timer timer)
  128.         //{
  129.         //    // what do we want to happen when the timer tick event fires
  130.         //    // get some xml from the internet
  131.         //    GetXmlFromWeb();
  132.         //}
  133.  
  134.         void StartWiFi()
  135.         {
  136.             wifi.UseDHCP();
  137.             Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
  138.             info.SSID = "name";
  139.             info.SecMode = WiFi_RS21.SecurityMode.WEP;
  140.             info.networkType = WiFi_RS21.NetworkType.AccessPoint;
  141.             wifi.Join(info, "pass");
  142.             wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown);
  143.             wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);
  144.         }
  145.  
  146.         void wifi_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
  147.         {
  148.             Debug.Print("network down: connecting....");
  149.             //timer.Stop();
  150.         }
  151.  
  152.         void wifi_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
  153.         {
  154.             Debug.Print("network up");
  155.             Debug.Print(wifi.NetworkSettings.IPAddress);
  156.             //GetXmlFromWeb();
  157.             //timer.Start();
  158.         }
  159.  
  160.         void Program_ResponseReceived(HttpRequest sender, HttpResponse response)
  161.         {
  162.             string elementHolder;
  163.  
  164.             XmlReader reader = XmlReader.Create(response.Stream);
  165.  
  166.             while (reader.Read())
  167.             {
  168.                 switch (reader.NodeType)
  169.                 {
  170.                     case XmlNodeType.Element:
  171.                         //Debug.Print("Element: " + reader.Name);
  172.                        
  173.                         if (reader.Name.Equals("simpleforecast"))
  174.                         {
  175.                             reader.ReadToFollowing("forecastday");
  176.                             reader.ReadToFollowing("period");
  177.                             if (reader.ReadElementString().Equals("1"))
  178.                             {
  179.                                 reader.ReadToFollowing("conditions");
  180.                                 Debug.Print("Today's forecast is: " + reader.ReadElementString());
  181.                             }
  182.                            
  183.                             reader.ReadToFollowing("forecastday");
  184.                             reader.ReadToFollowing("period");
  185.                             if (reader.ReadElementString().Equals("2"))
  186.                             {
  187.                                 reader.ReadToFollowing("conditions");
  188.                                 Debug.Print("Tomorrow's forecast is: " + reader.ReadElementString());
  189.                             }
  190.                            
  191.                             Debug.Print("Goodbye");
  192.                         }
  193.                        
  194.                         break;
  195.  
  196.                     case XmlNodeType.EndElement:
  197.                         //Debug.Print("End of Element");
  198.                         break;
  199.  
  200.                     case XmlNodeType.Attribute:
  201.                          //Debug.Print("Attribute");
  202.                         break;
  203.  
  204.                     case XmlNodeType.Text:
  205.                          //Debug.Print("Text");
  206.                          break;
  207.  
  208.                 }
  209.  
  210.             }
  211.  
  212.             //Debug.Print(response.StatusCode);
  213.             Debug.Print("Resetting bump");
  214.             accelerometer.ResetThresholdDetection();
  215.            
  216.  
  217.         }
  218.  
  219.  
  220.  
  221.         // void button_ButtonPressed(Button sender, Button.ButtonState state)
  222.         // {
  223.         //     GetXmlFromWeb();
  224.         // }
  225.  
  226.  
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment