Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PulseSensor™ Starter Project and Signal Tester
- * The Best Way to Get Started With, or See the Raw Signal of, your PulseSensor™ & Arduino.
- *
- * Here is a link to the tutorial
- * https://pulsesensor.com/pages/code-and-guide
- *
- * WATCH ME (Tutorial Video):
- * https://www.youtube.com/watch?v=82T_zBZQkOE
- *
- *
- -------------------------------------------------------------
- 1) This shows a live human Heartbeat Pulse.
- 2) Live visualization in Arduino's Cool "Serial Plotter".
- 3) Blink an LED on each Heartbeat.
- 4) This is the direct Pulse Sensor's Signal.
- 5) A great first-step in troubleshooting your circuit and connections.
- 6) "Human-readable" code that is newbie friendly."
- */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- const char* ssid = "YourNetworkName"; //masukin nama wifi
- const char* password = "YourNetworkPassword";
- const char* mqttServer = "m12.cloudmqtt.com";
- const int mqttPort = 13219;
- const char* mqttUser = "mwmauqfp";
- const char* mqttPassword = "gHrpJvNsX447";
- WiFiClient espClient;
- PubSubClient client(espClient);
- // Variables
- int PulseSensorPurplePin = 0; // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
- int LED13 = 13; // The on-board Arduion LED
- int Signal; // holds the incoming raw data. Signal value can range from 0-1024
- int Threshold = 550; // Determine which Signal to "count as a beat", and which to ingore.
- // The SetUp Function:
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("Connecting to WiFi..");
- }
- Serial.println("Connected to the WiFi network");
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
- Serial.println("connected");
- } else {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- pinMode(LED13,OUTPUT); // pin that will blink to your heartbeat!
- Serial.begin(9600); // Set's up Serial Communication at certain speed.
- client.publish("kamu", Signal);
- client.subscribe("kamu");
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived in topic: ");
- Serial.println(topic);
- Serial.print("Message:");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- Serial.println("-----------------------");
- }
- // The Main Loop Function
- void loop() {
- client.loop();
- Signal = analogRead(PulseSensorPurplePin); // Read the PulseSensor's value.
- // Assign this value to the "Signal" variable.
- Serial.println(Signal); // Send the Signal value to Serial Plotter.
- if(Signal > Threshold){ // If the signal is above "550", then "turn-on" Arduino's on-Board LED.
- digitalWrite(LED13,HIGH);
- } else {
- digitalWrite(LED13,LOW); // Else, the sigal must be below "550", so "turn-off" this LED.
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement