數位骰子 Nodemcu_Slack_DiceTalk_V6.ino

#include<Wire.h>

const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ,Dice,diceTwo,Resault=0,Count=0,Flag=0;

// include WIFI Library
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiClient client;
#define _baudrate 9600

// Define the ID and password of your Wi-Fi network
const char* ssid = “xxxxxx_GUEST";
const char* password = “xxxxxxxx";

// Slack webhook host and endpoint information
const char* host = “hooks.slack.com";
const char* SlackWebhookURL = “/services/T24BHUHST/B24BQSHQF/ey7O7ObjAI9nGOxxxxxxxxxx";

// SSL Certificate finngerprint for the host
//const char* fingerprint = “‎‎C2 05 53 45 E5 C9 9F 52 D8 7B 92 7A 5C C4 D6 ADxxxxxxxxxxx";

void setup() {
Wire.begin(D1, D2); // sda, scl
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);

Serial.begin(_baudrate);
Serial.println(“Setup completato…Serial Enable!");

// Initialize the WiFi client and try to connect to our Wi-Fi network
WiFi.begin(ssid, password);
Serial.println(“");

// Wait for a successful connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.");
}
// For debugging purposes print the network ID and the assigned IP address
Serial.println(“");
Serial.print(“Connected to “);
Serial.println(ssid);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
}

void loop() {

//———-Read Data from Sensor———-
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)

//———-Switch Case———-
if (AcZ>10000 & (AcX+AcY)<10000)
{
// Serial.println(“1″); // Print Index Test
Dice = 1;
}
else
if (AcY>10000 & (AcX+AcZ)<10000)
{
// Serial.println(“2″); // Print Index Test
Dice = 2;

}
else
if (AcX>10000 & (AcY+AcZ)<10000)
{
// Serial.println(“6″); // Print Index Test
Dice = 6;
}
else
if (AcX<-10000 & (AcY+AcZ)>-10000)
{
// Serial.println(“5″); // Print Index Test
Dice = 5;
}
else
if (AcY<-10000 & (AcX+AcZ)>-10000)
{
// Serial.println(“4″); // Print Index Test
Dice = 4;
}
else
if (AcZ<-10000 & (AcX+AcY)>-10000)
{
// Serial.println(“3″); // Print Index Test
Dice = 3;
}
else
// Serial.println(“None"); // Print Index Test
Dice = 0;

delay(200);

//過濾重複信號 Flag 判斷是否有移動過(如果結果和前一次不同,設定旗標=1)
if (Resault != Dice)
{
Flag= 1;
}

//過濾重複信號 如果二數值相等 Resault == Dice 且 Flag 判斷是沒有有移動過,就不處理後續動作
//if (Resault == Dice & Flag == 0)
if (Flag == 1)
{
//如果靜置未達 3秒,就不處理資料直到 3秒連續資料相同
if (Count!=3)
{
Serial.println(“Dice>");Serial.println(Dice); // Print Index Test
Serial.println(“diceTwo>");Serial.println(diceTwo); // Print Index Test
if(Dice == diceTwo) //判斷 3次判讀是不是相同,濾掉一些移動中的結果
{
Serial.println(“Count>");Serial.println(Count); // Print Index Test
Count++;
// 限制連續3次靜止一次 Wait a few seconds between measurements.
delay(1000);
}
else
{
Count = 0;
diceTwo = Dice;
}
return;
}
else
{
//———-呼叫傳送函數———-
Resault = Dice;
Serial.println(Dice); // Print Index Test
updateSlack();
Flag = 0;
Count = 0;
}
}

Serial.println(“Same Resault…"); // Print Index Test

// Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t)) {
// Serial.println(“Failed to read from DHT sensor!");
// return;
// }

}

void updateSlack()
{

String channel = “#tcbeer";
String username = “tcBeer";
String message = String (Dice,DEC);

Serial.print(“connecting to “);
Serial.println(host);

// create a secure connection using WiFiClientSecure
WiFiClientSecure client;
const int httpPort = 443;
if (client.connect(host, httpPort)) {

// // verify the signature of the ssl certificate
// if (client.verify(fingerprint, host)) {
// Serial.println(“ssl cert matches");
// } else {
// Serial.println(“ssl cert mismatch");
// }

String PostData="payload={\"channel\": \"" + channel + “\", \"username\": \"" + username + “\", \"text\": \"" + message + “\", \"icon_emoji\": \":ghost:\"}";
Serial.println(PostData);

client.print(“POST “);
client.print(SlackWebhookURL);
client.println(" HTTP/1.1″);
client.print(“Host: “);
client.println(host);
client.println(“User-Agent: ArduinoIoT/1.0″);
client.println(“Connection: close");
client.println(“Content-Type: application/x-www-form-urlencoded;");
client.print(“Content-Length: “);
client.println(PostData.length());
client.println();
client.println(PostData);

delay(500);

// Read all the lines of the reply from server and print them to Serial for debugging
while(client.available()){
String line = client.readStringUntil(‘\r’);
Serial.print(line);
}

Serial.println();
Serial.println(“closing connection");
}
}

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s