Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

David Weaver's Content

There have been 24 items by David Weaver (Search limited from 24-September 19)


By content type

See this member's

Sort by                Order  

#64588 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:52 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64589 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:52 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64585 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:49 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64590 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:52 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64584 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:49 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64586 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:49 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64587 SMTP Client

Posted by David Weaver on 01 December 2015 - 04:52 PM in Project Showcase

Good Morning,

 

I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail.  http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.

 

Sub Main()
 
        'Set the properties
        smtp.Properties.Host = "mail.gmx.com"
        smtp.Properties.Port = 587
        smtp.Properties.UserName = "YourEmailAddress@gmx.us"
        smtp.Properties.UserPassword = "YourPasswrod"
        smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
        smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
        smtp.Properties.Subject = "Netduino sent this message"
        smtp.Properties.Body = "Hello world my IP address is "
 
        'Send the message
        Dim Response As String = smtp.Send
 
        If Response <> "250" Then
            Debug.Print(Response)
        Else
            Debug.Print("Success!")
            Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
        End If
 
 
    End Sub
 
'Put the code below in a new class object
 
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
 
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky 
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
 
 
Public Class smtp
 
    Public Structure Properties
        Public Shared SenderAddress As String
        Public Shared RecipientAddress As String
        Public Shared UserName As String
        Public Shared UserPassword As String
        Public Shared Subject As String
        Public Shared Body As String
        Public Shared Port As Integer = 25
        Public Shared Host As String
        Public Shared IPAddress As String
    End Structure
 
    Private Shared Socket As Socket
 
    Public Shared Function Send() As String
 
        Dim Response As String = String.Empty
        Dim ReturnCode As String = String.Empty
 
        Try
 
            Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
 
            ' Create socket and connect to the server's IP address and port
            Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
 
            'Connect
            Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
 
            'Get the Mail Servers response
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "220" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("First Response should be 220: " & Response)
 
            '******Authentication
            SendCommand("EHLO localhost")
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            If ReturnCode <> "250" Then
                Socket.Close()
                Socket = Nothing
                Return Response
            End If
 
            Debug.Print("Authentication Response should be 250: " & Response)
 
            'Get outside IP address
            If InString(Response, "[") Then
 
                Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
                Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
                Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
 
            End If
 
            'Login
            If InString(Response, "LOGIN") Then
 
                SendCommand("AUTH LOGIN")
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
 
                Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "334" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response User Name should be 334: " & Response)
 
                data = Encoding.UTF8.GetBytes(Properties.UserPassword)
                SendCommand(System.Convert.ToBase64String(data))
                Response = GetResponse()
                ReturnCode = Response.Substring(0, 3)
                If ReturnCode <> "235" Then
                    Socket.Close()
                    Socket = Nothing
                    Return Response
                End If
 
                Debug.Print("Authentication Response for User Password should be 235: " & Response)
 
            Else
 
                'Plain
                
                If InString(Response, "PLAIN") Then
                    Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
                    Dim userLen As Integer = userBytes.Length
 
                    Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
                    Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
 
                    login(0) = 0
                    login(userLen) = 0
                    Array.Copy(userBytes, 0, login, 1, userLen)
                    Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
                    SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
 
                    Response = GetResponse()
                    ReturnCode = Response.Substring(0, 3)
                    If ReturnCode <> "235" Then
                        Socket.Close()
                        Socket = Nothing
                        Return Response
                    End If
 
                    Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
 
                End If
 
            End If
 
            SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail from Response should be 250 " & Response)
 
            'To
            SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
            Response = GetResponse()
            Debug.Print("Mail To Response should be 250 or 251 " & Response)
 
            'Make the message
            Dim message As String = "Subject: " + Properties.Subject & vbCrLf
            message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
            message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
            Dim now As DateTime = DateTime.Now
            message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
            message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
            message += Properties.Body & Properties.IPAddress
 
            'send the message
            SendCommand("DATA") '
            Response = GetResponse()
            Debug.Print("Response to DATA should be 354 " & Response)
            SendCommand(message & vbCrLf & "." & vbCrLf)
            Response = GetResponse()
            ReturnCode = Response.Substring(0, 3)
            Debug.Print("Response to send the message should be 250 " & Response)
 
            Socket.Close()
 
            Socket = Nothing
 
 
        Catch ex As Exception
            Socket.Close()
            Socket = Nothing
            Debug.Print("Error in smtp Send: " & ex.ToString)
            Return "Error in smtp Send: " & ex.ToString
        End Try
 
        Return ReturnCode
    End Function
 
    Private Shared Sub SendCommand(command As String)
        
        If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
            command += vbCrLf
        End If
 
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
 
        Socket.Send(buffer)
    End Sub
 
    Private Shared Function GetResponse() As String
        
        Dim buffer As Byte() = New Byte(511) {}
        Dim Response As String = String.Empty
 
 
        While Socket.Poll(15000000, SelectMode.SelectRead)
            Dim bytesRead As Integer = Socket.Receive(buffer)
            Dim c As Char() = Encoding.UTF8.GetChars(buffer)
 
            For i As Integer = 0 To c.Length - 1
                Response += c(i)
            Next
 
            If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
 
                Exit While
            End If
 
        End While
 
        Return Response
    End Function
    Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
 
        Try
 
            If String1 = String.Empty Then Return False
            If StringToFind = String.Empty Then Return False
 
            String1 = String1.ToUpper
            StringToFind = StringToFind.ToUpper
 
            If String1.IndexOf(StringToFind) = -1 Then
                Return False
            Else
                Return True
 
            End If
 
        Catch
            Return False
        End Try
 
    End Function
 
End Class
 
 

 




#64556 netduino Servo controller

Posted by David Weaver on 24 November 2015 - 01:13 PM in Netduino 3

Check my post http://forums.netdui.../11567-beerbot/ 

 

The servo code may be helpful.




#66060 Netduino Plus / Windows 10 / VS2015

Posted by David Weaver on 22 October 2016 - 01:20 PM in General Discussion

Brian,

 

This sounds like a nightmare.  I am not an expert but Ill try to get you up and running.

 

First I thank 4.5 is a Windows .net version not Micro Frameworks?

 

I have heard Visual Studio 2015 is not ready for prime time try using VS2013 express.

 

The latest stable Micro Framework version is 4.3. But my experience has shown 4.2 works just as well for most applications. 

 

Start you application as a .net framework console application. Then select the libraries from the directories where they exist.

 

C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3

C:\Program Files (x86)\Secret Labs\Netduino SDK\Assemblies\v4.3

 

For 4.2 use these directories:

 

C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2

C:\Program Files (x86)\Secret Labs\Netduino SDK\Assemblies\v4.2

 

I hope this helps,

 

David Weaver




#64633 Netduino dead?

Posted by David Weaver on 10 December 2015 - 01:09 PM in General Discussion

The new Netduino 3 wi-fi is a great product!  I hope this is a temporary respite. 




#64683 Netduino 3 WiFi - no WiFi connection, no IP?

Posted by David Weaver on 28 December 2015 - 12:18 PM in Netduino 3

I am glad you got it working.

 

I had a hard time getting mine to work also I couldn't remember why its been a few months.

 

The other issue I had was before I started using the connection I needed about a 3 second delay or I got an error.




#64677 Netduino 3 WiFi - no WiFi connection, no IP?

Posted by David Weaver on 27 December 2015 - 01:07 PM in Netduino 3

This could be the issue? Here is my code in vb it requires a reference to NetduinoExtentions.dll.

 

'Required a reference' to Secet Labs "NetduinoExtentions.dll"
 
        While Not System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable
            Thread.Sleep(100)
        End While
 
        Debug.Print("IP Address: " & IPAddress.GetDefaultLocalAddress.ToString)
 
 
Hope this helps



#64558 Missing System.Collections.BitArray

Posted by David Weaver on 24 November 2015 - 02:00 PM in General Discussion

Thank you for the help. This would be much better if vb had bit shifters "<<" and ">>" they are also missing. I could use the code above with this

 

http://forums.netdui...ors-to-a-vbnet/

 

Thanks again I hope the folks at MS are reading this forum and put the bit shifters back in the vb operating system.




#64553 Missing System.Collections.BitArray

Posted by David Weaver on 22 November 2015 - 04:05 PM in General Discussion

The System.Collections.BitArray function seems to be missing in micro framework 4.3 vb. Has anyone had this problem and can it be replaced?




#64562 Missing System.Collections.BitArray

Posted by David Weaver on 24 November 2015 - 02:15 PM in General Discussion

Thanks again for the help




#64555 Missing System.Collections.BitArray

Posted by David Weaver on 24 November 2015 - 12:36 PM in General Discussion

The System.Collections.BitArray is in the micosoft support help pages. I just checked and it is not in C# either. I am working on a project that the hex byte returned from the device has information in the bits I need to read. 

 

Example: bits returned in hex "00100000" the third bit is ON.

 

Here is the solution I came up with I know there has to be a better way but this works. To use it pass the hex value as a string and parse the 

returned bit string to find if it is a 1 or 0. I hope this is helps someone. I have posted in VB and C#.

 

 

 

Private Shared Function HexStringToBinaryString(hex As String) As String

        Try
 
            Dim str As String = String.Empty
            Dim c As String = String.Empty
 
            For i = 0 To hex.Length - 1
                c = hex.Substring(i, 1)
                Select Case c.ToLower
                    Case "0"
                        str += "0000"
                    Case "1"
                        str += "0001"
                    Case "2"
                        str += "0010"
                    Case "3"
                        str += "0011"
                    Case "4"
                        str += "0100"
                    Case "5"
                        str += "0101"
                    Case "6"
                        str += "0110"
                    Case "7"
                        str += "0111"
                    Case "8"
                        str += "1000"
                    Case "9"
                        str += "1001"
                    Case "a"
                        str += "1010"
                    Case "b"
                        str += "1011"
                    Case "c"
                        str += "1100"
                    Case "d"
                        str += "1101"
                    Case "e"
                        str += "1110"
                    Case "f"
                        str += "1111"
 
                End Select
 
            Next
            Return str
 
        Catch ex As Exception
            Return "00000000"
        End Try
    End Function
 
 
The C# version
 
private static string HexStringToBinaryString(string hex)
{
 
try {
string str = string.Empty;
string c = string.Empty;
 
for (i = 0; i <= hex.Length - 1; i++) {
c = hex.Substring(i, 1);
switch (c.ToLower) {
case "0":
str += "0000";
break;
case "1":
str += "0001";
break;
case "2":
str += "0010";
break;
case "3":
str += "0011";
break;
case "4":
str += "0100";
break;
case "5":
str += "0101";
break;
case "6":
str += "0110";
break;
case "7":
str += "0111";
break;
case "8":
str += "1000";
break;
case "9":
str += "1001";
break;
case "a":
str += "1010";
break;
case "b":
str += "1011";
break;
case "c":
str += "1100";
break;
case "d":
str += "1101";
break;
case "e":
str += "1110";
break;
case "f":
str += "1111";
 
break;
}
 
}
return str;
 
} catch (Exception ex) {
return "00000000";
}
}
 



#64616 Issue with Create Directory

Posted by David Weaver on 05 December 2015 - 10:01 PM in General Discussion

Hello kiwBryn, (I won't pretend to even know what day it is :) )

 

This is the code I am using may be over kill be it seems to work every time?

 

If Directory.Exists("\SD\Scenes") = False Then

            Debug.Print("Creating Scenes")
            Directory.CreateDirectory("\SD\Scenes")
            Thread.Sleep(10)
            Dim df() As String = Directory.GetDirectories("\SD\")
            Dim d As Object = df.SyncRoot
            Thread.Sleep(10)
            'Need reference to microsoft.spot.io.dll
            Dim vf As New Microsoft.SPOT.IO.VolumeInfo("\SD")
            vf.FlushAll()
            Thread.Sleep(10)
        End If
 
You have a much bettter understanding of the operating system. What I would like to do is access the MFDeploy engine to write a simple Visual Studio 2005 vb program to set the Wi-Fi variables for Netduino 3 Wi-Fi. The project I am working on requires MFDeploy. MFDeploy has more steps than my users may be able to complete successfully. 
 
Thanks again for your help



#64611 Issue with Create Directory

Posted by David Weaver on 05 December 2015 - 12:19 PM in General Discussion

Good morning kiwBryn or afternoon New Zealand is a long way from Illinios,

 

After adding a reference to Microsoft.SPOT.IO the code below works in vb version 4.3 using the Netduino 3.

 

 Dim vf As New Microsoft.SPOT.IO.VolumeInfo("\SD")
 vf.FlushAll()
 
The call seems to be similar to the  syncroot above. Do you know if one is better to use than the other?

 

Thanks for the help



#64607 Issue with Create Directory

Posted by David Weaver on 04 December 2015 - 08:26 PM in General Discussion

Bug in Directory.CreateDirectory? 
 
When I use the function and then remove the SD card and put it in my computer the directory is not created.
 
If I make a call to the function syncroot the new directory is created and saved to the SD card.
 
The code below seems to work.
 
Directory.CreateDirectory("\SD\Temp)
 
 
Dim df() As String = Directory.GetDirectories("\SD\")
 
 
Dim d As Object = CObj(df.SyncRoot)
         For Each e In d
        Debug.Print(e.ToString)
        Next
 
Making the call to syncRoot saves the directory? Anyone else have this problem?
 



#64561 I2C strange behavior

Posted by David Weaver on 24 November 2015 - 02:13 PM in Netduino Plus 2 (and Netduino Plus 1)

Here is the link to the 12c source code from stefan's Toolbox.

 

http://netmftoolbox....ore/MultiI2C.cs




#64559 I2C strange behavior

Posted by David Weaver on 24 November 2015 - 02:08 PM in Netduino Plus 2 (and Netduino Plus 1)

This may help,

 

http://netmftoolbox....017/Mcp23017.cs

 

Stefan Thoolen did a great job with the Toolbox.




#64719 Gadgeteer Load Module

Posted by David Weaver on 06 January 2016 - 07:20 PM in General Discussion

I would like to use the Gadgeteer Load module https://www.ghielect...log/product/364with my Netduino 3. Has anyone tried the Load module with Netduino? 

 

Thanks for any ideas




#64684 Deploy with custom software (without VS)

Posted by David Weaver on 28 December 2015 - 12:26 PM in Visual Studio

Could try this

 

https://fabienroyer....ith-a-netduino/




#65648 Configure Wi-fi

Posted by David Weaver on 23 August 2016 - 05:18 PM in General Discussion

Has anyone found an easy way for users to configure Wi-fi using the Netduino 3 Wi-fi. 
The only way I am aware of is to use MFDeploy.exe. 
I have a commercial product in mind but having the user configure Wi-fi
using MFDeploy.exe will not work in my opinion. Anyone have any ideas?
 




home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.