In this tutorial, we will be going over how to display information about microsoft stock, or any stock for that matter to TFT LCD Screen Module on a Wia Dot One.
Figure 1
Requirements- Wia Dot One Buy yours here
- Grove Servo Motor Buy yours here
- Micro USB Cable Buy yours here
- Battery Power Pack Buy yours here
- Account on Wia
- Account at Wia You will need to be registered and /or logged in to your Wia account athttps://www.wia.io/.
- Set up your Dot One You will need to have a set up Dot One and you can find the tutorial on how to do that Here.
- Set up your code project Now you will need to create a space and then a code project on your Wia Dashboard
- Note: This portion requires a decent background in coding, specifically in C++ in order to understand how the Dot One and TFT Screen work to print the stock opening and closing prices
- Below is the code for this project which you can follow by reading the comments attached to the side of the code.
- Feel free to try and rewrite this code yourself so you can better understand the device! Some advice; if you are having trouble, make a temporary Block Project and take note of what code is pasted into the preview window when you drag certain blocks over!
#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <Wia.h>
#define TFT_RST -1
#define TFT_CS 16
#define TFT_DC 17
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Wia wiaClient = Wia();
int i = 0;
String price = "";
String previous_open = "";
String previous_close = "";
int iterator = 0;
void powered_by() { // this is a function which is used to display the header "Powered by: Wia Dot One"
// each of these sections is specific to different texts which are printed in the TFT scree
// as you can see each time for new text we define where the text starts, the color, the size and rotation
tft.setCursor(5, 5); // this function moves where this snipet of text "Powered by:" is
tft.setTextColor(ST7735_BLACK); // this functioin changes the color of the text "Powered by:"
tft.setTextSize(1); // this defines the size of the text "Powered by:"
tft.setRotation(1); // this sets the rotation of the text "Powered by:"
tft.println("Powered by:"); // this displays the text "Powered by:" with the settings defined above
// the following 4 sections of code work the same but are for different pieces of text.
tft.setCursor(85, 12);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(2);
tft.setRotation(1);
tft.println("Wia");
tft.setCursor(95, 0);
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(2);
tft.setRotation(1);
tft.println(".");
tft.setCursor(55, 30);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(2);
tft.setRotation(1);
tft.println("Stock");
}
void setup()
{
WiFi.begin();
delay(2500);
tft.initR(INITR_144GREENTAB);
tft.fillScreen(ST7735_WHITE);
}
void loop()
{
if(previous_open != wiaClient.getDeviceState("MSFT_Open") || previous_close != wiaClient.getDeviceState("MSFT_Close") || i == 0)
{
// This block covers the previous Open text in white keeping the TFT from flashing
tft.setCursor(50, 70);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(2);
tft.setRotation(1);
tft.println(previous_open);
// This then prints the new Open price in black
tft.setCursor(50, 70);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(2);
tft.setRotation(1);
tft.println(wiaClient.getDeviceState("MSFT_Open").substring(0,6)); // BTC
previous_open = wiaClient.getDeviceState("MSFT_Open");
// This block covers the previous text in white keeping the TFT from flashing
tft.setCursor(50, 110);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(2);
tft.setRotation(1);
tft.println(previous_close);
// This then prints the new Open price in black
tft.setCursor(50, 110);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(2);
tft.setRotation(1);
tft.println(wiaClient.getDeviceState("MSFT_Close").substring(0,6)); // BTC
previous_close = wiaClient.getDeviceState("MSFT_Close");
tft.setCursor(3, 30);
tft.setTextColor(ST7735_ORANGE);
tft.setTextSize(2);
tft.setRotation(1);
tft.println("MSFT");
tft.setCursor(3, 54);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(2);
tft.setRotation(1);
tft.println("Open:");
tft.setCursor(3, 90);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(2);
tft.setRotation(1);
tft.println("Close:");
powered_by();
}
i++;
delay(3600000);;
}
Finding your API Key for Alpha Advantage- For this project we need to find an API that can provide us with the information we need about Microsofts stock and we will be using https://www.alphavantage.co
- Go to this linkand put in your information to get your free API key which can be found under the get free API key button when you put in correct information above and press the button.
Figure 2
- Once you have this API key you can use the following link to make the Http request although you need to replace where it says demo with the API key you found for yourself above.
https://www.alphavantage.co/query?apikey=demo&function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT
- For example if I found my API key was “ABCDEFGHIJKLMNOPQ” the http request for me would be:
Note:These links are actually all on one line they are just too long to physically fit on one line on this page.
- If you would like to use these http request to find other stocks you can do this by simply changing the MSFT at the end of the link to whatever other stock you want such as AAPL for Apple, or GOOGL for Google.
- It can be hard to read these JSON files so this is not required but I recommend you add a JSON viewer extension to your browser. I use JSON Viewer Awesome which can be found in the Google Chrome Web store under extensions here.
Now we will create a flow that will go to this link and parse through the data so we can get the opening and closing prices.
- Proceed by going to your dashboard and create a new flow
- Drag Timer node which can be found under Trigger > Wia and set the timer to trigger every 30 minutes as seen in figure 3.
Figure 3
- Now drag a HTTP Services node into the workspace and make the method Get and then in the URL paste the http request we found for instance I pasted: https://www.alphavantage.co/query?apikey=ABCDEFGHIJKLMNOPQ&function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFTas seen in figure 4.
Figure 4
- Now drag a Run Function node from logic > Wia into the workspace and copy the following code into the run function.
if(input.body)
{
let response = JSON.parse(input.body);
let timeSeries = response['Time Series (Daily)'];
let datetime = new Date();
let daily = timeSeries[datetime.toISOString().slice(0,10)];
output.body.data =
{
open: daily['1. open'],
close: daily['4. close']
};
}
- This code takes the input from the http request and parses through it to find the “Open” and “Close” price. It is like a following the path of “Time Series (Daily)” to “2019-07-09” to “1. open” and “4. close”. If you have the JSON viewer when you put the http request in your own search bar you can see this path as I see it in figure 5.
Figure 5
- Now drag two Update State nodes from logic > Wia into the workspace and select your Dot One from the list in both nodes and one of these update states will be responsible for the Open and one for the Close.
- The Open key should be what you named the state in the code project which for us is MSFT_Open while the value parses through the data input to open, thus the value should be ${input.body.data.open} as seen in figure 6.
- The Close key should be what you named the state in the code project which for us is MSFT_Close while the value parses through the data input to open. Thus, value should be ${input.body.data.close} as seen in figure 7.
Figure 6
Figure 7
- Now connect your nodes as seen on figure 8, turn on the flow, deploy your code to your Dot One and you’re all set!
Figure 8
Comments