2017-12-18 35 views
0

私はArduino UnoとWindows IoT Coreを搭載したRaspberry Pi 3を持っています。私はthis methodを使用して、Arduinoに情報を渡そうとしました。ピンを開始したり、文字列を取得したり解析したりするように伝えました。このメソッドは、Arduinoからの情報を取得するために完璧に機能します(センサパラメータなど)。UWPプロジェクトでI2Cを使用してArduinoに接続したRaspberry Pi IoTによるLEDの点灯と消灯方法は?

私はArduinoにバイトを送信し、送信されたバイトに基づいてArduinoコード内でアクションを実行することができました(番号2を取得するとinitピン7のように)。しかし、それは一度しか動作しません。私はArduinoをリセットして、再びラズベリーパイのバイトを受け入れるようにしなければなりません(私はラズベリーパイからArduinoに接続されたLEDをオンにすることはできますが、それをオフにすることはできません)。

しかし、私はUWPを使い始めています。私は、ラズベリーパイ3で動いているIoTコアからArduino Uno(逆ではない)にデータを渡そうとしています。そして、I2C接続でArduinoの宇野ピンを制御

マイMainPage.xamlを:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using Windows.Devices.I2c; 
using System.Threading.Tasks; 
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace I2CComm { 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page { 
     private I2cDevice arduio; // Used to Connect to Arduino 
     private DispatcherTimer timer = new DispatcherTimer(); 
     public MainPage() { 
      this.InitializeComponent(); 
      Initialiasecom(); 
     } 
     public async void Initialiasecom() { 
      var settings = new I2cConnectionSettings(0x40); 
      // Slave Address of Arduino Uno 
      settings.BusSpeed = I2cBusSpeed.FastMode; 
      // this bus has 400Khz speed 
      string aqs = I2cDevice.GetDeviceSelector("I2C1"); 
      // This will return Advanced Query String which is used to select i2c device 
      var dis = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs); 
      arduio = await I2cDevice.FromIdAsync(dis[0].Id, settings); 
      timer.Tick += Timer_Tick; 
      // We will create an event handler 
      timer.Interval = new TimeSpan(0,0,0,0,500); 
      // Timer_Tick is executed every 500 milli second 
      timer.Start(); 
     } 

     private async void Timer_Tick(object sender, object e) { 
      byte[] response = new byte[2]; 
      try { 
       arduio.Read(response); 
       // this function will read data from Arduino 
      } 
      catch (Exception p) { 
       Windows.UI.Popups.MessageDialog msg = new Windows.UI.Popups.MessageDialog(p.Message); 
       await msg.ShowAsync(); 
       // this will show error message(if any) 
      } 
     } 

     private void TurnOn_Click(object sender, RoutedEventArgs e) { 
      try { 
       byte[] sendpos; 
       sendpos = BitConverter.GetBytes(2); 
       arduio.Write(sendpos); 
      } 
      catch (Exception p) { 
       Windows.UI.Popups.MessageDialog msg = new Windows.UI.Popups.MessageDialog(p.Message); 
      } 
     } 

     private void TurnOff_Click(object sender, RoutedEventArgs e) { 
      try { 
       byte[] sendpos; 
       sendpos = BitConverter.GetBytes(1); 
       arduio.Write(sendpos); 
      } 
      catch (Exception p) { 
       Windows.UI.Popups.MessageDialog msg = new Windows.UI.Popups.MessageDialog(p.Message); 
      } 
     } 
    } 
} 

マイArduinoのコードがある:

#include <Wire.h> 
// Library that contains functions to have I2C Communication 
#define SLAVE_ADDRESS 0x40 
// Define the I2C address to Communicate to Uno 

byte response[2]; // this data is sent to PI 
volatile short LDR_value; // Global Declaration 
const int LDR_pin=A0; //pin to which LDR is connected A0 is analog A0 pin 
const int ledPin = 7; 

void setup() { 
    Serial.begin(9600); 
    pinMode(ledPin, OUTPUT); 
    Wire.begin(SLAVE_ADDRESS); 
    // this will begin I2C Connection with 0x40 address 
    Wire.onRequest(sendData); 
    // sendData is a function called when Pi requests data 
    Wire.onReceive(I2CReceived); 
    pinMode(LDR_pin,INPUT); 
    digitalWrite(ledPin, HIGH); 
} 

void loop() { 
    delay(500); 
} 

void I2CReceived(int NumberOfBytes) { 
    /* WinIoT have sent data byte; read it */ 
    byte ReceivedData = Wire.read(); 
    Serial.println(ReceivedData); 
    if (ReceivedData == 2) { 
    digitalWrite(ledPin, HIGH); 
    return; 
    } else if (ReceivedData == 1) { 
    digitalWrite(ledPin, LOW); 
    return; 
    } 
} 

void sendData() { 
    LDR_value=analogRead(LDR_pin); 
    // Arduino returns 10-bit data but we need to convert it to 8 bits 
    LDR_value=map(LDR_value,0,1023,0,255); 
    response[0]=(byte)LDR_value; 
    Wire.write(response,2); // return data to PI 
} 
+0

あなたは同じコードを2回投稿したことに気づきましたか? – dda

+0

ありがとう..私はそれを修正しました – Alborz

+0

UWPの内部にWebサーバーをホストできるのですか?私が最後に見た時、それは不可能でした。 – Pawel

答えて

0

ラズベリーパイは1バイトの代わりに4バイト(2はInt)を送信します。 Arduinoのすべてのバイトを受け取る必要があります。

void I2CReceived(int NumberOfBytes) { 
    /* WinIoT have sent data byte; read it */ 
    byte ReceivedData = Wire.read(); 
    Serial.println(ReceivedData); 

    while (0 < Wire.available()) { 
    byte UselessData = Wire.read(); 
    Serial.println(UselessData); 
    } 

    if (ReceivedData == 2) { 
    digitalWrite(ledPin, HIGH); 
    return; 
    } else if (ReceivedData == 1) { 
    digitalWrite(ledPin, LOW); 
    return; 
    } 
} 
+0

あなたはすごく面白いです。 – Alborz

関連する問題