Slide 1

Slide 1 text

.NET nanoFramework プログラミング 2023/02/18 Yoshinori Hayashi IoT Algyan

Slide 2

Slide 2 text

自己紹介 ・林 宜憲(@linyixian) ・2016~ MVP for Internet of Things ・所属 (株)リンシステムズ ・ほぼソフトウェアエンジニアのはずだけど、MVP受賞カテゴリーはハードウェア・・・・ ・仕事も業務システムの開発がメイン。たまにサーバー構築もしたり・・・ ・IoT Algyan関西支部で活動しています。

Slide 3

Slide 3 text

.NET nanoFrameworkプログラミングをするには ・公式サイト https://nanoframework.net/ https://github.com/nanoframework/ ・サンプル集 https://github.com/nanoframework/Samples ・デバイスライブラリ https://github.com/nanoframework/nanoFramework.IoT.Device

Slide 4

Slide 4 text

種類 型番 アナログ/デジタル コンバータ 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 対応しているライブラリ(抜粋)

Slide 5

Slide 5 text

プログラムテンプレート 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() { } } }

Slide 6

Slide 6 text

サンプルプログラム Private Lora WiFi End Device Gateway SCD40

Slide 7

Slide 7 text

プログラムリスト(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); } }

Slide 8

Slide 8 text

プログラムリスト(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); } }

Slide 9

Slide 9 text

プログラムリスト(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:");

Slide 10

Slide 10 text

プログラムリスト(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); }

Slide 11

Slide 11 text

参考 サンプルプログラムURL https://github.com/linyixian/NF_LoraHAT_Gateway https://github.com/linyixian/NF_ATOM_SCD40_Lora

Slide 12

Slide 12 text

気になっていることなど ・ファームウェアとライブラリのバージョンに注意 ・表示関連がもう少し充実してくれないかな~ ・対応デバイスがもう少し増えたらいいのにな(RP2040とか) ・簡単にIoTデバイスの開発できる

Slide 13

Slide 13 text

ご清聴ありがとうございました