Author Topic: Arduino IDE fails to compile with #define variables  (Read 3631 times)

dybm

  • Newbie
  • *
  • Posts: 1
Arduino IDE fails to compile with #define variables
« on: February 12, 2016, 07:58:47 am »
I was trying to connect my DHT11 sensor to my Oak, and I was having issues compiling the Adafruit DHT sensor library[1] . For whatever reason, it was failing because my DHT sensor pin was declared as:
#define DHTPIN 2
I got it to work by simply replacing that line with:
int DHTPIN = 2;
and the following line with
int DHTTYPE = 11;
I don't know why it won't compile with #define, but I figured I would share my solution with anyone else potentially running into a similar problem.

[1] https://github.com/adafruit/DHT-sensor-library

shifaz

  • Newbie
  • *
  • Posts: 8
Re: Arduino IDE fails to compile with #define variables
« Reply #1 on: April 08, 2016, 05:36:32 am »
Hello, Im also trying to setup DHT11 with the Oak. Its a little confusing for me at the moment. Will you be able to share the connectivity diagram and the code you used on this project.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Arduino IDE fails to compile with #define variables
« Reply #2 on: April 08, 2016, 07:08:07 pm »
Hey dybm, was that with the DHTtester example supplied with that library? As I just compiled it for the Oak using Arduino 1.6.8. Could you give a little more info about your setup (Operating system, Arduino IDE version, code used).

@shifaz I'll try and throw something up later today for you.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Arduino IDE fails to compile with #define variables
« Reply #3 on: April 08, 2016, 09:30:15 pm »
OK, for a DHT11 sensor with an Oak, the below code and example breadboard layout should help you. I've tested it to work, and I'm getting updates on my particle dashboard every two seconds with all three values (temperature, humidity and heat index). I have commented out most of the Fahrenheit related code, as I'm in Australia and we use Celsius. However, it is all still there, and you can just remove the //'s at the start of the lines to restore it all (look for the //'s that are to the far left - i.e. are not indented). If you are wondering why I chose to connect the DHT11 data line to Pin 5 of the Oak, it is only because I generally avoid using Pins 0 to 4 due to alternate functions of those pins. You could change it back to using Pin 2 without any issues - it works fine on Pin 2 also.

Pete

Code: [Select]
// Example testing sketch for various DHT humidity/temperature sensors
// Original Arduino examle written by ladyada, public domain
// Adapted for the Digistump Oak & Particle by Peter Feerick, public domain
// Note to user: Fahrenheit code is commented due to writer's locale. Remove non-indented // to enable Fahrenheit handling

#include "DHT.h"

#define DHTPIN 5     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to the Oaks VCC
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a resistor between 4.7K and 10K from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Particle.publish("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
//  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);


  //because Particle likes character strings, convert floats to char strings
  char buffer[8];

  //variables for conversions
  char hStr[8];
  char tStr[8];
//  char fStr[8];
  char hicStr[8];
//  char hifStr[8];
 
  //do the conversions
  dtostrf(h, 6, 2, hStr);
  dtostrf(t, 6, 2, tStr);
//  dtostrf(f, 6, 2, fStr);
  dtostrf(hic, 6, 2, hicStr);
//  dtostrf(hif, 6, 2, hifStr);

  //send to Particle
  Particle.publish("Humidity", hStr, 60, PRIVATE);
  Particle.publish("Temperature (C)", tStr, 60, PRIVATE);
//  Particle.publish("Temperature (F)", fStr, 60, PRIVATE);
  Particle.publish("Head Index (C)", hicStr, 60, PRIVATE);
//  Particle.publish("Heat Index (F)", hifStr, 60, PRIVATE);
}
« Last Edit: April 08, 2016, 09:39:34 pm by pfeerick »

shifaz

  • Newbie
  • *
  • Posts: 8
Re: Arduino IDE fails to compile with #define variables
« Reply #4 on: April 09, 2016, 10:55:28 am »
Thanks. I have made much progress on this now. Im also plotting the data on ThingSpeak and on cacti server running on a RaspberryPi.

Checkout ThingSpeak channel https://thingspeak.com/channels/107124

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Arduino IDE fails to compile with #define variables
« Reply #5 on: April 09, 2016, 11:18:44 pm »
Great work shifaz! Cacti looks interesting... might have to look into that :) One of my Oaks is posting to ThingSpeak also : https://thingspeak.com/channels/37399

gspadari

  • Newbie
  • *
  • Posts: 16
Re: Arduino IDE fails to compile with #define variables
« Reply #6 on: April 10, 2016, 01:23:11 pm »
Hi Pete,
I test your sketch and is working fine, but sometimes I get "Failed to read from DHT sensor!".
I try slowing down the loop (delay(3000) instead of 2000) but i don't think it's changed.
What could be wrong?
Thanks

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Arduino IDE fails to compile with #define variables
« Reply #7 on: April 11, 2016, 12:11:53 am »
Hi gspadari,

Is it just the odd intermittent failure, i.e. between readings that do work? If so, it is possible that your DHT11 is a bit fussy about the supply voltage as the low end is 3v... but I would have though it would be fine on the Oaks VCC pin as it should be putting out a steady 3.3v. There shouldn't be a problem with speed of readings as long as it's no faster than every second. I would suspect it is an issue in with data not been sent cleanly, and the checksum test failing, but to be honest I'm not really sure what the issue could be. I'd double check your wiring to make sure all the connections are good (slide them back and forwards a few times). And as a last resort, if the sensor mostly works... you could disable that error message! ;D

I'm presuming here you're basically using the example code I put up here, and you have it on a breadboard?

Pete