NodeMcu (2) LED Light Strips

クリスマスと新年の気分を出すため、LED Light Stripsを買った。しかし固定のパターンの点滅しかできないから厭きるね。

Raspberry Pi またはArduinoでコントルートできれば、何かいい物作れるではないからと考えた。

Arduinoできるなら安上がりなので、まず試す。LED Light Stripsは12V駆動なので、USB給電のWemosではなく、手元にあるNodeMcu V3 (電源拡張ボード付き)を活用することにした。

LED Light Stripsの電流が大きいので、GPIOの負荷能力を超え、直結できない。

Googleして、OmegaのLED lightstripsに繋ぐ回路があったので、それを真似してArduinoのつくる。

LED Probe2.png

手元に2N3904がないんで、ネットで購入。届くまで時間がかかるので、届いた頃も他の件で時間が取られ、半年以上棚上げ。

クリスマスも目の前だから、ハードウェアの回路を完成した。

プログラムの方は、簡単。

#define LEDR D7
#define LEDG D6
#define LEDB D8

void setup() 
{
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() 
{
  digitalWrite(LEDR, HIGH);
  delay(1000);
  digitalWrite(LEDG, HIGH);
  delay(1000);
  digitalWrite(LEDB, HIGH);
  delay(1000);
  digitalWrite(LEDR, LOW);
  delay(1000);
  digitalWrite(LEDG, LOW);
  delay(1000);
  digitalWrite(LEDB, LOW);
  delay(1000);
}

閃っている様子

 

 

NodeMcu (1) install & blink

NodeMCUとは

NodeMCUは、WeMosと同じコンセプトの開発ボードだ が、メインチップは ESP-12Eが使われている。

NodeMCUの種類

主に「LoLin NodeMCU」 「Amica NodeMCU」などがある。

WeMos、Lolin、Amica比較

WeMosD1 mini, AmicaのNodeMCUは小さいので、ブレッドボード1枚で使うことができ、Lolinは2枚ブレッドボードまたは専用のアダプターが必要。

WeMos
D1 mini
NodeMCU
Lolin Amica
MCU ESP-12 ESP-12E ESP-12E
ディジタルIO 11ピン 11ピン 12ピン
アナログIO 1ピン 1ピン 1ピン
3.3V出力 1ピン 3ピン 3ピン
5V入力(*3) 1ピン 1ピン 1ピン
USB出力(*5) 1ピン 1ピン なし
GNDピン 1ピン 4ピン 3ピン
SPI専用ピン なし あり あり
オンボードLED なし なし あり

 

Setup Arduino IDE

Arduino IDEがまたインストールしてないの場合、つかうPCにより、下記の何れを参考にArduino IDEのインストールする。

NodeMCUの種類により、USBシリアル変換チップに対応するドライバのインストールが必要。

「LoLin NodeMCU」

USBシリアル変換チップ:CH340 (WeMosと同じ)

UNO互換機を使っている方は、特に何も必要ないが、今まで互換機を使ったことが無い方はWeMosのホームページからCH340のドライバー(CH341SER_MAC.ZIP)をインストールする必要がある。

http://www.wch.cn/download/CH341SER_ZIP.html

「Amica NodeMCU」

USBシリアル変換チップ:CP210x

シリアル変換:CP2102のため、ドライバーのインストールが必要。

Silicon Labsの以下のURLから「Windows 7/8/8.1/10用」ドライバをダウンロード
USB – UART ブリッジ VCP ドライバ|Silicon Labs
ダウンロードしたZIP(CP210x_Windows_Drivers.zip)を展開し、CP210xVCPInstaller_x64.exeを実行し、ドライバインストールを開始する。

Blink

 

WeMos (c7) Web Clock

TM1637 7段4桁LED表示はできたけど、もっと何かWeb関係のものができないかと考えていて、時刻の表示くらいできると思って、やってみた。

WeMos (5) TM1637 7段4桁LED表示

WiFiManagerを利用してWiFiに接続。

結線ができるだけ省けると考えて、GPIOのPinから給電を試してみた。

プログラムはこれ、D4の隣はGNDだから、D4をVCCとして利用する様にプログラミングする。

#include <TM1637Display.h>
#include <time.h>
#include "WiFiManager.h"  
 
const int CLK = D2; //Set the CLK pin connection to the display
const int DIO = D3; //Set the DIO pin connection to the display
const int VCC = D4; //Set the VCC pin connection to the display
 
int numCounter = 0;
int numTime = 0;
String time_value;
 
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Hello!");

  WiFiManager wifiManager;
  if(!wifiManager.autoConnect()) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  } 

  configTime(0 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  pinMode(VCC, OUTPUT);
  digitalWrite(VCC, HIGH);
  display.setBrightness(0x04); //set the diplay to 0..7 brightness
}
 
 
void loop()
{
  for(numCounter = 0; numCounter < 1000; numCounter++) //Iterate numCounter
  {
    time_t now = time(nullptr);
    String time = String(ctime(&now));
    Serial.println("time:" + time);

    time_t utc, local;
    struct tm *tm_now;
    utc = now;
    local = utc + 9 *60 * 60;   // Tokyo time
    tm_now = localtime(&local);
    numTime = tm_now->tm_hour * 100 + tm_now->tm_min;
    Serial.printf("numTime: %d  ¥n", numTime);

    display.showNumberDecEx(numTime, 0x40); //Display the numCounter value;
    delay(500);
    display.showNumberDecEx(numTime, 0); //Display the numCounter value;
    delay(500);
  }
}

実動する画面:

デスクトップPCの肩に乗せた様子。

 

WeMos (d3) Web LED Matrix

最初はプレゼントタイマーを作るつもり。

しかし、TinyebDBの文字列を表示する方がはるかも簡単、汎用性もいい。

できたものはこれ:

電源が繋いたら、WiFiを自動で接続、それからTinyWebDBから文字列を取得と表示する。

これならプレゼントタイマーだけじゃなく、お知らせ、顔認証して名前で挨拶とかにも活用できそう。

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

WiFiClient client;

const char* resource = "http://tinydb.ml/api/";           // http resource
const unsigned long BAUD_RATE = 9600;      // serial connection speed
const unsigned long HTTP_TIMEOUT = 10000;  // max respone time from server
const size_t MAX_CONTENT_SIZE = 512;       // max size of the HTTP response

#include "WiFiManager.h"          //https://github.com/tzapu/WiFiManager

HTTPClient http;

int pinCS = D4; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays   = 1;
char time_value[20];

// LED Matrix Pin -> ESP8266 Pin
// Vcc            -> 3v  (3V on NodeMCU 3V3 on WEMOS)
// Gnd            -> Gnd (G on NodeMCU)
// DIN            -> D7  (Same Pin for WEMOS)
// CS             -> D4  (Same Pin for WEMOS)
// CLK            -> D5  (Same Pin for WEMOS)

Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);

int wait = 70; // In milliseconds

int spacer = 1;
int width  = 5 + spacer; // The font width is 5 pixels

void configModeCallback (WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  //if you used auto generated SSID, print it
  display_message(myWiFiManager->getConfigPortalSSID());
}

void setup() {
  configTime(0 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  matrix.setIntensity(10); // Use a value between 0 and 15 for brightness
  matrix.setRotation(0, 1);    // The first display is position upside down
  matrix.setRotation(1, 1);    // The first display is position upside down
  matrix.setRotation(2, 1);    // The first display is position upside down
  matrix.setRotation(3, 1);    // The first display is position upside down

  display_message("wifiManager autoConnect...");

  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset settings - for testing
  //wifiManager.resetSettings();

  //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  wifiManager.setAPCallback(configModeCallback);

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if(!wifiManager.autoConnect()) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  } 

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
}

void loop() {
  matrix.fillScreen(LOW);
  get_TinyWebDB("presentationtimer");
  delay(1000);
}

void display_message(String message) {
  if (message.length() > 5) scrool_message(message);
  else {
    for ( int i = 0 ; i < message.length(); i++ ) {
      matrix.drawChar(2 + i * width, 0, message[i], HIGH, LOW, 1); // 
    }
    matrix.write(); // Send bitmap to display
  }
}

void scrool_message(String message) {
  for ( int i = 0 ; i < width * message.length() + matrix.width() - spacer; i++ ) {
    //matrix.fillScreen(LOW);
    int letter = i / width;
    int x = (matrix.width() - 1) - i % width;
    int y = (matrix.height() - 8) / 2; // center the text vertically
    while ( x + width - spacer >= 0 && letter >= 0 ) {
      if ( letter < message.length() ) {
        matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background off, reverse to invert the image
      }
      letter--;
      x -= width;
    }
    matrix.write(); // Send bitmap to display
    delay(wait / 2);
  }
}


void get_TinyWebDB(const char* tag) {    
    int httpCode;
    char  tag2[32];
    char  value[128];

    httpCode = TinyWebDBGetValue(tag);

    // httpCode will be negative on error
    if(httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        if(httpCode == HTTP_CODE_OK) {
            String payload = http.getString();
            Serial.println(payload);
            const char * msg = payload.c_str();
            if (TinyWebDBreadReponseContent(tag2, value, msg)){
                TinyWebDBGotValue(tag2, value);
            }
        }
    } else {
        Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        TinyWebDBWebServiceError(http.errorToString(httpCode).c_str());
    }

    http.end();

    delay(1000);
}

// Parse the JSON from the input string and extract the interesting values
// Here is the JSON we need to parse
// [
//   "VALUE",
//   "LED1",
//   "on",
// ]
bool TinyWebDBreadReponseContent(char* tag, char* value, const char* payload) {
  // Compute optimal size of the JSON buffer according to what we need to parse.
  // See https://bblanchon.github.io/ArduinoJson/assistant/
  const size_t BUFFER_SIZE =
      JSON_OBJECT_SIZE(3)    // the root object has 3 elements
      + MAX_CONTENT_SIZE;    // additional space for strings

  // Allocate a temporary memory pool
  DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);

  // JsonObject& root = jsonBuffer.parseObject(payload);
  JsonArray& root = jsonBuffer.parseArray(payload);
  JsonArray& root_ = root;

  if (!root.success()) {
    Serial.println("JSON parsing failed!");
    return false;
  }

  // Here were copy the strings we're interested in
  strcpy(tag, root_[1]);   // "led1"
  strcpy(value, root_[2]); // "on"

  return true;
}

int TinyWebDBWebServiceError(const char* message)
{
}

// ----------------------------------------------------------------------------------------
// Wp TinyWebDB API
// Action        URL                      Post Parameters  Response
// Get Value     {ServiceURL}/getvalue    tag              JSON: ["VALUE","{tag}", {value}]
// ----------------------------------------------------------------------------------------
int TinyWebDBGetValue(const char* tag)
{
    char url[64];

    sprintf(url, "%s%s?tag=%s", resource, "getvalue/", tag);

    Serial.printf("[HTTP] %s\n", url);
    // configure targed server and url
    http.begin(url);
    
    Serial.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();

    return httpCode;
}

int TinyWebDBGotValue(const char* tag, const char* value)
{
    Serial.printf("[TinyWebDB] %s\n", tag);
    Serial.printf("[TinyWebDB] %s\n", value);

    display_message(value);
    delay(1000);
    display_message(value);

    
    return 0;   
}

 

TinyWebDBに文字列を用意方法はいくつもある。

  1. HTMLフォームで送信
  2. App Inventor で簡単なアプリ開発
  3. cURLで送信
  4. TinyWebDBサイトを直接いじる。
<h1>TinyWebDB test program</h1>

<form action="http://tinydb.ml/api/storeavalue" method="post">
  <div>tag: <input type="text" name="tag" value="presentationtimer"></div>
  <div>value: <input type="text" name="value" value="12:13"></div>
  <input type="submit" value="submit">
  <input type="reset" value="reset">
</form>