interfacing spo2 and dht11 sensor with esp-wroom-32

kashish
Posts: 1
Joined: Thu Apr 18, 2019 4:21 am

interfacing spo2 and dht11 sensor with esp-wroom-32

Postby kashish » Thu Apr 18, 2019 4:31 am

Hello All.
I am trying to interface OLED 0.96 display, DHT11 temperature sensor and SpO2 heart rate sensor with esp-wroom-32 with below code but i am unable to do so. SpO2 sensor gives perfect reading on UI until the DHT11 sensor code is written in the code below. Can anyone please tell me is it possible to interface both sensors with esp-wrrom-32 or not.

//code
//#include<SoftwareSerial.h>
#include <Wire.h> //I2C Arduino Library
#include "MAX30100_PulseOximeter.h"
#include <PubSubClient.h>
#include <WiFi.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define REPORTING_PERIOD_MS 1000
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 2
#define OLED_CLK 15
#define OLED_DC 5
#define OLED_CS 18
#define OLED_RESET 4
String hr, spo2, temperature, humidity;
PulseOximeter pox;
#define WIFI_ssid "rtpl1"
#define WIFI_PASSWORD "startupindia007"
#define TOKEN "IXp4vo1nXvgmVYNHNNW5"
#define DHTPIN 4
#define DHTTYPE DHT11
char thingsboardServer[]="demo.thingsboard.io";
uint32_t tsLastReport = 0;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
WiFiClient wifiClient;
DHT dht(DHTPIN, DHTTYPE);
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
Serial.println("Beat!");
}

const int txPin = 11;
const int rxPin = 10;
void getAndSendTemperatureAndHumidityData(){
//Serial.println("Collecting temperature data.");

// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();

// Read temperature as Celsius (the default)
float t = dht.readTemperature();

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

//SoftwareSerial USerial1(rxPin, txPin); // RX, TX
void InitWiFi()
{
Serial.println("Connecting to AP ...");

WiFi.begin(WIFI_ssid, WIFI_PASSWORD);
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(10, 0);
display.println(F("Connecting to AP"));
// attempt to connect to WiFi network
while (WiFi.status() != WL_CONNECTED) {
display.print(F("."));
display.display(); // Show initial text
delay(500);
Serial.print(".");
}
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(F("Connected to AP"));
Serial.println("Connected to AP");
}


void reconnect() {
// Loop until we're reconnected
//display.clearDisplay();
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_ssid, WIFI_PASSWORD);
// display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(F("Connecting to AP"));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.print(F("."));
display.display();
}
//display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0, 0);
Serial.println("Connected to AP");
display.println(F("Connected to AP"));
display.display();
}
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0, 0);
Serial.print("Connecting to ThingsBoard node ...");
display.print(F("Connecting to ThingsBoard node ..."));
// Attempt to connect (clientId, username, password)
if ( client.connect("ESP8266 Demo Device", TOKEN, NULL) ) {
Serial.println( "[DONE]" );
display.println(F("Connected to Thingsboard node"));
display.display();

} else {
Serial.print( "[FAILED] [ rc = " );
display.print(F("Connection failed - "));
display.println(client.state());
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
display.print(F(": retrying in 5 seconds"));
display.display();
// Wait 5 seconds before retrying
delay( 5000 );


}
}
}
unsigned long lastSend;
char attributes[100];
void setup()
{
Serial.begin(115200);
// USerial1.begin(9600);
pox.begin();
Serial.print("Initializing pulse oximeter..");
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
} else {
Serial.println("SUCCESS");
}
if(!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
InitWiFi();
client.setServer( thingsboardServer, 1883 );
display.println(F(thingsboardServer));
display.display();
delay(100);
pox.setOnBeatDetectedCallback(onBeatDetected);
lastSend=0;
}
void loop()
{
if ( !client.connected() ) {
reconnect();
}
pox.update();
// Make sure to call update as fast as possible




// Asynchronously dump heart rate and oxidation levels to the serial
// For both, a value of 0 means "invalid"
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
/* Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");*/
hr = String(pox.getHeartRate());
spo2 = String(pox.getSpO2());
// tsLastReport = millis();
//Serial.println("Collecting temperature data.");

// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();

// Read temperature as Celsius (the default)
float t = dht.readTemperature();

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

temperature = String(t);
humidity = String(h);


//Serial.println(hr);
//Serial.println(spo2);
String payload = "{";
payload += "\"temperature\":"; payload += temperature; payload += ",";
payload += "\"humidity\":"; payload += humidity; payload += ",";
payload += "\"Heart_Rate\":"; payload += hr; payload += ",";
payload += "\"SpO2\":"; payload += spo2;
payload += "}";


payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println(attributes);
//USerial1.println(attributes);
tsLastReport = millis();
}
client.loop();
}

ESP_Sprite
Posts: 2716
Joined: Thu Nov 26, 2015 4:08 am

Re: interfacing spo2 and dht11 sensor with esp-wroom-32

Postby ESP_Sprite » Thu Apr 18, 2019 6:37 am

Moving this to the Arduino subforum.

Who is online

Users browsing this forum: Bing [Bot] and 13 guests