Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding DHT11 or DHT22 sensor support #112

Closed
piotrfalba opened this issue Apr 13, 2013 · 53 comments
Closed

Adding DHT11 or DHT22 sensor support #112

piotrfalba opened this issue Apr 13, 2013 · 53 comments
Labels

Comments

@piotrfalba
Copy link

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

@rwaldron
Copy link
Owner

I agree!

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

@rwaldron
Copy link
Owner

Adding as a reference

http://learn.adafruit.com/dht

@piotrfalba
Copy link
Author

DHT11 datasheet in english http://www.svet-el.si/download/Datasheet_DHT11.pdf

@rwaldron
Copy link
Owner

Thanks!

@rwaldron
Copy link
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
Copy link
Owner

rwaldron commented Aug 7, 2013

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

@ottes
Copy link

ottes 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
Copy link
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 :)

@ottes
Copy link

ottes 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
Copy link
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.

@ottes
Copy link

ottes 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
Copy link

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.

@ottes
Copy link

ottes 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

@ottes
Copy link

ottes 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
Copy link
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
Copy link

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

@felixsanz
Copy link

any update on this?

@ottes
Copy link

ottes 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
Copy link

real life first np! thx

@davoclavo
Copy link

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
Copy link
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
Copy link
Owner

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

@rwaldron
Copy link
Owner

@davoclavo
Copy link

@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
Copy link
Owner

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

@anaibol
Copy link

anaibol commented Oct 9, 2014

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

@rwaldron
Copy link
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
Copy link

anaibol commented Oct 28, 2014

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

@derrickpelletier
Copy link

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

cc @BrianGenisio

@BrianGenisio
Copy link
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
Copy link
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
Copy link

@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
Copy link
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
Copy link
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
Copy link

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
Copy link
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
Copy link

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
Copy link

Any updates on this?

@soundanalogous
Copy link

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
Copy link

kevbost commented Aug 3, 2015

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

@soundanalogous
Copy link

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
Copy link
Collaborator

dtex commented Dec 7, 2015

Cross referencing: firmata/arduino#246

@jerwallace
Copy link

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

@soundanalogous
Copy link

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

@rwaldron
Copy link
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
Copy link
Collaborator

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

@DeanMW
Copy link

DeanMW commented Oct 13, 2016

Any update on this yet?

@gedeagas
Copy link

@DeanMW not yet.

@rwaldron
Copy link
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)

@michalvankodev
Copy link

Hello there,

I am going to read from DHT-22 sensor connected to raspberry. AFAIK DHT-22 doesn't have to be connected through I2C. For example this module will do exactly what I want https://github.com/momenso/node-dht-sensor. But I'd like to use DHT with johnny-five.

Can you recommend an alternative approach I should choose? Should I create a new analog thermometer and supply my own toCelsius function?

Thanks for reply

@rwaldron
Copy link
Owner

AFAIK DHT-22 doesn't have to be connected through I2C.

You're right DHT-22 doesn't have to be connected through I2C, but when using it with Johnny-Five, it does and always will. The reason for this is still the same as it was in 2013:

rwaldron commented on Jul 3, 2013
The code required to make this work requires too many process blocking delays

For example this module will do exactly what I want https://github.com/momenso/node-dht-sensor

That module includes compiled add-on code that ultimately appears to do the same thing that Johnny-Five's backpack firmware does.

@cesaraugusto7
Copy link

Any solution to make DHT11 communicate without IC2 with J5?

@dtex
Copy link
Collaborator

dtex commented Apr 17, 2019

@cesaragusto-dev No (see the comment just before yours). Assuming you are using firmata on an Arduino compatible device, the i2c backpack is the most practical solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests