I am trying to send my temperature and heart rate readings collected on the Arduino Uno board to ESP8266 which will then transmit these sensor readings to Node-Red using MQTT.
The following are my Arduino codes to receive the sensor readings.
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
const int PulseWire=0;
int Threshold=550;
PulseSensorPlayground pulseSensor;
int myBPM;
int tempPin=1;
int val;
float cel;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pulseSensor.analogInput(PulseWire);
pulseSensor.setThreshold(Threshold);
if(pulseSensor.begin()){
Serial.println("We created a PulseSensor Object!");
}
}
void loop() {
// put your main code here, to run repeatedly:
int myBPM =pulseSensor.getBeatsPerMinute();
if(pulseSensor.sawStartOfBeat()){
Serial.print("BPM:");
Serial.println(myBPM);
delay(500);
val=analogRead(tempPin);
float mv=(val/1024.0)*500;
float cel=mv/10;
Serial.print("TEMPERATURE: ");
Serial.println(cel);
}
delay(500);
}
The following sketch is used to program the nodemcu
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
SoftwareSerial NodeMcu_SoftSerial (D6,D5); //Rx,Tx pins
int myBPM;
float cel;
//Use a hotspot
const char* ssid = "kickyourass_5G";
const char* password = "mypassword";
const char* mqtt_server = "192.168.1.118";
#define mqtt_user "mabel"
#define mqtt_password "topsecret"
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [ ");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("AshesiOutTopic","Welcome to Mabel's World");
// ... and resubscribe
//client.subscribe("AshesiOutTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
NodeMcu_SoftSerial.begin (9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// Allow the hardware to sort itself out
delay(1500);
}
void loop() {
// put your main code here, to run repeatedly:
// String msg = NodeMcu_SoftSerial.readStringUntil('\r');
//Serial.println(msg);
// String myBPM_str= NodeMcu_SoftSerial.readStringUntil('\n');
//int myBPM= myBPM_str.toInt();
//Serial.println(myBPM_str);
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
now = millis();
if (now-lastMeasure>30000){
lastMeasure = now;
while(NodeMcu_SoftSerial.available()>0){
myBPM=NodeMcu_SoftSerial.parseInt();
cel=NodeMcu_SoftSerial.parseFloat();
// Check if any reads failed and exit early (to try again).
if (isnan(myBPM) || isnan(cel)) {
Serial.println("Failed to read from sensors!");
return;
}
String myBPM_Values= String(myBPM);
Serial.print("BPM:");
Serial.println(myBPM_Values);
client.publish("sensor/HeartRate",myBPM_Values.c_str());
cel=NodeMcu_SoftSerial.parseFloat();
String celValues=String(cel);
Serial.print("TEMPERATURE:");
Serial.println(celValues);
client.publish("sensor/temperature",celValues.c_str());
}
client.subscribe("HeartRateValues");
client.subscribe("TemperatureValues");
}
}
however, I keep getting results like these on the nodemcu serial monitor.
I am still very new to how MQTT works so any help would be useful.