Hallo everyone,
am using the Leonardo Board and have connected a few Humidity and Temperature Sensor via I2C to the board.
How? via dis-/connecting the data line of the sensor (which should be activ) change two identic I2C address from SHT21 - #21 by coolf - Sensors - Arduino Forum
I using a library from Christopher Ladden thanks that. I modified it an that it is:
Header .h and .cpp file you can find:
https://www.dropbox.com/sh/tpqm1vpe6cpm7qr/EZpaqC2tg_
Now to my Problem:
While I have just one active Sensor and unplug the sensor. The value is -1 for Temperature and also the Humility. So fare so good. When i connect it again it`ll work fine.
When I do have two active Sensors and unplug one Sensor the hole system hangs up. That problem has to be solved: HOW?
I guess in the function: readSensor
uint16_t LibHumidity::readSensor(uint8_t command) {
uint16_t result;
uint16_t counter = 0;Wire.beginTransmission(eSHT21Address); //begin
Wire.write(command); //send the pointer location
delay(readDelay);
Wire.endTransmission(); //endWire.requestFrom(eSHT21Address, 3); // ändert (Adresse, Bytes, boolean) 21.5 false for continuing restart after requestFrom()
while(Wire.available() < 3 && counter < 100) { //potentioal error return = -1
counter++; //wait
//delay(10); // for waiting much longer while the data arrives
}
if(counter >= 100)
{
counter = 0;
return -1;
}
else
{//Store the result
result = ((Wire.read()) << 8);
result += Wire.read();
result &= ~0x0003; // clear two low bits (status bits)
return result;
}}
My .ino Data:
/****************************
- This sketch reads the Sensirion Humidity and Temperature (SHT21) Sensor
- via I2C and print the value
- to the Serial terminal as well saves the value on the microSD card.
*****************************/#include <Wire.h>
#include <LibHumidity.h>
#include <SD.h>void my_init(); //inizializing for Sensor and microSD
void printdata();LibHumidity humidity = LibHumidity(0);
// Inizialize I2C comunication lines
int sda = 16; //Tennsy 19;
int scl = 17; //Tennsy 16;// inizializing switch for every aktiv sensor.
int anz_sensor = 1; //change to the amount of Sensors (momentan nur für die inizialisierung so far)
int i = 0; // counter for organizing the OUTput setting
int i_s = 0;
int mos1 = 11; //Digital port which activates and closes the data line of the Sensor
int mos2 = 8; //Digital port which activates and closes the data line of the Sensor
int mos3 = 9; //Digital port which activates and closes the data line of the Sensor//LED Settings
int green_led = 2;
int red_led = 3;//Variable
const int chipSelect = 10; // Chip Select at pin 10 for Leonardo (PB6)
unsigned long time;//SD saving
unsigned int initDelaySD = 5000;
File sensorFile;char filename[] = "log.txt"; //Data name, no more than 8 char`s!!
void setup() {
//led setting
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
digitalWrite(green_led, HIGH);
digitalWrite(red_led, HIGH);my_init();
layout_file();digitalWrite(green_led, LOW);
digitalWrite(red_led, LOW);
}void loop() {
// Time saving
time = (millis()/1000); //in seconddigitalWrite(mos3, LOW);
digitalWrite(mos2, LOW);
digitalWrite(mos1, LOW);//activating Sensor 1
digitalWrite(mos1, HIGH);
// delay(500);
// Serial.print("SENSOR 1");
// Serial.print("\t");printdata();
digitalWrite(green_led,LOW);
digitalWrite(mos1,LOW);// delay(500);
//* BESSSER FOR SCHLEIFE MACHEN
if (anz_sensor == 2)
{
//Auskommentiert für kleinen-test
//Sensor2 wird abgegriffen
digitalWrite(green_led, HIGH);
digitalWrite(mos2, HIGH);
// Serial.print("SENSOR 2");
//delay(500);
printdata();
digitalWrite(green_led, LOW);
digitalWrite(mos2,LOW);
//delay(500);
}
/*
//Sensor 3 wird abgegriffen
if(anz_sensor == 3)
{
digitalWrite(led, HIGH);
digitalWrite(mos3, HIGH);
// Serial.println("SENSOR 3");
// delay(500);
printdata();
digitalWrite(led, LOW);
digitalWrite(mos3,LOW);
}
*/
}void printdata() //Output of data on COM interface and saving on microSD
//function to convert float to string : dtostrf(floatVar,minStringWidthIncDecimalPoint,numVarsAfterDecimal,BUFFER);
{
float h = humidity.GetHumidity();
float t = humidity.GetTemperatureC();/if((h == -1) || (t == -1))
{
Wire.endTransmission();
}/if (i == anz_sensor)
{
Serial.println();
Serial.print(time);
i = 0;
}
i++;Serial.print("\t");
Serial.print(t);
Serial.print("\t");
Serial.print(h);
Serial.print("\t");
Serial.print("\t");
/* //function to convert float to string : dtostrf(floatVar,minStringWidthIncDecimalPoint,numVarsAfterDecimal,BUFFER);
char sensorString[8];
dtostrf(h,3,2,sensorString);
Serial.println(sensorString);
*/int counter=0;
while((!(sensorFile = SD.open(filename, FILE_WRITE))) && (counter < 100))
{
//red led on
digitalWrite(red_led, HIGH);
//Serial.println(counter++);
//Serial.println("error reading SD card");
counter++;
delay(20);
}if(sensorFile)
{
//red led off
digitalWrite(red_led,LOW);if (i_s == anz_sensor)
{
sensorFile.println();
sensorFile.print(time);
i_s = 0;
}
i_s++;sensorFile.print("\t");
sensorFile.print(t);
sensorFile.print("\t");
sensorFile.print(h);
sensorFile.print("\t");sensorFile.close();
// Serial.println("Data saved on sensorFile!");
}
else
{
Serial.println("error reading SD card");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
}
//Serial.println("card initialized.");
}
/*************************/};
void my_init()
{Serial.begin(9600);
int counter = 0;
while (!Serial && counter < 100) { //if pc is not connected wait 1 second than it would be powerd via battery
counter++; //wait
delay(50); // for waiting much longer while the data arrives
}
// wait for serial port to connect. Needed for Leonardo only//I2C init
pinMode(sda, OUTPUT);
digitalWrite(sda, LOW); //GND pin
pinMode(scl, OUTPUT);
digitalWrite(scl, HIGH); //VCC pinpinMode(mos1, OUTPUT);
pinMode(mos2, OUTPUT);
pinMode(mos3,OUTPUT);//Inizialized microSD Card
Serial.print("Initializing microSD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
delay(initDelaySD);if(!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present. Reboot!");
digitalWrite(red_led, OUTPUT);
//return;
}
else
{
Serial.println("Initalizing of microSD is set");
}
};
I would be very pleased if anyone could have an idea what i could dry or how i could optimize the code.
thank you for looking into and all the best
coolf