Category Archives: WeMos D1 Mini (e) Firebase

WeMos (e3) リレー制御

Published by:

制御の試し

参考資料1そのまま。

/*
 * Relay Shield - Blink
 * Turns on the relay for two seconds, then off for two seconds, repeatedly.
 *
 * Relay Shield transistor closes relay when D1 is HIGH
 */

const int relayPin = D1;
const long interval = 2000;  // pause for two seconds

void setup() {
  pinMode(relayPin, OUTPUT);
}

void loop() {
  digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
  delay(interval);              // pause
  digitalWrite(relayPin, LOW);  // turn off relay with voltage LOW
  delay(interval);              // pause
}

 

firebaseから制御

FAN_Onエントリーをみて、リレーを制御。

 

#include "Firebase_ESP_Client.h"
#include <ESP8266WiFi.h>

#define relayPin D1

#define FIREBASE_HOST "test2-***.firebaseio.com"                    // Enter the Firebase Database URL Without Https and backslash
#define API_KEY "AIzaSyDfl-s-7-**********"

FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

FirebaseJson json;

#define WIFI_SSID "SSID003"                     // Change the name of your WIFI
#define WIFI_PASSWORD "12345"                        // Change the password of your WIFI

#define USER_EMAIL "xxx@yyy.net"
#define USER_PASSWORD "12345"

void setup() {
  pinMode(relayPin, OUTPUT);
    
  Serial.begin(115200);
  Serial.println("Relay for FAN Test");

  WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) 
  {
   delay(500);
   Serial.print(".");
  }

  Serial.println ("");
  Serial.println ("WiFi Connected!");
  
  /* Assign the project host and api key (required) */
  config.host = FIREBASE_HOST;
  config.api_key = API_KEY;

  /* Assign the user sign in credentials */
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;

  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
}


void loop() {
String target;

//  if (Firebase.RTDB.getBool(&fbdo, "Thermostat/FAN_On")) {
//    target = fbdo.boolData();
  if (Firebase.RTDB.getString(&fbdo, "Thermostat/FAN_On")) {
    target = fbdo.stringData();
    Serial.println(target);
    Serial.println(fbdo.dataType());
    digitalWrite(relayPin, target.equals("true"));
  }
  else
  {
    Serial.println("FAILED");
    Serial.println("REASON: " + fbdo.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }
  delay(500);
}

 

 

参考

  1. https://www.wemos.cc/en/latest/d1_mini_shield/relay.html — リレーの情報

WeMos (e2) LED表示

Published by:

IoT Study Kit 2のLEDは、FirebaseのLEDデータに従って表示。

プログラム1

単純に文字、パタンの表示。

3連ベースで、ESP8266, LED Matrix, SHT30を装着する。

 

 

Features

  • 8×8 dot matrix LED
  • 8 step adjustable intensity

Pins

D1 mini GPIO Shield
D5 14 CLK
D7 13 DIN

 

ライブラリから、WEMOS_Matrix_Adafruit_GFXを追加してください。

つぎは、ライブラリ付属のLED Matrix表示サンプルの修正版。

# コンパイルエラー発生した。

MLED matrix(7);  から

MLED matrix(7, D7, D5); に変更した。

 

#include <Adafruit_GFX.h>
#include <WEMOS_Matrix_GFX.h>

MLED matrix(7, D7, D5); //set intensity=7 (maximum)

void setup() {
  Serial.begin(9600);
  Serial.println("8x8 LED Matrix Test");
  
 
}

static const uint8_t PROGMEM
  smile_bmp[] =
  { B00111100,
    B01000010,
    B10100101,
    B10000001,
    B10100101,
    B10011001,
    B01000010,
    B00111100 },
  neutral_bmp[] =
  { B00111100,
    B01000010,
    B10100101,
    B10000001,
    B10111101,
    B10000001,
    B01000010,
    B00111100 },
  frown_bmp[] =
  { B00111100,
    B01000010,
    B10100101,
    B10000001,
    B10011001,
    B10100101,
    B01000010,
    B00111100 };

void loop() {
  matrix.clear();
  matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
  matrix.writeDisplay();
  delay(500);

  matrix.clear();
  matrix.drawBitmap(0, 0, neutral_bmp, 8, 8, LED_ON);
  matrix.writeDisplay();
  delay(500);

  matrix.clear();
  matrix.drawBitmap(0, 0, frown_bmp, 8, 8, LED_ON);
  matrix.writeDisplay();
  delay(500);

  matrix.clear();      // clear display
  matrix.drawPixel(0, 0, LED_ON);  
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.clear();
  matrix.drawLine(0,0, 7,7, LED_ON);
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.clear();
  matrix.drawRect(0,0, 8,8, LED_ON);
  matrix.fillRect(2,2, 4,4, LED_ON);
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.clear();
  matrix.drawCircle(3,3, 3, LED_ON);
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.setTextSize(1);
  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextColor(LED_ON);
  for (int8_t x=0; x>=-36; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("Hello");
    matrix.writeDisplay();
    delay(100);
  }
  matrix.setRotation(3);
  for (int8_t x=7; x>=-36; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("World");
    matrix.writeDisplay();
    delay(100);
  }
  matrix.setRotation(0);
}

 

プログラム2

Firebaseのデータ(/Speech) をLEDに表示する

Firebaseへのコンタクトは、MobizTのライブラリを利用することで成功した。

https://github.com/mobizt/Firebase-ESP8266

#include <Adafruit_GFX.h>
#include <WEMOS_Matrix_GFX.h>
#include "FirebaseESP8266.h"
#include <ESP8266WiFi.h>

MLED matrix(7, D7, D5); //set intensity=7 (maximum)

#define PIN D1
#define NUM_LEDS 8

const char* ssid = "ssid003";
const char* password = "12345";

FirebaseData firebaseData;

// Current color values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("8x8 LED Matrix Test");

  connectWifi();
  
  Firebase.begin("https://test2.firebaseio.com/", "********4WxzwiCI");
}


void loop() {
String target;

  if (Firebase.getString(firebaseData, "/Speech")) {
    target = firebaseData.stringData();
    Serial.println(target);
    Serial.println(firebaseData.dataType());
    matrix.setTextSize(1);
    matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
    matrix.setTextColor(LED_ON);
    for (int8_t x=0; x>=-56; x--) {
      matrix.clear();
      matrix.setCursor(x,0);
      matrix.print(target);
      matrix.writeDisplay();
      delay(100);
    }
  }
  else
  {
    Serial.println("FAILED");
    Serial.println("REASON: " + firebaseData.errorReason());
    Serial.println("------------------------------------");
    Serial.println();
  }
  delay(500);
}

void connectWifi() {
  // Let us connect to WiFi
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println(".......");
  Serial.println("WiFi Connected....IP Address:");
  Serial.println(WiFi.localIP());

}

 

 

WeMos (e1) 温度の送信

Published by:

IoT Study Kit 2からFirebaseへ温度データの送信。

 

IoT Sensor

UbiSense Ver2を利用。

結線

プログラム1

3連ベースで、ESP8266, LED Matrix, SHT30を装着。温度と湿度を計測。

WEMOS_SHT3x_Arduino_Libraryを使用。

https://github.com/wemos/WEMOS_SHT3x_Arduino_Library

ただの温度と湿度の表示サンプル。

#include <WEMOS_SHT3X.h>

SHT3X sht30(0x45);

void setup() {

  Serial.begin(115200);

}

void loop() {

  if(sht30.get()==0){
    Serial.print("Temperature in Celsius : ");
    Serial.println(sht30.cTemp);
    Serial.print("Temperature in Fahrenheit : ");
    Serial.println(sht30.fTemp);
    Serial.print("Relative Humidity : ");
    Serial.println(sht30.humidity);
    Serial.println();
  }
  else
  {
    Serial.println("Error!");
  }
  delay(1000);

}

参考

  1. http://stigern.net/blog/using-wemos-d1-mini-sht30-sensor-shield/

プログラム2

検出した温度をFirebaseに送信。

#include "Firebase_ESP_Client.h"
#include  <ESP8266WiFi.h>

#define FIREBASE_HOST "test2-***.firebaseio.com"                    // Enter the Firebase Database URL Without Https and backslash
#define API_KEY "AIzaSyDfl-s-**** "

FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

FirebaseJson json;

#define WIFI_SSID "SSID003"                     // Change the name of your WIFI
#define WIFI_PASSWORD "12345"                        // Change the password of your WIFI

#define USER_EMAIL "xxx@yyy.net"
#define USER_PASSWORD "12345"

#include <WEMOS_SHT3X.h>
SHT3X sht30(0x45);

void setup() 
{
  Serial.begin(115200);
  WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) 
  {
   delay(500);
   Serial.print(".");
  }
  Serial.println ("");
  Serial.println ("WiFi Connected!");

  /* Assign the project host and api key (required) */
  config.host = FIREBASE_HOST;
  config.api_key = API_KEY;

  /* Assign the user sign in credentials */
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;

  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
}

void loop() 
{
 float h =0;
 float t = 0;         // Reading temperature as Celsius (the default)

  if(sht30.get()==0){
    t = sht30.cTemp;
    h = sht30.humidity;
    Serial.print("Temperature in Celsius : ");
    Serial.println(sht30.cTemp);
    Serial.print("Temperature in Fahrenheit : ");
    Serial.println(sht30.fTemp);
    Serial.print("Relative Humidity : ");
    Serial.println(sht30.humidity);
    Serial.println();
  }
  else
  {
    Serial.println("SHT30 Error!");
  }

  
  Firebase.RTDB.setFloat (&fbdo, "Temperature",t);
  Serial.println(t);
  Firebase.RTDB.setFloat (&fbdo, "Humidity",h);
  Serial.println(h);
  delay(200);
}