David Vodička
Published © CC BY

Intelligent garden

Automatically controlled irrigation of greenhouses.

BeginnerWork in progress6,130
Intelligent garden

Things used in this project

Story

Read more

Schematics

Garden_schem.png

Code

Helper for buffer comunication

C#
Encapsulates communication with I2CDevice and converts the data into C # data types.
using System;
using Windows.Devices.I2c;

namespace Examinos.Sensors
{
    public static class I2CExtension
    {
        public static UInt16 ReadUInt16(this I2cDevice device, byte addr)
        {
            byte[] data = new byte[2];

            device.WriteRead(new byte[] { addr }, data);

            return (UInt16)((data[0] << 8) | (data[1]));
        }

    }
}

Sensor HTU21 class

C#
Class THU21 for the sensor, which contains the necessary constants and calculations. It's a minimalist version of the code (for example, does not deal with error handling).
using System;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;

namespace Examinos.Sensors
{
    public class HTU21: IDisposable
    {
        #region constants
        private const ushort HTU21_I2C_ADDR = 0x0040;
        private const byte HTU21_I2C_CMD_TEMP = 0xE3;
        private const byte HTU21_I2C_CMD_HUMI = 0xE5;

        private const double HTU21_CoeffTemp = -0.15;
        #endregion constants

        private I2cConnectionSettings settings;
        private String id;
        private I2cDevice device;
        private UInt16 temp;
        private UInt16 humi;

        public HTU21(DeviceInformation deviceInformation)
        {
            settings = new I2cConnectionSettings(HTU21_I2C_ADDR);
            settings.BusSpeed = I2cBusSpeed.FastMode;
            settings.SharingMode = I2cSharingMode.Shared;
            id = deviceInformation.Id;
        }

        public void Dispose()
        {
            device?.Dispose();
        }

        public async Task Connect ()
        {
            device = await I2cDevice.FromIdAsync(id, settings);
        }

        public void Measure()
        {
            temp = device.ReadUInt16(HTU21_I2C_CMD_TEMP);
            humi = device.ReadUInt16(HTU21_I2C_CMD_HUMI);

            temp = (UInt16)(temp & 0xFFFC);
            humi = (UInt16)(humi & 0xFFFC);
        }

        public double Temp
        {
            get
            {
                return -46.85 + 175.72 * temp / 65536;
            }
        }

        public double Humi
        {
            get
            {
                double h = -6 + 125 * humi / 65536;
                return h + (25 - Temp) * HTU21_CoeffTemp;
            }
        }
        public double DevP
        {
            get
            {
                double p = Math.Pow(10, (8.1332 - (1762.39 / (Temp + 235.66))));
                return -((1762.39 / (Math.Log10(Humi * p / 100) - 8.1332)) + 235.66);
            }
        }

        public override string ToString()
        {
            return String.Format("Temp[°C] = {0:F2}; Humi[%] = {1:F2}; DevP[°C] = {2:F2}", Temp, Humi, DevP);
        }

    }
}

Main page xaml

C#
The visual portion of the main page.
<Page
    x:Class="Examinos.Garden.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Examinos.Garden"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" 
    
    Loaded="Page_Loaded" Unloaded="Page_Unloaded" 
    
    >

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        
        <StackPanel VerticalAlignment="Center">
            <TextBlock x:Name="greenhouseI" Text="greenhouse" FontSize="60" HorizontalAlignment="Center"/>
        </StackPanel>
        
    </Grid>
</Page>

Main page code

C#
The code of the main page.
using Examinos.Sensors;
using System;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Diagnostics;
using System.Threading;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Examinos.Garden
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private Timer timer;
        private HTU21 htu21;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            string aqs = I2cDevice.GetDeviceSelector();
            DeviceInformationCollection deviceInformation = await DeviceInformation.FindAllAsync(aqs);

            htu21 = new HTU21(deviceInformation[0]);
            await htu21.Connect();

            timer = new Timer(Tick, null, 0, 1000);
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            htu21?.Dispose();
        }

        private void Tick(object state)
        {
            htu21?.Measure();
            Debug.WriteLine(htu21);
            var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                greenhouseI.Text = htu21?.ToString();
            });
        }

    }
}

Credits

David Vodička

David Vodička

1 project • 10 followers
Raspberry, C#, sensors

Comments