WeMos (e3) リレー制御

制御の試し

参考資料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 — リレーの情報