Functional Programming with IOT (FIOT) – Day 13

After yesterday’s success with Firmata installation i decided to progress and test the ESP-01 with Clojure.  The idea was to test each pin and see if i can send HIGH and LOW signals on the output.

I wrote the following piece of code to test it. The full project can be found on my github.

(ns firmata-test.core
  (:require [clodiuno.core :refer :all]
            [clodiuno.firmata :refer :all]))

(defn -main []
  (let [board (arduino :firmata "/dev/tty.wchusbserial1450")]
    ;;allow arduino to boot
    (Thread/sleep 1000)
    (pin-mode board 0 OUTPUT)
    (while true
        (doseq [x (list 0)]
          (println x)
          (println "HIGH")
          (digital-write board x HIGH)
          (Thread/sleep 6000)
          (println x)
          (println "LOW")
          (digital-write board x LOW))
          (Thread/sleep 6000))
    (close board)))

Using the Cloudiuno library, this code is testing the HIGH and LOW signals on PIN 0 with a 6 seconds delay.

I used a voltmeter to see if the voltage was changing after 6 seconds. So this is what i could see on my terminal while it was running.

Pin 0 Test with Clojure
Pin 0 Test with Clojure

And i had a voltmeter attached to the PIN 0 of ESP-01 to see if the voltage was actually changing. You would need a voltmeter to check if the voltage is changing after 6 seconds.

PIN 0 at Low voltage
PIN 0 at Low signal

Then after 6 seconds,

PIN 0 at High signal
PIN 0 at High signal

During this experiment i noticed that the signal value was inversely proportional to the voltage that was being displayed on the multi-meter. I am still not sure why that happened. Could it be a bug in the library ?

Anyways, this is a great success as now we can finally use Clojure (The Functional Language of our choice) to program hardware such as the famous IOT module called “ESP8266” or to be more specific ESP-01.

I ordered few more of these from China which has now been delivered so i am thinking of a way to use them to prepare a good demo.

Wish me luck.. this is not the end, this is just the start so keep following !

Functional Programming with IOT (FIOT) – Day 12 – Firmata Success !!!

Alhamdulilah ! Success! After 12 days of continuous struggle of trying to make Firmata work with ESP-01, it is finally working. The following screenshot is a working proof.

Firmata Test Program Screenshot
Firmata Test Program Screenshot

So here is how i did it, I cleaned up all the Firmata instances from Arduino. This means i deleted Firmata from Libraries folder, in my case the path was :

/Users/<username>/Documents/Arduino/libraries/Firmata

and another location was:

/Applications/Arduino.app/Contents/Java/libraries/Firmata

Then i followed the solution presented in the firmata arduino github issue 257 which i interpreted as following steps :

  1. You need to install ESP8266 board in our arduino, follow installing the ESP8266 Arduino addon
  2. Clone the ESP8266 Firmata repository onto your local machine.
  3. Open the boards.h file into your editor.
  4. Add the ESP-01 board support by copy/pasting the following lines on Line 165 of the boards.h specified in step 2.
    // ESP8266 generic
    #elif defined(ARDUINO_ESP8266_ESP01)
    #define TOTAL_ANALOG_PINS 0
    #define TOTAL_PINS 17
    #define VERSION_BLINK_PIN 4
    #define IS_PIN_DIGITAL(p) ((p) == 0 || (p) == 2 || (p) == 4 || (p) == 5 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 || (p) == 16)
    #define IS_PIN_ANALOG(p) (false)
    #define IS_PIN_PWM(p) (false)
    #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
    #define IS_PIN_I2C(p) (false)
    #define IS_PIN_SPI(p) (false)
    #define PIN_TO_DIGITAL(p) (p)
    #define PIN_TO_ANALOG(p) ((p) - 17)
    #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
    #define PIN_TO_SERVO(p) p
  5. Run the ./release.sh which will generate the Firmata-2.5.0.zip
  6. Open Arduino, go to Sketch -> Include Library -> Add Zip and add that zip.
  7. Then go back to the repository folder and open StandardFirmata example into Arduino.
  8. Upload it onto ESP-01.
  9. Open Firmata Test program.
  10. Select the correct port and you will see the result as shown in the screenshot above.

Next step is to use Clojure to control ESP-01 (which has StandardFirmata firmware).

Functional Programming with IOT (FIOT) – Day 11

Today, i am going to start with a fresh mind-set and i am going to reiterate some of the things just to make sure i am going in the right direction.

I started with the basic sanity tests.

Test # 1 : Hardware 
So i have ESP-01, i have just verified that i haven’t burnt the IC yet by uploading the blinking example.

Test # 2 : Code
I went to Firmata Github and cloned the latest version of the code in my Arduino. For those who are not aware of how to do it the steps are listed in the readme of the linked github also i am copy/pasting them here:

  • Navigate to the Arduino application
  • Right click on the application icon and select Show Package Contents
  • Navigate to: /Contents/Resources/Java/libraries/ and replace the existing Firmata folder with latest Firmata release (note there is a different download for Arduino 1.0.x vs 1.6.x)
  • Restart the Arduino application and the latest version of Firmata will be available.

while reading through the code i saw the following note which suggests that we cannot use Serial.print() with ESP-01 which means it is hard to debug on ESP-01

Note: “The blue LED on the ESP-01 module is connected to GPIO1(which is also the TXD pin; so we cannot use Serial.print() at the same time)”

Test # 3 : Upload
To test the code is uploaded properly, i uploaded the blinking example first so that i can see the internal LED blinking and then uploaded the StandardFirmataWifi version that stopped the blinking.

Test # 4: Firmata Test
I used the same firmata test program that i used in my previous test to check if Firmata is loaded properly on the ESP-01. But again there were no buttons even though i had selected the correct port.

Looking at all those test results, i believe that Firmata Test Program is expecting ESP-01 to operate on a certain baud rate and it is not able to meet its requirements so TX and RX rate is very low and therefore no buttons are loading. But this is a hypothesis which i still need to test. So that will be my Test 5.

Next post will be about this test 5. Stay tuned !

 

Functional Programming with IOT (FIOT) – Day 9 and 10

I started with a sketch for programming ESP-01 because it was quick and easy way to explore the Frtizing software.

Fritzing Software Screenshot
Fritzing Software Screenshot

I really thought it was a simulator and it would make my life easier by just letting me work on my laptop until the whole project is complete. But i couldn’t find the “play” button. No wonder why. After some research i found their FAQs and the first question was “Does it simulate my circuit?”

http://fritzing.org/faq/#Does_it_simulate_my_circuit%3C/p%3E

“Does Fritzing simulate my circuit? (a.k.a. Where is the play button?)No, sorry. We don’t think that the advantage of having a simulation is worth the effort. Hardware is very difficult to simulate and it would also complicate the usage of Fritzing. Also, we think that it is important that you get hands on with the real stuff, and that you should try out your circuits physically. We will however add some simple checks in the future, to help you avoid common mistakes. For a more complete discussion, see http://fritzing.org/forum/thread/413/, particularly the comment from Brendan Howell.”

This is why we should read about the software first before overthinking its benefits. Now, i am back to where i was before my last post which was about making firmata work with ESP-01/ESP-12.

I did some more research on this topic and found many useful links. After reading the solutions offered by other engineers, i have come up with my own which i will discuss in my next post.

Functional Programming with IOT (FIOT) – Day 5,6,7 and 8

Weekdays are hard for me to document the approach because i only get half an hour in my lunch time to work on this project and then when you are stuck you just want the answer before you can write something.

I finally decided to put my tools down and summarise the approaches and findings. Well, first things first. I was right in my previous post that i was not using the compatible Firmata version for ESP8266 which lead me to investigate if it is actually compatible with ESP8266. The latest versions are compatible with Firmata according to the documentation. Many people have used this approach to talk to their devices using different languages.

It was interesting to find about this Javascript framework called Jonny-Five doing similar thing. I didn’t know there were already good tooling available to design your hardware before actually doing it in real. I started with making the real hardware work which was a bit time consuming. I found this great tool called Fritzing, which can make your life alot easier. All you have to do is virtually design hardware and i am assuming here (don’t catch me on this as i haven’t started using it) you upload your sketch onto the virtual hardware and test your code. Its like a hardware emulator.

Anyways, back to the main thing again. No, i have not yet been able to get Firmata work with ESP8266. I am still investigating the problem. So my next approach after all those findings is to ditch the real hardware world and use Fritzing and test my code there. If it is what it seems like meaning that i can test my code on the virtual hardware then implementation would become very easy.

 

Hopes are high, fingers crossed!

Functional Programming with IOT (FIOT) – Day 4

So here is the working code, which is suppose to talk to StandardFirmata in the ESP8266 and blink an LED. The code compiles properly and runs without any issues. But i don’t see any LED blinking.

Screen Shot 2017-04-05 at 13.47.22

Another approach i took was to verify if StandardFirmata is working. I used the Binary file from the project firmata-test and gave it the port my device was connected on. I got a completely blank UI as you can see in the screenshot which suggests there is some problem with the StandardFirmata. It may be incompatible with the type of hardware i am using for this test.

 

I also have ESP-12 handy, so the next test would be just to verify if StandardFirmata has compatibility issues with the ESP series. But also there is some good news, its about another way of compiling Clojure into binary, it needs more discussion so i will go in detail in the upcoming posts.

Functional Programming with IOT (FIOT) – Day 3

Well the direct compilation of the code didn’t work. I had to do some tweaks to make it work which included copying the librxtxSerial.jnilib from target to /Library/Java/Extensions.

$ cp $PROJECT_HOME/firmata-test/target/native/macosx/x86_64/librxtxSerial.jnilib /Library/Java/Extensions/

After fixing that issue the project was running with the following warning.

WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre2
native lib Version = RXTX-2.2pre3

I will add the full source code and the method to run it in my next post. I spent alot of time today just debugging the issue.

Functional Programming with IOT (FIOT) – Day 2

Today is the day 2 of the FIOT, as discussed in the previous post i shall be exploring the ESP8266 with a mainstream software engineering functional programming(FP) language.

In my current employment, we work in Clojure due to the language’s pureness and immutability. There is a discussion on StackOverflow that you can follow to read more about it being “probably the closest” to functional.

Following is the picture of the hardware i have chosen for this experiment.

ESP8266 Programming Kit
ESP8266 Programming Kit

At this stage, we have a set hardware and programming language, now we need some sort of IDE to convert our code into binary right?

Wrong, it is not that simple.

Unfortunately, Clojure cannot be directly compiled into binary at this stage. So we would have to use an intermediate bridge to communicate with the hardware. This is where Firmata comes to the rescue. The concept is that your hardware will run a Firmata protocol server and you will use a client library to instruct the hardware to do the job. It is not ideal but it is one step forward then not using a functional programming language at all.

The Firmata protocol is based on the midi message format but fortunately, we don’t have to learn what they are as the client libraries are a good wrapper. Since we have picked Clojure we have the following client library choices:

I have chosen Clodiuno for this task simply because it has good documentation and the code is straight-forward. I noticed the examples in clj-Firmata are using channels in clojure which i don’t think is needed for a “hello world” program.

So lets start,

Step 1 is to load the Standard Firmata onto the ESP-01.

Connect the ESP-01 in programming mode with your computer.

ESP01 in Programming Mode
ESP01 in Programming Mode

Open Arduino IDE and from Files->Examples->Firmata, select StandardFirmata. Upload that onto the ESP-01, you should see something like this:

Ardunio IDE Firmata Upload
Ardunio IDE Firmata Uploaded

After loading the StandardFirmata onto the ESP-01 i have noticed that the built-in LED is always HIGH.

I am going to test the blinking LED example in Clojure to see if that works. Next step is to create a Clojure project. Then use the example provided by the Clodiuno for blinking LED.

From the Ardunio IDE, i know that my hardware is connected on “/dev/cu.wchusbserial1450” so i will replace that in the code for communication.

The next step is to compile and test the Clojure code. Fingers crossed.

Lets continue from here tomorrow and see what exciting results we get from the compilation.

I am looking forward to it.

Functional Programming with IOT (FIOT) – Day 1

Today, i have started exploring how we can use Functional Programming with hardware. Typically, hardware programming is done in C, Assembly or dialects of C/C++. It is easy to compile the code written in those languages and convert it into binary to burn it onto the hardware as a new firmware.

A simple google search will show you that there are people in the past like John Lee[1] who published a book in 1972, who presented the concept of describing hardware as notations. Then in 1985, Meshkinpour[2] and Ercegovac [3] proposed another Functional Programming style language called FHDL.  So the idea about using functional programming with hardware is not new but the approach that has always been taken is to develop a separate language from the mainstream software programming languages and look at hardware as a completely different world.

The book Infrastructure as Code disagrees with the idea that hardware should be treated separately from your software component. The reason for treating hardware separately in the past was that it was expensive and it took more effort to add a new hardware or get a bigger machine for your use case. There is no reason to do that anymore as hardware is more affordable and easily accessible.

So in the upcoming blog posts i shall explore one of the most famous ICs for Internet of Things ESP8266 using a functional programming language. I am interested in hardware parallelisation, performance and code simplicity. Also, i shall explore some design patterns for functional programming languages like function composability.

Lets start this journey!

 

References:

[1] J. A. N. Lee, Computer Semantics, Van Nostrand Reinhold Company, 1972.

[2] F. Meshkinpour and M. D. Ercegovac, “A functional language for description and design of digital systems: sequential constructs,” in Proceedings of the 22nd ACM/IEEE Conference on Design Automation, pp. 238–244, ACM Press, 1985. View at Scopus

[3] M. D. Ercegovac and T. Lang, “A high-level language approach to custom chip layout design,” Technical Report MICRO Project Reports 1982-83, University of California, Berkeley, Calif, USA, 1982. View at Google Scholar