FELIPE DE OLIVEIRA SAUNDERS
Published © GPL3+

Vending machine

Vending machine with 1 snack and a drink connected to AWS to control it and keep track of products

IntermediateWork in progress5 hours218
Vending machine

Things used in this project

Story

Read more

Schematics

Schematics photo

new photo

Code

cpp_code

C/C++
Code for iot project in cpp to manipulate esp32
#include <Arduino.h>
#include <WiFi.h>
#include "Secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include <iostream>
#include <string>
#include <Stepper.h>

// The MQTT topics that this device should publish/subscribe
#define P_SET_FOOD_TOPIC "set_food"
#define P_SET_DRINK_TOPIC "set_drink"
#define LED_PIN_5 0
#define PUMP 2 //bebida
#define BUTTON_1 15 //BOTÃO TESTE

#define IN1 19
#define IN2 18
#define IN3 5
#define IN4 17

const int stepsPerRevolution = 2048;

Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);

int counter = 0;

void messageHandler(String &topic, String &payload);

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

void connectAWS() {
	Serial.println("teste");
	WiFi.mode(WIFI_STA);
	WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

	Serial.println("Connecting to Wi-Fi");
	Serial.println(WIFI_SSID);
	Serial.println("\n");
	Serial.println(WIFI_PASSWORD);

	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(P_SET_FOOD_TOPIC);
	client.subscribe(P_SET_DRINK_TOPIC);

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

void open() {
	Serial.println("teste print open");
	myStepper.step(2 * stepsPerRevolution/10);
	delay(5000);
	myStepper.step(8 * stepsPerRevolution/10);
}

void manutencao() {
	myStepper.step(2 * stepsPerRevolution/10);
}

void get_drink(String &type) {
	digitalWrite(PUMP, HIGH);
	delay(4000);
	digitalWrite(PUMP, LOW);
	delay(1000);
}

void publishMessage(int topic) {
	StaticJsonDocument<200> doc;

	doc["type"] = topic;

	char jsonBuffer[512];
	serializeJson(doc, jsonBuffer);

	client.publish(P_SET_FOOD_TOPIC, jsonBuffer);
}

void messageHandler(String &topic, String &payload) {
	StaticJsonDocument<200> doc;

	DeserializationError error = deserializeJson(doc, payload);

    if (error) {
        Serial.print("deserializeJson() failed: ");
        Serial.println(error.c_str());
        return;
    }

	String func = doc["func"].as<String>();

	if (topic == P_SET_FOOD_TOPIC && func == "1") {
		open();
	} else if (topic == P_SET_DRINK_TOPIC){
		manutencao();
	}

	const char* message = doc["message"];
}

void setup() {
	Serial.println("teste setup 1234123423");
	Serial.begin(115200);

	myStepper.setSpeed(8);

	pinMode(LED_PIN_5, OUTPUT);
	pinMode(PUMP, OUTPUT);
	pinMode(BUTTON_1, INPUT_PULLUP);
	// pinMode(BUTTON_2, INPUT_PULLUP);

	connectAWS();
}

void set_food(int food_type) {
	Serial.println("test");

	StaticJsonDocument<200> doc;

	doc["type"] = food_type;

	char jsonBuffer[512];

	serializeJson(doc, jsonBuffer); // print to client
    client.publish(P_SET_FOOD_TOPIC, jsonBuffer);

    delay(1000); // Debouncing delay
}

void loop() {
	client.onMessage(messageHandler);
	client.loop();

	int button_1 = !digitalRead(BUTTON_1);
	// int button_2 = !digitalRead(BUTTON_2);

  	if (button_1 == HIGH) {
		set_food(1);
	}

	// if (button_2 == HIGH) {
	// 	Serial.println(button_2);
	// 	close();
	// }

	delay(500);
}

Food dispenser repository

Credits

FELIPE DE OLIVEIRA SAUNDERS
2 projects • 0 followers

Comments