---
# System prepended metadata

title: 'Arduino物聯網設計開發實務班,產頭人才投資方案(2020/11/01-2021/01/17)'
tags: [IOT, 物聯網, Arduino]

---

---
title: Arduino物聯網設計開發實務班,產頭人才投資方案(2020/11/01-2021/01/17)
tags: Arduino,物聯網,IOT
description: 
lang: zh
---
# Arduino物聯網設計開發實務班
###### tags: C#,Arduino,IoT

## 課堂重要分享網址
1. <a href="https://www.tinkercad.com/circuits">Arduion 模擬網址</a>
2. 韌體更新網址 http://arduino.esp8266.com/stable/package_esp8266com_index.json

## 基礎練習

### 練習一:Hello World
在模擬軟體上練習如何顯示出Hello World
```c=
void setup()
{
	Serial.begin(9600);
}

void loop()
{
    Serial.println("Hello World");
    delay(100);
}
```

### 練習二: led 顯示
![](https://i.imgur.com/u9ybTZs.png)

```c=
void setup()
{
    Serial.begin(9600);
    pinMode(2, OUTPUT);
}

void loop()
{
    digitalWrite(2, HIGH); 
    delay(1000);
    digitalWrite(2, LOW); 
    delay(1000);
}
```
### 練習三: 簡易紅綠燈
![](https://i.imgur.com/LtsmcSA.png)

```c=
void setup()
{
	Serial.begin(9600);
  	pinMode(3, OUTPUT);
	pinMode(4, OUTPUT);
 	pinMode(5, OUTPUT);
}

void loop()
{
        digitalWrite(3, LOW);
        digitalWrite(5, HIGH); 
        delay(5000);
        digitalWrite(5, LOW); 
        digitalWrite(4, HIGH);
        delay(2000);
        digitalWrite(4, LOW); 
        digitalWrite(3, HIGH);
        delay(5000);
}
```
### Serial 輸入練習
```c=
void setup()
{
  	Serial.begin(38400);
}

void loop()
{
  if(Serial.available())
  {
      while(Serial.available()){
          Serial.print((char)Serial.read());
          delayMicroseconds(250);
      }
      Serial.println();
  }
}
```
## 進階紅綠燈練習
```c=
void setup()
{
    Serial.begin(9600);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
}

bool isStart = false;
unsigned long time;


void loop()
{
  //Reading now times
  time = millis() / 1000;
    	
  //Reading UserIn Command
  if(Serial.available()){
      char userIn = (char)Serial.read();
      if(userIn == 'S')
      {
        Serial.println("Start");
        isStart = true;
      }
      if(userIn == 'E')
      {
        Serial.println("End");
        isStart = false;
        //Turn off all Led
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW); 
      }            
    }

    //Running Thread if isStart is true
    if(isStart)
    {
      switch(time % 8)
      {
          case 0:
          case 1:
          case 2:
              digitalWrite(3, LOW);
              digitalWrite(4, LOW);
              digitalWrite(5, HIGH); 
              break;
          case 3:
          case 4:
              digitalWrite(3, LOW);
              digitalWrite(4, HIGH); 
              digitalWrite(5, LOW);
              break;
          case 5:
          case 6:
          case 7:
              digitalWrite(3, HIGH);
              digitalWrite(4, LOW); 
              digitalWrite(5, LOW);
              break;
      }
    }

    delay(50);
}
```

## 進階紅綠燈練習(共陽)
```c=
void setup()
{
    Serial.begin(9600);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH); 
}

bool isStart = false;
unsigned long time;


void loop()
{
      //Reading now times
      time = millis() / 1000;
    	
      //Reading UserIn Command
      if(Serial.available()){
      char userIn = (char)Serial.read();
      if(userIn == 'S')
      {
        Serial.println("Start");
        isStart = true;
      }
      if(userIn == 'E')
      {
        Serial.println("End");
        isStart = false;
        //Turn off all Led
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH); 
      }            
    }

  	//Running Thread if isStart is true
  	if(isStart)
    {
      switch(time % 8)
      {
          case 0:
          case 1:
          case 2:
          	  digitalWrite(3, HIGH);
    		  digitalWrite(4, HIGH);
              digitalWrite(5, LOW); 
              break;
          case 3:
          case 4:
        	  digitalWrite(3, HIGH);
              digitalWrite(4, LOW); 
           	  digitalWrite(5, HIGH);
              break;
          case 5:
          case 6:
          case 7:
        	  digitalWrite(3, LOW);
              digitalWrite(4, HIGH); 
           	  digitalWrite(5, HIGH);
              break;
      }
    }

	delay(50);
}
```

## NeoPixel
```c=
    #include <Adafruit_NeoPixel.h>
    Adafruit_NeoPixel strip(1, 3 , NEO_GRB + NEO_KHZ800);

    void setup()
    {
        Serial.begin(9600);
        strip.begin();
        strip.show(); 
    }

    void loop()
    {
        strip.setPixelColor(0, 30, 55, 123);
        strip.show();	
        delay(10);
    }
```

## NeoPixel(進階)
```c=
#include <Adafruit_NeoPixel.h>
int ledCount = 60;

Adafruit_NeoPixel ledArray(ledCount, 3 , NEO_GRB + NEO_KHZ800);

void setup()
{
    Serial.begin(9600);
    ledArray.begin();
    ledArray.show(); 
}
int temp = 0;
void loop()
{
  	//Turn Off All LED
  	for(int i =0;i<ledCount;i++)
    {
     	ledArray.setPixelColor(i, 0, 0, 0);
    }
  
        ledArray.setPixelColor((temp + 0) % ledCount, 0, 0, 255);
        ledArray.setPixelColor((temp + 1) % ledCount, 0, 255, 0);
  	ledArray.setPixelColor((temp + 2) % ledCount, 255, 0, 0);

  	ledArray.show();
  	delay(50);
  	temp++;

}
```

## 賽跑比賽
![](https://i.imgur.com/SX4oEia.png)
```c=
#include <Adafruit_NeoPixel.h>
int ledCount = 36;

Adafruit_NeoPixel ledArray(ledCount, 3 , NEO_GRB + NEO_KHZ800);

void setup()
{
 	Serial.begin(9600);
    ledArray.begin();
    ledArray.show(); 
}
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
void loop()
{

  	//Turn Off All LED
  	for(int i =0;i<ledCount;i++)
    {
     	ledArray.setPixelColor(i, 0, 0, 0);
    }
  
  	ledArray.setPixelColor((temp1) % ledCount, 0, 0, 255);
    ledArray.setPixelColor((temp2) % ledCount, 0, 255, 0);
  	ledArray.setPixelColor((temp3) % ledCount, 255, 0, 0);
 
  	ledArray.show();
  
  	temp1 = temp1 + rand() % 2;
    temp2 = temp2 + rand() % 2;
    temp3 = temp3 + rand() % 2;

    delay(100);

}
```

## LCD 顯示器
![](https://i.imgur.com/3NhJwb3.png)

```c=
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 2, 3, 4, 5);

void setup() {
    Serial.begin(9600);
    lcd.begin(16, 2);
    lcd.print("Hello World");
    
    lcd.setCursor(4, 1);
    lcd.print("Jerry!");
}
void loop() {
  	
    lcd.setCursor(0, 1);
  	lcd.print("Time:");
 	lcd.setCursor(5, 1);
  	lcd.print(millis()/1000);
    delay(1000);

    //Serial.println(Serial.available());
	

}
```

## 常見 Restful API 操作
```
1.重新設定網路
http://localhost/rest/

2.GPIO 控制(輸出)
http://localhost/gpio?腳位=值
http://localhost/gpio?7=1

3.GPIO 控制(讀取)
http://localhost/gpio/read?腳位=
http://localhost/gpio/read?7=

4.PWM 控制(輸出)
http://localhost/gpio/pwm?腳位=值
http://localhost/gpio/pwm?7=1023

5.讀取類比訊號(輸入)
http://localhost/gpio/adc

6.DHT11 Reader
http://localhost/dht?pin=5&type=11
```

## Restful API
```csharp=
//建立HTTP 連線並取得資料
while (true)
{
    //建立HTTP 連線並取得資料
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://192.168.0.114");
        HttpResponseMessage response = client.GetAsync("dht11?5=").Result;
        response.EnsureSuccessStatusCode();
        string result = response.Content.ReadAsStringAsync().Result;

        JObject jo = JObject.Parse(result);
        if (jo.HasValues)
        {
            Console.WriteLine("目前的室內溼度為:{0}%,溫度為:{1}度", jo.GetValue("Humidity"),jo.GetValue("C"));    
        }
    }
    Thread.Sleep(1000);
}
```