Adding DHT11 or DHT22 sensor support #112

Closed
piotrfalba opened this Issue Apr 13, 2013 · 49 comments

Projects

None yet
@piotrfalba

It would be great to have DHT11 humidity, temperature sensor support.

@rwaldron
Owner

I agree!

I just ordered one of each, when they arrive I will work on support :)

@rwaldron
Owner

Adding as a reference

http://learn.adafruit.com/dht

@rwaldron
Owner

Thanks!

@rwaldron
Owner
rwaldron commented Jul 3, 2013

The code required to make this work requires too many process blocking delays https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp#L78-L139

@rwaldron
Owner
rwaldron commented Aug 7, 2013

Looks like support for this has landed firmata/arduino#44 (comment) so we can probably revisit this now :)

@xFragger
xFragger commented Apr 1, 2014

Did something happen about this? I would need this too, but have no idea if/how i can use ConfigurableFirmata with Johnny-Five and Stepper-support

@rwaldron
Owner
rwaldron commented Apr 1, 2014

@xFragger I haven't looked at this in a very long time, I will try to make some time this week :)

@xFragger
xFragger commented Apr 1, 2014

yeah would be nice... i'm wondering.... johnnyfive checks for Firmata name (==AdvancedFirmata), but this would differ, when i use ConfigurableFirmate, right? Do i have to set the name myself so Advanced to use the steppersupport of five?

@rwaldron
Owner
rwaldron commented Apr 1, 2014

Sorry, but I'm not sure I understand what you're asking. If you need ConfigurableFirmata, then test for that name and throw if it's not present—that part is easy. The hard part is knowing whether or not ConfigurableFirmata was compiled with the right support. Things like this are usually too much work for the average user. I'm not really sure how to approach this, so I'm open to whatever ideas you may have.

@xFragger
xFragger commented Apr 2, 2014

Okay.... again :)
Johnny-five checks in its code for the stepper for the io.firmata.name (to be Advanced), but if i install ConfigurableFirmata on my Arduino, then this check would fail, and propably the stepper won't work, i guess.... so: is it possible to use the ConfigurableFirmata with DHT11 AND stepper support, without "hacking" the johnny-five/stepper.js ?

@soundanalogous

As a temporary work around, you could if you rename ConfigurableFirmata to AdvancedFirmata. The name of the Firmata doesn't matter. Take ConfigurableFirmata.ino, add the DHT11 code (from the example) by adding the includes... make sure the DHT11 files are in the same directory as your sketch and name that AdvancedFirmata.ino. Also remove (don't include) features you may not be using such as OneWire or you risk running out of RAM if you're using an Arduino Uno, Leonardo or other board with limited SRAM.

@rwaldron The stepper code in johnny-five should be updated to not specifically look for a specific firmata name (https://github.com/rwaldron/johnny-five/blob/master/lib/stepper.js#L117). It should instead look for the Stepper pin mode to be set when performing a capability query. Pin modes are used in configurable Firmata as indicators of which features are supported in the particular build.

@xFragger
xFragger commented Apr 2, 2014

So the stepper support in advancedfirmata does work the same way as in configurable? Then i will give it a try...

I think i've got enough memory on the nano v3

@xFragger
xFragger commented Apr 2, 2014

okay, managed to compile the ConfigurableFirmata with Stepper + DHT11 from examples..... but my stepper won't move anymore, with same js code, as for AdvancedFirmata....

How do i call the dht11 device (ext) ?

var stepper = new five.Stepper({
        type: five.Stepper.TYPE.FOUR_WIRE,
        stepsPerRev: 64,
        pins: {
            motor1: 9,
            motor2: 11,
            motor3: 10,
            motor4: 12
        }
    });
    stepper.step({ steps: 500, direction: 0, accel: 3500, decel: 3500 }, function() { /*....*/ });
#include <Firmata.h>

#include <utility/DigitalInputFirmata.h>
DigitalInputFirmata digitalInput;

#include <utility/DigitalOutputFirmata.h>
DigitalOutputFirmata digitalOutput;

#include <utility/AnalogInputFirmata.h>
AnalogInputFirmata analogInput;

#include <utility/AnalogOutputFirmata.h>
AnalogOutputFirmata analogOutput;

#include <utility/StepperFirmata.h>
StepperFirmata stepper;

#include <utility/FirmataExt.h>
FirmataExt firmataExt;

#include <utility/FirmataScheduler.h>
FirmataScheduler scheduler;

#include <utility/AnalogWrite.h>

#include <utility/FirmataReporting.h>
FirmataReporting reporting;

#include "dht11.h"
#include "DHT11Feature.h"
DHT11Feature dht11;


/*==============================================================================
 * FUNCTIONS
 *============================================================================*/

void systemResetCallback()
{
  // initialize a defalt state

  // pins with analog capability default to analog input
  // otherwise, pins default to digital output
  for (byte i=0; i < TOTAL_PINS; i++) {
    if (IS_PIN_ANALOG(i)) {
      Firmata.setPinMode(i, ANALOG);
    } else if (IS_PIN_DIGITAL(i)) {
      Firmata.setPinMode(i, OUTPUT);
    }
  }

  firmataExt.reset();
}

/*==============================================================================
 * SETUP()
 *============================================================================*/

void setup() 
{
  Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  firmataExt.addFeature(digitalInput);
  firmataExt.addFeature(digitalOutput);
  firmataExt.addFeature(analogInput);
  firmataExt.addFeature(analogOutput);
  firmataExt.addFeature(stepper);
  firmataExt.addFeature(reporting);
  firmataExt.addFeature(scheduler);

  /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */
  Firmata.attach(SYSTEM_RESET, systemResetCallback);

  // start up the default Firmata using Serial interface:
  Firmata.begin(57600);

  systemResetCallback();  // reset to default config
}

/*==============================================================================
 * LOOP()
 *============================================================================*/
void loop() 
{
  digitalInput.report();
  while(Firmata.available()) {
    Firmata.processInput();
    if (!Firmata.isParsingMessage()) {
      goto runtasks;
    }
  }
  if (!Firmata.isParsingMessage()) {
runtasks: scheduler.runTasks();
  }

  if (reporting.elapsed()) {
    analogInput.report();
    dht11.report();
  }
  stepper.update();

}
@rwaldron
Owner
rwaldron commented Apr 2, 2014

The stepper code in johnny-five should be updated to not specifically look for a specific firmata name (https://github.com/rwaldron/johnny-five/blob/master/lib/stepper.js#L117). It should instead look for the Stepper pin mode to be set when performing a capability query. Pin modes are used in configurable Firmata as indicators of which features are supported in the particular build.

@soundanalogous heh, I actually didn't write that module, but this makes sense. I'll file a bug and maybe the original author can patch it or someone else here?

@soundanalogous

@xFragger you need to add this line to setup: firmataExt.addFeature(dht11);

@felixsanz

any update on this?

@xFragger
xFragger commented Aug 4, 2014

my real life eats all the time... could not work on this these days, next for the next weeks..... so i'm out for the moment, sorry

@felixsanz

real life first np! thx

@davoclavo

Hi, I'm trying to add DHT11 measurements to my project. I am using ConfigurableFirmata with the DHT11Feature.

Copied DHT11Feature.h, DHT11Feature.cpp, dht11.cpp and dht11.h to libraries/Firmata/utility.

Then added these lines to my ConfigurableFirmata.ino

#include <utility/DHT11Feature.h>
DHT11Feature dht11;

in setup()

#ifdef DHT11Feature_h
  firmataExt.addFeature(dht11);
#endif

in loop()

#ifdef DHT11Feature_h
    // report dht11 values
    dht11.report();
#endif

Full gist here
EDIT: The sketch doesn't even work with firmata clients (yet to figure out why)

I have seen that using a custom sysex and setting the pin mode to 0x7e I could poll for DHT measurements, but haven't found a way to do it with Johnny-Five.

Any ideas?

@rwaldron
Owner

There is currently no special DHT11 device class in Johnny-Five. If anything, there will be a Temperature class that allows for analog or i2c device profiles, but that's work that takes time so contributions would be welcome.

@rwaldron
Owner

@davoclavo I won't be home until Wednesday, but when I get home I can take a closer look.

@davoclavo

@rwaldron Great! I will contribute, but I don't yet fully understand the firmata protocol and the project. I am using a very similar sensor module.

Because of its custom 1-wire protocol (handled by the dht11.cpp library), I guess we have to set the pinMode to a custom one (0x7E). If you have any idea on how to do this, it would be great!

@rwaldron
Owner

Not yet, but will post as much info as I can when I get home and dig in :) Thanks for your patience

@anaibol
anaibol commented Oct 9, 2014

Hi, as far as I understand there is no humidity sensor support yet for arduino, right?

@rwaldron
Owner
rwaldron commented Oct 9, 2014

@anaibol sorry, I haven't had the time to work on this that I had hoped to have. If there is an analog humidity sensor then it's already supported by the generic Sensor class, otherwise no support yet.

@anaibol
anaibol commented Oct 28, 2014

@rwaldron Ok, thanks! Nice to meet you :)

@derrickpelletier

Noticed that there was some progress on the temperature sensor front, has anyone made any headway with the dht22?

cc @BrianGenisio

@BrianGenisio
Collaborator

@derrickpelletier I haven't looked at that device, though I do have one in my pile. Is this a OneWire device? We already have a OneWire temperature device in the Temperature class, so it might not be too hard? Difficulty is breaking out Temperature and Humidity as separate items. How do we handle Humidity? Is that a new class? Or a dynamically added property on Temperature? Im having trouble figuring out where this device fits :)

@rwaldron
Owner

Or a dynamically added property on Temperature? Im having trouble figuring out where this device fits

Could just add "humidity" as a property to all Temperature instances and default it to null (where it would remain for controllers that don't have that capability)

@derrickpelletier

@BrianGenisio The product page on adafruit says

Although it uses a single-wire to send data it is not Dallas One Wire compatible!
https://www.adafruit.com/products/385


@rwaldron I think either having a null humidity on all temperature items is alright, but also just having it available when the DHT22 / DHT11—or other supported devices—would make sense too?

@rwaldron
Owner

but also just having it available when the DHT22 / DHT11

I'd like to avoid classes that produce instances with inconsistent shape. (There may very well be existing classes that are like this, but those are bugs and need to be fixed)

@BrianGenisio
Collaborator

It looks like the DHT11Firmata extension uses the RESERVED_COMMAND message (0x00) with a specific address to report the data. As far as I can tell, there is no way of getting to that command in firmata.js. Or am I missing something?

I think we need to send a PR to firmata.js along with this, to support RESERVED_COMMAND handling in some way. Looks like we need to set the pinMode to 0x7E and then listen to RESERVED_COMMAND messages with that 0x7E address.

The Temperature controller would look something like this, where readReserved (or something similar) would be a new function in firmata.js.

var ADDRESS = 0x7E,
var LENGTH = 5;
io.readReserved(ADDRESS, LENGTH, function(data) {
  var pin = data[0];
  var temp = read16(data[1], data[2]);
  var humidity = read16(data[3], data[4]);
});

Or am I off in the weeds, drinking too much Gulden Draak on NYE? This doesn't feel right. What does readReserved look like to other board IO layers that don't actually speak Firmata? Is there already a way of doing this in firmata.js that I am overlooking?

(P.S. I don't always hack/program near midnight on NYE... sick wife and kids... canceled plans... waiting for midnight... celebrating the new year by myself and hacking. Why not look into reading data from humidity sensors?)

@soundanalogous

DHT11Firmata will be getting a command ID soon. ConfigurableFirmata features that wrap 3rd party libraries will be separate repositories. The proof-of-concept is here (temporarily): https://github.com/soundanalogous/DHT11Firmata. I'll be moving this to the firmata organization once I have all of the details figured out.

Regarding the command ID I need to determine if devices should get an ID in the existing 0x0F - 0x7F range which will not scale well, or if I designate a single command ID to mean "device" or a range of IDs to classify types of devices (sensor, actuator, radio, etc). The later 2 options will add a second byte ("device" or "device type" followed by "specific-device ID") to the protocol, the former option would not but it's possible at some point to run out of values.

@BrianGenisio
Collaborator

OK, so it sounds like J5 should wait for Firmata before we support this
device.
On Thu, Jan 1, 2015 at 12:21 AM Jeff Hoefs notifications@github.com wrote:

DHT11Firmata will be getting a command ID soon. ConfigurableFirmata
features that wrap 3rd party libraries will be separate repositories. The
proof-of-concept is here (temporarily):
https://github.com/soundanalogous/DHT11Firmata. I'll be moving this to
the firmata organization https://github.com/firmata once I have all of
the details figured out.

Regarding the command ID I need to determine if devices should get an ID
in the existing 0x0F - 0x7F range which will not scale well, or if I
designate a single command ID to mean "device" or a range of IDs to
classify types of devices (sensor, actuator, radio, etc). The later 2
options will add a second byte to the protocol, the former option would not
but it's possible at some point to run out of values.


Reply to this email directly or view it on GitHub
#112 (comment)
.

@soundanalogous

You may also want to consider this I2C temp + humidity sensor: https://www.adafruit.com/products/1293. The advantage of using an I2C device over something like the DHT11/22 is that the processing is offloaded to the client device. This avoids having to add significant delays to the main loop in the Firmata code running on the Arduino.

@zanechua

Any updates on this?

@soundanalogous

I'm still working out the details for 3rd party contributed libraries. Once that is settled I can add DHT11/22 support to Firmata.

@kevbost
kevbost commented Aug 3, 2015

Commenting for update update? Just got a DHT22 in the mail and then discovered this thread.

@soundanalogous

I need to revisit this. I moved on to other tasks before finalizing the addressing scheme for 3rd party Firmata libraries. Additional input on the proposal would help get the process started again: firmata/ConfigurableFirmata#16.

@dtex
Collaborator
dtex commented Dec 7, 2015

Cross referencing: firmata/arduino#246

@jerwallace

Hey all.. +1 for DHT22 support... It's been a while since the last post.. Any updates here?

@soundanalogous

There will be no Firmata device support until more people weigh in on and there is a concrete resolution here: firmata/ConfigurableFirmata#16

@rwaldron
Owner
rwaldron commented Apr 8, 2016

In addition to @soundanalogous's information, johnny-five supports DHT11 via I2C backpack, as shown here: #1068 (comment)

@reconbot
Collaborator

Looks like we have two solutions with some broader discussions for the future.

@reconbot reconbot closed this Aug 23, 2016
@DeanMW
DeanMW commented Oct 13, 2016

Any update on this yet?

@gedeagas

@DeanMW not yet.

@rwaldron
Owner

@DeanMW @gedeagas Sorry for the delay, it turned out to be a lot more work than I thought. I've tested the latest version using all three: DHT11, DHT21 and DHT22. Be sure to follow the diagrams in the examples, make sure you load the provided firmware onto the nano (or whatever uC you prefer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment