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.
In 2014, I decided to usePanama Hitek Arduino SDKand 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.
IoT, embedded systems and smart devices are different concepts and there’s a constant confusion when using these terms.
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.
Register your app in Twitter to consume their API. AccessTwitter Devto create your Twitter application and generate an access token to enable tweeting from your account (“Keys and Access Tokens” > “Create my access token”)
You can develop your own C program to connect with Arduino and send HTTPS requests to the Twitter API. There’s aTweet Library for Arduinoavailable but it’s deprecated and needs refactoring to work with Arduino versions higher than 1.6.4.
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 Pythondoesn’t expose an API.
- Python 2.7
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 > Examples > Ethernet) just by replacing:
Download UIPEthernet and install Flask and Twython:
Create a configuration file, insert information generated by Twitter for you:
In the same folder, create tweet.py:
Upload to Arduino:
Give the script permission to execute:
Hello world by Arduino
06:45 AM - 17 Jul 2016
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.
Note that this Python scriptdoesn’t expose an APIand 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.