Arduino Ethernet Shield 连接到套接字服务器 [英] Arduino Ethernet Shield connection to socket server

查看:23
本文介绍了Arduino Ethernet Shield 连接到套接字服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Arduino 的以太网扩展板将其连接到套接字服务器(不同的计算机),以便我可以从它接收消息以激活某些例程.这是我的代码:

I'm using an ethernet shield for Arduino to connect it to a socket server (different computer) so that I can receive messages from it to activate some routine. Here is my code:

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x21 };
byte ip[] = { 192,168,1,11 }; //ip shield
byte server[] = { 192,168,1,7 }; // ip server

EthernetClient client;
String readString;

int ledPins[] = {19, 17, 2,3, 5, 6, 7, 8, 9};  // leds pins
int pinCount = 8;// number of leds
int const PINEYES = 9; //pin for different led
int const TIMERLEDS = 1000; 
int const TIMERTOOFF= 3000; 

//--------------------------------------------------------------------------

void setup() {
  turnOffLeds();
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");
  if (client.connect(server, 1400)) {
    Serial.println("connected");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  pinMode(PINEYES, OUTPUT);      
  int thisPin;
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
}

//--------------------------------------------------------------------------    
void loop() {
  if (client.available()) {
    char c = client.read();

    if (readString.length() < 30) {
      if(c!='|')
        readString.concat(c);
      else {
        client.flush();
        //if (readString == "START_SENSATIONS") {
        if (readString == "on") {
          Serial.println("recebi");
          client.stop();
          turnOnMaya();
        }
        resetString();
      }
    }
    Serial.println(readString);
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

//--------------------------------------------------------------------------
void turnOnMaya(){
  turnOnLeds();
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
     delay(TIMERLEDS);                  
     digitalWrite(ledPins[thisPin], LOW);    
  }
  turnOnEyes();
  delay(TIMERTOOFF);    
  turnOffLeds();
  digitalWrite(PINEYES, LOW);   
  client.connect(server, 1400);
}

//--------------------------------------------------------------------------
void turnOnLeds(){
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    digitalWrite(ledPins[thisPin], HIGH);   
  }
}

//--------------------------------------------------------------------------
void turnOffLeds(){
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    digitalWrite(ledPins[thisPin], LOW);   
  }
}

//--------------------------------------------------------------------------
void turnOnEyes(){
  digitalWrite(PINEYES, 255);
}

//--------------------------------------------------------------------------
void resetString() {
  readString = "";
}

问题是,当我的服务器停止或在某些时候不可用时,我需要我的 Arduino 继续尝试连接到它,直到它再次可用.但我无法完成这项工作.我试过这个:

The problem is, when my server stops or is not available for some moments, I need my Arduino to keep on trying to connect to it until it is available again. But I can't make this work. I tried this:

  while(!client.available()){
    Serial.println("connection failed, trying again...");
    client.connect(server, 1400);
    delay(1000);
  }

但它不起作用.它只是永远打印连接失败,再试一次...".我怎样才能做到这一点?谢谢!

But it doesn't work. It just prints "connection failed, trying again..." forever. How can I do this? Thanks!

推荐答案

我假设您 PC 中的服务器是正常的 javac(或任何其他标准 tcp 服务器)

I'm assuming that the server in your PC is a normal java or c (or any other standard tcp server)

但是您的 arduino 客户端没有指定它是 TCP.因此,要么更改您的服务器或客户端(如此处- 这使用 wifi 连接).如果您的服务器是 Java,则可能是这样的:

But your arduino client doesn't specify that it is TCP. So either change your server or the client(like in here - this uses wifi connection). If your server is in java, it could be like this:

int port=9999;
    try{
        System.out.println("Starting server...");
        ServerSocket ss=new ServerSocket(port);
        Socket clientSocket=ss.accept();
        System.out.println("Connection has been established...");
        PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true);
        BufferedReader br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine;
        System.out.println("Listening.....");
        while((inputLine=br.readLine())!=null)
            System.out.println(inputLine);
    }catch(Exception e){System.out.println(e.getMessage());}

这篇关于Arduino Ethernet Shield 连接到套接字服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆