连接Arduino与ProtoPie Connect

Arduino使用串口(USB)进行通信,是唯一不使用Socket.IO或Bridge App,而可以直接与ProtoPie Connect通信的硬件。

ProtoPie Connect支持与Arduino板的串口通信。最典型的设置是通过USB将Arduino硬件连接到运行ProtoPie Connect的电脑或机器上。

了解更多关于如何使用Arduino硬件。

通过USB连接Arduino和ProtoPie Connect

  1. 点击溢出菜单中的Connect Arduino(连接Arduino)
[object Object]

2. 根据Arduino硬件的信息设置端口和波特率

[object Object]

连接Arduino与ProtoPie Connect

ProtoPie Connect和Arduino之间以Message||Value格式进行串口通信。若要发送一个不带值的消息,则只需传送Message即可。

从Arduino发送消息

使用Serial.println()函数将消息(和值)发送到ProtoPie Connect,然后ProtoPie Connect会将这些消息转发给原型。

在以下示例代码中,Arduino每2秒向ProtoPie Connect发送一条名为ROTATE的消息和一条名为90的值。

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

void loop() {
  // Send "ROTATE" to ProtoPie
  // message: ROTATE
  // value: 90
  Serial.println("ROTATE||90");
  delay(2000);
}

向Arduino发送消息

Arduino硬件需要单独的代码才能将收到的消息解释为Message||Value格式。

在以下示例代码中,Arduino接收并解释来自ProtoPie Connect的消息。

#include <string.h>

// Declare struct
struct MessageValue {
  String message;
  String value;
};

// Declare function that parse message format
struct MessageValue getMessage(String inputtedStr) {
  struct MessageValue result;

  char charArr[50];
  inputtedStr.toCharArray(charArr, 50);
  char* ptr = strtok(charArr, "||");
  result.message = String(ptr);
  ptr = strtok(NULL, "||");

  if (ptr == NULL) {
    result.value = String("");
    return result;
  }

  result.value = String(ptr);

  return result;
}

// Declare MessageValue struct's instance
struct MessageValue receivedData;

void setup() {
  Serial.begin(9600);

	/*
		if you want to make waiting time for reading serial data short, 
		set waiting time with `Serial.setTimeout` function.
	*/
	Serial.setTimeout(10); 
}

void loop() {
	// Take out strings until Serial buffer is empty
	while (Serial.available() > 0) {
    // From ProtoPie Connect 1.9.0, We can use '\0' as delimiter in Arduino Serial
		String receivedString = Serial.readStringUntil('\0'); 
	
		receivedData = getMessage(receivedString);
  }

	// Do something with received message
	
	// if (receivedData.message.equals("FIRST")) {
  //   analogWrite(firstLED, receivedData.value.toInt());
  // } else if (receivedData.message.equals("SECOND")) {
  //   analogWrite(secondLED, receivedData.value.toInt());
  // } else {
  //   analogWrite(thirdLED, receivedData.value.toInt());
  // }
}

使用案例

为了更好地理解Arduino是如何与ProtoPie Connect一起工作的,请一起尝试创建以下案例。

灯光控制原型

本案例演示了通过原型分别调节多个灯光。跟随以下步骤开始制作。

1. 将原型添加至ProtoPie Connect

2. 按照以下电路图设置Arduino板和灯光控制装置

[object Object]

3. 将Arduino连接到ProtoPie Connect

4. 使用以下示例代码,从Arduino向ProtoPie Connect发送消息。

#include <string.h>

struct MessageValue {
  String message;
  String value;
};

struct MessageValue getMessage(String inputtedStr) {
  struct MessageValue result;

  char charArr[50];
  inputtedStr.toCharArray(charArr, 50);
  char* ptr = strtok(charArr, "||");
  result.message = String(ptr);
  ptr = strtok(NULL, "||");

  if (ptr == NULL) {
    result.value = String("");
    return result;
  }

  result.value = String(ptr);

  return result;
}

int firstLED = 3;
int secondLED = 5;
int thirdLED = 6;
struct MessageValue receivedData;

void setup() {
  pinMode(firstLED, OUTPUT);
  pinMode(secondLED, OUTPUT);
  pinMode(thirdLED, OUTPUT);
  Serial.begin(9600);
  Serial.setTimeout(10); // Set waiting time for serial data to 10 milliSeconds
}

void loop() {
  while (Serial.available() > 0) { // Take out strings until Serial is empty
    String receivedString = Serial.readStringUntil('\0'); // From 1.9.0 version, We can use '\0' as delimiter in Arduino Serial
    receivedData = getMessage(receivedString);
  }

  if (receivedData.message.equals("FIRST")) {
    analogWrite(firstLED, receivedData.value.toInt());
  } else if (receivedData.message.equals("SECOND")) {
    analogWrite(secondLED, receivedData.value.toInt());
  } else {
    analogWrite(thirdLED, receivedData.value.toInt());
  }
}
Back To Top