---
# System prepended metadata

title: Raspberry Pi Pico MicroPython

---

# Raspberry Pi Pico MicroPython 
## 配件
- [x] Pi Pico本體
- [x] 風速感測 https://shop.playrobot.com/products/sensor-dg0753 
- [x] 紅外線溫度感測 https://shop.playrobot.com/products/9614baa-non-contact-clinical-thermometer-infrared-temperature-sensor-5cm 
- [ ] 濕度感測 https://shop.playrobot.com/products/bme680-iaq-
- [ ] 紅外線訊號傳送 https://www.digikey.tw/zh/products/detail/everlight-electronics-co-ltd/IRM-3638N3/2693794
- [x] 麵包版
- [ ] 日光燈調光模組 
- [ ] 代增加
#### 紅外線控制參考 
https://blog.robodock.net/shi-yong-raspberry-pi-zhi-zuo-hong-wai-xian-yao-kong-qi/
### Part 1 連接Wi-Fi
```
import network
import socket
from time import sleep
from picozero import pico_temp_sensor, pico_led
import machine
ssid = 'TMD 1.0 (26.3)'
password = 'ttun1007'
def connect():
    #Connect to WLAN
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    while wlan.isconnected() == False:
        print('Waiting for connection...')
        sleep(1)
    print(wlan.ifconfig())

try:
    connect()
except KeyboardInterrupt:
    machine.reset()
```
### Part 2 DataHub感測數據上傳
```
import datetime
import time
import string
import random
import threading

from wisepaasdatahubedgesdk.EdgeAgent import EdgeAgent
import wisepaasdatahubedgesdk.Common.Constants as constant
from wisepaasdatahubedgesdk.Model.Edge import EdgeAgentOptions, MQTTOptions, DCCSOptions, EdgeData, EdgeTag, EdgeStatus, EdgeDeviceStatus, EdgeConfig, NodeConfig, DeviceConfig, AnalogTagConfig, DiscreteTagConfig, TextTagConfig
from wisepaasdatahubedgesdk.Common.Utils import RepeatedTimer


def on_connected(edgeAgent, isConnected):
  print("connected !")
  config = __generateConfig()
  _edgeAgent.uploadConfig(action = constant.ActionType['Create'], edgeConfig = config)
  
  
def on_disconnected(edgeAgent, isDisconnected):
  print("disconnected !")

def edgeAgent_on_message(agent, messageReceivedEventArgs):
  print("edgeAgent_on_message !")

def __sendData():
  data = __generateData()
  _edgeAgent.sendData(data)


def __generateBatchData():    # send data in batch for high frequency data
  array = []
  for n in range(1, 10):
    edgeData = EdgeData()
    for i in range(1, 1 + 1):
      for j in range(1, 1 + 1):
        deviceId = 'Device' + str(i)
        tagName = 'Wind' + str(j)
        value = random.uniform(0, 100)
        tag = EdgeTag(deviceId, tagName, value)
        edgeData.tagList.append(tag)
      for j in range(1, 1 + 1):
        deviceId = 'Device' + str(i)
        tagName = 'Temp' + str(j)
        value = random.uniform(0, 100)
        tag = EdgeTag(deviceId, tagName, value)
        edgeData.tagList.append(tag)
      for j in range(1, 1 + 1):
        deviceId = 'Device' + str(i)
        tagName = 'Humid' + str(j)
        value = random.uniform(0, 100)
        tag = EdgeTag(deviceId, tagName, value)
        edgeData.tagList.append(tag)
    array.append(edgeData)
  return array

def __generateData():
  edgeData = EdgeData()
  for i in range(1, 1 + 1):
    for j in range(1, 1 + 1):
      deviceId = 'Device' + str(i)
      tagName = 'Wind' + str(j)
      value = random.uniform(0, 100)
      tag = EdgeTag(deviceId, tagName, value)
      edgeData.tagList.append(tag)
    for j in range(1, 1 + 1):
      deviceId = 'Device' + str(i)
      tagName = 'Temp' + str(j)
      value = random.uniform(0, 100)
      tag = EdgeTag(deviceId, tagName, value)
      edgeData.tagList.append(tag)
    for j in range(1, 1 + 1):
      deviceId = 'Device' + str(i)
      tagName = 'Humid' + str(j)
      value = random.uniform(0, 100)
      tag = EdgeTag(deviceId, tagName, value)
      edgeData.tagList.append(tag)
  return edgeData

def __generateConfig():
  config = EdgeConfig()
  deviceConfig = DeviceConfig(id = 'Device1',
    name = 'Device1',
    description = 'Device1',
    deviceType = 'Smart Device1',
    retentionPolicyName = '')
  
  analog = AnalogTagConfig(name = 'Wind',
    description = 'Wind',
    readOnly = False,
    arraySize = 0,
    spanHigh = 1000,
    spanLow = 0,
    engineerUnit = '',
    integerDisplayFormat = 4,
    fractionDisplayFormat = 2)
  deviceConfig.analogTagList.append(analog)

  analog = AnalogTagConfig(name = 'Temp',
    description = 'Temp',
    readOnly = False,
    arraySize = 0,
    spanHigh = 1000,
    spanLow = 0,
    engineerUnit = '',
    integerDisplayFormat = 4,
    fractionDisplayFormat = 2)
  deviceConfig.analogTagList.append(analog)
  
  analog = AnalogTagConfig(name = 'Humid',
    description = 'Humid',
    readOnly = False,
    arraySize = 0,
    spanHigh = 1000,
    spanLow = 0,
    engineerUnit = '',
    integerDisplayFormat = 4,
    fractionDisplayFormat = 2)
  deviceConfig.analogTagList.append(analog)

  config.node.deviceList.append(deviceConfig)
  return config


_edgeAgent = None
edgeAgentOptions = EdgeAgentOptions(nodeId = '1f68db2f-9c63-47ce-a727-fe44aa56d13b')
edgeAgentOptions.connectType = constant.ConnectType['DCCS']
dccsOptions = DCCSOptions(apiUrl = 'https://api-dccs-ensaas.education.wise-paas.com/', credentialKey = 'ca9c2354fdb371110d29eda06df41bze')
edgeAgentOptions.DCCS = dccsOptions
_edgeAgent = EdgeAgent(edgeAgentOptions)
_edgeAgent.on_connected = on_connected
_edgeAgent.on_disconnected = on_disconnected
_edgeAgent.on_message = edgeAgent_on_message

_edgeAgent.connect()

time.sleep(5)  # Waiting for connection to be established

for i in range(1, 60):
    __sendData()
    time.sleep(1)
```
### Part 3 IoTHub感測數據上傳

### Part 4 IoTHub控制數據下載
