Combine DHT11 & Ambient Light Sensor in Sketch

Hi community,

this is my first post as I need to learn this function/ method it is driving me insane.

I want to use a sketch inside a sketch. I want to run my dht11 on the wemos d1 mini on pin gpio2 and I also want to run a part in there that will cover adding a gl5528 ambient light sensor to my project so I can view temp, humidity, and light values.

I know that there must be a way and to clear it up I am not a coder I try to make things and try then find the code, modifying my design as the code suggests, backwards I know.

please could someone assist me? I also thought if the sensors light value drops it may even be possible to put a servo in and run it in the same sketch so it will move as required.

but for now, I need to know how to combine the two sketches.
below is the only sketch I have I haven’t added the light sensor just yet.

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "ghfjgfkjyhgjkhgkkhgkjgkjgkjgkjhgjgjg";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "D_Net";
char pass[] = "H@1l3yB3@r";


#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

@StonerStuart please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Thank you

I gave you triple backticks to copy/paste and you used something else instead, which hasn’t worked.

Pete.

that code works to read the DHT11, i want to include a GL5528 Ambient light sensor to the sketch so i can view its data in the same blynk app i created.

@StonerStuart please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

You cannot replace triple backticks with other characters and expect your code to display correctly on the forum.
Please re-edit your post and use the correct characters, copying and pasting the ones I’ve provided above if necessary. Otherwise, your unformatted code will be deleted.

Pete.

Sorry i user full stops then numeric points. i eventually coppied your characters.
are they ‘’’ ? like Chris’s [The apostrophe]

No. on a UK keyboard it’s the key below the Esc key. Not sure about in SA though.

What board type are you using, and what connection method?
(You currently have an incomplete mix of WiFi and Ethernet in your sketch).

Pete

I googled it.
i would really like to learn how to construct these codes.

I am using wifi, it is connected to my D_Net.
it is the same key on the SA keyboard on the same key with ~

should blynksimpleesp.h be removed from the sketch?

What type of board are you using?

Pete.

it is a WEMOS D! Mini Pro v3 [no external antenna]

Okay, the Sketch Builder example here is by far the best starting point:

https://examples.blynk.cc/?board=NodeMCU&shield=ESP8266%20WiFi&example=More%2FDHT11

But, you should change the baud rate to 74880 and the timer interval to 5000L as the DHT11 sensors don’t respond well to being polled once every second.

Obviously your DHT11 will remain connected to GPIO2 (the pin labelled D4 on your board) and you’ll put your own WiFi credentials and Blynk auth code into the sketch

Test this code and confirm that it is working successfully for you with Blynk and you can also see information in the serial monitor about the Blynk connection process.

Pete.

hi yes its working but i cant seem to find the higher baud rate is it 9600 i should change to 744880?
changing the reading to 5 seconds i should probably do in the blynk app too before reporting its jumping from 29.00c to -13.4c and 38rh to 134rh :slight_smile:
trying that in the app now.

but i want to add a ldr to the board i made.

Yes

That won’t make any difference.

Are all of your electrical connections good?

No point in doing that until your basic code is working correctly.

Pete.

how do i post pictures here?

image

That seems like a very complex/messy wiring setup when all you need are 3 wires to connect the Wemos to the DHT11
What is the function of the resistor?

Pete.