初接觸
用網頁來控制LED亮滅
發佈感知訊息程式解析
在上圖我們可以清楚地看到在IoT content目錄下,有三個原始程式:
- IoT.cs
- ReadStream.cs
- WebStreamReader.cs
當您點選右上角的codeContainer,只看到兩個腳本程式,分別是IoT.cs和ReadStream.cs,但沒有看到WebStreamReader.cs。其實WebStreamReader是被宣告在ReadStream程式內,如下:
public class ReadStream : MonoBehaviour
{
public string PhotonParticleURL = "https://api.particle.io/v1/devices/events?access_token=649e7d09d0980e4b649e42f6dcff79887d9570e2";
WebStreamReader request = null;
我們以在ReadStream程式中找到WRequest()函式,您就可以看到程式是如何建立物件new WebStreamReader(),如何啟動網站連結Start(PhotonParticleURL),如何取得網站資料request.GetNextBlock(),再解析出溫度的資料Contains ("temperature"),最後再顯示在AR畫面上gameObject.GetComponent ().microTemperatureVal 。
IEnumerator WRequest()
{
request = new WebStreamReader();
request.Start(PhotonParticleURL); //https://www.ourtechart.com//wp-content/uploads/2016/04/jsonAllData.txt");
string stream = "";
while (true)
{
string block = request.GetNextBlock();
if (!string.IsNullOrEmpty(block))
{
stream += block;
//Debug.Log ("Stream1: " + stream);
string[] data = stream.Split(new string[] { "\n\n" }, System.StringSplitOptions.None);
//Debug.Log ("Data length: " + data.Length);
stream = data[data.Length - 1];
for (int i = 0; i < data.Length - 1; i++)
{
if (!string.IsNullOrEmpty(data[i]))
{
// Debug.Log ("Data: " + data [i]); // print all block of data (event + data)
if (data [i].Contains ("light")) {
lightTrue = true;
string output = data [i].Substring(data [i].IndexOf("{"));
parseDataLight = JsonUtility.FromJson (output);
//Debug.Log ("Data of Photoresistor: " + parseData.data);
//text.text = parseData.data.ToString ();
//gameObject.GetComponent ().microPhotoresistorVal = parseDataLight.data;
}
if (data [i].Contains ("temperature")) {
temperatureTrue = true;
string output = data [i].Substring(data [i].IndexOf("{"));
parseDataTemperature = JsonUtility.FromJson (output);
//Debug.Log ("Data of Temperature sensor: " + parseData.data);
//text.text = parseData.data.ToString ();
//gameObject.GetComponent ().microTemperatureVal = parseDataTemperature.data;
}
if (data [i].Contains ("motion")) {
motionTrue = true;
string output = data [i].Substring(data [i].IndexOf("{"));
parseDataMotion = JsonUtility.FromJson (output);
//Debug.Log ("Data of PIR sensor: " + parseData.data);
//text.text = parseData.data.ToString ();
//gameObject.GetComponent ().motionDetectedBool = Convert.ToBoolean(parseDataMotion.data);
}
if (data [i].Contains ("ultraviolet")) {
ultravioletTrue = true;
string output = data [i].Substring(data [i].IndexOf("{"));
parseDataUltraviolet = JsonUtility.FromJson (output);
//Debug.Log ("Data of PIR ultraviolet: " + parseData.data);
//text.text = parseData.data.ToString ();
//gameObject.GetComponent ().microUltravioletVal = parseDataUltraviolet.data;
}
if (data [i].Contains ("humidity")) {
humidityTrue = true;
string output = data [i].Substring(data [i].IndexOf("{"));
parseDataHumidity = JsonUtility.FromJson (output);
//Debug.Log ("Data of Humidity: " + parseData.data);
//text.text = parseData.data.ToString ();
//gameObject.GetComponent ().microHumidityVal = parseDataHumidity.data;
}
//Debug.Log ("TEst: " + humidityTrue + temperatureTrue + lightTrue + ultravioletTrue);
if (humidityTrue && temperatureTrue && lightTrue && ultravioletTrue) {
//Debug.Log ("PRINT ALLLLLLLLLLLLLL");
gameObject.GetComponent ().microPhotoresistorVal = parseDataLight.data;
gameObject.GetComponent ().microTemperatureVal = parseDataTemperature.data;
gameObject.GetComponent ().motionDetectedBool = Convert.ToBoolean(parseDataMotion.data);
gameObject.GetComponent ().microUltravioletVal = parseDataUltraviolet.data;
gameObject.GetComponent ().microHumidityVal = parseDataHumidity.data;
humidityTrue = false;
motionTrue = false;
temperatureTrue = false;
lightTrue = false;
ultravioletTrue = false;
}
}
}
}
yield return new WaitForSeconds(1);
}
真正要顯示在畫面上是IoT.cs的任務,在Update()中可以找溫度的片斷程式如下:
microTemperatureText.text = microTemperatureVal.ToString ();
checkTemperature ();
依照顏色來改變按鈕的顏色值:
private void checkTemperature() {
index = 0;
if (microTemperatureVal >= minTemp && microTemperatureVal <= maxTemp) // Green Color - temperature is ok
greenButtonColor (index);// (Buttons[0]);
if (microTemperatureVal > maxTemp) // Red Color - temperature is too Hot
orangeButtonColor(index);
if (microTemperatureVal < minTemp) // Blue Color - temperature is too Cold
blueButtonColor (index);
}
沒有留言:
張貼留言