FIOT – Chapter 3 – The Demo

Following is the demo that i presented at the Lambda Lounge.

Slides that i prepared but never presented as audience was seeking more excitement, so i went freestyle.

 

https://docs.google.com/presentation/d/1F3EY5B8HbqHT4hJ4J6G98tbnuzAmoAKYtvthJN9mSWo/edit?usp=sharing

 

Enjoy!

FIOT – Chapter 2 : Controlling multiple ESP-01 – Success !!

Previously, i was controlling the ESP-01 over TCP/IP using NodeJS but my main target from the beginning was to control it using Clojure. I have finally made it just two days before the actual demo in Lambda Lounge.

So all the details are in my previous post, the only difference is that this one is a Clojure version, i will build on top of it to create the demo that i would be presenting on Monday.

Here is a summary if you don’t want to read the previous post, i have flashed StandardFirmataWifi onto an ESP8266 IOT Module, to be specific the module is ESP-01. I am controlling the GPIO pin 0 of that module using Clojure without using any external library.

The communication is carried out using the messages in MIDI format. The IP Address of my ESP-01 on the network was 192.168.0.18 and the port open in the flashed version of StandardFirmataWifi was 3030.

So here is the code:


(import (java.net Socket))

(def socket (Socket. "192.168.0.18" 3030))

(def SET-PIN-MODE     0xF4)
(def RESERVED-COMMAND 0x00)
(def DIGITAL-MESSAGE  0x90)
(def PIN              0x00)
(def OUTPUT-MODE      0x01)
(def LOW              0x01)
(def HIGH             0x00)


(defn send-command [& bs]
  (let [out (.getOutputStream socket)]
    (doseq [b bs]
      (.write out b))
    (.flush out)))


(send-command SET-PIN-MODE    PIN  OUTPUT-MODE)
(send-command DIGITAL-MESSAGE LOW  RESERVED-COMMAND)
(send-command DIGITAL-MESSAGE HIGH RESERVED-COMMAND)

I am going to start building the main demo now and will add pictures for it. Stay in touch with the blog feed if you are interested or just show up in the Lambda Lounge to see the LIVE DEMO.

Functional Programming with IOT – Chapter 2 : Controlling multiple ESP-01 – Progress Update

In the introduction of this chapter, i mentioned about several technologies that can be used for controlling multiple ESP8266. This blog post explores them further in detail and describes a way of controlling multiple ESP8266 using a javascript v8 library.

The challenging part of controlling multiple ESP8266 is that it is a physical hardware and if i start plugging each one of them onto my machine’s serial port then i will run out of them. Thankfully computer science has already solved this problem by introducing the topic of networking. ESP8266 is a successful IOT module due to its capability to connect to any wireless network, this means we can connect multiple one of them to the same network and send commands to make them do things.

In my day 13 blog post, i explained how to use Firmata to send commands to a ESP-01 module which is connected to your serial port. This blog post is taking the same concept further but instead of using StandardFirmata we will be using StandardFirmataWifi. This gives you the ability to control Firmata over a wireless network and in theory you can interact with multiple one of them. Make sure you have the correct board configuration in your firmata library as described in day 13 blog post as that is a must requirement for ESP-01.

So here is what i did.

I flashed my ESP-01 with a StandardFirmataWifi with custom configuration for my wireless settings as described in the following screenshot:

Wifi Configuration for ESP-01
Wifi Configuration for ESP-01

 

I then connected this ESP-01 to an independent power supply.

ESP-01 Connected to Independent Power Supply
ESP-01 Connected to Independent Power Supply

Clojure libraries are hard to find for this particular kind of problem and i quickly wanted to test the theory so i went with a javascript v8 solution.

Following is the code that i used to send HIGH and LOW signals on pin 0.


'use strict';

var five = require("johnny-five");
var EtherPortClient = require("etherport-client").EtherPortClient;
// update host to the IP address for your ESP board
var board = new five.Board({
    port: new EtherPortClient({
        host: "192.168.0.18", //ESP-01 IP Adress
        port: 3030 //Port that we configured in wifi config
    }),
    timeout: 1e5,
    repl: false
});


console.log("Starting");
board.on("ready", function() {
      console.log("READY!");
      var led = new five.Led(0);
      console.log("BLINKING...");
      led.blink(500);
});

Following is my terminal screenshot, as you can see when you connect to the module you get a confirmation or “ready” back which means that you can now start sending HIGH and LOW signals to the module.

Running the code in terminal
Running the code in terminal

Then i verified the packets are being sent out from my machine by using a packet sniffing tool called “wireshark”.

Verifying the TCP Packets are being sent out
Verifying the TCP Packets are being sent out

As you can see we have source 192.168.0.11 which is my host machine and destination 192.168.0.18 which is ESP-01 module. I found out the ip address for the ESP8266 through my wifi router’s admin interface in the connected devices section.

Next, i wanted to verify the output of the signals that i was sending so i used a multimeter to see if the voltage is fluctuating between a HIGH and LOW and it was.

ESP-01 - Voltage High
ESP-01 – Voltage High

This proves that we can connect multiple of them to the same network and send commands to them just by adding multiple boards in the code. But our topic is functional programming with IOT and the language that we chose to demonstrate that it is possible is Clojure. So the next step is to repeat the same experiment but in Clojure Language.

Stay in touch, i will soon be posting an update on that.

Functional Programming with IOT – Chapter 2 : Controlling multiple ESP-01

In my previous blog posts, i was verifying the theory of using a functional programming language like Clojure with a very well-known IOT module ESP8266. It was a great success and we can see that we can now control that module’s GPIO pins by sending HIGH and LOW signals.

Now begins the next chapter. In this chapter we shall explore how we can control multiple of them. I would be asking myself questions like is it possible to control them synchronously and asynchronously? Can i use Clojure pipelines ? How about using Kafka ?

Traditionally, MQTT brokers are used for controlling multiple IOT modules. You can read more about using them on Intel’s blog post Using Intel® Edison: Securely Connect IoT Sensor to the Internet with MQTT. This is a widely supported protocol and therefore you don’t have to setup these brokers yourself, you can simply use Amazon’s MQTT Message Broker.

My next steps are basically using that and understanding how they fit with Clojure and Firmata.

I have a speech at Lamda Lounge where i would be demonstrating my findings LIVE. You can also follow this blog to see updates.

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.