The main objective of this program is to send the value of the variable called 'Count' from ESP8266 to google sheets. When I tried to test this, I was only able to get the value of Count from the OLED Display and neither it was displayed on the serial monitor nor it was sent to the google sheets. I have checked my code multiple times and I think There isn't any sort of syntax error. I would kindly request you to point out any logical or any other programming errors that I have made, so that I can learn from my mistakes. NOTE : I have used an Arduino mega 2560 with built in wifi module wherein I have configured the board to only act as an ESP8266.
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char* ssid = "Galaxy M3xxxx";
const char* password = "nxen015xxxxx";
// pins assignment
constexpr uint8_t RST_PIN = 8; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 9; // Configurable, see typical pin layout above
#define screen_width 128
#define screen_height 64
Adafruit_SSD1306 display(-1);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int j=0;
const char* GScriptId = "AKfycbw1JtQ4bKM3UgnwLamcFwrKqFgnnw1CLsRO2c7lsQDFMhHgqd2iHCA8qEUGA5ebynoWIA";
const int dataPostDelay = 3000; // 3 seconds
const char* host = "script.google.com";
const char* googleRedirHost = "script.googleusercontent.com";
const int httpsPort = 443;
HTTPSRedirect client(httpsPort);
// Prepare the url (without the varying data)
String url = String("/macros/s/") + GScriptId + "/exec?";
const char* fingerprint = "F0 5C 74 77 3F 6B 25 D7 3B 66 4D 43 2F 7E BC 5B E9 28 86 AD";
void setup() {
Serial.begin(115200);
Serial.println("Connecting to wifi: ");
Serial.println(ssid);
Serial.flush();
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
display.begin(SSD1306_SWITCHCAPVCC,0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Count");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" IP address: ");
Serial.println(WiFi.localIP());
Serial.print(String("Connecting to "));
Serial.println(host);
bool flag = false;
for (int i=0; i<5; i++){
int retval = client.connect(host, httpsPort);
if (retval == 1) {
flag = true;
break;
}
else
Serial.println("Connection failed. Retrying…");
}
// Connection Status, 1 = Connected, 0 is not.
Serial.println("Connection Status: " + String(client.connected()));
//Serial.flush();
if (!flag){
Serial.print("Could not connect to server: ");
Serial.println(host);
Serial.println("Exiting…");
Serial.flush();
return;
}
// Data will still be pushed even certification don’t match.
/* if (client.verify(fingerprint, host)) {
Serial.println("Certificate match.");
} else {
Serial.println("Certificate mis-match");
}*/
}
// This is the main method where data gets pushed to the Google sheet
void postData(String tag, float value){
if (!client.connected()){
Serial.println("Connecting to client again…");
client.connect(host, httpsPort);
}
String urlFinal = url + "tag=" + tag + "&value=" + String(value);
client.printRedir(urlFinal, host, googleRedirHost);
}
// Continue pushing data at a given interval
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "01 07 16 DB") //change here the UID of the card/cards that you want to give access
{
Serial.println("accepted");
Serial.println();
j++;
Serial.println(j);
tcount();
display.display();
delay(dataPostDelay);
}
else {
Serial.println(" Access denied");
delay(30);
}
}
void tcount(void)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Count");
display.setCursor(40,40);
display.print(j);
display.display();
int data = j;
postData("Count",data);
Serial.println("Data has been posted !");
}
The following code from the google sheets app script.
function doGet(e){
Logger.log("--- doGet ---");
var tag = "",
value = "";
try {
// this helps during debuggin
if (e == null){e={}; e.parameters = {tag:"test",value:"-1"};}
tag = e.parameters.tag;
value = e.parameters.value;
// save the data to spreadsheet
save_data(tag, value);
return ContentService.createTextOutput("Wrote:\n tag: " + tag + "\n value: " + value);
} catch(error) {
Logger.log(error);
return ContentService.createTextOutput("oops...." + error.message
+ "\n" + new Date()
+ "\ntag: " + tag +
+ "\nvalue: " + value);
}
}
// Method to save given data to a sheet
function save_data(tag, value){
Logger.log("--- save_data ---");
try {
var dateTime = new Date();
// Paste the URL of the Google Sheets starting from https thru /edit
// For e.g.: https://docs.google.com/..../edit
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1HDVGsOEqkz_4mSeTHYLJxWL99EomohwIcOP8CEdfO7g/edit");
var summarySheet = ss.getSheetByName("Summary");
var dataLoggerSheet = ss.getSheetByName("DataLogger");
// Get last edited row from DataLogger sheet
var row = dataLoggerSheet.getLastRow() + 1;
// Start Populating the data
dataLoggerSheet.getRange("A" + row).setValue(row -1); // ID
dataLoggerSheet.getRange("B" + row).setValue(dateTime); // dateTime
dataLoggerSheet.getRange("C" + row).setValue(tag); // tag
dataLoggerSheet.getRange("D" + row).setValue(value); // value
// Update summary sheet
summarySheet.getRange("B1").setValue(dateTime); // Last modified date
// summarySheet.getRange("B2").setValue(row - 1); // Count
}
catch(error) {
Logger.log(JSON.stringify(error));
}
Logger.log("--- save_data end---");
}