2017年3月25日 星期六

Blynk CDS light sensor Turning on/off the lights







Blynk  數值顯示器 (Value Display) :

此種顯示元件有四個, 可以綁定硬體設備的 Digital/Analog/Virtual 輸出腳, 主要差別是元件外觀的大小與是否具有將輸出資料格式化的功能 :


  1. Value Display S : 數值顯示器 (短)
  2. Value Display M : 數值顯示器 (中)
  3. Labeled Value M : 格式化的數值顯示器 (中)
  4. Labeled Value L : 格式化的數值顯示器 (長)

 Blynk 有三個通知元件 :
  1. Twitter : 傳送訊息到個人的推特
  2. Email : 傳送訊息到指定郵件信箱
  3. Push : 推送訊息到手機 

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "46wqwr1366bb2e7d46b7b0a0d4d785da7c3b";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "74170287";
char pass[] = "24063173";

//set name/sensor pin and a name for its value
int sensor_light = A0;                          //connect light sensor to A0 pin on nodemcu
int LEDRELAY = 2 ;
int value_light;

//include timer library and start timer function

//start a task named "light"
void light()
{
    value_light = analogRead(sensor_light);               //everytime called for value_light, get an analog reading from sensor set above
    Blynk.virtualWrite(V2, value_light);                //virtually write sensor read value to blynk server
    Serial.print("analogRead(sensor_light)=");
    Serial.println(value_light);
    if (value_light < 300) {                    //if light sensor value is below set number then turn on AC
        Blynk.email("alex9ufo@gmail.com", "sun down", "turning on lights");  
        digitalWrite (LEDRELAY, LOW);                     //relay on 
        Serial.println("turning on lights");
     }else{                            
        Blynk.email("alex9ufo@gmail.com", "sun up", "turning off lights"); 
        digitalWrite (LEDRELAY, HIGH);                          //relay off
        Serial.println("turning off lights");
        }
}

void setup()
{
    pinMode (LEDRELAY, OUTPUT);            //add relay module on pin D2, 4
    digitalWrite (LEDRELAY, LOW);          //keep relay off while device turning on
    Serial.begin(9600);                     //Set console baud rate
    Blynk.begin(auth, ssid, pass);        //enter your location wifi name/password
    timer.setInterval(5000, light);     //run light task every 5 seconds
}

void loop() {
    Blynk.run();                //start blynk
    timer.run();                //start timer
}

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...