arduino-ethernet

misha_1006
Offline
Зарегистрирован: 27.06.2020

всем кто разбирается:

что не так?

#include <SPI.h>
#include <Ethernet.h>
 
 
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,2);
 
// initialize the library instance:
EthernetClient client;
 
const unsigned long requestInterval = 60000;  // delay between requests
 
char serverName[] = "www.twitter.com";  // twitter URL
 
boolean requested;                   // whether you've made a request since connecting
unsigned long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds
 
String currentLine = "";            // string to hold the text from server
String tweet = "";                  // string to hold the tweet
boolean readingTweet = false;       // if you're currently reading the tweet
 
void setup() {
  // reserve space for the strings:
  currentLine.reserve(256);
  tweet.reserve(150);
 
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
 
  // attempt a DHCP connection:
  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }
  Serial.print("My address:");
  Serial.println(Ethernet.localIP());
  // connect to Twitter:
  connectToServer();
}
 
 
 
void loop()
{
  if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();
 
      // add incoming byte to end of line:
      currentLine += inChar;
 
      // if you get a newline, clear the line:
      if (inChar == '\n') {
        currentLine = "";
      }
      // if the current line ends with <text>, it will
      // be followed by the tweet:
      if ( currentLine.endsWith("<text>")) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
        tweet = "";
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet += inChar;
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
          Serial.println(tweet);  
          // close the connection to the server:
          client.stop();
        }
      }
    }  
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and two minutes have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
  }
}
 
void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=[   ]&count=1 HTTP/1.1");
    client.println("HOST: www.twitter.com");
    client.println("Connection: close");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}
misha_1006
Offline
Зарегистрирован: 27.06.2020

подскажите правильно ли выполнен get запрос

misha_1006
Offline
Зарегистрирован: 27.06.2020
Attempting to get an IP address using DHCP:
My address:192.168.1.2
connecting to server...
connecting to server...
making HTTP request...
connecting to server...
making HTTP request...
connecting to server...
making HTTP request...
connecting to server...
making HTTP request...
connecting to server...
making HTTP request...
connecting to server...
making HTTP request...
 
результат такой
misha_1006
Offline
Зарегистрирован: 27.06.2020

Думаю понял вас.

 

misha_1006
Offline
Зарегистрирован: 27.06.2020

Интересна тема управления устройствами через ethernet.

Взял код с сайта Arduino. В моем понимании данный код должен брать последнюю публикацию из моего аккаунта в твитере и выводить ее в последовательный порт.

Далее хочу изменить код с целью чтобы  в зависимости от текста публикации выводить управляющие сигналы на определенный вывод.

Код такой: 

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


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,2);

// initialize the library instance:
EthernetClient client;

const unsigned long requestInterval = 60000;  // delay between requests

char serverName[] = "www.twitter.com";  // twitter URL

boolean requested;                   // whether you've made a request since connecting
unsigned long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds

String currentLine = "";            // string to hold the text from server
String tweet = "";                  // string to hold the tweet
boolean readingTweet = false;       // if you're currently reading the tweet

void setup() {
  // reserve space for the strings:
  currentLine.reserve(256);
  tweet.reserve(150);

 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // attempt a DHCP connection:
  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }
  Serial.print("My address:");
  Serial.println(Ethernet.localIP());
  // connect to Twitter:
  connectToServer();
}



void loop()
{
  if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar;

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        currentLine = "";
      }
      // if the current line ends with <text>, it will
      // be followed by the tweet:
      if ( currentLine.endsWith("<text>")) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
        tweet = "";
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet += inChar;
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
          Serial.println(tweet);  
          // close the connection to the server:
          client.stop();
        }
      }
    }  
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and two minutes have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
  }
}

void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=[   ]&count=1 HTTP/1.1");
    client.println("HOST: www.twitter.com");
    client.println("Connection: close");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}

Подскажите так ли это работает как я думаю?

Можно взять данный скетч за основу?

inspiritus
Offline
Зарегистрирован: 17.12.2012

Кррутто

а теперь напишите , как Вы запустили это на таком-то железе и оно заработало такТо и такТо

если чтоТо неправильно опишите проблему

sadman41
Offline
Зарегистрирован: 19.10.2016

локализуйте ошибку - запросите / с google.com

misha_1006
Offline
Зарегистрирован: 27.06.2020

Железо:Arduino uno+ethernetShield(w5500)

ждал публикацию с твитера

com порт выдал это

 

misha_1006
Offline
Зарегистрирован: 27.06.2020

Поясните пожалуйста. Вы имеете в виду сделать запрос на google.com вместо twitter.com