MenuToggle Menu

在ProtoPie Connect中使用Arduino

此功能在专业版和企业版中均可使用。

得益于ProtoPie Connect内置的Arduino插件,可以实现软硬件交互和多屏幕交互体验。

ProtoPie Connect支持与Arduino开发板之间使用串口通信。最典型的组建方式是将Arduino硬件通过USB连接到运行ProtoPie Connect的主机上。

了解更多关于如何使用Arduino的知识。

通过USB将Arduino连接到ProtoPie Connect

1. 在ProtoPie Connect的插件列表中选择Arduino

[object Object]

2. 选择所需的端口(Port)和波特率(Baud Rate)

a. 端口(Port): 选择与Arduino开发板相对应的端口。

[object Object]

b. 波特率(Baud Rate):波特率决定了串口连接上检查消息更新的频率。一般来说可以选择默认值9600。

[object Object]
  • 请留意,Arduino插件实际上可以与任何微控制器通过串口进行通信。例如,要使用ESP32微控制器,只需在连接ESP32控制器后选择适当的端口和波特率来打开ESP32的串口,即可在ProtoPie Connect中接收数据。

在ProtoPie Connect中使用Arduino

ProtoPie Connect和Arduino之间使用消息(Message)||值(Value) 的格式进行通信。如果仅需要发送消息而不需要值,那么仅使用消息(Message)就足够了。

发送消息到Arduino

使用Serial.println()函数来将消息(和值)发送到ProtoPie Connect,然后ProtoPie Connect会将消息(和值)传递给所有监听这一消息的相应原型。

在下面的例子中,Arduino每2秒发送一次值为90ROTATE消息到ProtoPie Connect。

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协同工作,请尝试创建以下范例。

控制屋内灯光

连续执行开关灯操作。请自行使用一块连接到ProtoPie Connect的Arduino板进行测试。

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