Fred Hügel
Published © GPL3+

Monitoring 3D print head movement

Monitoring 3D print head movement with ESP8266 and sensor SW18010p. The status is sent to an MQTT broker and evaluated with Openhab

IntermediateFull instructions provided1 hour111
Monitoring 3D print head movement

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
AMS1117 3.3V
×1
SW-18010P
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Through Hole Resistor, 470 kohm
Through Hole Resistor, 470 kohm
×1

Software apps and online services

Arduino IDE
Arduino IDE
MQTT
MQTT

Story

Read more

Custom parts and enclosures

3D Model Sensorholder

Holder for Anycubic Mega

Schematics

sw18010_29AiQTtL5g.jpeg

Code

Sketch

C/C++
#include <ESP8266WiFi.h>
#include <MQTTClient.h>

WiFiClient net;

const char* WLANSSID = "SSID";
const char* WLANPWD = "****";

const char* HOST = "192.168.xx.yy"; // MQTT Host
const char* outTopic = "ESP-3DP";
IPAddress ipAdr;
boolean bwg = false;

#define SENS1 2

unsigned long tmMillis = 0;
unsigned long tsMillis = 0;
int deltaTime = 30000; // 30s

void setup() {
  Serial.begin(115200);
  delay(2000);
  Serial.println(F(""));
  Serial.print(F("Chip-ID: "));
  Serial.println(ESP.getChipId());

  pinMode(SENS1, INPUT);
  initWifi();

  send_raspi(1);
  delay(1000);
  send_raspi(0);
}

void loop() {
  unsigned long currentMillis;
  bool sentRaspi = false;

  currentMillis = millis();

  if (digitalRead(SENS1) == LOW) {
    tmMillis = currentMillis;
    if (bwg == false) {
      send_raspi(1);
      sentRaspi = true;
      bwg = true;
    }
  }
  else {
    if (bwg) {
      if ((currentMillis - tmMillis) > deltaTime) {
        send_raspi(0);
        sentRaspi = true;
        bwg = false;
      }
    }
  }

  if (sentRaspi == false) {
    if ((currentMillis - tsMillis) > deltaTime) {
      send_raspi((bwg) ? 1 : 0);
      tsMillis = currentMillis;
    }
  }
  else
    sentRaspi = false; 
  delay(20);
}

void initWifi() {
  if (WiFi.status() == WL_CONNECTED)
  {
    return;
  }

  WiFi.begin(WLANSSID, WLANPWD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  delay(1000);
  ipAdr = WiFi.localIP();
}

void send_raspi(int x) {
  WiFiClient mqclient;
  MQTTClient mqtt;
  String s;
  
  Serial.print(x);
  initWifi();
  
  mqtt.begin(HOST, 1884, mqclient);
  if (mqtt.connect("ESP8266-3DP") ) {
    s = (x == 1) ? "ON" : "OFF";
    mqtt.publish("ESP-3DP/PRN", s);

    Serial.println(" gesendet ..");
    delay(2000);
  }
}

Credits

Fred Hügel
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.