Advertisement
Guest User

aaa

a guest
May 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1.  
  2. /*  PulseSensor™ Starter Project and Signal Tester
  3.  *  The Best Way to Get Started  With, or See the Raw Signal of, your PulseSensor™ & Arduino.
  4.  *
  5.  *  Here is a link to the tutorial
  6.  *  https://pulsesensor.com/pages/code-and-guide
  7.  *
  8.  *  WATCH ME (Tutorial Video):
  9.  *  https://www.youtube.com/watch?v=82T_zBZQkOE
  10.  *
  11.  *
  12. -------------------------------------------------------------
  13. 1) This shows a live human Heartbeat Pulse.
  14. 2) Live visualization in Arduino's Cool "Serial Plotter".
  15. 3) Blink an LED on each Heartbeat.
  16. 4) This is the direct Pulse Sensor's Signal.
  17. 5) A great first-step in troubleshooting your circuit and connections.
  18. 6) "Human-readable" code that is newbie friendly."
  19.  
  20. */
  21. #include <ESP8266WiFi.h>
  22. #include <PubSubClient.h>
  23.  
  24. const char* ssid = "YourNetworkName"; //masukin nama wifi
  25. const char* password =  "YourNetworkPassword";
  26. const char* mqttServer = "m12.cloudmqtt.com";
  27. const int mqttPort = 13219;
  28. const char* mqttUser = "mwmauqfp";
  29. const char* mqttPassword = "gHrpJvNsX447";
  30.  
  31. WiFiClient espClient;
  32. PubSubClient client(espClient);
  33.  
  34. //  Variables
  35. int PulseSensorPurplePin = 0;        // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
  36. int LED13 = 13;   //  The on-board Arduion LED
  37.  
  38.  
  39. int Signal;                // holds the incoming raw data. Signal value can range from 0-1024
  40. int Threshold = 550;            // Determine which Signal to "count as a beat", and which to ingore.
  41.  
  42.  
  43. // The SetUp Function:
  44. void setup() {
  45.     Serial.begin(115200);
  46.  
  47.   WiFi.begin(ssid, password);
  48.  
  49.   while (WiFi.status() != WL_CONNECTED) {
  50.     delay(500);
  51.     Serial.println("Connecting to WiFi..");
  52.   }
  53.   Serial.println("Connected to the WiFi network");
  54.  
  55.   client.setServer(mqttServer, mqttPort);
  56.   client.setCallback(callback);
  57.  
  58.   while (!client.connected()) {
  59.     Serial.println("Connecting to MQTT...");
  60.  
  61.     if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
  62.  
  63.       Serial.println("connected");  
  64.  
  65.     } else {
  66.  
  67.       Serial.print("failed with state ");
  68.       Serial.print(client.state());
  69.       delay(2000);
  70.  
  71.     }
  72.   }
  73.  
  74.    pinMode(LED13,OUTPUT);         // pin that will blink to your heartbeat!
  75.    Serial.begin(9600);         // Set's up Serial Communication at certain speed.
  76.  
  77.   client.publish("kamu", Signal);
  78.   client.subscribe("kamu");
  79.  
  80. }
  81.  
  82. void callback(char* topic, byte* payload, unsigned int length) {
  83.  
  84.   Serial.print("Message arrived in topic: ");
  85.   Serial.println(topic);
  86.  
  87.   Serial.print("Message:");
  88.   for (int i = 0; i < length; i++) {
  89.     Serial.print((char)payload[i]);
  90.   }
  91.  
  92.   Serial.println();
  93.   Serial.println("-----------------------");
  94.  
  95. }
  96.  
  97. // The Main Loop Function
  98. void loop() {
  99.     client.loop();
  100.   Signal = analogRead(PulseSensorPurplePin);  // Read the PulseSensor's value.
  101.                                               // Assign this value to the "Signal" variable.
  102.  
  103.    Serial.println(Signal);                    // Send the Signal value to Serial Plotter.
  104.  
  105.  
  106.    if(Signal > Threshold){                          // If the signal is above "550", then "turn-on" Arduino's on-Board LED.
  107.      digitalWrite(LED13,HIGH);
  108.    } else {
  109.      digitalWrite(LED13,LOW);                //  Else, the sigal must be below "550", so "turn-off" this LED.
  110.    }
  111.  
  112.  
  113. delay(10);
  114.  
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement