Skip to content

Instantly share code, notes, and snippets.

@rafaelloab
Last active August 20, 2023 02:21
Show Gist options
  • Save rafaelloab/80a80b5bc55426691f7e9c712c3e5e2c to your computer and use it in GitHub Desktop.
Save rafaelloab/80a80b5bc55426691f7e9c712c3e5e2c to your computer and use it in GitHub Desktop.
Creating virtual serial ports for Arduino and Processing communication on Mac.
VIDEO
https://youtu.be/01QoPgcVRzo
Terminal command:
socat -d -d pty,raw,echo=1 pty,raw,echo=1
ARDUINO CODE
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#define DHTTYPE DHT22
const int DHTPIN=5;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.println(t);
}
PROCESSING CODE
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float inByte = 0;
void setup () {
size(400, 300);
println(Serial.list());
String portName = "/dev/ttys002";
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
background(0);
}
void draw () {
// draw the line:
delay(100);
stroke(127, 34, 255);
line(xPos, height, xPos, height - 4*inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
} else {
// increment the horizontal position:
xPos++;
}
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
inByte = float(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment