Dhairya Parikh
Published © LGPL

C# Smart IoT SwitchBoard with Netduino

A smart IoT switchboard that allows you to control the connected appliances from anywhere. An added functionality of temperature monitoring.

IntermediateFull instructions provided5 hours2,031
C# Smart IoT SwitchBoard with Netduino

Things used in this project

Hardware components

Relay Module (Generic)
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Breadboard (generic)
Breadboard (generic)
×1
USB-A to B Cable
USB-A to B Cable
×1
Android device
Android device
×1

Software apps and online services

Xamarin
Xamarin
Visual Studio 2015
Microsoft Visual Studio 2015

Story

Read more

Schematics

Schematics for relay connection

DHT11 Connections

Code

DHT11 Code

C#
Library for DHT11
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;


namespace Glovebox.Netduino.Drivers {
    public class DHT11 : DhtSensor{
        //private OutputPort m_op;
        //private OneWire m_ow;
        private OneWireBus.Device m_dev;


        public DHT11(Cpu.Pin data1, Cpu.Pin data2):base(data1,data2,Port.ResistorMode.Disabled)
        {
         //   m_op = new OutputPort(pin, false);
        }
                

        public float ConvertAndReadTemperature() {
            var data = 0L;

            // if reset finds no devices, just return 0
            if (m_ow.TouchReset() == 0)
                return 0;

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // tell the device to start temp conversion
            m_ow.WriteByte(Command.StartTemperatureConversion);

            // wait for as long as it takes to do the temp conversion,
            // data sheet says ~750ms
            while (m_ow.ReadByte() == 0)
                System.Threading.Util.Delay(1);

            // reset the bus
            m_ow.TouchReset();

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // read the data from the sensor
            m_ow.WriteByte(Command.ReadScratchPad);

            // read the two bytes of data
            data = m_ow.ReadByte(); // LSB
            data |= (ushort)(m_ow.ReadByte() << 8); // MSB

            // reset the bus, we don't want more data than that
            m_ow.TouchReset();

            // returns C
            // F would be:  (float)((1.80 * (data / 16.00)) + 32.00);
            return (float)data / 16f;
        }

        public void StartConversion() {
            // if reset finds no devices, just return 0
            if (m_ow.TouchReset() == 0)
                return;

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // tell the device to start temp conversion
            m_ow.WriteByte(Command.StartTemperatureConversion);
        }

        public float ReadTemperature() {
            var data = 0L;

            // reset the bus
            m_ow.TouchReset();

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // read the data from the sensor
            m_ow.WriteByte(Command.ReadScratchPad);

            // read the two bytes of data
            data = m_ow.ReadByte(); // LSB
            data |= (ushort)(m_ow.ReadByte() << 8); // MSB

            // reset the bus, we don't want more data than that
            m_ow.TouchReset();

            // returns C
            // F would be:  (float)((1.80 * (data / 16.00)) + 32.00);
            return (float)data / 16f;
        }

        public static float ToFahrenheit(float tempC) {
            return (9f / 5f) * tempC + 32f;
        }

        private void WriteBytes(byte[] data) {
            for (var i = 0; i < data.Length; i++)
                m_ow.WriteByte(data[i]);
        }

        private static class Command {
            public const byte SearchROM = 0xF0;
            public const byte ReadROM = 0x33;
            public const byte MatchROM = 0x55;
            public const byte SkipROM = 0xCC;
            public const byte AlarmSearch = 0xEC;
            public const byte StartTemperatureConversion = 0x44;
            public const byte ReadScratchPad = 0xBE;
            public const byte WriteScratchPad = 0x4E;
            public const byte CopySratchPad = 0x48;
            public const byte RecallEEPROM = 0xB8;
            public const byte ReadPowerSupply = 0xB4;
        }
    }
}

Git Repo for Netduino Samples

Credits

Dhairya Parikh

Dhairya Parikh

21 projects • 123 followers
Project Developer | IoT and Machine Learning Enthusiast | Open Source Enthusiast |

Comments