Help with sketch - can't find library suggested

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
MultiTech_Visions
 
Posts: 7
Joined: Tue Oct 26, 2021 1:49 pm

Help with sketch - can't find library suggested

Post by MultiTech_Visions »

The following sketch was generated using OpenAI's Playground.
https://beta.openai.com/playground/p/HK ... avinci-003

When I try and verify the sketch, things get caught up on the "<Adafruit_QTpy.h>" portion.
I've tried searching high and low for where to get this library... can someone point me in the right direction?
- Or maybe the sketch needs to be adjusted?

---------------------------------------------------------------------------------------------------------------------

Code: Select all

/* 
This is an Arduino IDE sketch, which will run on the "Adafruit QT Py ESP32-C3 WiFi Dev Board"
Input is collected from a "NeoKey 1x4 QT I2C - Four Mechanical Key Switches with NeoPixels - STEMMA QT / Qwiic" connected to the  "Adafruit QT Py ESP32-C3 WiFi Dev Board" through a STEMMA QT cable.

General Idea:
  - Push a button on the NeoKey, and the QT Py connects to the internet and inserts a new record at the bottom of a Google Sheet - recording the key press.

Configurable Parameters Include:
1)  Google Sheets API key to use
2)  Google Sheet URL to connect to
3)  Google Sheet tab-name to insert a row into
4)  The individual values to use for each of the 4 NeoKeys
5)  The WiFi SSID to connect to
6)  The Password for the WiFi

Details that need to be recorded into the Google Sheet Row:
1)  Timestamp of the event
2)  The value from the key pressed
3)  a GUID

Explanation of the main loop:
Start watching the NeoKeys for changes, querying them only once a second
If a key is pressed, turn the key's LED Yellow (as long as it's held - up to 2 seconds); if they key is held for less than 2 seconds and released, turn the LED off
If the key is held for longer than 2 seconds: begin a record creation process
First begin connecting to wifi; while attempting to connect, flash the led of the key that was pressed blue every half second.  Once successfully connected to wifi, stop blinking the LED and turn it solid blue
Next insert the google sheet row at the bottom of the sheet specified using the Google Sheets API
If the record is successfully created, turn the LED green for 5 seconds and then turn it off.
If the record is NOT created, turn the LED red for 5 seconds and then turn it off.
*/

// Include the necessary libraries
#include <Arduino.h>
#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_QTpy.h>
#include <Adafruit_NeoKey.h>

// Create a QTpy object
Adafruit_QTpy qtpy;

// Create a NeoKey object
Adafruit_NeoKey neokey;

// Configure the WiFi connection
const char* ssid = "MY_SSID";
const char* password = "MY_PASSWORD";

// Configure the Google Sheets API connection
const char* apiKey = "MY_API_KEY";
const char* sheetUrl = "MY_SHEET_URL";
const char* sheetName = "MY_SHEET_NAME";

// Configure the NeoKeys
int key1Value = 1;
int key2Value = 2;
int key3Value = 3;
int key4Value = 4;

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

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Initialize the QTpy and NeoKey
  if(!qtpy.begin(Wire)){
    Serial.println("Failed to initialize QTpy!");
    while(1);
  }
  if(!neokey.begin(&qtpy)){
    Serial.println("Failed to initialize NeoKey!");
    while(1);
  }
  Serial.println("QTpy and NeoKey initialized");
}

void loop() {
  // Check for key presses
  int keyPressed = neokey.readSwitch();
  if (keyPressed > 0) {
    Serial.println("Key pressed: " + String(keyPressed));
    int keyValue = 0;
    switch (keyPressed) {
      case 1:
        keyValue = key1Value;
        break;
      case 2:
        keyValue = key2Value;
        break;
      case 3:
        keyValue = key3Value;
        break;
      case 4:
        keyValue = key4Value;
        break;
      default:
        break;
    }
    if (keyValue > 0) {
      // Turn on the LED of the key that was pressed
      neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(255, 255, 0));
      neokey.show();
      delay(2000);
      // Connect to WiFi
      Serial.println("Connecting to WiFi...");
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(0, 0, 255));
        neokey.show();
        delay(500);
        neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(0, 0, 0));
        neokey.show();
        delay(500);
      }
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      // Turn on the LED of the key that was pressed
      neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(0, 0, 255));
      neokey.show();
      delay(500);
      // Create the HTTP request
      HTTPClient http;
      http.begin(sheetUrl);
      http.addHeader("Content-Type", "application/json");
      http.addHeader("Authorization", "Bearer " + String(apiKey));
      int httpCode = http.POST("{\"values\": [[\"" + String(millis()) + "\", \"" + String(keyValue) + "\", \"GUID\"]]}");
      http.end();
      Serial.println("Request sent, response code: " + String(httpCode));
      // Turn on the LED of the key that was pressed
      if (httpCode == 200) {
        neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(0, 255, 0));
        neokey.show();
        delay(5000);
      } else {
        neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(255, 0, 0));
        neokey.show();
        delay(5000);
      }
      // Turn off the LED of the key that was pressed
      neokey.setPixelColor(keyPressed - 1, Adafruit_NeoPixel::Color(0, 0, 0));
      neokey.show();
    }
  }
  delay(1000);
}
Last edited by dastels on Sun Dec 04, 2022 10:16 pm, edited 1 time in total.
Reason: Add code tags

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Help with sketch - can't find library suggested

Post by adafruit_support_carter »

The code was generated by an AI?

Non-AI generated usage is covered here:
https://learn.adafruit.com/neokey-1x4-qt-i2c/arduino

User avatar
MultiTech_Visions
 
Posts: 7
Joined: Tue Oct 26, 2021 1:49 pm

Re: Help with sketch - can't find library suggested

Post by MultiTech_Visions »

It was, as I have very little understanding of the language.

It's the QTpy libraries that the IDE can't find. I imagine it's either finding the right name of the package that contains what I'm looking for (so I can install it and everything works); or maybe the library doesn't exist, and it's something the AI is imagining that would make sense as an answer in this situation?

I'll keep at it; thanks for your time!

User avatar
AnneBarela
Learn User Page
 
Posts: 757
Joined: Sat Mar 24, 2012 8:56 pm

Re: Help with sketch - can't find library suggested

Post by AnneBarela »

There is no library by that name that I know of. Try without it.

Also please ensure you set up the Arduino IDE to use the board you are using (Adafruit QT Py, I take it). For non-standard libraries, you'll need to use the Arduino Library Manager to pull in libraries like the ones beginning with Adafruit_

When the prompt comes up to include dependent libraries, click yes.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”