Created
April 28, 2020 00:39
-
-
Save clive520/84747ff995a6167a85ef579932855a34 to your computer and use it in GitHub Desktop.
數字時鐘_ws2812b_ESP8266__RemoteXY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////// | |
// 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 "clock" | |
#define REMOTEXY_WIFI_PASSWORD "12345678" | |
#define REMOTEXY_SERVER_PORT 6377 | |
// RemoteXY configurate | |
#pragma pack(push, 1) | |
uint8_t RemoteXY_CONF[] = | |
{ 255,3,0,3,0,17,0,10,13,1, | |
6,0,2,33,59,59,2,26,65,15, | |
4,5,53,22 }; | |
// this structure defines all the variables and events of your control interface | |
struct { | |
// input variables | |
uint8_t rgb_1_r; // =0..255 Red color value | |
uint8_t rgb_1_g; // =0..255 Green color value | |
uint8_t rgb_1_b; // =0..255 Blue color value | |
// output variables | |
uint8_t led_1_r; // =0..255 LED Red brightness | |
uint8_t led_1_g; // =0..255 LED Green brightness | |
uint8_t led_1_b; // =0..255 LED Blue brightness | |
// other variable | |
uint8_t connect_flag; // =1 if wire connected, else =0 | |
} RemoteXY; | |
#pragma pack(pop) | |
///////////////////////////////////////////// | |
// END RemoteXY include // | |
///////////////////////////////////////////// | |
#include <ESP8266WiFi.h> | |
#include "esp_NTPClient.h" | |
#include <WiFiUdp.h> | |
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket | |
#endif | |
#define PIN 2 // 連接在哪一個腳位上? | |
#define NUMPIXELS 30 // 串接多少個燈 | |
#define DELAYVAL 500 | |
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); | |
//宣告變數 | |
//0-9數字亮燈,num[數字][7] | |
int num[10][7]={{0, 1, 1, 1, 1, 1, 1},{0, 1, 0, 0, 0, 0, 1},{1, 1, 1, 0, 1, 1, 0},{1, 1, 1, 0, 0, 1, 1},{1, 1, 0, 1, 0, 0, 1},{1, 0, 1, 1, 0, 1, 1},{1, 0, 1, 1, 1, 1, 1},{0, 1, 1, 1, 0, 0, 1},{1, 1, 1, 1, 1, 1, 1},{1, 1, 1, 1, 0, 1, 1}}; | |
String T_Time = ""; | |
String T_Time_STR = "" ; | |
int H01=0; //H01代表小時的個位數 | |
int H10=0; //H10代表小時的十位數 | |
int HH=0; //HH代表小時的數量 | |
int M01=0; //M01代表分鐘的個位數 | |
int M10=0; //M10代表分鐘的十位數 | |
int MM=0; //MM代表分鐘的數量 | |
int S01=0; //S01代表秒的個位數 | |
int S10=0; //S10代表秒的十位數 | |
int S_S=0; //S_S代表秒的數量 | |
int R_light=250 ; | |
int G_light=250 ; | |
int B_light=250 ; | |
//開啟wifi | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP); | |
void setup() { | |
RemoteXY_Init (); | |
// TODO you setup code | |
Serial.begin(9600); | |
//連接wifi | |
WiFi.begin("Free","ruby3217"); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
//取得網路時間必須要放 | |
timeClient.begin(); | |
timeClient.setTimeOffset(28800); | |
Serial.println(WiFi.localIP()); | |
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) | |
clock_prescale_set(clock_div_1); | |
#endif | |
pixels.begin(); // 初始化燈條(必需) | |
} | |
void loop() { | |
RemoteXY_Handler (); | |
// TODO you loop code | |
// use the RemoteXY structure for data transfer | |
get_time(); //取得時間數字 | |
color_light(); | |
pixels.clear(); // 將所有像素顏色設置為“關閉” | |
light_on(H10,0); | |
light_on(H01,7); | |
if(S_S % 2 == 0){pixels.setPixelColor(14, pixels.Color(R_light, G_light, B_light));pixels.setPixelColor(15, pixels.Color(R_light, G_light, B_light));} | |
else{pixels.setPixelColor(14, pixels.Color(0, 0, 0));pixels.setPixelColor(15, pixels.Color(0, 0, 0));} | |
light_on(M10,16); | |
light_on(M01,23); | |
pixels.show(); | |
delay(1000); | |
} | |
//副程式 | |
//收到數字,確定數字中那些燈要亮 | |
void light_on(int time_num,int start_num) { | |
for(int i=0; i<=6 ; i++){ | |
if (num[time_num][i] == 1){pixels.setPixelColor(start_num, pixels.Color(R_light, G_light, B_light));} else {pixels.setPixelColor(start_num, pixels.Color(0, 0, 0));} | |
start_num = start_num + 1 ; | |
} | |
} | |
//副程式 | |
//決定燈的顏色 | |
void color_light() { | |
R_light=map(RemoteXY.rgb_1_r,0,255,30,255) ; | |
G_light=map(RemoteXY.rgb_1_g,0,255,30,255) ; | |
B_light=map(RemoteXY.rgb_1_b,0,255,30,255) ; | |
RemoteXY.led_1_r =RemoteXY.rgb_1_r; | |
RemoteXY.led_1_g =RemoteXY.rgb_1_g; | |
RemoteXY.led_1_b =RemoteXY.rgb_1_b; | |
} | |
//副程式 | |
//取得網路時間,轉成數字,H10代表小時的十位數,H01代表小時的個位數,HH代表小時 | |
//M10代表分鐘的十位數,M01代表分鐘的個位數,MM代表小時 | |
void get_time() { | |
timeClient.update(); | |
T_Time = timeClient.getFormattedTime(); | |
Serial.println(T_Time); | |
T_Time_STR = T_Time.charAt(0); | |
H10 = T_Time_STR.toInt(); | |
T_Time_STR = T_Time.charAt(1); | |
H01 = T_Time_STR.toInt(); | |
HH = H10 * 10 + H01; | |
T_Time_STR = T_Time.charAt(3); | |
M10 = T_Time_STR.toInt(); | |
T_Time_STR = T_Time.charAt(4); | |
M01 = T_Time_STR.toInt(); | |
MM = M10 * 10 + M01; | |
T_Time_STR = T_Time.charAt(6); | |
S10 = T_Time_STR.toInt(); | |
T_Time_STR = T_Time.charAt(7); | |
S01 = T_Time_STR.toInt(); | |
S_S = S10 * 10 + S01; | |
Serial.println((String("1=") + String(H10) + String(" 2=") + String(H01) + String(" HH=") + String(HH))); | |
Serial.println((String("4=") + String(M10) + String(" 5=") + String(M01) + String(" MM=") + String(MM))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment