Hardi Kurnianto
Published

PATAGI : Your Personal ELPIGI Assistance

Smart assistant for ELPIJI tube holder that can do online monitoring and ordering

BeginnerWork in progress4 hours155
PATAGI : Your Personal ELPIGI Assistance

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Load Cell, Low Profile
Load Cell, Low Profile
×1
SparkFun Load Cell Amplifier - HX711
SparkFun Load Cell Amplifier - HX711
×1

Software apps and online services

Arduino IDE
Arduino IDE
VS Code
Microsoft VS Code

Story

Read more

Code

PATAGI.ino

Arduino
#include "HX711.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "Anya Geraldine";
const char* password = "langsungmasukkok";
const char* mqtt_server = "rmq2.pptik.id"; 
const char* mqtt_user = "TMDG2022";
const char* mqtt_pass= "TMDG2022";
 
WiFiClient espClient;
PubSubClient client(espClient); 
HX711 scale(D5, D6);

float weight;
float calibration_factor = -444525; 

void setup_wifi() {
  // Connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void setup() 
{
  Serial.begin(115200);
  scale.set_scale();
  scale.tare();
  long zero_factor = scale.read_average(); //Get a baseline reading
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}
 
void loop() 
 
{
 char msg[8];
 if (!client.connected()) {reconnect();}
 scale.set_scale(calibration_factor);
 weight = scale.get_units(5);
 sprintf(msg,"%f",weight); 
 client.publish("mq2_mqtt",msg);
 Serial.print("Weight: ");
 Serial.print(weight);
 Serial.println(" KG");
 Serial.println();
  
}

void reconnect() {
  // Loop until we're reconnected
  Serial.println("In reconnect...");
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("Arduino_Gas", mqtt_user, mqtt_pass)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
 

receiver.py

Python
from email import message
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
    print("Connected to broker")
 
def on_message(client, userdata, message):
    message.payload = message.payload.decode("utf-8")
    print ("Berat: "  + message.payload)

client = mqtt.Client()
client.username_pw_set("TMDG2022", password='TMDG2022')
client.connect("rmq2.pptik.id", 1883) 

client.on_connect = on_connect       #attach function to callback
client.on_message = on_message       #attach function to callback

client.subscribe("mq2_mqtt") 
client.loop_forever()                 #start the loop

Credits

Hardi Kurnianto

Hardi Kurnianto

17 projects • 16 followers
Master student at Intelligent Control and Systems Engineering Department

Comments