跳到主要内容

Reading Github Repository Stats from Wio Terminal

This wiki introduces how to use Wio Terminal to access to HTTPS server, which means you could use Wio Terminal to grab live data from the internet! Here, Wio Terminal is connected to a Wi-Fi and obtain data from the Github.

With this, you can implement the Demo to others api such as grabbing Weather information, Youtube Stats and etc.

Part List

Features

  • Connects to a https server

  • Grab repo stats from Github

  • Can change to any Github Repositories

Arduino libraries needed

  • Install the LCD screen Library Seeed_Arduino_LCD, please visit Wio Terminal LCD for more information.

  • Visit the ArduinoJson repositories and download the entire repo to your local drive.

    • Now, the ArduinoJson library can be installed to the Arduino IDE. Open the Arduino IDE, and click sketch -> Include Library -> Add .ZIP Library, and choose the ArduinoJson file that you've have just downloaded.
  • For more information about Https connection, please visit here.

Arduino Instructions

  • Download the Free_Fonts.h header file so that can use abbreviations for the free fonts included in the LCD library. Please make sure to put this header file in the same location as the Arduino Sketch.

  • Download the Complete code here or copy the following.

  • Upload the code.

Complete Code

Note: Change the ssid and password to your Network Wi-Fi.

#include "rpcWiFi.h"
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include"Free_Fonts.h"
#include"TFT_eSPI.h"
TFT_eSPI tft;

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

//29*27
const unsigned char watchIMG[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00,
0x00, 0xFC, 0x07, 0x00, 0x00, 0xFF, 0x1F, 0x00, 0x80, 0x1F, 0x3F, 0x00,
0xC0, 0x07, 0x7C, 0x00, 0xE0, 0x03, 0xF8, 0x00, 0xF0, 0xE1, 0xF0, 0x01,
0xF8, 0xF1, 0xF1, 0x01, 0xF8, 0xF8, 0xF1, 0x03, 0xFC, 0xF8, 0xF1, 0x03,
0xF8, 0xF0, 0xF1, 0x03, 0xF0, 0xF1, 0xF0, 0x01, 0xF0, 0x01, 0xF8, 0x00,
0xE0, 0x03, 0x78, 0x00, 0xC0, 0x07, 0x3E, 0x00, 0x80, 0xFF, 0x1F, 0x00,
0x00, 0xFE, 0x0F, 0x00, 0x00, 0xF8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char starIMG[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
0x00, 0xC0, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xE0, 0x01, 0x00,
0x00, 0xF0, 0x01, 0x00, 0x00, 0xF0, 0x03, 0x00, 0x00, 0xF8, 0x03, 0x00,
0x00, 0xF8, 0x07, 0x00, 0x00, 0xFE, 0x0F, 0x00, 0xFE, 0xFF, 0xFF, 0x1F,
0xFE, 0xFF, 0xFF, 0x0F, 0xFC, 0xFF, 0xFF, 0x07, 0xF8, 0xFF, 0xFF, 0x03,
0xF0, 0xFF, 0xFF, 0x01, 0xC0, 0xFF, 0xFF, 0x00, 0x80, 0xFF, 0x7F, 0x00,
0x00, 0xFF, 0x3F, 0x00, 0x80, 0xFF, 0x3F, 0x00, 0x80, 0xFF, 0x3F, 0x00,
0x80, 0xFF, 0x3F, 0x00, 0x80, 0xFF, 0x7F, 0x00, 0x80, 0xBF, 0x7F, 0x00,
0xC0, 0x1F, 0x7E, 0x00, 0xC0, 0x07, 0x78, 0x00, 0xC0, 0x01, 0x70, 0x00,
0x40, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char forkIMG[] = {
0xF8, 0x00, 0x00, 0x00, 0xFE, 0x03, 0x00, 0x00, 0x0F, 0x07, 0x00, 0x01,
0x07, 0x07, 0xE0, 0x0F, 0x07, 0x07, 0x78, 0x1E, 0x9E, 0x07, 0x38, 0x38,
0xFC, 0x01, 0x38, 0x38, 0xF8, 0x00, 0x38, 0x3C, 0xF8, 0x00, 0xF0, 0x1F,
0xF8, 0x00, 0xE0, 0x07, 0xF8, 0x00, 0xC0, 0x07, 0xF8, 0x00, 0xE0, 0x07,
0xF8, 0x00, 0xE0, 0x07, 0xF8, 0x00, 0xF8, 0x03, 0xF8, 0x00, 0xFE, 0x01,
0xF8, 0xE0, 0xFF, 0x00, 0xF8, 0xFF, 0x7F, 0x00, 0xF8, 0xFF, 0x1F, 0x00,
0xF8, 0xFF, 0x03, 0x00, 0xF8, 0x1F, 0x00, 0x00, 0xF8, 0x03, 0x00, 0x00,
0xF8, 0x01, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x00,
0x9E, 0x07, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00,
0x0F, 0x07, 0x00, 0x00, 0xFE, 0x03, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00,
};

const char* server = "api.github.com"; // Server URL

const char* test_root_ca = \
"-----BEGIN CERTIFICATE-----\n"
"MIIEsTCCA5mgAwIBAgIQBOHnpNxc8vNtwCtCuF0VnzANBgkqhkiG9w0BAQsFADBs\n"
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
"d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n"
"ZSBFViBSb290IENBMB4XDTEzMTAyMjEyMDAwMFoXDTI4MTAyMjEyMDAwMFowcDEL\n"
"MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\n"
"LmRpZ2ljZXJ0LmNvbTEvMC0GA1UEAxMmRGlnaUNlcnQgU0hBMiBIaWdoIEFzc3Vy\n"
"YW5jZSBTZXJ2ZXIgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK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SHwD2f2LSaCYJkJA69aSEaRkCldUxPUd1gJea6zu\n"
"xICaEnL6VpPX/78whQYwvwt/Tv9XBZ0k7YXDK/umdaisLRbvfXknsuvCnQsH6qqF\n"
"0wGjIChBWUMo0oHjqvbsezt3tkBigAVBRQHvFwY+3sAzm2fTYS5yh+Rp/BIAV0Ae\n"
"cPUeybQ=\n"
"-----END CERTIFICATE-----\n";

// You can use x.509 client certificates if you want
//const char* test_client_key = ""; //to verify the client
//const char* test_client_cert = ""; //to verify the client

String github_data;

WiFiClientSecure client;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);

tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setCursor((320 - tft.textWidth("Connecting to Wi-Fi.."))/2, 120);
tft.print("Connecting to Wi-Fi..");

// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
}

Serial.print("Connected to ");
Serial.println(ssid);

tft.fillScreen(TFT_BLACK);
tft.setCursor((320 - tft.textWidth("Connected!"))/2, 120);
tft.print("Connected!");

getData();
}

void loop() {
// do nothing
}

void getData() {
client.setCACert(test_root_ca);
//client.setCertificate(test_client_key); // for client verification
//client.setPrivateKey(test_client_cert); // for client verification

tft.fillScreen(TFT_BLACK);
tft.setCursor((320 - tft.textWidth("Conectting to Server.."))/2, 120);
tft.print("Connecting to Server..");

Serial.println("\nStarting connection to server...");
if (!client.connect(server, 443)) {
Serial.println("Connection failed!");
tft.fillScreen(TFT_BLACK);
tft.setCursor((320 - tft.textWidth("Connection failed!"))/2, 120);
tft.print("Connection failed!");
} else {
Serial.println("Connected to server!");
tft.fillScreen(TFT_BLACK);
tft.setCursor((320 - tft.textWidth("Connected to Server!"))/2, 120);
tft.print("Connected to Server!");

// Make a HTTP request:
client.println("GET https://api.github.com/repos/micropython/micropython HTTP/1.0");
client.println("Host: api.github.com");
client.println("User-Agent: Seeed-Studio");
client.println("Connection: close");
client.println();

while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}

while(client.available())
{
String line = client.readStringUntil('\r');
github_data = line;
}
//Serial.println(github_data);
client.stop();
Serial.println("closing connection");
}

//ArduinoJson to parse data, plesae check ArduinoJson for more info
const size_t capacity = 2*JSON_OBJECT_SIZE(18) + JSON_OBJECT_SIZE(77) + 6050;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, github_data);

int subscribers_count = doc["subscribers_count"];
int forks = doc["forks"];
int stargazers_count = doc["stargazers_count"];

Serial.println(subscribers_count);
Serial.println(forks);
Serial.println(stargazers_count);

//TFT SCREEN SESSION
tft.setFreeFont(FSS9);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE);
tft.fillScreen(TFT_BLACK);
tft.drawString("MicroPython Github",20,10);
tft.drawFastHLine(185,18,120 ,TFT_RED);
tft.drawFastHLine(185,19,120 ,TFT_RED);
tft.drawFastHLine(185,20,120 ,TFT_RED);
tft.drawFastHLine(10,33,300 ,TFT_NAVY);
tft.drawFastHLine(10,34,300 ,TFT_NAVY);
tft.drawFastHLine(10,35,300 ,TFT_NAVY);

tft.drawXBitmap(20,60, watchIMG, 29, 27, TFT_WHITE);
tft.drawXBitmap(20,120, starIMG, 30, 30, TFT_WHITE);
tft.drawXBitmap(20,180, forkIMG, 30, 30, TFT_WHITE);

tft.setFreeFont(FMB9);
tft.drawString("Watchers: ",60, 68);
tft.drawString("Stars: ",60, 128);
tft.drawString("Forks: ",60, 188);

tft.drawNumber(subscribers_count, 165, 68);
tft.drawNumber(stargazers_count, 130, 128);
tft.drawNumber(forks, 130, 188);
}
Loading Comments...