DEV Community

Hector de Isidro
Hector de Isidro

Posted on

Internet of Things — from zero to DIY Bitcoin Ticker

tl;dr source code & gallery

I’m sure almost everyone has heard at least once about Arduino and/or Raspberry Pi low cost computers. Both are used in countless ingenious DIY electronic projects, on their own or by using one of the many expansion modules available, due to their low price and small credit-card size form factor (even smaller if we refer to Raspberry Pi Zero model).

But they aren’t the only ones (in fact there are many): among the most popular is NodeMCU aka The Arduino Killer🏆 (based on ESP8266 wifi-soc)

What are their differences?

They are totally different concepts because they were conceived to perform different tasks: while Arduino is actually an almost indestructible open source¹ microcontroller which we can start playing with using a simple script (in C/C++; one application at a time) from the minute one, Raspberry Pi is a full minicomputer² on one small board (SBC) that runs a dedicated Linux OS distribution (so it can be programmed using a wide variety of languages) which allows us to run different programs very much like we do on our desktop computers.

“If your application is more about controlling things, the Arduino is probably a better choice. While if you need to process lots of data the PI is your best bet”
Arduino vs Raspberry Pi — Which is best?

That said, more like a microcontroller than a minicomputer, we have our hero NodeMCU: an Arduino-compatible dev board with built-in WIFI for less than $5 that also supports Lua scripting language (there are several variants of ESP8266 chipset out there but it’s highly recommended to buy from the second-generation ESP-12E variant because it has a built-in power regulator).

NodeMCU on Arduino IDE

The Arduino platform has its own IDE (more of an editor than an IDE actually) available for free and it is quite easy to make it compatible with NodeMCU:

  1. Install standalone Arduino IDE following these instructions depending on your machine’s OS

  2. Launch Arduino IDE

Done!

Hello world!

Now that our development environment is ready, let’s verify that everything works well using one of the most easiest examples included: LED Blinking.

We will use stock “Arduino language” (limiting the project to a single file with .ino extension known as “sketch”) along this journey which is basically a set of C/C++ functions (IMO they have done an excellent job in general abstracting the different interfaces because even a beginner would able to read and understand easily almost all the code). Advanced use cases perhaps will need a real IDE³ plus the use of plain C/C++ language.

  1. Launch Arduino IDE

  2. Connect the board to the computer (it’s quite common to tape the back to ensure that no metal scrap can short-circuit the board)

  3. Select the correct COM port for your serial adapter (Tools > Port…)⁴

  4. Load Blink sketch (File > Examples > ESP8266 > Blink). Nothing to explain here as we can see the code is very easy to understand⁵.

  5. Add the following line to the very beginning of the example (because the example is written for an older module version and not for ESP-12): #define LED_BUILTIN 2

  6. Upload it and wait a few seconds (the led should blink quickly during the uploading)

Welcome to life!

Serial monitor

In order to make a real “Hello, World!” example we will modify the previous Blink example to print that well-known message.

  1. Add Serial.begin(9600); Serial.print(“Hello, World”); lines within setup() method

  2. Upload it

  3. Open Serial Monitor (Tools > Serial Monitor)

  4. Select 9600 baud option in the drop-down list

  5. Push the reset button on the board

Logging!

Who is out there?

As we have seen before, our tiny and dirt cheap NodeMCU has a built-in WIFI chipset. Therefore, the second thing we will do is display in the Serial Monitor all the wireless networks available around us using again one of the included examples: WifiScan (Files > Examples > ESP8266WiFi > WiFiScan).

Be sure to set the same baud rate in the code (Serial.begin(xxx);) as in the Serial Monitor drop-down list.

Home is where the WIFI connects

This time there is no example included showing us how to make the connection to our lovely Internet, so we will create a new script (File > New) containing the code below (as we can see the code is almost self-explanatory and doesn’t need any additional comment 🤞):

Check twice the ssid and/or password values before start to think the board is broken 😉

Do you even fetch, bro?

Now, it is time to start coding our project 👏, a Physical Bitcoin Ticker, fetching some data from a well-known free API⁶ (CoinMarketCap).

  1. Go to Tools > Manage Libraries, and search for ArduinoJson (6 or above version)

  2. Create a new sketch and paste the code below

  3. Fill in ssid, password and api_key variables (get your API key here⁶)

  4. Upload!

    There is a wrapper around CoinMarketCap API available for NodeMCU but it seems pretty abandoned and old so we will use our own implementation.

Rise & shine

At this point we all agree on this: it makes no sense to use an IoT device of any kind to dump the information to the Serial Monitor. So, we should use a display to make our BitCoin Ticker really useful.

Although there are a vast offer of I2C/SPI displays models (ie SSD1306 or SH1106 models) to buy (these kind of add-on boards are also known as “shields”) we don’t want to mess around with cables, GPIOs, breadboards, voltages and that sort of thing by the moment so, for just less than $7, we can buy an ESP8266+OLED built-in on PCB.

Using the easiest (also the cheapest) option described above we must install the U8g2lib library (even if we follow another path that library support almost all types of displays so I can stop recommending it), paste the code below and upload it.

We have to setup the display driver at the beginning using one of the default constructors the library give us. Printed on its back side we can find where SDA and SCL are connect to. For example SDA-D1 and SCL-D2 means that the first one is connected to GPIO5 (D1) and the second to GPIO04 (D2). So later we use these values as data (5 in this case) and clock (4).

Example working for the ESP8266+OLED said above: U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(*/* rotation=*/ *U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 4, /* data=*/ 5);

For more details on those parameters refer to this example.

Where do we go from here?

“To infinity and beyond!” — Buzz Lightyear

As we have already seen in a couple of hours and a few lines of code we can have our own Bitcoin Ticker running. So based on this and reading in depth all the shared links we will able to improve it or start a new project totally different. No limits! 🚀

Thanks for reading. If you enjoyed this article, feel free to hit that clap button 👏 as much as you can to help others find it.

This article was originally published on Medium


[1] We can buy an official Arduino in their online store for about $25 without WIFI module (supporting the company to continue researching and improving their products) but as it is an open source product there are some manufacturers who have created perfect clones that we can buy for less than $3 (Ebay is also a great place to buy them).
[2] One of the “biggest cons” of Raspberry Pi is that its hardware is not open source (Broadcom has an agreement with the Foundation to sell its chipset on exclusivity) so we cannot buy any cheaper Chinese clone anywhere.
[3] There are more powerful alternatives (personally I always stay as far as I can from that Eclipse thing) to the official “IDE” such as PlatformIO (which looks promising but perhaps quite complicated IMO) or this other Visual Studio Code extension for Arduino (which is backed by Microsoft)
[4] On macOS you will probably have to install one of these drivers (Bluetooth is not a valid port to uploading our sketches 😆)
[5] Anyway here is your new Bible.
[6] They will be migrating their Public API to a “Professional API” on Dec 4th, 2018. For our intentions their FREE tier meets our needs 😉


External links 👀

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.