gpt4 book ai didi

sockets - Arduino Ethernet Shield 连接到套接字服务器

转载 作者:可可西里 更新时间:2023-11-01 02:42:44 34 4
gpt4 key购买 nike

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

#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 继续尝试连接它,直到它再次可用。但我无法完成这项工作。我试过这个:

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

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

最佳答案

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

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

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());}

关于sockets - Arduino Ethernet Shield 连接到套接字服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25450106/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com