peony3000

Gadgeteer Weather Crier XML 01 v 02

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