DasFilisera Green House

Be kind to your plants! Learn about the green house environment using MKR1000 and sensors. Control soil humidity and air conditions!

DasFilisera Green House

Things used in this project

Hardware components

DHT22 Temperature Sensor
DHT22 Temperature Sensor
× 1
DS18B20
× 1
Soil temperature and humidity sensor FS200-SHT25
× 1
4-Channel Relay Module
× 1
2-Channel Relay Module
× 1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor – HC-SR04 (Generic)
× 1
Arduino MKR1000
Arduino MKR1000
× 1

Software apps and online services

DasData
Visual Studio 2015
Microsoft Visual Studio 2015

Story

Data and Interface

As long that we have an interface our device we could also consume this data and see it on the local network.

We’ve been using Visual Studio 2015 and .net to build up a Windows application that will help you visualize green house live conditions but also a bit of daily history or all sort of logs that you want to integrate.

.net Visual Studio Project
No preview (download only).
.net file that will read data from arduino and show interface
Imports System.Globalization
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Windows.Forms.DataVisualization.Charting
Imports Newtonsoft.Json

Public Class Form1

    Private _deviceIP As String = ""  'DEVICE IP 
    Private intVolumContainer, intUmidSol, intUmidSera, intTempAfara, intTempSera, intTempSol, intUmidex As String  ' VARIABILES
    Private strR1, strR2 As String   'RELAYS 

    Public Count As CountDown   'COUNTDOWN TICK 
    Private _dasResult As String = ""  'RESULTAT CITIRE SENZORI DEVICE

    Private _AKey As String = ""   'USER KEY DASDATA
    Private _DSKey As String = ""  'DATASET KEY DASDATA

    Private _myDas As New dasData.das
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Count = New CountDown(50, 0)   '5 SECONDS UPDATE 
        AddHandler Count.Tick, AddressOf Count_Tick
        AddHandler Count.TimesOut, AddressOf Times_up

        Try  'READ SETTINGS FROM .txt FILE
            Dim path As String = Directory.GetCurrentDirectory()
            Dim FILE_NAME As String = path & "\Settings.txt"
            Dim myText As String = "" ' 
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME, System.Text.Encoding.Default)
                Do While objReader.Peek() <> -1
                    myText &= objReader.ReadLine()
                Loop
                Dim _settingItems() As String = myText.Split(" ")
                _deviceIP = _settingItems(0)   'IP-ul DEVICE 
                _AKey = _settingItems(1).Replace("akey:", "")
                _DSKey = _settingItems(2).Replace("dskey:", "")
                cbxEnabled.Text = "Device:" & _deviceIP
                txtInfo.Text = "IP device: " & _deviceIP & vbNewLine & "AKey: " & _AKey & vbNewLine & "DSKey: " & _DSKey & vbNewLine
                strR1 = _settingItems(3).Replace("relay1:", "")
                txtInfo.Text += "Relay1: " & strR1 & vbNewLine
                strR2 = _settingItems(4).Replace("relay2:", "")
                txtInfo.Text += "Relay2: " & strR2 & vbNewLine
                '   txtInfo.Text = "Settings: " & myText
                objReader.Dispose()
                objReader.Close()
            End If
        Catch ex As Exception
            lblDevice.Text = ex.Message.ToString
        End Try

        cmdCheckDevice()

        'LOCAL SETTINGS  
        Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
        'Thread.CurrentThread.CurrentUICulture = New CultureInfo("ro-RO")  
        cmdStartFilisera()

    End Sub

    Private Sub Count_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        cmdGetSensorValues()
        cmdTriggers()
        If CDate(dtFrom.Text).ToString("dd.MM.yyyy") = Date.Now().ToString("dd.MM.yyyy") Then
            cmdStartFilisera()
        End If


    End Sub


    Private Sub Times_up(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' Count.Reset()
        'Count.Pause() 
    End Sub

    Private Sub cmdStartFilisera()
        Try
            dtFrom.MaxDate = DateTime.Now

            _dasResult = _myDas.getDas(_DSKey, "json", 0, 100)
            cmdFillData()
        Catch ex As Exception
            lblDevice.Text += ex.Message.ToString
        End Try

    End Sub



    Private Sub cbxEnabled_CheckedChanged(sender As Object, e As EventArgs) Handles cbxEnabled.CheckedChanged
        cmdCheckDevice()
    End Sub

    Private Sub cmdCheckDevice()
        Try
            If cbxDemo.Checked = False Then
                lblDevice.Text = ("Enable device!")
                lblEvenimente.Text = Date.Now.ToString("dd.MM.yyyy HH:mm:ss")
                If cbxEnabled.Checked = True Then
                    Dim pingsender As New Net.NetworkInformation.Ping
                    If Not pingsender.Send(_deviceIP).Status = Net.NetworkInformation.IPStatus.Success Then
                        lblDevice.Text = ("Device not availble on the local network!")
                        cbxEnabled.Checked = False
                        btnTemperatura.Enabled = False
                        btnVolumContainer.Enabled = False
                        Count.Reset()
                        Count.Pause()
                        MessageBox.Show("Device at " & _deviceIP & " not found!")
                    Else
                        lblDevice.Text = ("Device connected!")
                        btnTemperatura.Enabled = True
                        btnVolumContainer.Enabled = True
                        cmdGetSensorValues()
                        Timer1.Enabled = True
                        cmdAddData()
                        Count.Start()
                    End If
                Else
                End If
            Else
                btnTemperatura.Enabled = True
                btnVolumContainer.Enabled = True
                cmdGetSensorValues()
                Timer1.Enabled = True
                cmdAddData()
                Count.Start()
            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnVolumContainer_Click(sender As Object, e As EventArgs) Handles btnVolumContainer.Click
        '  http://192.168.0.102/digital/7/1
        'CLOSE WATER FROM BUTTON 
        Dim _ItemsR1() As String = strR1.Split(",")
        If btnVolumContainer.Text.Contains("Open") Then
            cmdRelay(_ItemsR1(0), 1)
            cmdRelay(_ItemsR1(1), 1)
            cmdRelay(_ItemsR1(2), 0)
            cmdRelay(_ItemsR1(3), 0)
            btnVolumContainer.Text = "Close water"
        Else
            cmdRelay(_ItemsR1(0), 0)
            cmdRelay(_ItemsR1(1), 0)
            cmdRelay(_ItemsR1(2), 1)
            cmdRelay(_ItemsR1(3), 1)
            btnVolumContainer.Text = "Open water"
        End If
        '    Dim _url As String = _deviceIP & "digital/7/1"
    End Sub

    Private Sub btnTemperatura_Click(sender As Object, e As EventArgs) Handles btnTemperatura.Click
        'CLOSE WINDOW FROM BUTTON 
        Dim _ItemsR2() As String = strR2.Split(",")
        If btnTemperatura.Text.Contains("Open") Then
            cmdRelay(_ItemsR2(0), 1)
            cmdRelay(_ItemsR2(1), 1)
            btnTemperatura.Text = "Close window"
        Else
            cmdRelay(_ItemsR2(0), 0)
            cmdRelay(_ItemsR2(1), 0)
            btnTemperatura.Text = "Open window"
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        cmdAddData()
    End Sub



    Private Sub btnTestCloud_Click(sender As Object, e As EventArgs) Handles btnTestCloud.Click
        Try
            cmdStartFilisera()
        Catch ex As Exception
            MessageBox.Show("Lost connection with DasData!")
        End Try
    End Sub

    Private Function cmdRelay(ByVal _DPin As String, ByVal _cmd As Int16)
        Try
            Dim _urlCommand As String = _deviceIP & "/digital/" & _DPin & "/" & _cmd
            Dim client As New WebClient()
            client.Headers.Add("user-agent", "Arduino/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
            Dim data As Stream = client.OpenRead(_urlCommand)
            Dim reader As New StreamReader(data)
            Dim s As String = reader.ReadToEnd()
            '  txtInfo.Text += (s)
            data.Close()
            reader.Close()
        Catch ex As Exception
        End Try
    End Function

    Private Sub dtFrom_ValueChanged(sender As Object, e As EventArgs) Handles dtFrom.ValueChanged
        Try 'GET VALUES BASED ON DATE SELECTION
            _dasResult = _myDas.getDas(_DSKey, "jsond", CDate(dtFrom.Text).ToString("dd.MM.yyyy"), CDate(dtFrom.Text).ToString("dd.MM.yyyy"))
            cmdFillData()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        End Try
    End Sub

    Private Sub cmdGetSensorValues()
        Try
            ''GET SENSOR VALUES FROM DEVICE 
            If cbxDemo.Checked = False Then
                Dim client As New WebClient()
                client.Headers.Add("user-agent", "Arduino/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")

                Dim data As Stream = client.OpenRead("http://" & _deviceIP & "/index.html")
                Dim reader As New StreamReader(data)
                Dim s As String = reader.ReadToEnd()
                s = s.Replace("""", "").Replace("}", "").Replace("{", "").Replace(" ", "").Replace("variables:", "")
                Dim sItems() As String = s.Split(",")

                intVolumContainer = cmdGetVal(sItems(0))
                intUmidSol = cmdGetVal(sItems(1))
                intUmidSera = cmdGetVal(sItems(2))
                intTempSera = cmdGetVal(sItems(3))
                intTempAfara = cmdGetVal(sItems(4))
                intTempSol = cmdGetVal(sItems(5))

                data.Close()
                reader.Close()
            Else
                intVolumContainer = _rnd(60)
                intUmidSol = _rnd(80)
                intUmidSera = _rnd(20)
                intTempSera = _rnd(45)
                intTempAfara = _rnd(57)
                intTempSol = _rnd(66)
            End If

            ggVolum.Value = intVolumContainer
            rgVolum.Value = intVolumContainer

            ggUmidSol.Value = intUmidSol
            ggUmidSera.Value = intUmidSera
            ggTempSera.Value = intTempSera
            ggTempOut.Value = intTempAfara
            ggTempSol.Value = intTempSol
        Catch ex As Exception
            '{"variables" {"Volum_container": 0, "Umiditate_sol":  0.00, "Umiditate_aer": 36.60, "Temp_in": 26.50, "Temp_out":  0.00, "Temp_sol":  0.00, "Umidex_in": 26.37}, "id": "1", "name": "FiliSera", "hardware": "arduino", "connected": true}
        End Try

    End Sub

    Private Function cmdGetVal(ByVal _sItem As String)
        Try
            'SPLIT STRING AND GET VALUES BACK
            Dim _s() As String = _sItem.Split(":")
            Return _s(1)
        Catch ex As Exception

        End Try
    End Function

    Private Sub cmdAddData()
        Try
            'SEND DATA TO DASDATA CLOUD STORAGE
            Dim _strFiliseraDataSend As String = intVolumContainer & "|" & intUmidSol & "|" & intUmidSera & "|" & intTempAfara & "|" & intTempSera & "|" & intTempSol '& "|"&  intUmidex   & "|"& 
            Dim _dasResult As String = _myDas.sendDas(_strFiliseraDataSend, _DSKey, _AKey)
        Catch ex As Exception
            cmdLogMe("ERROR..." & ex.Message.ToString(), "error")
        End Try
    End Sub

    Private Sub cmdFillData()
        Try
            'FILL INTERFACE WILL ALL SORT OF DATA 
            Dim dataSet As DataSet = JsonConvert.DeserializeObject(Of DataSet)(_dasResult)
            Dim dataTable As DataTable = dataSet.Tables(0)
            'GRIDVIEW 
            DataGridView1.DataSource = dataSet.Tables(0)
            DataGridView1.AutoGenerateColumns = True

            'CHARTS
            ChartTemp.DataSource = dataTable
            ChartTemp.Series.Clear()

            chartUmid.DataSource = dataTable
            chartUmid.Series.Clear()

            chartVolum.DataSource = dataTable
            chartVolum.Series.Clear()

            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim k As Integer = 0
            For Each table In dataSet.Tables
                For Each row In table.Rows
                    For Each col In table.Columns
                        Dim colName As String = col.ColumnName.ToString
                        If colName <> "id" And colName <> "AIDate" Then
                            Try
                                Dim serieName As String = RTrim(colName)
                                'VOLUME 
                                If colName.Contains("Nivel") Then
                                    chartVolum.Series.Add(serieName)
                                    chartVolum.Series(serieName).LabelFormat = "N1"
                                    chartVolum.Series(i).ChartType = SeriesChartType.Line
                                    chartVolum.Series(i).BorderWidth = 2
                                    chartVolum.Series(i).Color = Color.Aqua
                                    chartVolum.Series(i).BorderDashStyle = ChartDashStyle.Solid
                                    chartVolum.Series(serieName).YValueMembers = RTrim(colName)
                                    chartVolum.Series(serieName).XValueMember = "AIDate"
                                    i += 1
                                End If
                                'HUMIDITY
                                If colName.Contains("Umid") Then
                                    chartUmid.Series.Add(serieName)
                                    chartUmid.Series(serieName).LabelFormat = "N1"
                                    chartUmid.Series(j).ChartType = SeriesChartType.Line
                                    chartUmid.Series(j).BorderWidth = 2
                                    chartUmid.Series(j).BorderDashStyle = ChartDashStyle.Solid
                                    chartUmid.Series(serieName).YValueMembers = RTrim(colName)
                                    chartUmid.Series(serieName).XValueMember = "AIDate"
                                    j += 1
                                End If
                                'TEMP
                                If colName.Contains("Temp") Then
                                    ChartTemp.Series.Add(serieName)
                                    ChartTemp.Series(serieName).LabelFormat = "N1"
                                    ChartTemp.Series(k).ChartType = SeriesChartType.Line
                                    ChartTemp.Series(k).BorderWidth = 2
                                    ChartTemp.Series(k).BorderDashStyle = ChartDashStyle.Solid
                                    ChartTemp.Series(serieName).YValueMembers = RTrim(colName)
                                    ChartTemp.Series(serieName).XValueMember = "AIDate"
                                    k += 1
                                End If

                            Catch ex As Exception
                            End Try
                        End If
                    Next
                    Exit For
                Next
            Next
            'FILL THE CHARTS
            ChartTemp.DataBind()
            ChartTemp.Visible = True

            chartUmid.DataBind()
            chartUmid.Visible = True

            chartVolum.DataBind()
            chartVolum.Visible = True

        Catch ex As Exception
            cmdLogMe("ERROR..." & ex.Message.ToString(), "error")
        End Try

    End Sub


    Private Sub cmdTriggers()
        'SET TRIGGERS AND STORE THE LOG
        Select Case intVolumContainer
            Case > 80
                cmdLogMe("HIGH HIGH - Container volume - stop water", "0")
            Case > 70
              '  cmdLogMe("HIGH - Container volume - water will start", "1")
            Case < 20
              '  cmdLogMe("LOW - Container volume low - water will stop", "1")
            Case < 10
                cmdLogMe("LOW LOW - Container volume high high high - start water", "0")
        End Select

        Select Case intTempSera
            Case > 38
                cmdLogMe("HIGH HIGH - Temperature in green house - open windows", "0")
            Case > 30
               ' cmdLogMe("HIGH - Temperature in green house - prepare windows", "1")
            Case < 10
               ' cmdLogMe("LOW - Temperature in green house - prepare windows", "1")
            Case < 10
                cmdLogMe("LOW LOW - Container volume high high high - start water", "0")
        End Select

    End Sub


    Public Function _rnd(ByVal n As Integer) As Integer
        'initialize random number generator
        Dim r As New Random(System.DateTime.Now.Millisecond)
        Return r.Next(1, n)
    End Function



    Private Sub cmdLogMe(message As String, _type As String)
        Try
            Dim _strLog As String = "adeewdf4-4aa34-14ass24-8436-aea1"  'LOG DASDATA DS KEY 
            lstEvenimente.Items.Add((System.DateTime.Now & " " & message) + Constants.vbNewLine)

            'STORE LOGS TO DASDATA
            _myDas.sendDas(message & "|" & _type, _strLog, _AKey)
        Catch ex As Exception
            cmdLogMe("ERROR..." & ex.Message.ToString(), "error")
        End Try

    End Sub


End Class

 


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top