I have written about the ESP8266 before in my post about the magic button. For the button I am using the nodeMCU firmware which let’s you run lua script on the ESP. But recently the people over at esp8266.com created an Arduino IDE version which is compatible with the ESP8266. I was already using an ESP connected to an Arduino to log the output of my solar panels so I decided to try and run this code natively on the ESP. And it worked, so although this is not automation or virtualization related, here are some more details about my ESP8266 PV Logger.
The logger connects to my Mastervolt Soladin 600. This inverter has an RS485 port but connecting it to a TTL serial port is easy. All you need is two resistors. For the schematic see the readme on github. I have been monitoring my PV panels for years using this library. That lib only works with software serial ports which are not supported by the ESP so I changed that to use the Stream class instead. Both Software Serial and Serial are inherited from Stream so now it should work with both on a normal arduino but I haven’t tested it with the software serial. I also changed the serial delay and timeout settings to get reliable communication between the ESP and the Soladin. You can find the updated lib here.
There are a couple places you could send your logging data to. Until today I was using data.sparkfun.com and that worked fine. But it’s lacking nice grapical presentation of you data. And I’m too lazy to build my own so I looked for something else. Now I am using thingspeak and that seems to work just a s well with the added bonus of nice graphs and google gauge visualisation. You can find my stats here.
The logger code is pretty easy. It tries to connect to the Soladin, if that succeeds it sends data to thingspeak. This is repeated every 15 seconds. If it can’t make a connection to the Soladin the loop slows down. You can find the whole code here on github. Don’t forget to compile it with arduino 1.6.1-esp8266-1 instead of the regular arduino IDE.
All you need to run this is a 3.3volt power supply, two resistors, a cable with an RJ11 connect and a ESP-01. Mince looks like this.
The best part? The whole thing costs about 5 bucks! That’s a lot cheaper than Mastervolt’s PC-Link cable.
I recently posted an article about the automate-it robotic arm. I promised to go into more detail on the brain of the Automate-IT robot so time to awake you inner geek because here is a write up about the controller hardware and software that brings the thing to life.
The hardware for the controller is an Arduino Yun microcontroller. In case you’re not familiar with Arduino according to the their website: “Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects”. There are different Arduino compatible boards available. The Yun is a special one because it combines an AVR ATmega 32u4 chip with a System on a Chip running Linux on the same board. Both are connected via a serial bus. A bridge library provides an API which enables a developers to integrate the Linux SoC into Arduino projects. The SoC part of the board provides WLAN, Ethernet, MicroSD card slot and USB Connectivity. The SoC is running an openWRT linux distribution which runs a webserver and some other services. The webserver can be manipulated from the Arduino code. This makes it very easy to build an http API on your Arduino powered device.
The board can provide about 50mA of power. All servo motors combined can draw up to 3Amps so I needed something a little bit more powerful. I choose the LM123K voltage regulator which gets its juice from a 12v brick type power supply. The voltage regulator magically converts it into 5v. I put the LM123K on a piece of perf board and added headers to connect the servos. Male headers on the bottom of the board turn it into a very simple Arduino shield. It doesn’t look pretty but it works fine.
The roboticArm library
The arm is programmed by recording the exact position of all the joints step by step. With each step the time it takes to execute the step is stored as well. Putting the arm to work is just a matter of playing back the recorded sequences. My roboticArm library implements this logic and presents it as a single class. The object instantiated from the class represents the robotic arm and stores a pointer to a recorded or empty sequence. The object can be in recording mode or in playback mode. When in recording mode the current position in the sequence can be selected and the positions of the servos can be set. It’s also possible to add or delete steps from the sequence.
In playback mode the step number will be automatically increased when the recorded duration for a step is expired. At the end of a sequence the playback will be stopped until the step counter is reset to 0.
My library uses the Arduino servo library so there is did not develop any servo control code. But since this isn’t a dedicated Arduino or development blog I won’t go into too much detail about the actual code. If you’re interested feel free to download the RoboticArm library.
Using the library
Below is an excerpt from my controller code. I did not actually test the code below and it is not very usefull since it doesn’t include any code to program a sequence. But at least the servos should come to life and go to the position configured in the defaults sequences.
The controller presents a REST API via the linux part of the Yun. The beauty of the Yun is that you can actually code this API in the Arduino code. Unfortunately this also means there is no framework available that makes creating a REST API easier. And even if there was I doubt it would fit in the Atmega’s RAM or Flash. The first thing to tackle when creating a REST API is the routing. Routing basically means figuring out what we need to do when a certain URL is requested. You could read the request and use a huge switch case contruction to figure out where to go. but this uses up valuable program storage and ram in a microcontroller. On top of that I find it cumbersome to program in that way. So I used a slightly different approach. I use a URL structure that looks like this: http://RESThost/category/command/parameter. Based on that I wrote the code below:
To figure out which function to run my code uses 3 arrays. The first is an array of strings (or technically a two dimensional array of characters) which defines the categories we have. The second array is a three dimensional array of characters which defines the commands available for each category. The last array is a two dimensional array of function pointers. When a request comes in the code looks for the corresponding element in the first array. Then it know the index number of the category so it starts looking for the element matching the command in the second array. When the command is found the code knows both the index number for the category and the command and uses these index number to find the pointer to the right function in the third array and runs the function. If the function need any parameters the rest of the URL is read and used as parameter. For example: http://roboticarm/status/record will start the function statusRecordCommand() which toggles the recording mode flag of the roboticArm object.
Putting it all together
I made the whole project available for download. You can find it here: robotic_arm. Don’t forget to also download the RoboticArm library and put it somewhere the arduini IDE can find it. The download also includes the AJAX Gui to program the arm. This was my first time building such a gui so it doesn’t like very nice and the code can probably be optimized. So feel free to modify the code and send me the results :).
Disclaimer: The code was only tested on Arduino Yun. The library will run fine on any other Arduino as long as you configure the correct pin numbers. The REST API and Ajax Gui will not work on any other Arduino board currently available.