<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Daniela Morais</title>
    <description>The latest articles on Forem by Daniela Morais (@danielammorais).</description>
    <link>https://forem.com/danielammorais</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F20003%2F3829c4b7-fe97-425c-afea-1353065c7fcb.png</url>
      <title>Forem: Daniela Morais</title>
      <link>https://forem.com/danielammorais</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/danielammorais"/>
    <language>en</language>
    <item>
      <title>Tweeting with Arduino Uno</title>
      <dc:creator>Daniela Morais</dc:creator>
      <pubDate>Mon, 19 Jun 2017 02:52:41 +0000</pubDate>
      <link>https://forem.com/danielammorais/tweeting-with-arduino-uno</link>
      <guid>https://forem.com/danielammorais/tweeting-with-arduino-uno</guid>
      <description>&lt;p&gt;&lt;em&gt;A long time ago I bought an Ethernet ENC28J60 but I just recently tested it and, although I enjoy Java and I've used it in all of my previous projects to develop the interface between Arduino and server, it was clear to me that Python would be a much better choice in this case.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In 2014, I decided to use &lt;a href="https://panamahitek.com/" rel="noopener noreferrer"&gt;Panama Hitek Arduino SDK&lt;/a&gt; and then I sent them an email sharing my experience with their new SDK version. In 2016, I randomly met the guys from the Panama Hitek at the Fedora Latin America Meeting in IRC. Sharing your experience as a developer and be engaging in communities is very important, maybe you will meet the developers of your favorite framework in these spaces or even work with them. For me, it's one more example of the importance of sharing knowledge and ideas through articles, talks, meetings etc. So, don't be shy or scared of sharing what you know.      &lt;/p&gt;

&lt;h2&gt;
  
  
  IoT is about being connected
&lt;/h2&gt;

&lt;p&gt;IoT, embedded systems and smart devices are different concepts and there's a constant confusion when using these terms.&lt;br&gt;
If your device is disconnected from Internet, it probably isn't generating the value of an IoT device. Being connected is more than reading commands remotely from an app, it's a possibility to interact in different ways with your client and other systems like Twitter and also to collect useful information that can be used to make it even smarter.            &lt;/p&gt;
&lt;h2&gt;
  
  
  First step
&lt;/h2&gt;

&lt;p&gt;Register your app in Twitter to consume their API. Access &lt;a href="https://apps.twitter.com/app/new" rel="noopener noreferrer"&gt;Twitter Dev&lt;/a&gt; to create your Twitter application and generate an access token to enable tweeting from your account ("Keys and Access Tokens" &amp;gt; "Create my access token")  &lt;/p&gt;
&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;
&lt;h3&gt;
  
  
  The Chuck Norris way
&lt;/h3&gt;

&lt;p&gt;You can develop your own C program to connect with Arduino and send HTTPS requests to the Twitter API. There's a &lt;a href="https://arduino-tweet.appspot.com/" rel="noopener noreferrer"&gt;Tweet Library for Arduino&lt;/a&gt; available but it's deprecated and needs refactoring to work with Arduino versions higher than 1.6.4. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3oriNUhx4FLc707jq0/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3oriNUhx4FLc707jq0/giphy.gif" alt="SpongeBob SquarePants"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The easy way
&lt;/h3&gt;

&lt;p&gt;Working with pointers and in a low level can be complicated if you don't have experience with C and micro-controller, registers, digital systems etc. Use a python script and it will be much easier to code and maintain the software. Python will be responsible for exposing a web server which will then be consumed by your Arduino and send the content (e.g, sensor values, commands to turn on or off the lights etc) to Twitter. Note that Arduino UNO has limitations when building HTTP requests (it was necessary to use GET method instead of POST to create a tweet) and for this reason Python &lt;strong&gt;doesn't expose an API&lt;/strong&gt;.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;UIPEthernet&lt;/li&gt;
&lt;li&gt;Python 2.7&lt;/li&gt;
&lt;li&gt;pip &lt;/li&gt;
&lt;li&gt;twython&lt;/li&gt;
&lt;li&gt;flask&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;EtherShield and ETHER_28J60 are common libraries used in most of tutorials, but UIPEthernet is compatible with the original Ethernet which allows you to use the examples avaiable in the Arduino IDE (File &amp;gt; Examples &amp;gt; Ethernet) just by replacing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;SPI.h&amp;gt;
#include &amp;lt;Ethernet.h&amp;gt; //Replace here with &amp;lt;UIPEthernet.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Download UIPEthernet and install Flask and Twython:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ~/Arduino/libraries/
$ git clone https://github.com/ntruchsess/arduino_uip UIPEthernet
$ sudo pip install flask
$ sudo pip install twython 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a configuration file, insert information generated by Twitter for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# api_config 
APP_KEY = 'your_api_key'  
APP_SECRET = 'your_api_secret'  
OAUTH_TOKEN = 'your_access_token'  
OAUTH_TOKEN_SECRET = 'your_token_secret'  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the same folder, create tweet.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from twython import Twython  
from flask import Flask  
import api_config

app = Flask(__name__)

APP_KEY = api_config.APP_KEY  
APP_SECRET = api_config.APP_SECRET  
OAUTH_TOKEN = api_config.OAUTH_TOKEN  
OAUTH_TOKEN_SECRET = api_config.OAUTH_TOKEN_SECRET 

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

@app.route("/")
def index():  
    return "Hello world, I'm running!"

@app.route("/tweet/&amp;lt;message&amp;gt;")
def sendTweet(message):  
    twitter.update_status(status=message); 
    return "Done." 

if __name__ == '__main__':  
    #Fix here
    app.run(host="your_ip", port=8080, debug=True)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upload to Arduino:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;SPI.h&amp;gt;
#include &amp;lt;UIPEthernet.h&amp;gt;

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  
char server[] = "ip_server"; //fix here  
IPAddress ip(192, 168, 1, 177); //fix here
EthernetClient client;

void setup() {  
  Serial.begin(9600);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
}

void loop() {  
  if (client.connect(server, 8080)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /sendTweet/Hello%20world%20by%20Arduino HTTP/1.1");
    client.println("Host: ip_server_with_port"); //fix here
    client.println("Connection: close");
    client.println();
    client.stop();  
  } else {
    Serial.println("connection failed");
  }

  delay(5000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Give the script permission to execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ chmod a+x tweet.py
$ python tweet.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And magic.....&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-754567798819479553-311" src="https://platform.twitter.com/embed/Tweet.html?id=754567798819479553"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-754567798819479553-311');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=754567798819479553&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;h2&gt;
  
  
  Observations
&lt;/h2&gt;

&lt;p&gt;For some reason, I can't make requests to local servers and it was necessary to host the Python script on Digital Ocean. The server doesn't support HTTPS and some times an error about this was received. &lt;br&gt;
Note that this Python script &lt;strong&gt;doesn't expose an API&lt;/strong&gt; and you must always use URL Encoder/Decoder to send messages in the path param. Special characters can be a problem, try to use curl or wget if you get any errors. &lt;/p&gt;

&lt;h2&gt;
  
  
  Cool things
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/danielamorais/tweetduino.git" rel="noopener noreferrer"&gt;Tweetduino&lt;/a&gt; &lt;br&gt;
&lt;a href="https://medium.com/@AlexandraBowen/iot-is-eating-the-world-ed97f2b5a2ca" rel="noopener noreferrer"&gt;IoT is eating the world&lt;/a&gt;&lt;br&gt;
&lt;a href="http://sensedia.com/blog/apis/apis-para-dispositivos-iot/" rel="noopener noreferrer"&gt;APIs for IoT devices (Brazilian Portuguese)&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;p&gt;Arduino Ethernet – Pushing data to a (PHP) server &lt;a href="http://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-push/" rel="noopener noreferrer"&gt;http://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-push/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to Send Tweet From Command Line using Python &lt;a href="http://www.techplugg.com/send-tweet-command-line-python/" rel="noopener noreferrer"&gt;http://www.techplugg.com/send-tweet-command-line-python/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="http://danielammorais.com/2016/07/17/como-postar-tweets-com-arduino/" rel="noopener noreferrer"&gt;my personal blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>development</category>
      <category>arduino</category>
      <category>twitter</category>
      <category>iot</category>
    </item>
  </channel>
</rss>
