Friday, May 8, 2015

Retrieving Bitcoin Price Using ESP8266 WiFi Module



Before you read this post, I recommend reading these tutorials about the ESP8266:

Introduction

In this project you’re going to create a simple HTTP client with an ESP8266 WiFi module. Having your ESP8266 connected to your network, it requests the current Bitcoin price. This is an example on how to  retrieve data from the web.


First, flash your ESP8266 module with NodeMCU

NodeMCU is a firmware that allows you to program the ESP8266 modules with LUA script. And you’ll find it very similar to the way you program your Arduino. With just a few lines of code you can establish a WiFi connection, control the ESP8266 GPIOs, turning your ESP8266 into a web server and a lot more. Follow this tutorial to Flash your ESP8266 with NodeMCU.

Where to buy?

You can click here to get this module on eBay for less than $4.


Schematics (3.3V FTDI Programmer)
The schematics for this project are very straight forward. You only need to establish a serial communication between your FTDI programmer and your ESP8266. You can buy one FTDI programmer on eBay.

Wiring:
  • RX -> TX
  • TX -> RX
  • CH_PD -> 3.3V
  • VCC -> 3.3V
  • GND -> GND

Downloading ESPlorer

I recommend using the ESPlorer program created by 4refr0nt to create and save LUA files into your ESP8266. Follow these instructions to download and install ESPlorer:
  1. Click here to download ESPlorer
  2. Unzip that folder
  3. Go to the dist folder (here’s the path: ESPlorer-master\ESPlorer\dist)
  4. Run ESPlorer.jar. It’s a JAVA program, so you need JAVA installed on your computer.
  5. Open the ESPlorer

How to send commands to your ESP8266

When you open the ESPlorer you should see a window similar to the preceding Figure, follow these instructions to send commands to your ESP8266:
  1. Connect your FTDI programmer to your computer
  2. Select your FTDI programmer port (COM8, for example)
  3. Press Open/Close
  4. Select NodeMCU+MicroPtyhon tab
  5. Copy the following script into ESPlorer
Note: You need to add your own network credentials to line 2.

wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR_NETWORK_NAME","YOUR_NETWORK_PASSWORD")
print(wifi.sta.getip())
conn=net.createConnection(net.TCP, 0) 
conn:on("receive", function(conn, payload) print(payload) end )
conn:connect(80,"api.coindesk.com")
conn:send("GET /v1/bpi/currentprice.json HTTP/1.1\r\nHost: api.coindesk.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

Then you simply click the button Send to ESP. And you should see a query appearing in your screen with the current Bitcoin price and a few other detailsEverything that you need to worry about or change is highlighted in red box in the following Figure.




Retrieved Data

Here’s the data that your ESP8266 is requesting, you can access it with your web browser right now: http://api.coindesk.com/v1/bpi/currentprice.json
Below you can see the data that your ESP8266 requested which is the current price of Bitcoins in USD, GPB and EUR.
{“time”:{“updated”:”Mar 4, 2015 15:10:00 UTC”,”updatedISO”:”2015-03-04T15:10:00+00:00″,”updateduk”:”Mar 4, 2015 at 15:10 GMT”},”disclaimer”:”This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org”,”bpi”:{“USD”:{“code”:”USD“,”symbol”:”$”,”rate”:”282.5891“,”description”:”United States Dollar”,”rate_float”:282.5891},”GBP”:{“code”:”GBP“,”symbol”:”£”,”rate”:”184.6234“,”description”:”British Pound Sterling”,”rate_float”:184.6234},”EUR”:{“code”:”EUR“,”symbol”:”€”,”rate”:”254.3166“,”description”:”Euro”,”rate_float”:254.3166}}}

Troubleshooting

Sometimes your ESP8266 doesn’t have enough time to send the HTTP request when you press “Send to ESP”. So It won’t show any data retrieved. You might want to use this feature on the ESPlorer that is called “Send current line to ESP8266“. And send each line of your script at a time.

Taking it further
You’re requesting the current Bitcoin price, that doesn’t seem that useful… right? Wrong! Just think for one second with a $4 WiFi module you can request any data from the web, how awesome is that?
Now my question is: which data is important for your project? The weather? Sport scores? Just type in your search engine: “weather api”, “finances api”, pretty much any term that contains some sort of data followed by api will be available to retrieve data.

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.

No comments:

Post a Comment