the-cup/the-cup.ino

144 lines
4.3 KiB
Arduino
Raw Normal View History

2025-08-10 17:53:45 +02:00
/* Pin layout used:
* -----------------------------------------------------------------------------------------
* 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
#include <ESP8266WiFi.h>
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
#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>
2025-09-27 11:45:54 +02:00
#define LED_PIN 16
2025-08-07 23:31:30 +02:00
MFRC522DriverPinSimple ss_1_pin(4); // Configurable, take an unused pin, only HIGH/LOW required, must be different to SS 2.
MFRC522DriverSPI driver_1{ss_1_pin};
MFRC522 reader{driver_1}; // Create MFRC522 instance.
void setup() {
// WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
// put your setup code here, to run once:
Serial.begin(115200);
2025-09-27 11:45:54 +02:00
pinMode(LED_PIN, OUTPUT);
2025-10-27 17:26:53 +01:00
displaySuccess();
2025-08-07 23:31:30 +02:00
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
// wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
2025-09-26 11:30:37 +02:00
res = wm.autoConnect("KLUK"); // password protected ap
2025-08-07 23:31:30 +02:00
if(!res) {
Serial.println("Failed to connect");
2025-08-10 18:15:41 +02:00
displayError();
2025-08-07 23:31:30 +02:00
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
2025-10-27 17:26:53 +01:00
displaySuccess();
}
2025-08-07 23:31:30 +02:00
reader.PCD_Init(); // Init each MFRC522 card.
}
void loop() {
2025-09-26 11:31:14 +02:00
auto client = std::make_unique<BearSSL::WiFiClientSecure>();
2025-08-07 23:31:30 +02:00
if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) {
// Halt PICC.
reader.PICC_HaltA();
// Stop encryption on PCD.
reader.PCD_StopCrypto1();
2025-09-26 11:31:14 +02:00
client->setInsecure();
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
2025-08-07 23:31:30 +02:00
// configure traged server and url
2025-09-26 11:31:14 +02:00
https.begin(*client, "https://mood.robiweb.net/rfid_tags"); // HTTP
https.addHeader("Content-Type", "application/json");
2025-08-07 23:31:30 +02:00
char str[32] = "";
array_to_string(reader.uid.uidByte, 4, str); //Insert (byte array, length, char array for output)
Serial.println(str); //Print the output uid string
// start connection and send HTTP header and body
2025-09-26 11:31:14 +02:00
int httpCode = https.POST("{\"identifier\":\"" + String(str) + "\"}");
Serial.print("httpCode: ");
Serial.print(httpCode);
2025-08-07 23:31:30 +02:00
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// file found at server
// if (httpCode == HTTP_CODE_OK) {
if (httpCode == HTTP_CODE_CREATED) {
displaySuccess();
2025-09-27 11:45:54 +02:00
} else {
2025-08-07 23:31:30 +02:00
displayError();
2025-09-27 11:45:54 +02:00
}
2025-08-07 23:31:30 +02:00
}
2025-09-26 11:31:14 +02:00
https.end();
2025-10-27 17:26:53 +01:00
Serial.print("[HTTPS] ended...\n");
2025-08-07 23:31:30 +02:00
}
}
2025-10-27 17:26:53 +01:00
void array_to_string(byte array[], unsigned int len, char buffer[]) {
2025-08-07 23:31:30 +02: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-10-27 17:26:53 +01:00
void displayError() {
2025-08-07 23:31:30 +02:00
for (unsigned int i = 0; i < 6; i++)
{
2025-09-27 11:45:54 +02:00
digitalWrite(LED_PIN, HIGH);
2025-08-07 23:31:30 +02:00
delay(600);
2025-09-27 11:45:54 +02:00
digitalWrite(LED_PIN, LOW);
2025-08-07 23:31:30 +02:00
delay(300);
}
}
2025-10-27 17:26:53 +01:00
void displaySuccess() {
2025-09-27 11:45:54 +02:00
digitalWrite(LED_PIN, HIGH);
2025-08-07 23:31:30 +02:00
delay(1000);
2025-09-27 11:45:54 +02:00
digitalWrite(LED_PIN, LOW);
2025-08-07 23:31:30 +02:00
delay(300);
}