the-cup/the-cup.ino

135 lines
3.4 KiB
Arduino
Raw Normal View History

2025-11-06 16:47:34 +01:00
/* Pin layout used:
2025-08-10 17:53:45 +02:00
* -----------------------------------------------------------------------------------------
* MFRC522 ESP8266
* Reader/PCD Feather Huzzah
* Signal Pin Pin
* -----------------------------------------------------------------------------------------
* SPI SS 1 SDA(SS) SDA/4
* SPI SCK SCK 14 / SCK
* SPI MOSI MOSI 13 / MOSI
* SPI MISO MISO 12 / MISO
*
*/
2025-08-07 23:31:30 +02:00
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
2025-11-01 16:52:03 +01:00
2025-11-07 17:58:54 +01:00
#include <ESP8266WiFi.h> // https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html
2025-08-07 23:31:30 +02:00
#include <ESP8266HTTPClient.h>
2025-09-26 11:31:14 +02:00
#include <WiFiClientSecureBearSSL.h>
2025-11-01 16:52:03 +01:00
// #include "certs.h"
2025-08-07 23:31:30 +02:00
2025-11-07 17:58:54 +01:00
#include <MFRC522v2.h> // https://github.com/OSSLibraries/Arduino_MFRC522v2
2025-08-07 23:31:30 +02:00
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>
2025-11-06 16:40:52 +01:00
#define LED_PIN 15
2025-11-01 17:44:03 +01:00
#define SS_PIN 4
2025-09-27 11:45:54 +02:00
2025-11-01 17:44:03 +01:00
MFRC522DriverPinSimple ss_1_pin(SS_PIN); // Configurable, take an unused pin, only HIGH/LOW required, must be different to SS 2.
2025-08-07 23:31:30 +02:00
MFRC522DriverSPI driver_1{ss_1_pin};
MFRC522 reader{driver_1}; // Create MFRC522 instance.
void read_card(char reader_uid[]);
void send_uid(char reader_uid[]);
2025-08-07 23:31:30 +02:00
void setup() {
2025-11-06 16:47:34 +01:00
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
2025-08-07 23:31:30 +02:00
2025-11-06 16:47:34 +01:00
reader.PCD_Init(); // Init each MFRC522 card.
2025-08-07 23:31:30 +02:00
}
void loop() {
2025-11-06 16:47:34 +01:00
if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) {
2025-11-08 10:49:17 +01:00
char reader_uid[32] = "";
read_card(reader_uid);
send_uid(reader_uid);
}
}
void read_card(char reader_uid[]) {
reader.PICC_HaltA();
reader.PCD_StopCrypto1();
byte_to_string(reader.uid.uidByte, 4, reader_uid);
Serial.println(reader_uid);
2025-11-08 12:17:49 +01:00
displaySuccess();
}
void send_uid(char reader_uid[]) {
2025-11-08 12:17:49 +01:00
WiFiManager wm;
bool res;
res = wm.autoConnect("KLUK"); // password protected ap
if(!res) {
Serial.println("Failed to connect");
displayError();
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
auto client = std::make_unique<BearSSL::WiFiClientSecure>();
client->setInsecure();
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
// configure traged server and url
https.begin(*client, "https://mood.robiweb.net/rfid_tags"); // HTTP
https.addHeader("Content-Type", "application/json");
// start connection and send HTTP header and body
int httpCode = https.POST("{\"identifier\":\"" + String(reader_uid) + "\"}");
Serial.print("httpCode: ");
Serial.println(httpCode);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_CREATED) {
2025-11-06 16:47:34 +01:00
} else {
2025-11-08 12:30:17 +01:00
Serial.println("Remote error while sending uid");
2025-11-06 16:47:34 +01:00
displayError();
2025-08-07 23:31:30 +02:00
}
} else {
2025-11-08 12:30:17 +01:00
Serial.print("Remote error while sending uid");
displayError();
2025-11-06 16:47:34 +01:00
}
https.end();
Serial.print("[HTTPS] ended...\n");
2025-08-07 23:31:30 +02:00
}
2025-10-27 17:26:53 +01:00
2025-11-08 10:49:17 +01:00
void byte_to_string(byte array[], unsigned int len, char buffer[]) {
2025-11-06 16:47:34 +01:00
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
2025-08-07 23:31:30 +02:00
}
2025-10-27 17:26:53 +01:00
void displayError() {
2025-11-08 12:02:28 +01:00
blink(6, 300);
}
void displaySuccess() {
blink(1, 1000);
}
void blink(unsigned int times, unsigned int duration) {
for (unsigned int i = 0; i < times; i++)
2025-08-07 23:31:30 +02:00
{
2025-09-27 11:45:54 +02:00
digitalWrite(LED_PIN, HIGH);
2025-11-08 12:02:28 +01:00
delay(duration);
2025-09-27 11:45:54 +02:00
digitalWrite(LED_PIN, LOW);
2025-11-08 12:02:28 +01:00
delay(duration);
2025-08-07 23:31:30 +02:00
}
}