2021/10/18 HCYang
A. Adafruit.io
#include <ESP8266WiFi.h> #include <PubSubClient.h> // WiFi const char *ssid = "CueMei"; // WiFi ssid const char *password = "123456789"; // WiFi password // MQTT Broker const char *mqtt_broker = "io.adafruit.com"; const char *topic = "hcyang0207/feeds/home.control"; const char *mqtt_username = "hcyang0207"; const char *mqtt_password = "aio_BKwE178y7NDTg4K651**********"; const int mqtt_port = 1883; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(9600); // connecting to a WiFi network WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); // Serial.println("Connecting to WiFi.."); } // Serial.println("Connected to the WiFi network"); // connecting to a mqtt broker client.setServer(mqtt_broker, mqtt_port); client.setCallback(callback); while (!client.connected()) { String client_id = "esp8266-client-"; client_id += String(WiFi.macAddress()); // Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str()); if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { // Serial.println("Public mqtt broker connected"); } else { // Serial.print("failed with state "); // Serial.print(client.state()); delay(2000); } } // publish and subscribe client.publish(topic, "device is online"); client.subscribe(topic); } void callback(char *topic, byte *payload, unsigned int length) { char strarr[length]; for (int i = 0; i < length; i++) { strarr[i] = (char) payload[i]; } Serial.println(strarr); } void loop() { client.loop(); }
A-4. mosquitto-client 傳送控制指令 Publish
root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "1.50" root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "1.70" root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "1.20" root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "1.0" root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "2.50" root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "2.100" root@localhost:~# sudo mosquitto_pub -h io.adafruit.com -p 1883 -u hcyang0207 -P aio_BKwE178y7NDTg4K651********** -t "hcyang0207/feeds/home.control" -m "2.0"
A-5. Arduino Pro Mini 整流器端(加熱端)
#include <TimerOne.h> const byte pin = 5; // TRIAC 訊號輸出接腳 const byte trigPin = 3; // TRIAC PIN 只能用pin2或3 volatile boolean zeroCross = false; // 目前trig狀態(儲存零交越狀態的變數) float dim = 64; int i = 0; float dim_tmp = 0; String str = ""; // 暫存輸入字串的變數 char chr; int val; String str_check = "2."; // 控制組標頭(1 = 加熱, 2 = 降溫) boolean str_flag = false; void setup() { pinMode(pin, OUTPUT); pinMode(trigPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(trigPin), zeroCrossISR, RISING); Serial.begin(9600); Timer1.initialize(65); // 設定讓定時器每隔65微秒,自動執行dim_check函數。 Timer1.attachInterrupt(dim_check); } void zeroCrossISR() { zeroCross = true; i=0; digitalWrite(pin, LOW); // 關閉電源TRIAC dim = dim_tmp; } void dim_check() { if (zeroCross) { // 若已經過零交越點.... if (i > 127 - dim) { // 判斷是否過了延遲觸發時間... digitalWrite(pin, HIGH); // 開啟TRIAC i = 0; // 重設「計數器」 zeroCross = false; // 零交越 flag } else { i++; } } } void loop() { while (Serial.available() > 0) { char chr = Serial.read(); if (chr != '\n'){ str += chr; // 把字元連結成字串 } else if(str != NULL) { for(int i=0;i<2;i++) { if(str[i]!=str_check[i]) { Serial.print("Input data: "); dim_tmp = 0; Serial.println(dim_tmp); break; } if(i == 1) { str_flag == true; str.remove(0,2); Serial.print("Input data: "); if(str.toInt() <= 100 && str.toInt() >= 0 && str != " " ) { Serial.println(str.toInt()); // 將字串轉換成數字 dim_tmp = str.toInt(); dim_tmp = (dim_tmp/100)*128-1; str_flag = false; } } } if(str_flag == true) {} str = ""; } } }