Go Down

Topic: Arduino Yun TCP socket to LabVIEW (Read 2669 times) previous topic - next topic

Life_Learner

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#define PORT 255

YunServer server(PORT);
YunClient client;

static boolean clientActive = false;


void setup() {
  // put your setup code here, to run once:
Bridge.begin();
server.noListenOnLocalhost();
server.begin();
client = server.accept();

}

void loop() {
  // put your main code here, to run repeatedly:
if (client.connected())
{
   if (!clientActive)
   Serial.println("New client connection.");
   
   clientActive = true;
   
   if (client.available())
   {
      Serial.print("From client: \"");
      while (client.available())
      Serial.print((char)client.read());
      Serial.println("\"");
   }
   
   client.println("Hello");
   
   }
   else
   {
      if (clientActive)
      {
         client.stop();
         Serial.println("Client disconnected.");
         
      }
      
      clientActive = false;
      client = server.accept();
   }
}


Hi, thanks in advance for helping. I'm required to use the LabVIEW graphical user interface to on/off the relay connected to pin 13 & gnd of arduino Yun.

Through this sample code I've found, I'm able to read the incoming message "Hello" from LabVIEW.
This sample code has allowed me to connect LabVIEW client to arduino yun server via TCP Open Connection.

However, now that the first part of the connection has been done, I'm stuck at figuring out how to write a 'H' to arduino Yun and let arduino Yun process the command 'H' to supply a HIGH to the relay.
A 'L' to supply a LOW to the relay.

Any ideas? Thank You.

sonnyyu

#1
Jun 22, 2015, 12:10 pm Last Edit: Jun 22, 2015, 06:47 pm by sonnyyu
ATmega32u4 code:

http://forum.arduino.cc/index.php?topic=323690.msg2243510#msg2243510

Setup TCP bridge from localhost ( 127.0.0.1 ) to private address.

http://forum.arduino.cc/index.php?topic=323690.msg2243520#msg2243520


Plan B - Paln F:

http://forum.arduino.cc/index.php?topic=325561.0

Please use code tag to wrap your code.










ShapeShifter

Life_Learner: that looks like my simple TCP socket demo sketch. sonnyyu has pointed you to some good resources, and I would certainly check them out.

The code above is easy, but not necessarily efficient, handling the networking on the Linux side would involve a lot less overhead. If you want to use that code, the key is here:

Code: [Select]
  if (client.available())
   {
      Serial.print("From client: \"");
      while (client.available())
      Serial.print((char)client.read());
      Serial.println("\"");
   }
  
   client.println("Hello");

The first section here is reading any received data, and echoing it to the serial port. You would want to look at the received data, and use that to control your Yun's actions.

Similarly, the client.println() is sending data out over the connection. This is where you would want to send data back after receiving a command. You will probably want to make it inside an IF statement, because as it is now the sketch is sending "Hello" over and over.

There are also places where I print out that the client is connected or disconnected, you can add code there to do any special processing when those events happen (maybe a graceful shutdown of outputs of the connection is closed?)

Life_Learner

#3
Jul 16, 2015, 06:42 pm Last Edit: Jul 19, 2015, 07:03 pm by Life_Learner
Code: [Select]

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#define PORT 255

YunServer server(PORT);
YunClient client;

int ledPin = 13;
char incomingByte;
static boolean clientActive = false;


Code: [Select]

void setup() {
  // put your setup code here, to run once:
Bridge.begin();
pinMode(ledPin, OUTPUT);
server.noListenOnLocalhost();
server.begin();
client = server.accept();
}


Code: [Select]

void loop() {
  // put your main code here, to run repeatedly:
if (client.connected())
{
   if (!clientActive)
   Serial.println("New client connection.");
   
   clientActive = true;
   
   if (client.available())
   { 
      incomingByte == client.read();

      Serial.print("From client: \"");
      while (client.available())
      Serial.print((char)client.read());
      Serial.println("\"");
   }

   if (client.read() == 'H') {
        digitalWrite(ledPin, HIGH);
   }
   if (client.read() == 'L') {
        digitalWrite(ledPin, LOW);
   }
}
   else
   {
      if (clientActive)
      {
         client.stop();
         Serial.println("Client disconnected.");
         
      }
     
      clientActive = false;
      client = server.accept();
   }
}

Life_Learner

Hi, Thanks ya'all for the inputs. I was able to add in some codes into the sample code for the Arduino Yun to work with NI LabVIEW to switch the light blub On/Off. For this mini project, the Arduino Yun is acting as a Server and NI LabVIEW as a Client. Thanks

pal30130

How to use labview and Arduino yun's Wireless Protocol??

luckysam


Go Up