Skip to content

Instantly share code, notes, and snippets.

@clive520
Created February 24, 2020 12:53
Show Gist options
  • Save clive520/870f1a4f1f7f140da5a66c28943bd6f6 to your computer and use it in GitHub Desktop.
Save clive520/870f1a4f1f7f140da5a66c28943bd6f6 to your computer and use it in GitHub Desktop.
用RemoteXY_ESP8266_樓梯自動開燈
/*
-- 樓梯 --
樓梯上下各有一個紅外線感應器,有人靠近樓梯及開啟樓梯電燈,並傳送LINE告知,傳送開燈時間到thingspeak
DHT11 溫溼度感應器,傳送溫溼度到thingspeak
RemoteXY利用wifi連線
RemoteXY_stairs 12345678
D5==14 繼電器
D6==12 樓上人體紅外線感應器
D7==13 樓下人體紅外線感應器
D2==4 DHT11 溫溼度感應器
A0==17 光敏電阻
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.4.3 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.5.1 or later version;
- for iOS 1.4.1 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "RemoteXY_stairs"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,1,0,60,0,18,1,10,13,2,
65,4,86,7,9,9,51,8,9,9,
65,4,6,7,9,9,2,9,9,9,
2,0,22,4,22,11,16,5,29,12,
37,24,31,31,79,78,0,79,70,70,
0,129,0,84,1,12,6,49,1,12,
6,136,230,168,147,228,184,138,0,129,
0,4,1,12,6,1,1,12,6,122,
230,168,147,228,184,139,0,65,11,52,
7,17,11,19,20,16,10,129,0,54,
1,12,6,5,22,12,6,93,233,155,
187,231,135,136,0,129,0,2,19,18,
6,2,33,18,6,6,230,186,171,229,
186,166,239,188,154,0,129,0,54,19,
19,6,2,55,19,6,6,230,191,149,
229,186,166,239,188,154,0,129,0,31,
47,19,6,2,76,19,6,6,228,186,
174,229,186,166,239,188,154,0,67,4,
20,19,20,5,19,33,21,6,2,26,
11,67,4,74,19,20,5,19,55,22,
6,2,26,11,67,4,31,54,20,5,
19,76,21,6,2,26,11,68,17,3,
25,48,16,2,40,60,14,8,36,68,
17,52,25,47,17,2,61,60,14,8,
36,68,17,52,43,47,16,2,82,60,
15,8,36,67,4,71,5,10,5,37,
21,14,8,2,26,11,129,0,73,11,
6,6,53,22,6,6,93,231,167,146,
0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t switch_1; // =1 if switch ON and =0 if OFF
// output variables
uint8_t led_1_r; // =0..255 LED Red brightness
uint8_t led_2_r; // =0..255 LED Red brightness
uint8_t led_3_g; // =0..255 LED Green brightness
uint8_t led_3_b; // =0..255 LED Blue brightness
char text_1[11]; // string UTF8 end zero
char text_2[11]; // string UTF8 end zero
char text_3[11]; // string UTF8 end zero
float onlineGraph_1;
float onlineGraph_2;
float onlineGraph_3;
char text_4[11]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#define PIN_SWITCH_1 14
//moto
/* Sketch was generated by motoblockly
Website: http://www.motoblockly.com
Author: www.motoduino.com
Date: Sun Feb 23 2020 19:26:41 GMT+0800
*/
#include <ESP8266WiFi.h>
#include "esp_DHTesp.h"
#include <WiFiClientSecure.h>
long C_TIME;
long light_on_time;
long light_on;
long light_on_time_all;
long LINE;
float temperature;
float humidity;
float brightness;
void Declare() {
// 全場計數器,控制秒數
C_TIME = 0;
// 燈亮的時間
light_on_time = 0;
// 0為燈熄。1為燈亮
light_on = 0;
light_on_time_all = 0;
LINE = 0;
temperature = 0;
humidity = 0;
brightness = 0;
}
void NET() {
WiFi.begin("Free","ruby3217");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
for (int count = 0; count < 3; count++) {
digitalWrite(14,HIGH);
delay(500);
digitalWrite(14,LOW);
delay(500);
}
}
DHTesp dht;
void read_data() {
brightness = 1024 - analogRead(17);
temperature = dht.getTemperature();
humidity = dht.getHumidity();
}
WiFiClientSecure Secure_client;
WiFiClient client;
void UP() {
if (digitalRead(12) == HIGH) {
digitalWrite(14,HIGH);
light_on = 1;
light_on_time = 1;
if (LINE == 0) {
if (Secure_client.connect("notify-api.line.me",443)) {
String data = "message="+String("有人下樓!!");
Secure_client.println("POST /api/notify HTTP/1.1");
Secure_client.println("Host: notify-api.line.me");
Secure_client.println("Authorization: Bearer uGBowGLFtrQ9BbBNukTDEJ9GCFkGgOcqg29koYnOcHd");
Secure_client.println("Content-Type: application/x-www-form-urlencoded");
Secure_client.print("Content-Length: ");
Secure_client.println(data.length());
Secure_client.println();
Secure_client.println(data);
Secure_client.stop();
}
LINE = 1;
}
}
}
void DOWN() {
if (digitalRead(13) == HIGH) {
digitalWrite(14,HIGH);
light_on = 1;
light_on_time = 1;
if (LINE == 0) {
if (Secure_client.connect("notify-api.line.me",443)) {
String data = "message="+String("有人上樓!!");
Secure_client.println("POST /api/notify HTTP/1.1");
Secure_client.println("Host: notify-api.line.me");
Secure_client.println("Authorization: Bearer uGBowGLFtrQ9BbBNukTDEJ9GCFkGgOcqg29koYnOcHd");
Secure_client.println("Content-Type: application/x-www-form-urlencoded");
Secure_client.print("Content-Length: ");
Secure_client.println(data.length());
Secure_client.println();
Secure_client.println(data);
Secure_client.stop();
}
LINE = 1;
}
}
}
void OFF() {
if (light_on_time >= 20) {
digitalWrite(14,LOW);
UP_lihgt_on_time_thingspeak();
light_on_time = 0;
light_on_time_all = 0;
light_on = 0;
LINE = 0;
}
}
void count_light_time() {
if (light_on == 1) {
light_on_time = light_on_time + 1;
light_on_time_all = light_on_time_all + 1;
}
}
void UP_lihgt_on_time_thingspeak() {
if(client.connect("api.thingspeak.com", 80)) {
String things_request = "GET /update?api_key=2SSH0JIHXNOK63H6&field7="+ String(light_on_time_all)+"\r\n\r\n";
client.print(things_request);
client.stop();
}
}
void time_to_0() {
if (C_TIME >= 86400) {
brightness = 0;
}
C_TIME = C_TIME + 1;
}
void UP_data_thingspeak() {
if(client.connect("api.thingspeak.com", 80)) {
String things_request = "GET /update?api_key=2SSH0JIHXNOK63H6&field4="+ String(temperature)+"&field5="+ String(humidity)+"&field6="+ String(brightness)+"\r\n\r\n";
client.print(things_request);
client.stop();
}
}
void switch2() {
digitalWrite(14,HIGH);
light_on = 1;
light_on_time = 18;
}
//moto
void setup()
{
RemoteXY_Init ();
//pinMode (PIN_SWITCH_1, OUTPUT);
// TODO you setup code
Serial.begin(9600);
pinMode(14, OUTPUT);
pinMode(17, INPUT);
pinMode(4, INPUT);
dht.setup(4);
pinMode(12, INPUT);
pinMode(13, INPUT);
Declare();
NET();
}
void loop()
{
RemoteXY_Handler ();
//digitalWrite(PIN_SWITCH_1, (RemoteXY.switch_1==0)?LOW:HIGH);
// TODO you loop code
// use the RemoteXY structure for data transfer
UP();
DOWN();
count_light_time();
OFF();
if (C_TIME % 5 == 0) {
read_data();
}
if (C_TIME % 300 == 0) {
UP_data_thingspeak();
}
time_to_0();
if(digitalRead(12)!=0){RemoteXY.led_1_r = 255 ;} else {RemoteXY.led_1_r = 0 ;}
if(digitalRead(13)!=0){RemoteXY.led_2_r = 255 ;} else {RemoteXY.led_2_r = 0 ;}
if(digitalRead(14)!=0){RemoteXY.led_3_g = 255 ; RemoteXY.led_3_b = 255 ;} else {RemoteXY.led_3_g = 0 ; RemoteXY.led_3_b = 0 ;}
if (RemoteXY.switch_1!=0) { switch2(); /* switch on */ }
dtostrf(light_on_time_all, 0, 0, RemoteXY.text_4);
dtostrf(temperature, 0, 0, RemoteXY.text_1);
dtostrf(humidity, 0, 0, RemoteXY.text_2);
dtostrf(brightness, 0, 0, RemoteXY.text_3);
RemoteXY.onlineGraph_1 = temperature;
RemoteXY.onlineGraph_2 = humidity;
RemoteXY.onlineGraph_3 = brightness;
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment