Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views3 pages

Node MCU Configure Code For Health Monitoring System.

This document contains an Arduino sketch for an ESP8266 microcontroller that connects to WiFi and publishes sensor data (ECG, temperature, and humidity) to Adafruit IO. It utilizes a DHT sensor for temperature and humidity readings, and an ECG sensor for heart rate monitoring, displaying the temperature on an LCD. The code also handles serial data input to publish additional information to Adafruit IO.

Uploaded by

Palash Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Node MCU Configure Code For Health Monitoring System.

This document contains an Arduino sketch for an ESP8266 microcontroller that connects to WiFi and publishes sensor data (ECG, temperature, and humidity) to Adafruit IO. It utilizes a DHT sensor for temperature and humidity readings, and an ECG sensor for heart rate monitoring, displaying the temperature on an LCD. The code also handles serial data input to publish additional information to Adafruit IO.

Uploaded by

Palash Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <ESP8266WiFi.

h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows

// WiFi parameters
#define WLAN_SSID "abcde"
#define WLAN_PASS "123456789"

// Adafruit IO (Original)
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "rabby"
#define AIO_KEY "aio_RkSo67McLbn9K55Vy1AeGWjPA37K"

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);

// Original Adafruit IO Feeds


Adafruit_MQTT_Publish ecgFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/ecg-data");
Adafruit_MQTT_Publish tempFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/temperature");
Adafruit_MQTT_Publish humiFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/humidity");

// New Adafruit IO Feed for Serial Data


Adafruit_MQTT_Publish serialFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/serial-data");

int ecgOutputPin = A0; // ECG Sensor Module Pin Configuration


const float gainFactor = 90.0; // Adjust this value according to your module's
specifications

DHT dht(D4, DHT11); // DHT sensor connected to pin D2

unsigned long lastPublishTime = 0; // Variable to store the last publish time


const unsigned long publishInterval = 7000; // Set the publish interval in
milliseconds (e.g., 7 seconds)

String receivedData = ""; // Buffer to store received serial data

void connectWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
}
void setup() {
Serial.begin(115200);
connectWiFi();

dht.begin(); // Initialize the DHT sensor


lcd.begin(16, 2); // Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the backlight
}

void loop() {
unsigned long currentMillis = millis();

// Read ECG sensor value


int ecgValue = analogRead(ecgOutputPin);

// Convert the raw value to the ECG value


float ecg = (ecgValue / 1023.0) * gainFactor;

// Read temperature and humidity


float tempC = dht.readTemperature();
float humi = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(tempC);

lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempC);
lcd.print((char)223);
lcd.print("C");

// Check if it's time to publish


if (currentMillis - lastPublishTime >= publishInterval) {
// Publish the data to Adafruit IO
publishToAdafruitIO(ecg, tempC, humi);

// Update the last publish time


lastPublishTime = currentMillis;
}

// Maintain MQTT connection


if (!mqtt.connected()) {
connectToAdafruitIO();
}
mqtt.processPackets(3000); // Process MQTT packets every 1 second

// Check if there is data available on Serial


while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
// End of message, send to Adafruit IO
if (mqtt.connected()) {
publishSerialDataToAdafruitIO(receivedData);
}
receivedData = ""; // Clear the buffer
} else {
receivedData += c; // Add character to buffer
}
}
}

void connectToAdafruitIO() {
Serial.println("Connecting to Adafruit IO...");
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
if (ret == -1) {
Serial.println("Connection failed. Check your settings.");
} else {
Serial.print("Connection failed. Retrying in 5 seconds...");
delay(5000);
}
}
Serial.println("Adafruit IO Connected!");
}

void publishToAdafruitIO(float ecg, float tempC, float humi) {


// Publish ECG to Adafruit IO
char ecgBuffer[10];
dtostrf(ecg, 4, 2, ecgBuffer);
if (ecgFeed.publish(ecgBuffer)) {
Serial.println("ECG data sent to Adafruit IO: " + String(ecg));
} else {
Serial.println("Failed to send ECG data to Adafruit IO.");
}

// Publish temperature to Adafruit IO


char tempBuffer[10];
dtostrf(tempC, 4, 2, tempBuffer);
if (tempFeed.publish(tempBuffer)) {
Serial.println("Temperature data sent to Adafruit IO: " + String(tempC));
} else {
Serial.println("Failed to send Temperature data to Adafruit IO.");
}

// Publish humidity to Adafruit IO


char humiBuffer[10];
dtostrf(humi, 4, 2, humiBuffer);
if (humiFeed.publish(humiBuffer)) {
Serial.println("Humidity data sent to Adafruit IO: " + String(humi));
} else {
Serial.println("Failed to send Humidity data to Adafruit IO.");
}
}

void publishSerialDataToAdafruitIO(String data) {


char buffer[data.length() + 1];
data.toCharArray(buffer, sizeof(buffer));
if (serialFeed.publish(buffer)) {
Serial.println("Data sent to Adafruit IO: " + data);
} else {
Serial.println("Failed to send data to Adafruit IO.");
}
}

You might also like