Arduino Yún的第步-《實戰物聯網開發-使用Arduino Yún》筆記

    因為開學專題的關係,我使用CAVEDU教育團隊翻譯的《實戰物聯網開發-使用Arduino Yún》當作我的第一本Arduino Yún的入門書,另外該書的作者Marco Schwartz也有提供這本書所需要的全部程式碼marcoschwartz-Code for the Geeky Projects with Arduino Yun book

    這是第一次碰Arduino Yún,一開始會買不外乎是被他許多神奇的功能,像Wifi、Linux,所吸引,當然是希望可以熟悉熟悉這些功能,所以希望買一本書,讓自己對Yún有全觀的了解。不過市面上,至少中文書中,在成堆的Arduino教學書籍中Yún卻很難找。終於在學校的圖書館中找到了《Arduino雲物聯網系統開發(入門篇)》和這本,其中這本是以專題的方式來講述Yún,這更符合我的需求,因此《Arduino雲物聯網系統開發(入門篇)》基本上就被我丟在旁邊當工具書拉~

    這篇單純記錄我在對Yún和實作這些專題遇到的想法和困難。


前言、Arduino Yún的基本設定

    關於基本設定,可以參考葉難-Arduino Yún:基本設定與無線燒錄。以下是我遇到的一些問題。

1. 預設帳密

    若是原本的arduino.cc公司生產的 Arduino Yún,預設Wifi名稱為Arduino Yún-XXXXXXXXXXXX,其中X為Mac位址,密碼為arduino。若由比較新的arduino.org公司生產,則預設Wifi名稱為Linino-XXXXXXXXXXXX,密碼為doghunter。

    參考資料:Arduino-Unable to login to Arduino Yun with the default password

2. 無線分享器

    每個無線分享器都有預設IP、帳密,我使用的是台灣大寬頻的Hitro Technologies分享器,它的預設IP為192.168.100.1,帳號為admin,密碼為password。


第一章、建置雲端氣象站

    這章的專題利用Yún收集DHT11感測器的溫濕度,再利用Temboo,這個非常方便的雲端服務,將資料傳到Google Sheets、使用Gmail發送郵件及使用Twitter推播功能。

    由於我手邊並沒有DHT11,而且我只是想了解Yún和Temboo的實作細節,所以我只實作了將一個開關的狀態傳到Google Sheets及使用Gmail發送郵件,兩者分別使用/Library/Google/Spreadsheets/AppendRow和/Library/Google/Gmail/SendEmail這兩個Choreo。

    前者,將資料傳到Google Sheets(試算表),因為Google Spreadsheeds API有改,所以使用Username和Password進行認證已經不再被支援了,如果直接使用書上的方式,將會出現以下錯誤:

Error
A Step Error has occurred: "A Google authentication error has occurred. Authenticating with Username and Password is no longer supported by Google. We strongly recommend updating your code so that your app authenticates via OAuth. You can find detailed instructions for using OAuth credentials here: https://temboo.com/library/Library/Google/Spreadsheets/.".  The error occurred in the LaunchChoreo (RetrieveSpreadsheetDetailsByName) step.
HTTP_CODE
500

    取而代之,現在必須經由Google OAuth進行認證,以下是相關網站:Temboo-Google . SpreadsheetsCAVEDU-物聯網裝置連線社群網站_使用Temboo取得Google的OAuth認證How to transmit data from Yun to GoogleDocTed Lee-Arduino Yun使用Temboo云端服務自动通过Google認証:將溫溼度測值日誌寫到Google試算表

    個人覺得Temboo提供的官方教學就已經十分好用了,而且都是step-by-step、還直接附上網址,非常方便!

    後者的Gmail的發送,則仍使用Username和Password來進行認證,不過要注意的是Password必須是App Password,否則會出現以下錯誤:

Error
A Step Error has occurred: "A Google authentication error has occurred. Authenticating with Username and Password is no longer supported by Google. We strongly recommend updating your code so that your app authenticates via OAuth. You can find detailed instructions for using OAuth credentials here: https://temboo.com/library/Library/Google/Spreadsheets/.".  The error occurred in the LaunchChoreo (RetrieveSpreadsheetDetailsByName) step.
HTTP_CODE
500

    而使用App Password的前提是你的帳號必須是兩段式認證的,由於我的Google帳號使學校配發的教育帳號,似乎不能讓我把兩段式認證打開,所以我只能用我原先的帳號使用。

    同樣這在Temboo中也有完整的說明:Temboo-Emailing from your Arduino Yún is easy。以下是我的程式碼:

/*  Arduino_Yun01_Temboo_Google_Practice
Author: Po-Hsuan Yen
Language: Arduino
Describing:
    This is a practice that try to use yun to handle Gmail & Google Sheets via Temboo.
Reference: 《實戰物聯網開發-使用Arduino Yun》第一章
Creating Date: 2017/07/23
Completive Date: 2017/07/23
*/


#include 
#include 
#include 

#define TEMBOO_ACCOUNT yourTembooAccount
#define TEMBOO_APP_KEY_NAME yourTembooAppName
#define TEMBOO_APP_KEY yourTembooAppKey

void appendRowToGoogleSheet(int value);

const int testPin = 3;

//For appendRowToGoogleSheet(.)
const String CLIENT_ID_VALUE = yourClientIdValue;
const String CLIENT_SECRET_VALUE = yourClientSecretValue;
const String REFRESH_TOKEN_VALUE = yourRefreshTokenValue;
const String GOOGLE_SPREADSHEET_TITLE = yourGoogleSheetTitle;

//For sendEmail(.)
const String GOOGLE_USERNAME = yourGoogleUserName;//e.g. abc123@gmail.com.tw
const String GOOGLE_PASSWORD = yourGooglePassword;//App password
const String RECEIVING_EMAIL = yourReceivingEmail;//Recipient of Gmail

// Process to get the measurement time
Process date;

int time = 0;

void setup() {
	//Initialize
	pinMode(testPin, INPUT);
	Bridge.begin();

	/*Debug*/
	Serial.begin(115200);

	//Time Obtain
	long time = millis();
	if (!date.running()) {
		date.begin("date");
		date.addParameter("+%D-%T");
		date.run();
	}


}

void loop() {

	/*Debug*/
	Serial.print(time++);
	Serial.print("-TestPin:");
	Serial.println(digitalRead(testPin));

	/*Excute*/
	appendRowToGoogleSheet(digitalRead(testPin));

	if (digitalRead(testPin)) {
	sendEmail("Arduino Yun Test!!\nOn!!");
	}

	//Delay 5 minutes
	delay(300000);

}

void appendRowToGoogleSheet(int value) {
	TembooChoreo appendRowChoreo;

	//Access Temboo
	appendRowChoreo.begin();
	appendRowChoreo.setAccountName(TEMBOO_ACCOUNT);
	appendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
	appendRowChoreo.setAppKey(TEMBOO_APP_KEY);
	appendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow");

	//Access Google Sheets
	appendRowChoreo.addInput("ClientID", CLIENT_ID_VALUE);
	appendRowChoreo.addInput("ClientSecret", CLIENT_SECRET_VALUE);
	appendRowChoreo.addInput("RefreshToken", REFRESH_TOKEN_VALUE);

	appendRowChoreo.addInput("SpreadsheetTitle", GOOGLE_SPREADSHEET_TITLE);

	if (!date.running()) {
		date.begin("date");
		date.addParameter("+%D-%T");
		date.run();
	}

	String rowStr = date.readString() + "," + digitalRead(testPin);

	appendRowChoreo.addInput("RowData", rowStr);

	unsigned int returnCode = appendRowChoreo.run();
	/*Debug*/
	if (returnCode == 0) {
		Serial.println("Completed execution of the /Library/Google/Spreadsheets/AppendRow Choreo.");
	}
	else {
		Serial.println("Error of the /Library/Google/Spreadsheets/AppendRow Choreo.");
		while (appendRowChoreo.available()) {
			char c = appendRowChoreo.read();
			Serial.print(c);
		}
		Serial.println();
	}

	appendRowChoreo.close();

}

void sendEmail(String message) {
	TembooChoreo sendEmailChoreo;

	//Access Temboo
	sendEmailChoreo.begin();
	sendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
	sendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
	sendEmailChoreo.setAppKey(TEMBOO_APP_KEY);
	sendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");

	//Access Google Sheets
	sendEmailChoreo.addInput("Username", GOOGLE_USERNAME);
	sendEmailChoreo.addInput("Password", GOOGLE_PASSWORD);

	sendEmailChoreo.addInput("ToAddress", RECEIVING_EMAIL);
	sendEmailChoreo.addInput("Subject", "Alert: SweetDream");
	sendEmailChoreo.addInput("MessageBody", message);

	unsigned int returnCode = sendEmailChoreo.run();

	if (returnCode == 0) {
		Serial.println("Completed execution of the /Library/Google/Gmail/SendEmail Choreo.");
	}
	else {
		Serial.println("Error of the /Library/Google/Gmail/SendEmail Choreo.");
		while (sendEmailChoreo.available()) {
			char c = sendEmailChoreo.read();
			Serial.print(c);
		}
		Serial.println();
	}

	sendEmailChoreo.close();
}



第二章、製作遠距耗能監控裝置

留言