wokwi / rp2040js

A Raspberry Pi Pico Emulator in JavaScript
MIT License
384 stars 40 forks source link

How to simulate DHT22 and use it with RP2040 to read temperature and humidity? #127

Closed TBWXY777 closed 1 year ago

TBWXY777 commented 1 year ago

Hi, I am trying to use the DHT22 temperature and humidity sensor with RP2040, and I want to simulate the sensor. I am hoping to find some guidance on how to simulate the DHT22 sensor and use it with RP2040 to read temperature and humidity. I have attached my current Arduino code below:

#include "DHT.h"

#define GPIOPIN 2
#define DHTTYPE DHT22

DHT dht(GPIOPIN, DHTTYPE);

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

void loop() {
  delay(200);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial1.print("Humidity: ");
  Serial1.print(h);
  Serial1.print("%  Temperature: ");
  Serial1.print(t);
  Serial1.println("°C");
}

I have seen DHT22 on both wokwi-elements and the Wokwi platform, but I am not sure how it interacts with RP2040. Thank you very much for your help!

urish commented 1 year ago

Hello, nice to meet you!

I'm curious, what are you building?

Re DHT22 - you'll have to implement the protocol. Since the protocol is timing sensitive, I suggest to wait a bit until I have a chance to push the clock changes for wokwi/wokwi-features#186 (see my comment here).

Another way to go about this would be to read the symbols from the ELF file, intercept the calls to readHumidity() and readTemperature(), and return a fake value instead of calling the actual code. You could do it by replacing the blTaken() hook with your own implementation that would compare the PC value with the symbol you read from the ELF file, and in case of a match will put the return value in the relevant register (r0, if I'm not wrong), and update PC with the value of LR to immediately return from the call.

TBWXY777 commented 1 year ago

Hello, nice to meet you!

I'm curious, what are you building?

Re DHT22 - you'll have to implement the protocol. Since the protocol is timing sensitive, I suggest to wait a bit until I have a chance to push the clock changes for wokwi/wokwi-features#186 (see my comment here).

Another way to go about this would be to read the symbols from the ELF file, intercept the calls to readHumidity() and readTemperature(), and return a fake value instead of calling the actual code. You could do it by replacing the blTaken() hook with your own implementation that would compare the PC value with the symbol you read from the ELF file, and in case of a match will put the return value in the relevant register (r0, if I'm not wrong), and update PC with the value of LR to immediately return from the call.

I am glad to receive your reply. I am not building anything, I am just curious about the components of wokwi-elements. Recently, I successfully simulated the interaction between LCD1602 and PICO of wokwi-elements, which I found very interesting. Therefore, I want to try simulating DHT22. However, simulating DHT22 is a bit difficult for a beginner like me. Your suggestions have given me a lot of inspiration. Thank you very much.

urish commented 1 year ago

Good luck!