@vetedde

Как подключиться к loaclhost с Arduino через ethernet shield?

Мне надо отправлять данные с ардуино на локальный сервер, который будет запущен на компе. Комп подключен к wi-fi, Arduino Uno с Eternet Shield через кабель к роутеру.

Я взяла стандартный скетч из примеров WebClient и поменяла на свой IPv4-адрес, который нашла через ipconfig. Это должно было бы решить проблему, но в монитор порта выдает connection failed. В чем может быть проблема?

5fc4e0fed8a2c783626540.png

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192, 168, 1, 4);  // numeric IP for Google (no DNS)
//char server[] = "localhost";    // name address for Google (using DNS)

IPAddress ip(192, 168, 1, 229);

EthernetClient client;

void setup() {
  Serial.begin(9600);
  Ethernet.init(4);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 3000)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: localhost");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}
  • Вопрос задан
  • 102 просмотра
Пригласить эксперта
Ответы на вопрос 1
@Codebaker
Всё умею, всё могу!
А на локальной машине с адресом 192.168.1.4 у вас уже запущен сервер на 3000 порту? Если ещё нет, то вот вам и ответ.

Кстати, судя по коду, это должен быть сервис Гугл (если вы что-то на своей машине запускать решили, то это должнен быть некий прокси к гугл-поисковику). В оригинале было что-то вроде 216.239.32.10? Только порт на 80 верните. Оставьте его и попробуйте запустить скетч.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы