Reputation: 55
I'm new to on Processing and Arduino. I would like to interface Arduino and the humidity sensor SHT75 with Processing to get humidity and temperature data and show it on a GUI. I'm able to easily control the SHT75 sensor with the Arduino IDE and receive data using the Serial Monitor, after the sensor's library have been installed (link). Here is the Arduino code:
#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");
}
Using the Processing with Arduino Library (Firmata), I can easily communicate with other analog (for example LDR) or I²C sensors (simply following the thousand of tutorials available in the Web!), but I don't know how to interface Arduino + SHT75 with Processing IDE. SHT75 sensor has a sort of I²C-like communication protocol. Here is the datasheet. I've tried with the "serial.Arduino" command (both import processing.serial
and import cc.arduino
have been used), but nothing. How do I fix this problem?
Upvotes: 0
Views: 3388
Reputation: 5740
If you're receiving the values in the Arduino's Serial Monitor, it means that everything is fine and you just have to read that values from the serial port in Processing.
Here you have an example of what you have to do in your Processing sketch:
import processing.serial.*;
Serial myPort;
void setup() {
// List all the available serial ports:
println(Serial.list());
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
while (myPort.available() > 0) {
int inByte = myPort.read();
println(inByte);
}
}
This is taken from here. It's very easy :) Keep in mind that to have everything working, you have to close the Serial Monitor in Arduino.
Upvotes: 0
Reputation: 397
According to the datasheet:
The serial interface of the SHT7x is optimized for sensor readout and effective power consumption. The sensor cannot be addressed by the I²C protocol, however, the sensor can be connected to an I²C bus without interference with other devices connected to the bus. The microcontroller must switch between protocols."
This means that the physical layer (voltages, timings and bus type) is I²C compatible. But the chip doesn't use standard I²C packets. Thus you can use the I²C bus embedded in your Arduino, but you can't use the I²C library. You have to deal with the low level I²C module of the chip yourself.
Fortunately, this is not so difficult, because I²C is not that subtle and you could even perform valid I²C packets using the direct GPIO control rather than the embedded I²C module. This would require some time, but it's feasible without extensive knowledge of microcontrollers or electronics.
Upvotes: 0