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:
//*************************************************************************************************************************************
// 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:
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.