B H
Published

Smart manufacturing RPM counter

Smart manufacturing is all about harnessing data, here we will monitor the RPM of a lathe for maintenance

BeginnerShowcase (no instructions)15 hours4
Smart manufacturing RPM counter

Things used in this project

Hardware components

Argon
Particle Argon
×1
Flora RGB Neopixel LEDs- Pack of 4
Adafruit Flora RGB Neopixel LEDs- Pack of 4
×1
Hall Effect Sensor
Hall Effect Sensor
×1
Resistor 221 ohm
Resistor 221 ohm
×1

Software apps and online services

adafruit.io

Story

Read more

Custom parts and enclosures

attachemnt

attachment stl file

Attachment

The printed piece right after curing

Schematics

bread board fritzing file

RMP fritzzing

Code

Untitled file

C/C++
#include "clickButton.h"
#include "Particle.h"
#include "neopixel.h"

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT/Adafruit_MQTT.h" 
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h" 
#include "Adafruit_MQTT/Adafruit_MQTT.h" 

/************************* Adafruit.io Setup *********************************/ 
#define AIO_SERVER      "io.adafruit.com" 
#define AIO_SERVERPORT  1883                   // use 8883 for SSL 
#define AIO_USERNAME  
#define AIO_KEY       


/************ Global State (you don't need to change this!) ***   ***************/ 
TCPClient TheClient; 

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); 


/****************************** Feeds ***************************************/ 
// Setup Feeds to publish or subscribe 
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> 
Adafruit_MQTT_Publish RPMfeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/RPM_Feed");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
/************Feed Declare Variables*************/
unsigned long last;
float value;
int ledPin = D7;
int variable1, lastTime;


//-----These variables are used for clickButton --------
int Button = D2;
ClickButton button1(Button, CLICKBTN_PULLUP); //LOW, CLICKBTN_PULLUP);
long now = (long)millis(); // this is for the button
int lastBounceTime, debounceTime, btnState, depressed, clickCount, clicks, multiclickTime; //these are for the button

//---NeoPixel Variables ----//
#define PIXEL_PIN D6
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel pixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int HallSensor = D3; // hallsensor

//---RMP function variables---///
int currentTime, lastSecond, countRpm;
bool hallSensorState;
int count = 0;
const unsigned long oneSecond = 1000;
boolean countFlag = LOW;


// setup() runs once, when the device is first turned on.
void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("setup begin");
  pinMode(HallSensor, INPUT_PULLUP);
  digitalRead(HallSensor);
  pixel.begin();
  pixel.show();
  ButtonSetup();
  // mqtt.subscribe(&onoffbutton);
  count = 0;
  // dashBoardSetup();
}
void loop() {

  // button1.Update();
  // buttonFunctionWbounce();
  hallSensorFunction();
  neoPixelRed();
  RMPcounter();
  MQTT_connect();
  MQTTping();
  adafruitPublishing();
  
}

void hallSensorFunction(){
  hallSensorState = digitalRead(HallSensor);
  // Serial.println(hallSensorState);
}
void RMPcounter(){
    //---this code is used so when the senors is high it only counts once---//
    if (hallSensorState == HIGH)
      {
        countFlag = HIGH;
      }
    if (hallSensorState == LOW && countFlag == HIGH)
      {
        
        count++;
        countFlag=LOW;
      }
    currentTime = millis();
    //--timer to muliutply one second of movement by 60 to get RMP---//
    if((currentTime - lastSecond) >1000){  
      countRpm = 60 * count; 
      Serial.printf("counter value is %i, RPM = %i \n", count, countRpm);
      count = 0;
      lastSecond = millis();
    }
}
void buttonFunctionWbounce(){
  if (button1.clicks != 0){
     //--add function here--//
  }
}
void neoPixelRed(){
  //---this lightsup the neo pixel when the sensor is high ---//
    pixel.setPixelColor(0, 255*hallSensorState, 0, 0);
    pixel.setBrightness(10);
    pixel.show();
}
void ButtonSetup(){
  pinMode(Button, INPUT);
  button1.debounceTime   = 20;   // Debounce timer in ms
  button1.multiclickTime = 250;  // Time limit for multi clicks
  button1.longClickTime  = 1000;
  }
// void dashBoardSetup(){
//     mqtt.subscribe(&RPMfeed);
  // }
void MQTT_connect() {
    int8_t ret;
 
    // Stop if already connected.
    if (mqtt.connected()) {
     return;
   }
 
    Serial.print("Connecting to MQTT... ");
 
   while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
    }
    Serial.println("MQTT Connected!");
  }
void MQTTping(){
  if ((millis()-last)>120000) {
      Serial.printf("Pinging MQTT \n");
      if(! mqtt.ping()) {
        Serial.printf("Disconnecting \n");
        mqtt.disconnect();
      }
      last = millis();
  }
}
void adafruitPublishing(){
    if((millis()-lastTime > 120000)) {
    if(mqtt.Update()) {
      RPMfeed.publish(countRpm);
             } 
    lastTime = millis();
  }
}

Credits

B H

B H

2 projects • 3 followers

Comments

Add projectSign up / Login