Upgrade to Pro — share decks privately, control downloads, hide ads and more …

.NET nanoFramework programming

Avatar for linyixain linyixain
February 18, 2023

.NET nanoFramework programming

Avatar for linyixain

linyixain

February 18, 2023
Tweet

More Decks by linyixain

Other Decks in Technology

Transcript

  1. 自己紹介 ・林 宜憲(@linyixian) ・2016~ MVP for Internet of Things ・所属

    (株)リンシステムズ ・ほぼソフトウェアエンジニアのはずだけど、MVP受賞カテゴリーはハードウェア・・・・ ・仕事も業務システムの開発がメイン。たまにサーバー構築もしたり・・・ ・IoT Algyan関西支部で活動しています。
  2. 種類 型番 アナログ/デジタル コンバータ ADS1115,MCP3428,MCP3xxx 加速度、ジャイロ、コンパス ADXL345,ADXL357,LSM9DS1,MPU6500,MPU9250 ガスセンサ AGS01DB,BMxx80,CCS811,MH-Z19B 光センサ

    BH1750FVI,TSL2561 気圧センサ BMxx80,LPS25H, 温度、湿度センサ BMxx80,DHTxx,SHT3x, Si7021 赤外線センサ MLX90614, AMG88xx 距離センサ HC-SR04,VL53L0X IOエキスパンダ MCP23xxx,PCx857x カラーセンサ TCS3472x LEDドライバ WS28xx,SK6812 対応しているライブラリ(抜粋)
  3. プログラムテンプレート using System; using System.Diagnostics; using System.Threading; //ライブラリ namespace NF_Sample

    { public class Program { //オブジェクトインスタンス等の宣言 //メインスレッド public static void Main() { //初期化処理など //イベント定義 //スレッドを無期限待機 Thread.Sleep(Timeout.Infinite); } //イベントプロシージャ private static void fooEvent() { } } }
  4. プログラムリスト(End Device) //SCD40 Configuration.SetPinFunction(26, DeviceFunction.I2C1_DATA); Configuration.SetPinFunction(32, DeviceFunction.I2C1_CLOCK); I2cConnectionSettings settings =

    new I2cConnectionSettings(1, 0x62); using I2cDevice device=I2cDevice.Create(settings); device.Write(start); Thread.Sleep(1000); SpanByte buffer = new byte[9]; device.Write(read); Thread.Sleep(50); while (true) { device.Read(buffer); var co2 = BinaryPrimitives.ReadInt16BigEndian(buffer.Slice(0,3)); var tmp = -45 + 175 * (float)(BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(3, 3))) / 65536; var hum = 100 * (float)(BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(6, 3))) / 65536; string data = $"{{¥"co2¥":{co2.ToString()},¥"tmp¥":{tmp.ToString("F2")},¥"hum¥":{hum.ToString("F2")}}}¥r¥n"; serial.Write(data); Debug.Write(data); Thread.Sleep(60000); } }
  5. プログラムリスト(End Device) //SCD40 Configuration.SetPinFunction(26, DeviceFunction.I2C1_DATA); Configuration.SetPinFunction(32, DeviceFunction.I2C1_CLOCK); I2cConnectionSettings settings =

    new I2cConnectionSettings(1, 0x62); using I2cDevice device=I2cDevice.Create(settings); device.Write(start); Thread.Sleep(1000); SpanByte buffer = new byte[9]; device.Write(read); Thread.Sleep(50); while (true) { device.Read(buffer); var co2 = BinaryPrimitives.ReadInt16BigEndian(buffer.Slice(0,3)); var tmp = -45 + 175 * (float)(BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(3, 3))) / 65536; var hum = 100 * (float)(BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(6, 3))) / 65536; string data = $"{{¥"co2¥":{co2.ToString()},¥"tmp¥":{tmp.ToString("F2")},¥"hum¥":{hum.ToString("F2")}}}¥r¥n"; serial.Write(data); Debug.Write(data); Thread.Sleep(60000); } }
  6. プログラムリスト(Gateway) //DPS setting provisioning = ProvisioningDeviceClient.Create(dspAddress, idscope, registrationid, saskey); myDevice

    = provisioning.Register(null, new CancellationTokenSource(30000).Token); if (myDevice.Status != ProvisioningRegistrationStatusType.Assigned) { Debug.WriteLine($"Registration is not assigned: {myDevice.Status}, error message: {myDevice.ErrorMessage}"); return; } Debug.WriteLine($"Device successfully assigned:");
  7. プログラムリスト(Gateway) private static void IoTCentralConnect() { //IoTCentoral connect client =

    new DeviceClient(myDevice.AssignedHub, registrationid, saskey, MqttQoSLevel.AtMostOnce); var res = client.Open(); if (!res) { Debug.WriteLine("can't open the device"); return; } else { Debug.WriteLine("Open the device"); Console.WriteLine("Open the device"); } Thread.Sleep(5000); }