Hallo liebes Forum,
vor kurzem habe ich mir SHT31 Sensoren besorgt die auch 1A Funktionieren (getestet mit ESP32) nun will ich diese mit einem Attiny85 auslessen und mit einem HC-12 Modul versenden, das Funktioniert allerdings nicht.
VCC-Attiny85 verbunden mit VCC-SHT31 sowie mit dem VCC-HC12
GND-Attiny85 verbunden mit GND-SHT31 sowie mit dem GND-HC12
SCL-SHT31 verbunden mit PB2-Attiny
SDA-SHT31 verbunden mit PB0-Attiny
RX-HC12 verbunden mit PB3-Attiny
Der test Sketch:
#include <Arduino.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <SoftwareSerial.h>
#include <Adafruit_SHT31.h>
constexpr byte SHT31_ADDR {0x44};
constexpr byte UNUSEDPINS[] {4};
SoftwareSerial hc12(4, 3); // TX = 4, RX = 3
Adafruit_SHT31 sht31 = Adafruit_SHT31();
constexpr byte WATCHDOG_WAKEUPS_TARGET {37}; // 8 * 7 = 56 seconds between each data collection
// watchdog ISR
ISR(WDT_vect)
{
// nothing to do here, just wake up
}
void enableWatchdog()
{
cli();
MCUSR &= ~(1 << WDRF);
WDTCR |= (1 << WDCE) | (1 << WDE);
WDTCR = 1 << WDP0 | 1 << WDP3;
WDTCR |= (1 << WDIE);
sei();
}
// function to go to sleep
void enterSleep(void) {
ADCSRA &= ~(1 << ADEN); // switch off ADC -320µA
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // SLEEP_MODE_PWR_DOWN for lowest power consumption.
sleep_enable();
sleep_mode();
sleep_disable();
}
void initADC0() {
// Read 1.1V reference as input with reference operating voltage vcc
// Reference Vcc and analog input = internal reference 1.1V
// Initialise ADC with REFS[2:0] is 0 = VCC as Ref, MUX[3:0] 1100 = Vbg as Input,
ADMUX = _BV(MUX3) | _BV(MUX2);
bitSet(ADCSRA, ADEN); // Enable
delay(100); // Wait until the reference has stabilized
// After activating the ADC, a "dummy readout" is recommended.
// In other words, a value is read and discarded to allow the ADC to "warm up"
ADCSRA |= _BV(ADSC); // Start conversion
while ((ADCSRA & _BV(ADSC))) { ; } // measure
(void)ADCW; // Discard dummy readout..
}
constexpr uint16_t INTERN {1070}; // determined per IC
constexpr uint32_t INTERNxRESOLUTION {INTERN * 1024UL};
float measurementVCC()
{
initADC0();
ADCSRA |= _BV(ADSC); // Start conversion
while ((ADCSRA & _BV(ADSC))) { ; } // measure
return static_cast<float>(INTERNxRESOLUTION / ADCW) / 1000;
}
void setup() {
hc12.begin(4800);
if (!sht31.begin(SHT31_ADDR)) {
while (1);
}
// Set unused pins to INPUT_PULLUP to save power.
for (auto pin : UNUSEDPINS) { pinMode(pin, INPUT_PULLUP); }
// switch off ADC -320µA
ADCSRA &= ~(1 << ADEN);
delay(2000);
// enable the watchdog
enableWatchdog();
}
void loop()
{
float Temperatur = sht31.readTemperature();
float Luftfeuchtigkeit = sht31.readHumidity();
float vcc = measurementVCC();
char toSend[17]; // Increased buffer size to accommodate VCC data
if (!isnan(Temperatur) && !isnan(Luftfeuchtigkeit)) {
char tempData[6];
char humData[4];
char vccData[5];
char trenner[] = ",";
// 4 is mininum width, 1 is precision; float value is copied onto buff
dtostrf(Temperatur, 4, 1, tempData);
dtostrf(Luftfeuchtigkeit, 2, 0, humData);
dtostrf(vcc, 1, 2, vccData); // Adjust precision as needed
strcpy(toSend, tempData);
strcat(toSend, trenner);
strcat(toSend, humData);
strcat(toSend, trenner);
strcat(toSend, vccData);
} else {
strcpy(toSend, "99.9,99,"); // Error
}
hc12.print(toSend); // Transmitting data with the HC12 transmitter
// deep sleep
for (uint8_t i = 0; i < WATCHDOG_WAKEUPS_TARGET; i++) { enterSleep(); }
}
Der Testaufbau: (wurde erst auf einem Steckboard aufgebaut)
Über jede unterstützung wäre ich sehr dankbar😊



