Can't read value of Weight and power when adding AWS coding. Without Aws Code, the reading is good. Can you guys help me out? Thanks

AWS CODE (The value of output= 0)

#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include "LiquidCrystal_I2C.h"
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Vp 25
#define ACS712_PIN 26
// The MQTT topics that this device should publish/subscribe
#define AWS_IOT_PUBLISH_TOPIC   "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"

int initialValue;
int avg;
int acc;
int x;
int ctr;
float weight2[5];
float current;
float weight;
float power;

WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);

void connectAWS()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("Connecting to Wi-Fi");

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  // Configure WiFiClientSecure to use the AWS IoT device credentials
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);

  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.begin(AWS_IOT_ENDPOINT, 8883, net);

  // Create a message handler
  client.onMessage(messageHandler);

  Serial.print("Connecting to AWS IOT");

  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if(!client.connected()){
    Serial.println("AWS IoT Timeout!");
    return;
  }

  // Subscribe to a topic
  client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

  Serial.println("AWS IoT Connected!");
}

void publishMessage()
{
  StaticJsonDocument<200> doc;
  doc["Weight (Kg)"] = weight;
  doc["Power consumption (W)"] = power;
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer); // print to client

  client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}

void messageHandler(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

  StaticJsonDocument<200> doc;
 deserializeJson(doc, payload);
 const char* message = doc["message"];
}

void setup() {
  Serial.begin(9600);
  pinMode(Vp, INPUT);
  pinMode(ACS712_PIN, INPUT);
  lcd.init();
  lcd.backlight();
  acc = 0;
  for (int i = 0; i < 5; i = i + 1) {
    delay(80);  //80
    acc = acc + analogRead(Vp);
    delay(50); //50
  }

  initialValue = acc / 5;
  delay(5000);
  Serial.println(initialValue);
  ctr = 0;

  connectAWS();
}

void loop() {
  acc = 0;

  avg = analogRead(Vp);

  for (int i = 0; i < 5; i = i + 1) {
    delay(80);
    acc = acc + analogRead(Vp);
    delay(50);
  }
    avg = acc / 5;
    weight = (avg - initialValue) / 17.5;
  if (weight < 0)
    weight = 0;
  // Read the current from the ACS712 sensor
  current = analogRead(ACS712_PIN) * 0.0048828125;
  // Calculate the power
  power = current * weight;
  
  
  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println("Kg");
  Serial.print("Current: ");
  Serial.print(current);
  Serial.println("A");
  Serial.print("Power Consumption: ");
  Serial.print(power);
  Serial.println("W");

  lcd.setCursor(0, 1);
  lcd.print("Power(W)=");
  lcd.print(power);

  lcd.setCursor(0, 0);
  lcd.print("Load(kg)=");
  lcd.print(weight);

  
  publishMessage();
  client.loop();
  delay(1000);
}

WITHOUT AWS (The value shown)

#include <Wire.h>
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27, 16,2);
#define Vp 25
#define ACS712_PIN 26

int initialValue;
int avg;
int acc;
int x;
int ctr;
float weight2[5];
float current;
float power;
float voltage=12;

void setup() {
  Serial.begin(9600);
  pinMode(Vp, INPUT);
  pinMode(ACS712_PIN, INPUT);
  lcd.init();
  lcd.backlight();
  
  acc = 0;
  for (int i = 0; i < 5; i = i + 1) {
    delay(80);  //80
   acc = acc + analogRead(Vp);
   delay(50); //50
  }
  
  initialValue = acc/5;
  delay(5000);
  Serial.println(initialValue);

  ctr = 0;
}

void loop() {
  acc = 0;

  avg = analogRead(Vp);

  for (int i = 0; i < 5; i = i + 1) {
    //delay(80);
    acc = acc + analogRead(Vp);
    delay(50);
  }

  avg = acc / 5;
  float weight = (avg - initialValue) / 17.5;
  if (weight < 0)
    weight = 0;

  // Read the current from the ACS712 sensor
  current = analogRead(ACS712_PIN) * 0.0048828125;

  // Calculate the power
  power = current * weight;

  // Print the current and power to the serial monitor
  //Serial.print("IV: ");
  //Serial.print(initialValue);
 // Serial.println("Avg: ");
  //Serial.print(avg);
  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println("Kg");
  Serial.print("Current: ");
  Serial.print(current);
  Serial.println("A");
  Serial.print("Power Consumption: ");
  Serial.print(power);
  Serial.println("W");
 

  // Print the current and power to the LCD
  
  lcd.setCursor(0, 1);
  lcd.print("Power(W)=");
  lcd.print(power);
  
  lcd.setCursor(0, 0);
  lcd.print("Load(kg)=");
  lcd.print(weight);
   
   
  delay(1000);
}

WITH AWS


**WITHOUT AWS **

is there a difference if the AWS code is added but not called?

1 Like

Yes, without adding AWS code, its called the sensor data when i put weight on the sensor. But, with adding AWS code sensor is not being called, it will send Weight: 0 kg even i already put weight. and it will send 0 value constantly to AWS bucket.

so adding the AWS code, but not using results in 0 weight?

I meant when i add 1.5kg load to coding aws, it also display 0 value at serial monitor and also sent to aws 0 value. It should be 1.5kg sent to aws and display on serial, right? seem like it doesnt read my calculation for weight and power if i add aws code

this is different than adding the code but not using it?

perhaps you can start with code that works. start adding the pieces of the AWS code without using and then calling parts of the AWS code

  • the net. calls
  • the client connect
  • client.onMessage
  • client.subscribe
  • client.loop

do half to figure out in which half the weighing stopped working, then half of that half and so on

2 Likes

Alright i”ll try first

its working untill the client.loop being called, the weighing stop working

i think i must be misunderstanding what the problem is because it looks like the weight is simply a read of an analog pin and i can't imagine how that is affected by an MQTT operation

in your code there is an initial read of the the Vp pin. is that correct?

yes Vp pin is correct, i use vp pin of esp32 for weighing ouput.

no.
did that code, in setup(), read and report the weight as expected?

yes without aws coding, but not read and report as expected result with aws function

Try different pins

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.