Attiny85 and DHT22+ Virtual Wire

Hello,

I’m trying to use a Attiny85 to read out a DHT22 Sensor and send the Data with Virtual Wire to a Arduino Uno Board.
I have successfully implemented the example sketch on an Arduino Uno. However, when I connect the one-wire sensor to the ATtiny, the Serial Out from the Uno is just zero.
When I use a fix Var for the Temperature 16.1 for example the UNO Receive the correct value.

I am running at 8MHz internal oscillator.

I use the DHT lib from Rob Tillaard

The Sketch on the Receiver(Arduino Uno)

#include <VirtualWire.h> 
 
#undef int
#undef abs
#undef double
#undef float
#undef round
const int receive_pin = 11;
void setup()
{
    Serial.begin(9600);   
   Serial.println("Go"); 
 
// Initialise the IO and ISR
    vw_set_rx_pin(receive_pin);
    vw_set_ptt_inverted(true);    
    vw_setup(2000);                  
    vw_rx_start();                      
}
 
void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
 
    if (vw_get_message(buf, &buflen)) 
    {
    int i;
     // Message with a good checksum received.
 
    for (i = 0; i < buflen; i++)
    {
        Serial.print((char)buf[i]);                    
        }
    Serial.println("");
     }
}

The Sketch on the Transmiter(Attiny85)

#include <VirtualWire.h>
#include <dht.h>
dht DHT;
#define DHT22_PIN 0
#undef int
#undef abs
#undef double
#undef float
#undef round
const int transmit_pin = 2;
unsigned long previousMillis = 0;       
const long interval = 5000; 

float ftemp;

void setup()
{

  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for RF Link module
  vw_set_tx_pin(transmit_pin);
  vw_setup(2000);                 // Bits per sec
  pinMode(1, OUTPUT);
}

void loop()
{
  digitalWrite(1, LOW);
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    int chk = DHT.read22(DHT22_PIN); 
    char msg[20];
    //float tempf = 16.1;  // the Check the Code
    float tempf = DHT.temperature;
    int temp=(100*tempf);
    digitalWrite(1, HIGH);
    sprintf (msg, "temp=%05i",temp );
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx();   // Wait for message to finish

  }

I hope that someone can help me with the code.

Kind Regards

maestrox

p.s sorry for my bad english ::slight_smile:

Instead of this:

int chk = DHT.read22(DHT22_PIN);

Use this:

while(DTH.read22(DHT22_PIN) != DHTLIB_OK){ /* wait ok */ }

Do you have two Unos (or other '328p-based boards)?

If so, try running both ends of the system on those - that will allow you to rule out - or rule in - a problem with the ATTiny85 itself.

Have you verified that ATTiny85 is running at the right speed? (use blink to verify that it's running at the speed you think it is) This is important to check, people constantly come here thinking they're running at 8mhz, when it's still being divided down to 1.

Have you verified that you're getting successful RF communication over VW?

Which RF receivers and transmitters are you using?

If you're using the very cheap green receivers, I've seen them inexplicably get into a state where the range falls to inches, and at best, they're flaky. Do you have an antenna on them (I've had great luck with solid-core wire pulled from old network cable, it outperforms the stupid spring antennas that sometimes come with 433mhz modules - finally something to do with that reel of CAT-3 in my closet that's been obsolete for the better part of 2 decades... of course I'd need to make a few thousand antennas to use up that stupid roll)? No antenna further degrades their already abysmal range. For much better performance (>1000' line of sight with the dirt cheap green transmitters in my tests), use the thin yellow ones - search ebay for RXB-12 - best part is, they're under $2 shipped! The cheapo green transmitters perform comparably to or better than the fancy ones, in my tests.

@giova014 Thank you. I will try the Code.

@DrAzzy No unfortunately I had only one Uno. Yes, I have checked the right Speed with a "LED Blink" in the Code. When I use a delay() an die VirutalWire lib I have a wrong timing. That is the reason why I use the mills instead of delay().

The Problem is not the Transmission Part because when I send a var with a fix Temperature Value the Receiver displays the correct value in the serial console.

maestrox

Hello everbody,

today I whant to check the dht lib with a serial out code.

include <SoftwareSerial.h>
SoftwareSerial softSerial(99, 4); // RX, TX   // bei Tiny 85
#include <dht.h>
dht DHT;
#define DHT22_PIN 0

void setup()  
{
  softSerial.begin(9600);
  delay(1000);
  softSerial.print  (F("Test....")); 
}

void loop()
{
     //float tempf = DHT.temperature;
     //int temp=(100*tempf);
     softSerial.println(DHT.temperature,1);
     delay(2000);
     
}

The Serial Out ist just :

Test....
0.0
0.0

It seems that that die DHT lib don’t work correct with the Attiny85… :confused:

How can you:

softSerial.println(DHT.temperature,1);

if you didn’t read the DHT22?

I mean, the

DHT.read22(DHT22_PIN)

is missing in your code.

Try this code:

include <SoftwareSerial.h>
SoftwareSerial softSerial(99, 4); // RX, TX   // bei Tiny 85
#include <dht.h>
dht DHT;
#define DHT22_PIN 0

void setup() 
{
  softSerial.begin(9600);
  delay(1000);
  softSerial.print  (F("Test...."));
}

void loop()
{
     //float tempf = DHT.temperature;
     //int temp=(100*tempf);
     while(DTH.read22(DHT22_PIN) != DHTLIB_OK){ /* wait ok */ }
     softSerial.println(DHT.temperature,1);
     delay(2000);
     
}

Hello,

first thank you very much for helping me with my problem and
sorry for my late reply.

I have tested a few days with different code’s and this comes out :

#include <VirtualWire.h>
const int transmit_pin = 2;

// DHT Lib from here https://github.com/nethoncho/Arduino-DHT22
#include <DHT22.h>
#define DHT22_PIN 0
DHT22 myDHT22(DHT22_PIN);

// Software Serial
// Just need Pin 4 for TX
#include <SoftwareSerial.h>
SoftwareSerial softSerial(99, 4); 

// Delay in the loop
unsigned long previousMillis = 0;       
const long interval = 5000; 
int id;

void setup()
{
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for RF Link module
  vw_set_tx_pin(transmit_pin);
  vw_setup(2000);                 // Bits per sec
  softSerial.begin(9600);
  id=1;
  }

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    DHT22_ERROR_t errorCode;
    errorCode = myDHT22.readData();
    
    switch(errorCode)
    {
      case DHT_ERROR_NONE:
        char buf[32];     
        int temp=myDHT22.getTemperatureCInt();
        int hum=myDHT22.getHumidityInt();
        //Build a Message
        sprintf (buf, "%01i,%04i,%04i",id,temp,hum);
        softSerial.println(buf);
        vw_send((uint8_t *)buf, strlen(buf));
        vw_wait_tx();
        break;        
  }
}
}

Now the Attiny reads the Temp an Humidity every 5 seconds and send a Message over
VW. Works great and stable.

Now I try the use a sleep Mode at the Attiny to safe Battery.