Devices & Components
SparkFun Single Lead Heart Rate Monitor - AD8232
Male/Female Jumper Wires
Battery, 3.7 V
ESP8266 ESP-12E
Software & Tools
Ubidots
Arduino IDE
Project description
Code
Ecg_Monitoring.ino
c_cpp
1/*Ecg Monitoring wirelessly from web*/ 2 3#include <ESP8266WiFi.h> 4#include <PubSubClient.h> 5 6#define WIFISSID "Abctest" 7#define PASSWORD "0oOP8QW-R54-OFg@yJ" 8#define TOKEN "BBFF-Zh7ucB7cvbDmIdPHXTRYVHhmNKWOwy" 9#define MQTT_CLIENT_NAME "myecgsensor" //enter your own 8-12 alphanumeric character ASCII string 10#define SENSOR A0 // Set the A0 as SENSOR //random and unique ascii string and different from all other devices 11 12 13 14#define VARIABLE_LABEL "myecg" // Assing the variable label 15#define DEVICE_LABEL "esp8266" // Assig the device label 16 17 18 19 20 21 22char mqttBroker[] = "industrial.api.ubidots.com"; 23char payload[100]; 24char topic[150]; 25 26char str_sensor[10]; 27 28/**************************************** 29 * Sub Functions 30 ****************************************/ 31WiFiClient ubidots; 32PubSubClient client(ubidots); 33 34void callback(char* topic, byte* payload, unsigned int length) { 35 char p[length + 1]; 36 memcpy(p, payload, length); 37 p[length] = NULL; 38 Serial.write(payload, length); 39 Serial.println(topic); 40} 41 42void reconnect() { 43 // Loop until we're reconnected 44 while (!client.connected()) { 45 Serial.println("Attempting MQTT connection..."); 46 47 // Attemp to connect 48 if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) { 49 Serial.println("Connected"); 50 } else { 51 Serial.print("Failed, rc="); 52 Serial.print(client.state()); 53 Serial.println(" try again in 2 seconds"); 54 // Wait 2 seconds before retrying 55 delay(2000); 56 } 57 } 58} 59 60/**************************************** 61 * Main Functions 62 ****************************************/ 63void setup() { 64 Serial.begin(115200); 65 WiFi.begin(WIFISSID, PASSWORD); 66 // Assign the pin as INPUT 67 pinMode(SENSOR, INPUT); 68 69 Serial.println(); 70 Serial.print("Waiting for WiFi..."); 71 72 while (WiFi.status() != WL_CONNECTED) { 73 Serial.print("."); 74 delay(500); 75 } 76 77 Serial.println(""); 78 Serial.println("WiFi Connected"); 79 Serial.println("IP address: "); 80 Serial.println(WiFi.localIP()); 81 client.setServer(mqttBroker, 1883); 82 client.setCallback(callback); 83} 84 85void loop() { 86 if (!client.connected()) { 87 reconnect(); 88 } 89 90 sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); 91 sprintf(payload, "%s", ""); // Cleans the payload 92 sprintf(payload, "{\\"%s\\":", VARIABLE_LABEL); // Adds the variable label 93 94 float myecg = analogRead(SENSOR); 95 96 /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/ 97 dtostrf(myecg, 4, 2, str_sensor); 98 99 sprintf(payload, "%s {\\"value\\": %s}}", payload, str_sensor); // Adds the value 100 Serial.println("Publishing data to Ubidots Cloud"); 101 client.publish(topic, payload); 102 client.loop(); 103 delay(10); 104} 105
Downloadable files
circuit diagram
circuit diagram

circuit diagram
circuit diagram

Comments
Only logged in users can leave comments