Arduino Serial Comm IOT

 avatar
user_8125766783
plain_text
2 years ago
2.7 kB
1
Indexable
Never
#include <SoftwareSerial.h>
SoftwareSerial ArduinoUno(2,3);                  // Serial Communication

//WEIGHT SENSOR
#include <HX711.h>
HX711 scale;
#define DOUT 12
#define CLK 11
int rbutton = 10;                             // this button will be used to reset the scale to 0.
float weight;
float calibration_factor = 419640; 

//VOLTAGE SENSOR
int Vsensor = A0;
int Initial_analogread = 0;                  // Just to check initial analog read for sensor accuracy
float vout = 0.0; 
float vin = 0.0; 
float R1 = 30000;     
float R2 = 7500;   
int value = 0; 
  
float sdata1 = 0; // Voltage sensor data
float sdata2 = 0; // Weight sensor data

const long sensor_delay = 1000;              // fixed all sensor delays (1 second)
long rememberTime=0;
 
String cdata; // complete data, consisting of sensors values
 
void setup()
{
Serial.begin(115200); 
ArduinoUno.begin(9600);
 
  pinMode(rbutton, INPUT_PULLUP); 
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");
  
  scale.begin(DOUT, CLK );
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  
  long zero_factor = scale.read_average();     //Get a baseline reading
  Serial.print("Zero factor: ");               //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
 
  pinMode(Vsensor, INPUT); // 
 
 
}
 
void loop()
{
  
  Voltage_Sensor();
  Weight_Sensor();
  
    
    sdata1 = vin;         // Voltage Sensor Value
    sdata2 = weight;      // Weight Sensor Value

    
 
   cdata = cdata + sdata1+","+sdata2;     //+","+sdata3;  comma will be used a delimeter

   if( (millis()- rememberTime) >= sensor_delay){     
     Serial.println(cdata); 
     ArduinoUno.println(cdata);                                           
     rememberTime=millis();                                          // remember Current millis() time
    }  
   cdata = ""; 
 
}

void Voltage_Sensor()
{
 // Voltage Sensor
   value = analogRead(Vsensor); 
   //Serial.println(value);       Just to check initial analog reading for Voltage sensor accuracy
   vout = ((value-Initial_analogread) * 4.85) / 1023.0;      //4.85V reading for sensor VCC  
   vin = vout / (R2/(R1+R2));  
}

void Weight_Sensor()
{
// Weight Sensor
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  weight = scale.get_units(5);  
}