vincent wong
Published © GPL3+

LED Air Quality Meter

An air quality meter's light will change based on the air quality value detected. Air quality data will be uploaded to a cloud's dashboard.

EasyFull instructions provided500
LED Air Quality Meter

Things used in this project

Hardware components

Netduino3 WiFi
Wilderness Labs (Netduino) Netduino3 WiFi
×1
LED (generic)
LED (generic)
×1
Grove - Gas Sensor(MQ2)
Seeed Grove - Gas Sensor(MQ2)
×1

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Adafruit IO

Story

Read more

Schematics

Netduino Sensors Adafruit

Netduino sensors adafruit bb 0mksxnssmc

Code

Program.cs

C#
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Threading;

namespace AirQualityMeter
{
    public class Program
    {
        public static void Main()
        {
            App app = new App();

            const int lightThreshold = 95;
            const int gasThreshold = 120;

            // configure an output port for us to "write" to the LED
            OutputPort led = new OutputPort(Pins.GPIO_PIN_D13, false);

            // configure an input port for light sensor
            var light = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0);
            light.SetRange(0, 100);

            // configure an input port for gas sensor
            var gas = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A5);
            gas.SetRange(0, 200);

            int lightVal;
            int gasVal;

            int i = 0;
            while (true)
            {
                lightVal = light.Read();
                gasVal = gas.Read();

                if (!app.IsRunning) app.Run(gasVal, lightVal);

                if (lightVal > lightThreshold || gasVal > gasThreshold) {
                    led.Write(true); // turn on the LED 
                } else {
                    led.Write(false); // turn off the LED 
                }

                // Debug.Print("Looping " + i);
                Debug.Print("light value = " + lightVal);
                Debug.Print("gas value = " + gasVal);

                i++;

                Thread.Sleep(2000); // sleep for 2000ms 
            }
        }

    }

}

App.cs

C#
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Net;
using System.IO;
using System.Threading;
using System.Text;

namespace AirQualityMeter
{
    public class App
    {
        NetworkInterface[] _interfaces;


        public bool IsRunning { get; set; }

        public void Run()
        {
            this.IsRunning = true;
            bool goodToGo = InitializeNetwork();

            if (goodToGo)
            {
                MakeWebRequest("http://www.payex.io/");
            }

            this.IsRunning = false;
        }

        public void Run(int gasVal,  int lightVal)
        {
            this.IsRunning = true;
            bool goodToGo = InitializeNetwork();

            if (goodToGo)
            {
                MakeAdafruitIOWebRequest(gasVal, lightVal);
            }

            this.IsRunning = false;
        }


        protected bool InitializeNetwork()
        {
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
            {
                Debug.Print("Wireless tests run only on Device");
                return false;
            }

            Debug.Print("Getting all the network interfaces.");
            _interfaces = NetworkInterface.GetAllNetworkInterfaces();

            // debug output
            ListNetworkInterfaces();

            // loop through each network interface
            foreach (var net in _interfaces)
            {

                // debug out
                ListNetworkInfo(net);

                switch (net.NetworkInterfaceType)
                {
                    case (NetworkInterfaceType.Ethernet):
                        Debug.Print("Found Ethernet Interface");
                        break;
                    case (NetworkInterfaceType.Wireless80211):
                        Debug.Print("Found 802.11 WiFi Interface");
                        break;
                    case (NetworkInterfaceType.Unknown):
                        Debug.Print("Found Unknown Interface");
                        break;
                }

                // check for an IP address, try to get one if it's empty
                return CheckIPAddress(net);
            }

            // if we got here, should be false.
            return false;
        }

        protected void MakeWebRequest(string url)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "GET";

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Debug.Print("this is what we got from " + url + ": " + result);
            }
        }

        protected void MakeAdafruitIOWebRequest(int gasVal, int lightVal)
        {
            Debug.Print("now is " + DateTime.Now);

            var url = "https://io.adafruit.com/api/v2/wesee/groups/my-group/data";
            string data = "{\"feeds\": [ { \"key\": \"light\", \"value\": \"" + lightVal + "\" }, { \"key\": \"mq4\", \"value\": \"" + gasVal + "\" } ] }";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Set("X-AIO-Key", "X-AIO-Key");
            httpWebRequest.ContentType = "application/json";

            byte[] byteArray = Encoding.UTF8.GetBytes(data);
            httpWebRequest.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = httpWebRequest.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Debug.Print("this is what we got from " + url + ": " + result);
            }
        }

        protected bool CheckIPAddress(NetworkInterface net)
        {
            int timeout = 10000; // timeout, in milliseconds to wait for an IP. 10,000 = 10 seconds

            // check to see if the IP address is empty (0.0.0.0). IPAddress.Any is 0.0.0.0.
            if (net.IPAddress == IPAddress.Any.ToString())
            {
                Debug.Print("No IP Address");

                if (net.IsDhcpEnabled)
                {
                    Debug.Print("DHCP is enabled, attempting to get an IP Address");

                    // ask for an IP address from DHCP [note this is a static, not sure which network interface it would act on]
                    int sleepInterval = 10;
                    int maxIntervalCount = timeout / sleepInterval;
                    int count = 0;
                    while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any && count < maxIntervalCount)
                    {
                        Debug.Print("Sleep while obtaining an IP");
                        Thread.Sleep(10);
                        count++;
                    };

                    // if we got here, we either timed out or got an address, so let's find out.
                    if (net.IPAddress == IPAddress.Any.ToString())
                    {
                        Debug.Print("Failed to get an IP Address in the alotted time.");
                        return false;
                    }

                    Debug.Print("Got IP Address: " + net.IPAddress.ToString());
                    return true;

                    //NOTE: this does not work, even though it's on the actual network device. [shrug]
                    // try to renew the DHCP lease and get a new IP Address
                    //net.RenewDhcpLease ();
                    //while (net.IPAddress == "0.0.0.0") {
                    //	Thread.Sleep (10);
                    //}

                }
                else
                {
                    Debug.Print("DHCP is not enabled, and no IP address is configured, bailing out.");
                    return false;
                }
            }
            else
            {
                Debug.Print("Already had IP Address: " + net.IPAddress.ToString());
                return true;
            }

        }

        protected void ListNetworkInterfaces()
        {
            foreach (var net in _interfaces)
            {
                switch (net.NetworkInterfaceType)
                {
                    case (NetworkInterfaceType.Ethernet):
                        Debug.Print("Found Ethernet Interface");
                        break;
                    case (NetworkInterfaceType.Wireless80211):
                        Debug.Print("Found 802.11 WiFi Interface");
                        break;
                    case (NetworkInterfaceType.Unknown):
                        Debug.Print("Found Unknown Interface");
                        break;

                }
            }
        }

        protected void ListNetworkInfo(NetworkInterface net)
        {
            Debug.Print("MAC Address: " + BytesToHexString(net.PhysicalAddress));
            Debug.Print("DHCP enabled: " + net.IsDhcpEnabled.ToString());
            Debug.Print("Dynamic DNS enabled: " + net.IsDynamicDnsEnabled.ToString());
            Debug.Print("IP Address: " + net.IPAddress.ToString());
            Debug.Print("Subnet Mask: " + net.SubnetMask.ToString());
            Debug.Print("Gateway: " + net.GatewayAddress.ToString());

            if (net is Wireless80211)
            {
                var wifi = net as Wireless80211;
                Debug.Print("SSID:" + wifi.Ssid.ToString());
            }

        }

        private static string BytesToHexString(byte[] bytes)
        {
            string hexString = string.Empty;

            // Create a character array for hexidecimal conversion.
            const string hexChars = "0123456789ABCDEF";

            // Loop through the bytes.
            for (byte b = 0; b < bytes.Length; b++)
            {
                if (b > 0)
                    hexString += "-";

                // Grab the top 4 bits and append the hex equivalent to the return string.        
                hexString += hexChars[bytes[b] >> 4];

                // Mask off the upper 4 bits to get the rest of it.
                hexString += hexChars[bytes[b] & 0x0F];
            }

            return hexString;
        }

    }
}

namespace System.Diagnostics
{
    public enum DebuggerBrowsableState
    {
        Never = 0,
        Collapsed = 2,
        RootHidden = 3
    }
}

Credits

vincent wong

vincent wong

42 projects • 123 followers

Comments

Add projectSign up / Login