Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Bouncing_ball.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <SPI.h>
#include <TFT_eSPI.h>

int pot;
float x, y, bin, b;

TFT_eSPI tft = TFT_eSPI();

void setup() {

Serial.begin(115200);
Serial.println("\n\n TFT_eSP Starting");

tft.init();
tft.setRotation(0); //0 = 0°; 1 = 90°; 2 = 180° clockwise

x = 0.0;
y = 12.0; //Starts at (0,12), avoiding top border
bin = 1.0;
b = 0.5; // Y changes by +/-0.5

}

void loop() { //Comments for debugging

tft.fillScreen(0x000000);
tft.fillCircle(x, y, 10, 0xFFFFFF); // position in x, position in y, radius, colour

if (x == 228){
bin = -1;
//Serial.println("\nSubtraindo x");
}else if (x == 12){
bin = 1;
//Serial.print("\n Somando x");
}
x += bin;

if (y == 268){
b = -0.5;
//Serial.print("\nSubtraindo y");
}else if (y == 12){
b = 0.5;
//Serial.println("\n Somando y");
}
y += b;

/*Serial.print("x = ");
Serial.println(x);
Serial.print("\n");
Serial.print("y = ");
Serial.print(y);*/
delay(100);
}
188 changes: 188 additions & 0 deletions ESP32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>

//Digital Inputs
#define sPin 22 //Sensor
#define aPin 4 //Analogic
#define magPin 21 //Magnetic

//Digital Outputs
#define pulsePin 15
#define lockPin 2

//ID MQTT
#define pubMag "MAGS"
#define subLock "DOOR"
#define TOPIC_PUBLISH "HALLWAY"
#define TOPIC_SUBSCRIBE "LOCK"

WiFiClient wifiClient;
PubSubClient MQTT(wifiClient);
PubSubClient MQTT2(wifiClient);

//Consts
const char* ssid = "LIEC_119";
const char* pass = "********";
const char* broker = "public.mqtthq.com";
int port = 1883;
int state, magnetic;

void connectWiFi();
void connectMQTT();
void keepConnections();
void receivePacket(char* topic, byte* payload, unsigned int length);

void setup() {
Serial.begin(9600);
pinMode(pulsePin, OUTPUT);
pinMode(magPin, INPUT_PULLUP);
digitalWrite(lockPin, OUTPUT);

connectWiFi();

MQTT.setServer(broker, port);
MQTT2.setServer(broker, port);
MQTT2.setCallback(receivePacket);
}

void connectWiFi(){
Serial.println("Connecting to");
Serial.print(ssid);
Serial.println("...");

WiFi.begin(ssid, pass);

Serial.print("\nConnecting to ");
Serial.print(ssid);
Serial.print(".");

while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print("...");
}
if (WiFi.status() == WL_CONNECTED){
Serial.print("\nConnected to ");
Serial.print(WiFi.localIP());
return;
}

}
void connectMQTT(){
if (MQTT.connected()){
return;
}
while (!MQTT.connected()){
Serial.print("\nConnecting to ");
Serial.print(broker);
if (MQTT.connect(pubMag)){
Serial.print("\nConnected to Broker for pubMag.");
magnetic = digitalRead(magPin);
if (magnetic == HIGH){
MQTT.publish(TOPIC_PUBLISH, "1");
Serial.print("\nPorta Aberta");
delay(3000);

} else if (magnetic == LOW) {
MQTT.publish(TOPIC_PUBLISH, "0");
Serial.print("\nPorta Fechada");
delay(3000);
}
break;
}
else if(!MQTT.connected()) {
Serial.print("\nCould not connect to pubMag. New attempt in 3 seconds.");
delay(3000);
}
}
}
void connectMQTT2(){
if (MQTT2.connected()){
return;
}
while (!MQTT2.connected()){
Serial.print("\nConnecting to ");
Serial.print(broker);
if (MQTT2.connect(subLock)){
Serial.print("\nConnected to Broker for subLock.");
MQTT2.subscribe(TOPIC_SUBSCRIBE);
if (MQTT.connected()){
magnetic = digitalRead(magPin);
if (magnetic == HIGH){
MQTT.publish(TOPIC_PUBLISH, "1");
Serial.print("\nPorta Aberta");
delay(3000);

} else if (magnetic == LOW) {
MQTT.publish(TOPIC_PUBLISH, "0");
Serial.print("\nPorta Fechada");
delay(3000);
}
}
if (MQTT2.connected()){
break;
}
}
else if(!MQTT2.connected()) {
Serial.print("\nCould not connect to sublock. New attempt in 3 seconds.");
delay(3000);
}
}
}
void pulse(){
digitalWrite(pulsePin, HIGH);
state = HIGH;
delay(500);
if (state == HIGH){
digitalWrite(pulsePin, LOW);
state = HIGH;
delay(3000);
}
}

void keepConnections(){
if(!MQTT.connected()){
connectMQTT();
}
if(!MQTT2.connected()){
connectMQTT2();
}
if(WiFi.status() != WL_CONNECTED){
connectWiFi();
}
}
void receivePacket(char* topic, byte* payload, unsigned int length){
String msg;

for(int i = 0; i < length; i++){
char c = (char)payload[i];
msg += c;
}
if (msg == "1"){
pulse();
Serial.print("\nMessage Received '1'");

}
}

void magSensor(){
magnetic = digitalRead(magPin);

if (magnetic == HIGH){
MQTT.publish(TOPIC_PUBLISH, "1");
Serial.print("\nPorta Aberta");
delay(3000);

} else if (magnetic == LOW) {
MQTT.publish(TOPIC_PUBLISH, "0");
Serial.print("\nPorta Fechada");
delay(3000);
}
}

void loop() {
magSensor();
keepConnections();
MQTT.loop();
MQTT2.loop();
}
16 changes: 0 additions & 16 deletions HelloWorld/HelloWorld.ino

This file was deleted.

39 changes: 39 additions & 0 deletions Module 01 - ESP32/include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
46 changes: 46 additions & 0 deletions Module 01 - ESP32/lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
18 changes: 18 additions & 0 deletions Module 01 - ESP32/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps =
miguelbalboa/MFRC522@^1.4.10
mbed-jackb/EEPROM@0.0.0+sha.b90c5754d8db
ESPNow
Loading