• I






      
           

Научно-популярный образовательный ресурс для юных и начинающих радиолюбителей - Popular science educational resource for young and novice hams

Основы электричества, учебные материалы и опыт профессионалов - Basics of electricity, educational materials and professional experience

КОНКУРС
language
 
Поиск junradio

Радиодетали
ОК
Сервисы

Stock Images
Покупка - продажа
Фото и изображений


 
Выгодный обмен
электронных валют

Друзья JR



JUNIOR RADIO

 

Arduino Ethernet Shield Учебное пособие. Часть 2



 

Я собирался написать некоторый код для чтения сообщений в Твиттере, но такой код уже существует в качестве примера в программисте Arduino. Вместо этого я просто слегка изменил его, чтобы включить светодиод, если читается специальное сообщение. Для этого подключите положительный вывод LED к контакту D2, а отрицательный вывод последовательно с резистором 220 Ом к земле. Не забудьте ввести свой собственный IP-адрес в приведенный ниже код, иначе он не будет работать.

 

Вот код:

/*

  Twitter Client with Strings

 This sketch connects to Twitter using an Ethernet shield. It parses the XML

 returned, and looks for <text>this is a tweet</text>

 You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,

 either one will work, as long as it's got a Wiznet Ethernet module on board.

 This example uses the DHCP routines in the Ethernet library which is part of the

 Arduino core from version 1.0 beta 1

 This example uses the String library, which is part of the Arduino core from

 version 0019. 

 Circuit:

  * Ethernet shield attached to pins 10, 11, 12, 13

 created 21 May 2011

 by Tom Igoe

 This code is in the public domain.

 */

#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[] = {

  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };

IPAddress ip(191,11,1,1); //<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance:

EthernetClient client;

const int requestInterval = 60000;  // delay between requests

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

boolean requested;                   // whether you've made a request since connecting

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() {

  pinMode(2, OUTPUT);

  // reserve space for the strings:

  currentLine.reserve(256);

  tweet.reserve(150);

// initialize serial:

  Serial.begin(9600);

  // attempt a DHCP connection:

  if (!Ethernet.begin(mac)) {

    // if DHCP fails, start with a hard-coded address:

    Ethernet.begin(mac, ip);

  }

  // 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);     

          if(tweet == ">Hello Cruel World"){

           digitalWrite(2, HIGH);

           Serial.println("LED ON!");

          }

          if(tweet != ">Hello Cruel World"){

           digitalWrite(2, LOW);

           Serial.println("LED OFF!");

          }  

          // 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=RandyMcTester&count=1 HTTP/1.1");

    client.println("HOST: api.twitter.com");

    client.println();

  }

  // note the time of this connect attempt:

  lastAttemptTime = millis();

}  

 

Предположительно, вы захотите прочитать что-то кроме недавнего поста в твиттере RandyMcTester.

 

Чтобы прочитать другие каналы Twitter, измените следующий текст:

client.println("GET /1/statuses/user_timeline.xml?screen_name=[NEW TWITTER NAME HERE]&count=1 HTTP/1.1");

 

 

В начало обзора


Купить радиодетали для ремонта
 



Необходимо добавить материалы...
Результат опроса Результаты Все опросы нашего сайта Архив опросов
Всего голосовало: 372



          

Радио для всех© 2024