0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Arduinoからツイート+Webサーバー化

Last updated at Posted at 2021-01-22

書きかけ

Arduino Nano EtherでツイートとWebサーバー化をします。

使用ボードはこちら
https://www.diymore.cc/products/enc28j60-ethernet-network-module-shield-v2-0-for-arduino

参考リンク
Tweet Library for Arduino
http://arduino-tweet.appspot.com/

Arduinoから温湿度をツイートする方法 - Grow up
https://knkomko.hatenablog.com/entry/2019/09/17/012226

# include <UIPEthernet.h>
# include <Twitter.h>

Twitter twitter("TWITTER_TOKEN");

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE  };  
IPAddress ip(192, 168, 0, 117);
IPAddress myDns(192, 168, 0, 1);
                        
EthernetServer server(80);

void setup() {

  Serial.begin(9600);

  Ethernet.begin(mac, ip, myDns);
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());

  server.begin();
  
  String message = "Hello, World! from Arduino Nano ethernet a :-) #Arduino";

  char* msg = message.c_str();
  Serial.print("connecting ...");

  if (twitter.post(msg)) {
    int status = 0;
    status = twitter.wait();
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }

}

void loop() {

  // listen for incoming clients
  EthernetClient client = server.available();

  if (client)
  {  
    Serial.println("-> New Connection");

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank)
        {
          client.println("<html><title>Hello World!</title><body><h3>Hello World!</h3></body>");
          break;
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }

    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("   Disconnected\n");
  }
}
~~~~
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?