Show Posts
Pages: [1]
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Arduino, PC, Ethernet, and VB on: December 16, 2012, 05:30:59 pm
Thank you everyone for your help and posts. I have finally got it working!! PaulS, thank you for your initial post. I took a better look at my code and compared it closer to a working version of the HTML working code and found that, as you said, I was never reading the client. As for other ways of doing the same thing I am wanting a momentary button so that on my PC I would click a button and while I am holding the mouse button down the light would be on and once I let go the light goes off until I click and hold the button again. I have implemented this using the keyboard but thought it would also be nice to do it in VB code as well using the mouse but haven't figured it out just yet. Also thank you to rbright for the link. I can definitely see some uses for that interface and program in the very near future. My working code is below. It has some extra items in it that I got from another example but I think I can modify it to make what I want.

Arduino Code:
Code:
//*************************************************************************************************************************************
// Declarations
//*************************************************************************************************************************************
#include <SPI.h>
#include <Ethernet.h>

//*************************************************************************************************************************************
// Wired configuration parameters
//*************************************************************************************************************************************
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xD1, 0x4B };
unsigned char local_ip[] = {192,168,1,100}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network

EthernetServer server(80);

//char buffer[20];
String buffer = "";

void setup()
{
  pinMode(8,OUTPUT);
  Ethernet.begin(mac, local_ip);
  Serial.begin(9600);
}

void loop()
{
  EthernetClient client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true; 
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        Serial.print(c);   
        buffer+=c;
       
        int val;
        if (c == 'O')
        {
          digitalWrite(8,HIGH);
        }
        else if (c == 'F')
        {
          digitalWrite(8,LOW);
        }
      }
    }
  }
}

VB Code:
Code:
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    Dim networkStream As NetworkStream
    Dim KeyPressed As Integer

    Private Function Arduino_Connect(ByVal IP As String, ByVal Port As Integer) As Boolean
        tcpClient.Connect(IP, Port)
        networkStream = tcpClient.GetStream()
        If Not networkStream.CanWrite Or Not networkStream.CanRead Then
            tcpClient.Close()
            networkStream = Nothing
            Return False
        End If
        Return True
    End Function

    Private Sub Arduino_Write(ByVal Output As String)
        If Not IsNothing(networkStream) Then
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(Output)
            Dim endByte As [Byte]() = {&HFE}
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            networkStream.Write(endByte, 0, 1)
        Else
            MsgBox("ERROR")
        End If
    End Sub

    Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Arduino_Connect("192.168.1.100", 80)

        picOff.Visible = True
        picOn.Visible = False
    End Sub
    Private Sub Arduino_Disconnect()
        If Not IsNothing(networkStream) Then
            tcpClient.Close()
            networkStream = Nothing
        End If
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOff.Visible = False
        picOn.Visible = True
        Arduino_Write("O")
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.CLick
        picOff.Visible = True
        picOn.Visible = False
        Arduino_Write("F")
    End Sub

    Private Sub Control_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Control.KeyDown
        Select Case KeyPressed
            Case 38 ' Up
                'Car_Stop()
            Case 40 ' Down
                ' Car_Stop()
            Case 39 ' Right
                'MsgBox("You pressed Ctrl and Arrowkey Right")
                picOff.Visible = False
                picOn.Visible = True
                Arduino_Write("O")
            Case 37 ' Left
                ' Car_TurnNone()
        End Select
        KeyPressed = 0
    End Sub

    Private Sub Control_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Control.KeyUp
        KeyPressed = e.KeyCode
        Select Case KeyPressed
            Case 38 ' Up
                'Car_ForwardDrive(Speed.Text)
            Case 40 ' Down
                'Car_BackwardDrive(Speed.Text)
            Case 39 ' Right
                picOff.Visible = True
                picOn.Visible = False
                Arduino_Write("F")
            Case 37 ' Left
                ' Car_TurnLeft()
        End Select
    End Sub

End Class
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Arduino, PC, Ethernet, and VB on: December 14, 2012, 05:00:32 pm
I haven't had much of a chance to play around with any of the code yet but I can answer some questions. I have tried the HTML version of control and I did get that to work with my Arduino. I guess one thing I am wondering here though is how can I implement a momentary button action while using Ethernet communications. From the small amount of web experience that I do have I believe a webpage is more like an on/off switch. Is that correct? I am assuming I could probably build in some kind of delay but it would get old having to click the button many times to keep something like a light on.

I do not have much experience with an Arduino so far and am trying to learn more everyday. I will attempt to work on the ideas mentioned above this weekend but if anyone has ideas of how I can implement a momentary style switch instead of an on/off switch it would be greatly appreciated if you would share your thoughts or ideas.
3  Using Arduino / Interfacing w/ Software on the Computer / Arduino, PC, Ethernet, and VB on: December 13, 2012, 08:42:05 pm
I have been scouring the internet for a simple example that I can expand upon and have had no luck and am hoping someone can help. I want to control an Arduino through an Ethernet cable using VB code on a PC. I have found where someone does this but uses a WiFi connection but I have not been able to successfully modify the code to get it to talk Ethernet instead of WiFi. I will attach the code I have so far but it may be easier if someone knows of a simple example to say turn on a single LED using the above I can modify the example to get what I want. The attached code does talk to my Arduino because I can see the TX and RX lights blink when I click on the form button but I cannot get the LED to turn on or off.

Arduino code:

Code:
//*************************************************************************************************************************************
// Declarations
//*************************************************************************************************************************************
#include <SPI.h>
#include <Ethernet.h>

//*************************************************************************************************************************************
// Wired configuration parameters
//*************************************************************************************************************************************
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xD1, 0x4B };
unsigned char local_ip[] = {192,168,1,100}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network

EthernetServer server(80);

char buffer[20];

void setup()
{
  pinMode(8,OUTPUT);
  Ethernet.begin(mac, local_ip);
  Serial.begin(9600);
}

void loop()
{
  EthernetClient client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true; 
    while (client.connected())
    {
      if (client.available())
      {
        int val;
        if (buffer[0] == 'O')
        {
          digitalWrite(8,HIGH);
        }
        else if (buffer[0] == 'F')
        {
          digitalWrite(8,LOW);
        }
      }
    }
  }
}

VB Code:

Code:
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    Dim networkStream As NetworkStream
    Dim KeyPressed As Integer

    Private Function Arduino_Connect(ByVal IP As String, ByVal Port As Integer) As Boolean
        tcpClient.Connect(IP, Port)
        networkStream = tcpClient.GetStream()
        If Not networkStream.CanWrite Or Not networkStream.CanRead Then
            tcpClient.Close()
            networkStream = Nothing
            Return False
        End If
        Return True
    End Function

    Private Sub Arduino_Write(ByVal Output As String)
        If Not IsNothing(networkStream) Then
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(Output)
            Dim endByte As [Byte]() = {&HFE}
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            networkStream.Write(endByte, 0, 1)
        Else
            MsgBox("ERROR")
        End If
    End Sub

    Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Arduino_Connect("192.168.1.100", 80)

        picOff.Visible = True
        picOn.Visible = False
    End Sub
    Private Sub Arduino_Disconnect()
        If Not IsNothing(networkStream) Then
            tcpClient.Close()
            networkStream = Nothing
        End If
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOff.Visible = False
        picOn.Visible = True
        Arduino_Write("O")
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.CLick
        picOff.Visible = True
        picOn.Visible = False
        Arduino_Write("F")
    End Sub

End Class

I have found posts that talk VB to Arduino through serial and I have that working, WiFi to Arduino but can't test due to not having a WiFi shield of any sort, and I have some HTML versions of the code working but I would really like to control the Arduino using VB and Ethernet.

I am not extremely familiar with the Arduino nor VB but I know enough to understand what is going on most of the time. Any help on this is greatly appreciated especially if a simple example that I can modify can be provided or linked to.
Pages: [1]