SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Threading; | |
| 3 | using System.Xml; | |
| 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 | ||
| 15 | namespace Gadgeteer_Servo_Weather_01 | |
| 16 | {
| |
| 17 | ||
| 18 | ||
| 19 | public partial class Program | |
| 20 | {
| |
| 21 | ||
| 22 | - | GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously); |
| 22 | + | GT.Timer timer = new GT.Timer(10000, GT.Timer.BehaviorType.RunContinuously); |
| 23 | GT.Interfaces.PWMOutput servo; | |
| 24 | GT.Interfaces.PWMOutput servo1; | |
| 25 | GT.Interfaces.PWMOutput servo2; | |
| 26 | GT.Interfaces.PWMOutput servo3; | |
| 27 | GT.Interfaces.PWMOutput servo4; | |
| 28 | GT.Interfaces.PWMOutput servo5; | |
| 29 | ||
| 30 | // This method is run when the mainboard is powered up or reset. | |
| 31 | void ProgramStarted() | |
| 32 | {
| |
| 33 | ||
| 34 | - | // jump to the StartWiFi method that starts the WiFi |
| 34 | + | |
| 35 | wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp); | |
| 36 | ||
| 37 | // jump to the StartWiFi method that starts the WiFi | |
| 38 | StartWiFi(); | |
| 39 | ||
| 40 | timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); | |
| 41 | - | extender.SetupPWMOutput(GT.Socket.Pin.Eight); |
| 41 | + | |
| 42 | - | extender.SetupPWMOutput(GT.Socket.Pin.Nine); |
| 42 | + | |
| 43 | - | extender1.SetupPWMOutput(GT.Socket.Pin.Seven); |
| 43 | + | |
| 44 | - | extender1.SetupPWMOutput(GT.Socket.Pin.Eight); |
| 44 | + | servo1 = extender.SetupPWMOutput(GT.Socket.Pin.Eight); |
| 45 | - | extender1.SetupPWMOutput(GT.Socket.Pin.Nine); |
| 45 | + | servo2 = extender.SetupPWMOutput(GT.Socket.Pin.Nine); |
| 46 | servo3 = extender1.SetupPWMOutput(GT.Socket.Pin.Seven); | |
| 47 | servo4 = extender1.SetupPWMOutput(GT.Socket.Pin.Eight); | |
| 48 | servo5 = extender1.SetupPWMOutput(GT.Socket.Pin.Nine); | |
| 49 | ||
| 50 | servo.Active = true; | |
| 51 | servo1.Active = true; | |
| 52 | servo2.Active = true; | |
| 53 | servo3.Active = true; | |
| 54 | servo4.Active = true; | |
| 55 | servo5.Active = true; | |
| 56 | ||
| 57 | // print something so we know we have started | |
| 58 | Debug.Print("Program Started");
| |
| 59 | } | |
| 60 | ||
| 61 | void timer_Tick(GT.Timer timer) | |
| 62 | {
| |
| 63 | GetXmlFromWeb(); | |
| 64 | } | |
| 65 | ||
| 66 | void StartWiFi() | |
| 67 | {
| |
| 68 | wifi.UseDHCP(); | |
| 69 | Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo(); | |
| 70 | info.SSID = "network_name"; | |
| 71 | info.SecMode = WiFi_RS21.SecurityMode.WEP; | |
| 72 | info.networkType = WiFi_RS21.NetworkType.AccessPoint; | |
| 73 | wifi.Join(info, "network_pass"); | |
| 74 | ||
| 75 | } | |
| 76 | ||
| 77 | void wifi_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) | |
| 78 | {
| |
| 79 | Debug.Print("network down: connecting....");
| |
| 80 | ||
| 81 | } | |
| 82 | ||
| 83 | void wifi_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) | |
| 84 | {
| |
| 85 | Debug.Print("network up");
| |
| 86 | Debug.Print(wifi.NetworkSettings.IPAddress); | |
| 87 | } | |
| 88 | ||
| 89 | void printServoDisplay(int lineNumber, uint displaySetting) | |
| 90 | {
| |
| 91 | //uint xPos = 0; | |
| 92 | //oledDisplay.SimpleGraphics.DisplayText(displayMessage, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, xPos, lineNumber*12); | |
| 93 | ||
| 94 | switch (lineNumber) | |
| 95 | {
| |
| 96 | case 0: | |
| 97 | servo.SetPulse(20 * 1000 * 1000, (displaySetting - 10) * 100 * 1000); | |
| 98 | break; | |
| 99 | case 1: | |
| 100 | servo1.SetPulse(20 * 1000 * 1000, (displaySetting - 10) * 100 * 1000); | |
| 101 | break; | |
| 102 | case 2: | |
| 103 | servo2.SetPulse(20 * 1000 * 1000, (displaySetting - 10) * 100 * 1000); | |
| 104 | goto case 1; | |
| 105 | case 3: | |
| 106 | servo3.SetPulse(20 * 1000 * 1000, (displaySetting - 10) * 100 * 1000); | |
| 107 | goto case 1; | |
| 108 | case 4: | |
| 109 | servo4.SetPulse(20 * 1000 * 1000, (displaySetting - 10) * 100 * 1000); | |
| 110 | goto case 1; | |
| 111 | case 5: | |
| 112 | servo5.SetPulse(20 * 1000 * 1000, (displaySetting - 10) * 100 * 1000); | |
| 113 | goto case 1; | |
| 114 | default: | |
| 115 | break; | |
| 116 | } | |
| 117 | ||
| 118 | } | |
| 119 | ||
| 120 | void GetXmlFromWeb() | |
| 121 | {
| |
| 122 | WebClient.GetFromWeb("http://www.google.com/ig/api?weather=02903").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
| |
| 123 | } | |
| 124 | ||
| 125 | void Program_ResponseReceived(HttpRequest sender, HttpResponse response) | |
| 126 | {
| |
| 127 | string elementHolder; | |
| 128 | ||
| 129 | XmlReader reader = XmlReader.Create(response.Stream); | |
| 130 | ||
| 131 | int lineNumberCount = 0; | |
| 132 | ||
| 133 | while (reader.Read()) | |
| 134 | {
| |
| 135 | ||
| 136 | switch (reader.NodeType) | |
| 137 | {
| |
| 138 | case XmlNodeType.Element: | |
| 139 | //Debug.Print("Element: " + reader.Name);
| |
| 140 | //if (reader.HasAttributes && reader.Name.Equals("current_conditions"))
| |
| 141 | if (reader.Name.Equals("current_conditions"))
| |
| 142 | {
| |
| 143 | reader.ReadToFollowing("temp_f");
| |
| 144 | reader.MoveToFirstAttribute(); | |
| 145 | Debug.Print("Today: " + reader.Value);
| |
| 146 | printServoDisplay(lineNumberCount, Convert.ToUInt32(reader.Value)); | |
| 147 | lineNumberCount++; | |
| 148 | } | |
| 149 | else if (reader.Name.Equals("forecast_conditions"))
| |
| 150 | {
| |
| 151 | reader.ReadToFollowing("day_of_week");
| |
| 152 | reader.MoveToFirstAttribute(); | |
| 153 | elementHolder = reader.Value; | |
| 154 | reader.ReadToFollowing("high");
| |
| 155 | reader.MoveToFirstAttribute(); | |
| 156 | Debug.Print(elementHolder + ": " + reader.Value); | |
| 157 | //lineNumberCount++; | |
| 158 | //printServoDisplay(lineNumberCount, elementHolder + ": "); | |
| 159 | printServoDisplay(lineNumberCount, Convert.ToUInt32(reader.Value)); | |
| 160 | lineNumberCount++; | |
| 161 | ||
| 162 | } | |
| 163 | break; | |
| 164 | ||
| 165 | case XmlNodeType.EndElement: | |
| 166 | //Debug.Print("End of Element");
| |
| 167 | break; | |
| 168 | } | |
| 169 | ||
| 170 | } | |
| 171 | ||
| 172 | //Debug.Print(response.StatusCode); | |
| 173 | ||
| 174 | } | |
| 175 | } | |
| 176 | } |