João Pedro lopes da silva
Published

Cafeteira online

Nesse projeto, nós buscamos simular a lógica de uma cafeteira e indentificamos quantas pessoas pediram de cada opção usando um ESP32

BeginnerShowcase (no instructions)6
Cafeteira online

Things used in this project

Story

Read more

Code

Cafeteira Online

C/C++
#include <WiFi.h>
#include <ThingSpeak.h>

// ---- Config WiFi ----
const char* ssid = "Web";       
const char* password = "SILVA2503";

// ---- Config ThingSpeak ----
unsigned long myChannelNumber = 3041580;      
const char* myWriteAPIKey = "8BPOTQFYJHQK5J3V";

WiFiClient client;

// Pinos dos botões
const int botaoCafe = 13;
const int botaoLeite = 12;
const int botaoChocolate = 14;

// Pinos dos LEDs
const int ledCafe = 25;
const int ledLeite = 26;
const int ledChocolate = 27;
const int ledVermelho = 33;
const int ledVerde = 32;

// Pino do buzzer
const int buzzer = 15;

// Contadores de bebidas
int totalCafe = 0;
int totalLeite = 0;
int totalChocolate = 0;

// Controle de tempo para ThingSpeak
unsigned long ultimaAtt = 0;

void setup() {
  Serial.begin(115200);

  // Conexão WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi conectado!");

  ThingSpeak.begin(client);

  // Configura botões
  pinMode(botaoCafe, INPUT_PULLUP);
  pinMode(botaoLeite, INPUT_PULLUP);
  pinMode(botaoChocolate, INPUT_PULLUP);

  // Configura saídas
  pinMode(ledCafe, OUTPUT);
  pinMode(ledLeite, OUTPUT);
  pinMode(ledChocolate, OUTPUT);
  pinMode(ledVermelho, OUTPUT);
  pinMode(ledVerde, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  // Verifica botões
  if (digitalRead(botaoCafe) == LOW) {
    totalCafe++;
    iniciarProcesso(ledCafe, "Café");
  } 
  else if (digitalRead(botaoLeite) == LOW) {
    totalLeite++;
    iniciarProcesso(ledLeite, "Leite");
  } 
  else if (digitalRead(botaoChocolate) == LOW) {
    totalChocolate++;
    iniciarProcesso(ledChocolate, "Chocolate");
  }

  // ---- Envia para ThingSpeak a cada 20s ----
  if (millis() - ultimaAtt > 20000) {
    ThingSpeak.setField(1, totalCafe);
    ThingSpeak.setField(2, totalLeite);
    ThingSpeak.setField(3, totalChocolate);

    int httpCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

    if (httpCode == 200) {
      Serial.println("✔ Dados enviados ao ThingSpeak!");
    } else {
      Serial.println("erro ao enviar: " + String(httpCode));
    }

    ultimaAtt = millis();
  }
}

void iniciarProcesso(int ledBebida, String nome) {
  // Acende LED e vermelho
  digitalWrite(ledBebida, HIGH);
  digitalWrite(ledVermelho, HIGH);
  Serial.println("Preparando " + nome + " ...");

  delay(5000);

  digitalWrite(ledVermelho, LOW);

  digitalWrite(ledVerde, HIGH);
  tone(buzzer, 1000);

  delay(1000);

  digitalWrite(ledBebida, LOW);
  digitalWrite(ledVerde, LOW);
  noTone(buzzer);
}

Credits

João Pedro lopes da silva
1 project • 0 followers
Thanks to Antônio Leonardo and Webton da Silva Pereira.

Comments