Need to store the keypad inputs as one input

Iam triying to make online attendance using Arduino.I need to use a number like 150 from the input using the keypad.

<if (input == '11' ) {
Serial.print("!!--");
Serial.println(key);
NAME = "Dog";
ID = "11";
makeIFTTTRequest();
Serial.print("Attendace Marked for "); Serial.println(NAME);
input = "";
} />

As in above i need to take many digits as one single number like 1130.
I need the help with the code

See Keypad data entry. A beginners guide

1 Like
  if (key >= '0' && key <= '9') 
  {
    Serial.print(key);
    input *= 10;
    input += key - '0';
  }

  if (key == '#') // 'enter'
  {
  switch (input)
    {
  case 11:
    Serial.print("!!--");
    NAME = "Dog";
    ID = "11";
    makeIFTTTRequest();
    Serial.print("!!--Attendace Marked for "); 
    Serial.println(NAME);
    input = 0;
    break;
    }
}
1 Like

I came with an error

Compilation error: no match for 'operator*=' (operand types are 'String' and 'int')

This is my whole code can you pls edit the errors

#include <WiFi.h>
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {13,12,14,27}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {26,25,33,32}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const char* NAME;
const char* ID;

String Event_Name = "Attendance";

String Key = "nlGjtBEifiySWwQmTgZAipFdSmCgQLXOa4cMcAnin05";

// Replace with your unique IFTTT URL resource
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Replace with your SSID and Password
const char* ssid     = "Dineth";
const char* password = "Dineth2008";
 
String input;

void setup()
{
  Serial.begin(9600);
  input.reserve(1000);

  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  delay(100);

  // set the data rate for the sensor serial port
  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);

  int timeout = 10 * 4; // 10 seconds
  while (WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}
void loop()         
{
  char key = keypad.getKey();
if (key){
  if (key >= '0' && key <= '9') 
  {
    Serial.print(key);
    input *= 10;
    input += key - '0';
  }

  if (key == '#') // 'enter'
  {
  switch (input)
    {
  case 11:
    Serial.print("!!--");
    NAME = "Dog";
    ID = "11";
    makeIFTTTRequest();
    Serial.print("!!--Attendace Marked for "); 
    Serial.println(NAME);
    input = 0;
    break;
    }
}
}
}

void makeIFTTTRequest() {
  Serial.print("Connecting to ");
  Serial.print(server);

  WiFiClient client;
  int retries = 5;
  while (!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if (!!!client.connected()) {
    Serial.println("Failed to connect...");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);

  // Temperature in Celsius
  String jsonObject = String("{\"value1\":\"") + NAME + "\",\"value2\":\"" + ID+ "\"}";

  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);

  int timeout = 5 * 10; // 5 seconds
  while (!!!client.available() && (timeout-- > 0)) {
    delay(100);
  }
  if (!!!client.available()) {
    Serial.println("No response...");
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}

#include <WiFi.h>
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 25, 33, 32}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const char* NAME;
const char* ID;

String Event_Name = "Attendance";

String Key = "nlGjtBEifiySWwQmTgZAipFdSmCgQLXOa4cMcAnin05";

// Replace with your unique IFTTT URL resource
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Replace with your SSID and Password
const char* ssid     = "Dineth";
const char* password = "Dineth2008";

int input;

void setup()
{
  Serial.begin(9600);
  // input.reserve(1000);

  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  delay(100);

  // set the data rate for the sensor serial port
  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);

  int timeout = 10 * 4; // 10 seconds
  while (WiFi.status() != WL_CONNECTED  && (timeout-- > 0))
  {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}
void loop()
{
  char key = keypad.getKey();
  if (key)
  {
    if (key >= '0' && key <= '9')
    {
      Serial.print(key);
      input *= 10;
      input += key - '0';
    }

    if (key == '#') // 'enter'
    {
      switch (input)
      {
        case 11:
          Serial.print("!!--");
          NAME = "Dog";
          ID = "11";
          makeIFTTTRequest();
          Serial.print("!!--Attendace Marked for ");
          Serial.println(NAME);
          input = 0;
          break;
      }
    }
  }
}

void makeIFTTTRequest()
{
  Serial.print("Connecting to ");
  Serial.print(server);

  WiFiClient client;
  int retries = 5;
  while (!!!client.connect(server, 80) && (retries-- > 0))
  {
    Serial.print(".");
  }
  Serial.println();
  if (!!!client.connected())
  {
    Serial.println("Failed to connect...");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);

  // Temperature in Celsius
  String jsonObject = String("{\"value1\":\"") + NAME + "\",\"value2\":\"" + ID + "\"}";

  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);

  int timeout = 5 * 10; // 5 seconds
  while (!!!client.available() && (timeout-- > 0))
  {
    delay(100);
  }
  if (!!!client.available())
  {
    Serial.println("No response...");
  }
  while (client.available())
  {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}
1 Like

thanks it worked.But how to add backspace using the * key

MY experience was that it is easier to add a "cancel" key and just start over.

1 Like

Did you mean if I pressed the * key, set input to 0?

Yes.

I did it thanks

Any way to enter the data quickly?Because I need to enter 100++ data.

case 11:
          SendData();
          NAME = "Parrot";
          ID = "11";
          break;

         case 12:
          SendData();
          NAME = "bla";
          ID = "12";
          break;

If you mean that you need to have 100 or so cases then start by copy/pasting a single case as many times as you need to then edit what you have pasted

As each case does basically the same thing you could achieve the same result by putting the NAMEs in an array and using the case value to access the NAME entry. That way there would be no need for switch/case at all

Either way, you will need to be careful not to run out of memory. Which Arduino are you using ?

1 Like

Im using esp32

Is your main problem that you have over 100 NAME values ? If so, don't use over 100 cases in switch. Instead build an array of the NAMEs then use the value of the case variable to access the associated NAME and to derive the value of the ID. Does the ID really need to be a string or could it just use the case variable value ? What is us used for ?

The fact that you call SendDate() before setting any variables also looks wrong but without seeing a complete sketch it is impossible to say

Where is the data now? Are the ID's already assigned or do you get to make those up?

This is my code. What are the edits I should make to make it easy?

#include <WiFi.h>
#include <Keypad.h>

const byte ROWS = 4;  //four rows
const byte COLS = 4;  //three columns
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 13, 12, 14, 27 };  //connect to the row pinouts of the keypad
byte colPins[COLS] = { 26, 25, 33, 32 };  //connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

const char* NAME;
const char* ID;

String Event_Name = "Attendance";

String Key = "nlGjtBEifiySWwQmTgZAipFdSmCgQLXOa4cMcAnin05";

// Replace with your unique IFTTT URL resource
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Replace with your SSID and Password
const char* ssid = "Dineth";
const char* password = "Dineth2008";

int input;

void setup() {
  Serial.begin(9600);

  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);

  int timeout = 10 * 4;  // 10 seconds
  while (WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    if (key >= '0' && key <= '9') {
      Serial.print(key);
      input *= 10;
      input += key - '0';
    }

    if (key == '#')  // 'enter'
    {
      switch (input) {
        case 11:
          SendData();
          NAME = "Parrot";
          ID = "11";
          break;

        case 12:
          SendData();
          NAME = "bla";
          ID = "12";
          break;

        default:
          input = 0;
          Serial.println("Wrong Input");
      }
    }
    if (key == '*') {
      input = 0;
      Serial.println("Input Cleared");
    }
  }
}

// Make an HTTP request to the IFTTT web service
void makeIFTTTRequest() {
  Serial.print("Connecting to ");
  Serial.print(server);

  WiFiClient client;
  int retries = 5;
  while (!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if (!!!client.connected()) {
    Serial.println("Failed to connect...");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);

  // Temperature in Celsius
  String jsonObject = String("{\"value1\":\"") + NAME + "\",\"value2\":\"" + ID + "\"}";

  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);

  int timeout = 5 * 10;  // 5 seconds
  while (!!!client.available() && (timeout-- > 0)) {
    delay(100);
  }
  if (!!!client.available()) {
    Serial.println("No response...");
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}

void SendData() {
  Serial.print("!!--");
  makeIFTTTRequest();
  Serial.print("!!--Attendace Marked for ");
  Serial.println(NAME);
  input = 0;
}

Put the names and ID's into either two parallel lists or one list of structures.

struct {
  const char * name;
  const int id;
} Users = {
{"Parrot", 11},
{"bla", 12},
};
size_t UserCount = sizeof Users / sizeof Users[0];

Then you can check the entered ID against the list:

    if (key == '#')  // 'enter'
    {
      for (size_t i=0; i < UserCount; i++)
      {
        if (input == Users[i].id)
        {
          NAME = Users[i].name;
          ID = Users[i].id;
          SendData();
          input = 0;  // Prepare for next input
          return;
        }
      }
      // User ID not found!
      input = 0;
    }

So I only need to add names and IDs to this array. Then the code will run as usual.
Is that all?

I think so. Put in a few names and IDs and get that working. Then you can add the rest.

Note: I should have written:

} Users[] = {
{"Parrot", 11},
{"bla", 12},
};