WIN10 IoT C # development 3-gpio Pin control Light Emitting diodes

Source: Internet
Author: User
Tags sendmsg

Original: Win10 IoT C # development 3-gpio Pin control Light Emitting diode

Windows IoT Core is one of Microsoft's key products for the internet of things, and unlike previous versions of Windows, it is designed for IoT devices, and hardware is not limited to the x86 architecture, but can also be run on an arm architecture.

In the previous article, we described in detail the process of installing the Raspberry IoT core system and setting up the development environment, and if you are not familiar with the installation process, you can refer to the WIN10 IoT C # development 1-raspberry Install the IoT system and build the development environment (HTTP/ www.cnblogs.com/cloudtech/p/5562120.html).

This time the operation of the pin to simulate the switch of traffic lights, the process is very simple. First red light up---red light 10 seconds after the yellow light on the yellow light, 3 seconds after the green light off-----green for 10 seconds after the turn off, and then start the next cycle. Each time the light is on, it pushes the current light message to the remote server.

Preparatory work:

Brush the Raspberry Pi 2 for the win IoT core system

Deploying a PC for the Visual Studio 2015 development environment

Red, yellow, green 3 colors of light-emitting diodes

220R Resistor

GPIO Expansion Board

IDC Line

DuPont Line

Bread Plate

1. Installing components

The Gpio expansion board is first installed on the breadboard and then connected to the Gpio interface of the Raspberry Pi 2 via the IDC cable.

The following start to connect the light-emitting diode, in order to ensure that the diode is not burned need to install a current-limiting resistor, we assume that three kinds of diode voltage drop is 1.8V, rated current 15mA, through the formula can be calculated 213.3ω= (5v-1.8v)/0.015a, so we choose 220R resistor.

The long end of the LED pin is connected with a 5V power supply, and a 220R resistor is installed in the middle. The red cathode is connected with the GPIO21, the yellow cathode is connected with the GPIO25, the green cathode is connected with the GPIO20.

2. Writing code

Opens Visual Studio 2015 to create an IoT project that encapsulates the control of the LEDs into a class called LED, which contains two main methods, the Open method illuminates the diode, and the Close method turns off the diode. The main process through the LED control class of red, yellow, green three instances to operate the light-emitting diode cycle switching, each time switching with httpclient to the remote server push messages.

Full code:

Namespace cloudtechiot2{//http://www.cnblogs.com/cloudtech public sealed class Startuptask:ibackgroundtask {public void Run (Ibackgroundtaskinstance taskinstance) {//Initialize pins var pins = new LED            [] {new LED (colors.red), New LED (Colors.yellow, 3), New LED (Colors.green, 10)};                Loop while (true) {//Operation led foreach (var pin in pins) {pin.                    Open (); Notifies the remote server of the switch sendmsg (PIN) of the LED.                    Color.tostring ()); Duration (seconds) Task.delay (Timespan.fromseconds (PIN). Interval)).                    Wait (); Pin.                Close (); }}}//Output private void sendmsg (string res) {//Print DEBUG.WR            Iteline (RES);            Push to server HttpClient HttpClient = new HttpClient (); Server program address and Port 192.168.1.5:8099 HttpclieNt. Getasync (The new Uri (string.        Format ("http://192.168.1.5:8099/{0}", res));     }}//led color public enum Colors:int {Red = $, Yellow = +, Green =//led Control class        public sealed class LED {//gpio private static Gpiocontroller _gpio;        Private Colors _color;            Color public Colors Color {get {return _color;        }} private Gpiopin _pin;            Pin public Gpiopin Pin {get {return _pin;        }} private ushort _interval;            Duration public ushort Interval {get {return _interval;            }}//Create PIN public LED (Colors led, ushort interval) {_color = LED; _pin = _gpio.            Openpin ((int) _color); _pin.            Setdrivemode (Gpiopindrivemode.output); _interval = InterVal            }//Initialize Gpio static LED () {_gpio = Gpiocontroller.getdefault ();            if (null = = _gpio) {throw new Exception ("Gpio initial failed"); }}//Open the LED public void open () {_pin.        Write (Gpiopinvalue.low); }//Turn off the LED public void close () {_pin.        Write (Gpiopinvalue.high); }    }}

3. Debugging code

Here we use Nodejs to write a program to simulate the server, listen to the 8099 port receiving the IoT device push message and print to console, start the program on the PC (ip:192.168.1.5) to start listening.

Switch on the Raspberry power supply and wait for the end of Win IoT Core to deploy the program we just wrote by using remote machine.

After the operation of the light-emitting diode in accordance with our process began to switch, red light on the red light 10 seconds after the turn off, yellow light on the yellow light 3 seconds after the green light on-off, and then start the next cycle.

At the same time each switchover, the server receives a handoff notification from the IoT device push.

To here through the Gpio control light-emitting two-level tube switching and push notification process is complete, if the code has optimized suggestions, welcome to leave a message or email me ([email protected]). You can also add my number to view the previous article.

Project Source GitHub Https://github.com/CloudTechx/CloudTechIot under the CloudTechIot2 directory.

WIN10 IoT C # development 4-uart serial Communication (http://www.cnblogs.com/cloudtech/p/5518306.html)

WIN10 IoT C # development 3-gpio Pin control Light Emitting diodes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.