Created
May 15, 2020 01:49
-
-
Save shikarunochi/da2a800224d61ebee80323c21213fa21 to your computer and use it in GitHub Desktop.
ニュース情報を取得してOLED表示 for M5Atom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5Atom.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include "efontEnableJa.h" | |
#include "efontSSD1306.h" | |
#include <WiFi.h> | |
#include <WiFiClientSecure.h> | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 32 // OLED display height, in pixels | |
//https://qiita.com/ichirowo/items/d794cee88ccd7f01ad7c | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
const char* ssid = "[YOUR_SSID]"; | |
const char* password = "[YOUR_PASSWORD]"; | |
WiFiClient client; | |
String buffer; | |
void setup() { | |
Serial.begin(115200); | |
M5.begin(true,false,true); | |
Wire.begin(23, 33); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 | |
Serial.println(F("SSD1306 allocation failed")); | |
for (;;); // Don't proceed, loop forever | |
} | |
delay(1000); | |
display.clearDisplay(); | |
display.setTextSize(1); // Normal 1:1 pixel scale | |
display.setTextColor(SSD1306_WHITE); // Draw white text | |
display.setCursor(0, 0); // Start at top-left corner | |
WiFi.mode(WIFI_STA); // STAモードで動作 | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
display.print(F(".")); | |
display.display(); | |
} | |
display.clearDisplay(); | |
display.setCursor(0, 0); | |
//display.print("IP: "); | |
//display.println(WiFi.localIP()); | |
display.print("Wi-Fi: OK"); | |
display.display(); | |
delay(500); | |
displayTitles(); | |
} | |
bool readDate() { | |
buffer = ""; | |
String host = "trends.google.co.jp"; | |
String url = "/trends/trendingsearches/daily/rss?geo=JP"; //Google Trend | |
WiFiClientSecure client; | |
client.setTimeout(500); | |
const int httpPort = 443; | |
const char* host2 = host.c_str(); | |
if (!client.connect(host2, httpPort)) { | |
Serial.println("connection failed"); | |
return false; | |
} | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"User-Agent: M5AtomLite/1.0\r\n" + | |
"Connection: close\r\n\r\n"); | |
unsigned long timeout = micros(); | |
while (client.available() == 0) { | |
if ( micros() - timeout > 5000000) { | |
Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
return false; | |
} | |
} | |
while (client.available()) { | |
String line = client.readStringUntil('\r'); | |
buffer.concat(line); | |
} | |
return true; | |
} | |
String startTag = "<ht:news_item_title>"; | |
String endTag = "</ht:news_item_title>"; | |
void displayTitles() | |
{ | |
readDate(); | |
int offset = 0; | |
if (buffer.length() > 0) { | |
while (true) { | |
//<ht:news_item_title></ht:news_item_title> までの間を取得 | |
int startIndex = buffer.indexOf(startTag, offset) + startTag.length(); | |
int endIndex = buffer.indexOf(endTag, offset); | |
if (startIndex < 0 || endIndex < 0) { | |
break; | |
} | |
//Serial.printf("Offset:%d StartIndex:%d EndIndex:%d \n", offset,startIndex,endIndex); | |
offset = endIndex + 1; | |
String title = buffer.substring(startIndex, endIndex); | |
scrollTitle(title); | |
} | |
} else { | |
display.print("News Get Failed"); | |
display.display(); | |
} | |
} | |
void scrollTitle(String title) { | |
//title分は全部バッファに入れておき、単純に表示位置だけ変える。 | |
char charBuf[title.length() * 2 + 1]; | |
title.toCharArray(charBuf, title.length() + 1); | |
for (int offset = 0 ; offset < title.length() * 8 - 16 ; offset++) { | |
display.clearDisplay(); | |
printEfont(&display, charBuf , 16 * 8 - offset, 12, 2); | |
//下の行に改行されて表示される分を消す | |
display.fillRect(0, 12 + 16, 128, display.height() - (12 + 16), SSD1306_BLACK); | |
display.display(); | |
delay(10); | |
} | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment