#include <AccelStepperWithDistance.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <Wire.h>
#define STEP_PIN 12
#define DIR_PIN 14
#define PIN_TRIG 4
#define PIN_ECHO 0
#define PIN_LED 15
#define NTP_SERVER "0.br.pool.ntp.org"
#define HTTP_SERVER_TASKS "https://67f1b757c733555e24ade81b.mockapi.io/api/v1/Tarefas/teste"
#define UTC_OFFSET -10800
#define UTC_OFFSET_DST 0
int targetHour = 19; // Hora alvo
int targetMinute = 0; // Minuto alvo
bool alreadyExecuted = false;
AccelStepperWithDistance stepper(AccelStepperWithDistance::DRIVER, STEP_PIN, DIR_PIN);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void stepperSetup(){
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
stepper.setStepsPerRotation(200); // For a 1.8° stepper motor
stepper.setMicroStep(2); // If using 1/16 microstepping
stepper.setDistancePerRotation(8); // If one rotation moves 8mm
}
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void updatingDatetime(){
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void printLocalTime(struct tm* timeinfo) {
LCD.setCursor(8, 0);
LCD.println(timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(timeinfo, "%d/%m/%Y %Z");
}
void wifiSetup(){
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Conectando-se ao");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
spinner();
}
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Conectado!");
LCD.setCursor(0, 1);
LCD.print(WiFi.localIP());
}
void processarJsonHttpResponse(String jsonResponse) {
StaticJsonDocument<2048> doc;
DeserializationError error = deserializeJson(doc, jsonResponse);
if (error) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Erro ao fazer parse do JSON: ");
LCD.setCursor(0, 1);
LCD.print(error.c_str());
delay(1000);
return;
}
for (JsonObject pessoa : doc.as<JsonArray>()) {
int deslocamento = pessoa["deslocamento"];
executaMotor(deslocamento);
}
}
void carregaTarefas(){
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(HTTP_SERVER_TASKS); // URL da API
int httpResponseCode = http.GET(); // Faz a requisição GET
if (httpResponseCode > 0) {
LCD.clear();
LCD.setCursor(0, 0);
processarJsonHttpResponse(http.getString()); // Pega a resposta como string
} else {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Erro na requisição: ");
LCD.print(httpResponseCode);
}
http.end(); // Libera a memória
}
}
void executaTarefa1(int t_hora, int t_min){
if (t_hora >= targetHour && t_min >= targetMinute && !alreadyExecuted) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Executando motor");
stepper.runToNewDistance(100); // Move 100mm
delay(1000);
stepper.runToNewDistance(0); // Return to starting position
delay(1000);
alreadyExecuted = true;
LCD.clear();
}
else{
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Tarefa 1 Já executada.");
delay(1000);
}
}
void executaMotor(int move){
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Executando motor");
LCD.setCursor(0, 1);
LCD.print("Desl. ");
LCD.print(move);
spinner();
stepper.runToNewDistance(move);
delay(1000);
}
void setupLed(){
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW);
}
void inicio(){
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Retrocedendo ao inicio.");
LCD.setCursor(0, 1);
LCD.print("Desl. 0");
stepper.runToNewDistance(0);
delay(10000);
}
void setupUltraSonic(){
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void medeSensorUltraSonico(){
delay(1000);
digitalWrite(PIN_LED, HIGH);
int dist = 100;
do{
// Inicia uma nova medição:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Leia o resultado:
int duration = pulseIn(PIN_ECHO, HIGH);
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Dist. cm:");
LCD.print(duration / 58);
LCD.setCursor(0, 1);
LCD.print("Dist. pol: ");
LCD.print(duration / 148);
spinner();
dist = duration / 58;
delay(1000);
}while(dist >= 3);
}
void setup() {
setupLed();
setupUltraSonic();
stepperSetup();
wifiSetup();
updatingDatetime();
digitalWrite(PIN_LED, LOW);
}
void loop() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Connection Err");
return;
}
digitalWrite(PIN_LED, LOW);
carregaTarefas();
inicio();
medeSensorUltraSonico();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Verif. taref.");
delay(1000);
printLocalTime(&timeinfo);
LCD.clear();
LCD.setCursor(0, 1);
LCD.print("Ocioso");
spinner();
delay(1000);
}