Hello,
I have written a sketch which is designed to read an RFID card ID and send it to a google sheet for logging, over wifi. I'm using an arduino wifi rev2 with the WiFiNINA library. However, I am struggling to get the HTTPS request to go through. I know it should be an SSL connection to connect to google apps, and in testing, when the 'client.println("GET /macros/s/"+webhook+"/exec?card_no="+ID+" HTTP/1.1");' block is placed in the setup() function, it works fine. However I need this block to run in the loop() section and when I move it here (or, indeed, to a separate function), the connection to the server fails.
Does anyone have any ideas for things to try?
Any help would be very much appreciated!!
Thanks!
Sketch:
////////////////////////////////////
///////// INCLUDE LIBRARIES ////////
////////////////////////////////////
///////Include WiFi Library
#include <SPI.h>
#include <MFRC522.h>
#include <WiFiNINA.h>
#include <WiFiSSLClient.h>
///////User Defined Variables
// Network Parameters
#define ssid "<NetworkID>"
#define pass "<Password>"
#define staticip "192.168.1.99" //if not required, uncomment line below)
//#define staticip ""
// Google Sheet parameters
char server[] = "script.google.com";
String webhook = "<GoogleSheetLinkHere>";
////////////////////////////////////
// (DO NOT EDIT BELOW THIS LINE!) //
////////////////////////////////////
// Set network parameters
int wifiStatus = WL_IDLE_STATUS;
int port = 443;
WiFiSSLClient client; // Initialise WiFi client
// Set cardReader parameters
#define RST_PIN 9
#define SS_PIN 10
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
byte nuidPICC[4]; // Init array that will store new NUID
// Set output pins
const int wifiGo = 13; // LED 13 (Yellow WiFi Status)
const int wifiOn = 12; // LED 12 (Green WiFi Status)
const int readOk = 11; // LED 11 (Card Reader Light)
const int buzzer = 8; // Buzzer
//Status params
int wifiTimeout = 5;
void setup() {
// Initialise all o/p pins
pinMode(wifiGo, OUTPUT); // LED 13 = Yellow WiFi Status
pinMode(wifiOn, OUTPUT); // LED 12 = Green WiFi Status
pinMode(readOk, OUTPUT); // LED 11 = Card Reader Light
pinMode(buzzer, OUTPUT); // Buzzer Pin
Serial.begin(9600); // Initialize serial communication
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
initialise(); // Test all lights + buzzer
// WiFi setup checks
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true); // don't continue
}
if (staticip != "") { // If you set a static IP, use it
IPAddress myIP;
myIP.fromString(staticip);
WiFi.config(myIP);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
while (wifiStatus != WL_CONNECTED) { // Try connecting to Wifi network:
Serial.println("Wifi Status Light: Blink Yellow");
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
wifiStatus = WiFi.begin(ssid, pass);
blink(wifiTimeout); // Indicate wait (with status LED) for wifi connection
}
printWifiStatus(); // you're connected now, so print out the status
rfid.PCD_Init(); // Initialise MFRC522
for (byte i = 0; i < 6; i++) { // NOT SURE - LOOK THIS UP!!
key.keyByte[i] = 0xFF;
}
}
// Declare a reset function (@ address 0) to reset device if wifi connection is lost
void(* resetFunc) (void) = 0;
void loop() {
// If WiFi drops out - reset device to restart connection
if (wifiStatus != WL_CONNECTED) {
Serial.println("WiFi connection lost!");
Serial.println("Restarting logger...");
resetFunc();
}
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
//Identify and print card ID
String ID = hexToString(rfid.uid.uidByte, rfid.uid.size);
Serial.println("Card ID: " + ID);
cardBeep();
client.connect(server, port);
delay(500);
while(client.connected()) {
Serial.println("connected to server");
// Make a HTTP request:
//client.println("GET /macros/s/AKfycbyHBXlsxBw4fXBuWD1N9LowE_yDaxpIKkZP9UwVOZywSFMtPhKaWuMPsbTUiRzbuEO0/exec?card_no=15 HTTP/1.1");
client.println("GET /macros/s/"+webhook+"/exec?card_no="+ID+" HTTP/1.1");
client.println("Host: script.google.com");
client.println("Connection: close");
client.println();
delay(20000);
}
client.stop();
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
delay(4500);
}
////////////////////////////////////
/////// ADDITIONAL FUNCTIONS ///////
////////////////////////////////////
// Initialise beep & flash
void initialise(){
delay(1000);
digitalWrite(wifiGo, HIGH);
digitalWrite(wifiOn, HIGH);
digitalWrite(readOk, HIGH);
tone(buzzer, 800);
delay(60);
noTone(buzzer);
digitalWrite(wifiGo, LOW);
digitalWrite(wifiOn, LOW);
digitalWrite(readOk, LOW);
}
// Print wifi information
void printWifiStatus() {
// Print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println("Wifi Status Light: Solid Green\n");
digitalWrite(wifiOn, HIGH);
}
// Create a string object from a hex code
String hexToString(byte *buffer, byte bufferSize) {
String hexString = "";
for (byte i = 0; i < bufferSize; i++) {
if(buffer[i] < 0x10) {
hexString += "0";
}
hexString += String(buffer[i], HEX);
}
return hexString;
}
// Flash the card LED and give a confirmation beep
void cardBeep(){
digitalWrite(readOk, HIGH);
tone(buzzer, 800);
delay(60);
tone(buzzer, 1200);
delay(60);
digitalWrite(readOk, LOW);
noTone(buzzer);
}
// Custom blinker for WiFi LED based on input int 'count'
void blink(int count) {
while ( count-- ) {
digitalWrite(wifiGo, HIGH);
delay(250);
digitalWrite(wifiGo, LOW);
delay(250);
}
}
// Generate a negative piezo-beep and flash an LED for 1 second
void ohOhBuzz(){
digitalWrite(readOk, LOW);
tone(buzzer, 700);
delay(160);
tone(buzzer, 666);
delay(200);
tone(buzzer, 632);
delay(400);
noTone(buzzer);
}