#include <Stepper.h>
#include <HX711.h>
#include <WiFi.h>
#include <ThingSpeak.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber = 2078964;
const char * myWriteAPIKey = "S3I1YJDBGNWHO7E7";
int statusCode;
#define DATA_PIN 4
#define CLOCK_PIN 2
HX711 hx711;
long units;
int stepPin=12;
int DIRPin=13;
int stepPin1=5;
int DIRPin1=18;
int stepPin2=19;
int DIRPin2=21;
int steps = 820;
Stepper myStepper(steps, stepPin, DIRPin);
Stepper myStepper1(steps, stepPin1, DIRPin1);
Stepper myStepper2(steps, stepPin2, DIRPin2);
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
hx711.begin(DATA_PIN, CLOCK_PIN);
hx711.set_scale(420.0983);
hx711.tare();
pinMode(stepPin, OUTPUT);
pinMode(DIRPin, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(DIRPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(DIRPin2, OUTPUT);
myStepper.setSpeed(20);
myStepper1.setSpeed(20);
myStepper2.setSpeed(20);
}
void loop()
{
connectToCloud();
MotorData();
}
void connectToCloud()
{
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
for(int i=0;i<5;i++)
{
Serial.print(".");
delay(1000);
}
}
Serial.println("\nConnected.");
}
}
void MotorData()
{
a:
units = hx711.get_units();
Serial.println(units);
if(units > 0)
{
myStepper.step(steps);
if(units < 25)
{
myStepper1.step(steps);
StoreData(units,1,1,0);
//delay(1000);
goto a;
}
else
{
myStepper2.step(steps);
StoreData(units,1,0,1);
//delay(1000);
goto a;
}
}
}
void StoreData(int weight, int lift,int right,int left)
{
ThingSpeak.setField(4,weight);
ThingSpeak.setField(5,lift);
ThingSpeak.setField(6,right);
ThingSpeak.setField(7,left);
statusCode = ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(statusCode == 200) //successful writing code
Serial.println("Channel update successful.");
else
Serial.println("Problem Writing data. HTTP error code :" +
String(statusCode));
delay(10000);
}