我是Processing和 Arduino 的新手。我想将 Arduino 和湿度传感器SHT75与 Processing 连接,以获取湿度和温度数据并将其显示在 GUI 上。安装传感器库后,我可以使用 Arduino IDE 轻松控制 SHT75 传感器并使用串行监视器接收数据(链接)。这是Arduino代码:
#include <Sensirion.h>
const uint8_t dataPin = 9; // SHT serial data
const uint8_t sclkPin = 8; // SHT serial clock
const uint8_t ledPin = 13; // Arduino built-in LED
const uint32_t TRHSTEP = 5000UL; // Sensor query period
const uint32_t BLINKSTEP = 250UL; // LED blink period
Sensirion sht = Sensirion(dataPin, sclkPin);
uint16_t rawData;
float temperature;
float humidity;
float dewpoint;
byte ledState = 0;
byte measActive = false;
byte measType = TEMP;
unsigned long trhMillis = 0; // Time interval tracking
unsigned long blinkMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
delay(15); // Wait >= 11 ms before first cmd
// Demonstrate blocking calls
sht.measTemp(&rawData); // sht.meas(TEMP, &rawData, BLOCK)
temperature = sht.calcTemp(rawData);
sht.measHumi(&rawData); // sht.meas(HUMI, &rawData, BLOCK)
humidity = sht.calcHumi(rawData, temperature);
dewpoint = sht.calcDewpoint(humidity, temperature);
logData();
}
void loop() {
unsigned long curMillis = millis(); // Get current time
// Rapidly blink LED. Blocking calls take too long to allow this.
if (curMillis - blinkMillis >= BLINKSTEP) { // Time to toggle the LED state?
ledState ^= 1;
digitalWrite(ledPin, ledState);
blinkMillis = curMillis;
}
// Demonstrate non-blocking calls
if (curMillis - trhMillis >= TRHSTEP) { // Time for new measurements?
measActive = true;
measType = TEMP;
sht.meas(TEMP, &rawData, NONBLOCK); // Start temp measurement
trhMillis = curMillis;
}
if (measActive && sht.measRdy()) { // Note: no error checking
if (measType == TEMP) { // Process temp or humi?
measType = HUMI;
temperature = sht.calcTemp(rawData); // Convert raw sensor data
sht.meas(HUMI, &rawData, NONBLOCK); // Start humidity measurement
}
else {
measActive = false;
humidity = sht.calcHumi(rawData, temperature); // Convert raw sensor data
dewpoint = sht.calcDewpoint(humidity, temperature);
logData();
}
}
}
void logData() {
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.print(" C, Humidity = ");
Serial.print(humidity);
Serial.print(" %, Dewpoint = ");
Serial.print(dewpoint);
Serial.println(" C");
}
使用带有 Arduino 库(Firmata)的处理,我可以轻松地与其他模拟(例如LDR)或I²C传感器通信(只需遵循网络上提供的数千个教程!),但我不知道如何连接 Arduino +带有处理 IDE 的 SHT75。SHT75 传感器具有一种类似 I²C 的通信协议。这是数据表。我试过使用“serial.Arduino”命令(两者import processing.serial
都import cc.arduino
使用过),但没有。我该如何解决这个问题?