ESP8266, MQTT, Node-Red, Arduino

Hello,

Its my first time publishing and looking for help from this community. I am a product designer, trying to create working prototype to my project.

In short, its an advanced IoT Coinbank where there is ultrasonic sensor counting coins that activates my 5v stepper motor. At the same time I want my product to send the data of the coin count to be seen on Node-red dashboard.( It is what we are instructed to do)

Thus, by now, I have a working codes both for the System(Sensor-motor) and NodeMcu. I am stuggling the next steps since I am fairly new to the programming logic.

Here is my codes for both of them, I cannot combine these two to be able to connect to a broker so that on Node-red, I can communicate. Would you help me to come with a single master code that enables me to do all these things?

ps. I thought about abandoning Arduino and use my Nodemcu as the main card but I learned that NodeMcu is working with 3.3V while my motor needs 5V. Thanks in advance.

Sensor-Motor:

#include <Wire.h> // Comes with Arduino IDE
#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
#include <Stepper.h>

long duration, distance; // Duration used to calculate distance
int sensorCounter = 0; // counter for the number of button presses
int lastsensorDistance = 0;
int setCounter = 20;
int incomingByte;
int stepsPerRevolution = 3200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
Serial.begin (115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myStepper.setSpeed(4);

}

void loop() {

if (Serial.available() > 0) { // see if there's incoming serial data:
incomingByte = Serial.read(); // read the oldest byte in the serial buffer:
if (incomingByte == 'R') { // if it's a capital R, reset the counter
Serial.println("Reset");
sensorCounter = 20;

}
}

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance <= 5&& lastsensorDistance >= 40){
sensorCounter++;
Serial.print("number of counts: ");
Serial.println(sensorCounter);
Serial.println(distance);
myStepper.step(stepsPerRevolution);
delay(100);
}

else {

//Serial.println("off"); not needed.

}

lastsensorDistance = distance;
delay(500);
}

NodeMcu:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "................";
const char* password = "...............";
const char* mqttServer = "postman.cloudmqtt.com";
const int mqttPort = 12394;
const char* mqttUser = "........";
const char* mqttPassword = "............";

WiFiClient espClient;
PubSubClient client(espClient);

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);

}
}

client.publish("esp/test", "Hello from ESP8266");
client.subscribe("esp/test");

}

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*);*

  • }*

  • Serial.println();*

  • Serial.println("-----------------------");*

}

void loop() {

  • client.loop();*
    }