MenuToggle Menu

Arduino with ProtoPie Connect

The only hardware that can communicate with ProtoPie Connect directly without using Socket.IO or bridge apps is Arduino. Arduino hardware uses serial (USB) connection for communication.

ProtoPie Connect supports serial communication with Arduino boards. The most typical setup would be connecting the Arduino hardware via USB to the computer or machine running ProtoPie Connect.

Learn more about how to use Arduino hardware.

Connecting Arduino to ProtoPie Connect via USB

1. Click on Connect Arduino from the overflow menu.

[object Object]

2. Select the right port and baud rate as you did for your Arduino hardware.

[object Object]

Using Arduino with ProtoPie Connect

The serial communication between ProtoPie Connect and Arduino happens in Message||Value format. If the intention is to send a message without a value, Message would suffice.

Sending messages from Arduino

Use the Serial.println() function to send messages (and values) to ProtoPie Connect which then relays these to the prototypes.

In this example code, Arduino sends a message ROTATE and value 90 every 2 seconds to ProtoPie Connect.

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

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

Sending messages to Arduino

The Arduino hardware needs a separate piece of code to interpret incoming messages in the Message||Value format.

In this example code, Arduino receives and interpret messages from 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());
  // }
}

Use Cases

To better understand how Arduino works with ProtoPie Connect, try recreating the following use case.

Control Your Home Lights

Turn on and off the lights individually. Test this on your own using an Arduino board connected to ProtoPie Connect.

1. Add this prototype to ProtoPie Connect.
2. Set up your Arduino board and the light controls following this circuit diagram.

[object Object]

3. Connect your Arduino hardware to ProtoPie Connect.
4. Use this example code to send messages from Arduino to 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