Skip to content
momysnow edited this page Feb 20, 2024 · 4 revisions

Libraries

to install

included with ESP32 board support

Code

File: boot.ino

Constant Variables:

  • server_ip: IP address of the camera server.
  • server_port: Port number of the camera server.
  • ota_host_name: Hostname for OTA updates.
  • SERVOMIN: Minimum pulse length count for servo control.
  • SERVOMAX: Maximum pulse length count for servo control.
  • USMIN: Minimum pulse length in microseconds.
  • USMAX: Maximum pulse length in microseconds.
  • SERVO_FREQ: Frequency of PWM signals for servo control.
  • temperaturaCritica: Critical threshold temperature for temperature sensor task.

Global Variables:

  • preferences: Object for managing preferences using EEPROM.
  • address: Index in EEPROM for storing a value.
  • touchPadThreshold: Threshold value for touchpad sensitivity.
  • Task handles for various tasks in the project.

Functions:

  • setup(): Initializes components such as temperature sensor, camera, SD card, WiFi connection, and servo motor.
  • StreamVideoTask(): Streams video from the camera.
  • AnimationTask(): Handles animation sequences.
  • TouchTask(): Handles touchpad input.
  • temp_sensorTask(): Monitors temperature.
  • EmotionTask(): Manages emotional states.
  • loop(): Handles OTA updates.
  • configModeCallback(): Callback for WiFiManager when failing to connect to a network.

Additional Notes:

  • Utilizes libraries for MQTT communication (PubSubClient), WiFi management (WiFiManager), and EEPROM storage.
  • Includes UI interactions for displaying text on the screen.

File: Emotion.h

Defines a class Emotion and a struct Activity for managing emotional states and activities.

Class Emotion

Methods:

  • Emotion(): Constructor for initializing emotional parameters.
  • void begin(uint8_t happiness, uint8_t tiredness, uint8_t anger, uint8_t sadness, uint8_t fear): Initializes emotional parameters.
  • void addActivity(uint8_t emotion, int points, void (*function)()): Adds an activity associated with a specific emotion.
  • std::vector<uint8_t> calculatePriority(): Calculates and returns prioritized emotions based on current emotional states.

Struct Activity

  • uint8_t emotion: Emotion associated with the activity.
  • int points: Points associated with the activity.
  • void (*function)(): Function pointer associated with the activity.

File: Emotion.cpp

Implements methods of the Emotion class for managing emotional states and activities.

Method Implementations:

  • Emotion::Emotion(): Constructor implementation.
  • void Emotion::begin(uint8_t happiness, uint8_t tiredness, uint8_t anger, uint8_t sadness, uint8_t fear): Initializes emotional parameters.
  • std::vector<uint8_t> Emotion::calculatePriority(): Calculates prioritized emotions based on emotional states.

Additional Notes:

  • Provides structured management of emotional states and activities.
  • Allows dynamic adjustments based on emotional states.

File: WeatherApi.h

Defines a class Weather and a struct WeatherData for fetching weather data from an API.

Class Weather

Methods:

  • Weather(String apiKey): Constructor for initializing with an API key.
  • WeatherData getWeather(int units): Fetches weather data based on specified units.

Struct WeatherData

  • float temperature: Temperature data.
  • int units: Units of temperature (0 for standard, 1 for metric, 2 for imperial).
  • String weatherMain: Main weather condition.
  • String weatherIcon: Weather icon.

File: WeatherApi.cpp

Implements methods of the Weather class for fetching weather data from an API.

Method Implementations:

  • Weather::Weather(String apiKey): Constructor implementation.
  • String Weather::getCoordinates(): Fetches location coordinates.
  • WeatherData Weather::getWeather(int units): Fetches weather data based on location and specified units.

Additional Notes:

  • Provides a convenient interface for accessing weather data from an API.
  • Supports different units for temperature measurement.

File: MQTT_Handler.h

Defines a class MqttHandler for MQTT communication.

Class MqttHandler

Methods:

  • MqttHandler(): Constructor for initializing MQTT handler.
  • void begin(const char* mqtt_server): Initializes MQTT handler with specified MQTT server.
  • void loop(): Handles MQTT connection and message processing.
  • void publish(const char* topic, const char* payload): Publishes a message to the specified MQTT topic.
  • void setCallback(MQTT_CALLBACK_SIGNATURE): Sets the callback function for incoming MQTT messages.

Private Attributes:

  • WiFiManager wifiManager: Manages WiFi connections.
  • WiFiClient espClient: Manages MQTT server connection.
  • PubSubClient mqttClient: Facilitates MQTT communication.
  • SimpleTimer timer: Timer for internal use.
  • const char* mqtt_server: MQTT server address.
  • long lastReconnectAttempt: Timestamp of last reconnect attempt.
  • bool shouldSaveConfig: Flag for saving WiFi configuration.

Private Methods:

  • void setupWiFi(): Sets up WiFi connection.
  • void reconnect(): Reconnects to MQTT server.
  • void saveConfigCallback(): Callback for saving WiFi configuration.

File: MQTT_Handler.cpp

Implements methods of the MqttHandler class for MQTT communication.

Method Implementations:

  • MqttHandler::MqttHandler(): Constructor implementation.
  • void MqttHandler::begin(const char* mqtt_server): Initializes MQTT handler with specified MQTT server.
  • void MqttHandler::loop(): Handles MQTT connection and message processing.
  • void MqttHandler::publish(const char* topic, const char* payload): Publishes a message to the specified MQTT topic.
  • void MqttHandler::setCallback(MQTT_CALLBACK_SIGNATURE): Sets the callback function for incoming MQTT messages.
  • void MqttHandler::setupWiFi(): Sets up WiFi connection.
  • void MqttHandler::reconnect(): Reconnects to MQTT server.
  • void MqttHandler::saveConfigCallback(): Callback for saving WiFi configuration.

Additional Notes:

  • Provides an easy-to-use interface for integrating MQTT communication into ESP32 projects.
  • Handles MQTT connection and message processing efficiently.

File: Animation.h

Defines various animations for a graphical display using the TFT_eSPI library.

Functions:

  • idle_eyes(): Animation function for idle eyes.
  • close_eyes(): Animation function for closing eyes.
  • wink_eyes(): Animation function for winking eyes.
  • sleep_eyes(): Animation function for sleeping eyes.
  • high_temperature_anim(): Animation function for displaying high temperature.
  • alarm_anim(): Animation function for displaying an alarm.

Constants and Variables:

  • extern TFT_eSPI tft: External declaration of the TFT display object.
  • extern TFT_eSprite eyes: External declaration of the TFT sprite object for eyes animation.
  • extern const unsigned int sreenW: Constant for screen width.
  • extern const unsigned int sreenH: Constant for screen height.
  • `extern

int w_eyes`: Width of eyes.

  • extern int h_eyes: Height of eyes.
  • extern int close_h_eyes: Height of closed eyes.
  • extern int eyes_distance: Distance between eyes.
  • extern int x_eyeL: X-coordinate of the left eye.
  • extern int y_eyeL: Y-coordinate of the left eye.
  • extern int x_eyeR: X-coordinate of the right eye.
  • extern int y_eyeR: Y-coordinate of the right eye.
  • #define AA_FONT_SMALL NotoSansBold15: Definition for small font.
  • #define AA_FONT_LARGE NotoSansBold36: Definition for large font.

File: Animation.cpp

This file contains implementations of animation functions declared in Animation.h.

Function Implementations:

  • idle_eyes(): Implements animation for idle eyes.
  • close_eyes(): Implements animation for closing eyes.
  • wink_eyes(): Implements animation for winking eyes.
  • sleep_eyes(): Implements animation for sleeping eyes.
  • high_temperature_anim(): Implements animation for displaying high temperature.
  • alarm_anim(): Implements animation for displaying an alarm.

Additional Notes:

  • These functions manipulate the TFT sprite object eyes and the TFT display object tft to achieve visual effects.
  • Constants and variables declared in Animation.h are used to define positions and dimensions of graphical elements on the screen.

File: Microphone_h.h

Defines configurations and functions related to the microphone module.

Constants and Macros:

  • I2S_PORT: I2S port number.
  • I2S_SAMPLE_RATE: Sample rate for I2S.
  • I2S_SAMPLE_BITS: Sample bits for I2S.
  • I2S_READ_LEN: Length of data read from I2S.
  • RECORD_TIME: Recording time in seconds.
  • I2S_CHANNEL_NUM: Number of I2S channels.
  • FLASH_RECORD_SIZE: Size of flash record.
  • filename: Name of the recording file.
  • headerSize: Size of WAV header.

External Variables:

  • extern File file: External declaration of SPIFFS file object.
  • extern bool isWIFIConnected: External declaration of WiFi connection status.

Function Declarations:

  • SPIFFSInit(): Initializes SPIFFS and opens the recording file.
  • i2sInit(): Initializes I2S configuration.
  • i2s_adc_data_scale(): Scales ADC data.
  • i2s_adc(): Task function for reading ADC data.
  • example_disp_buf(): Displays buffer content (for debugging).
  • wavHeader(): Generates WAV header.
  • listSPIFFS(): Lists files in SPIFFS.
  • uploadFile(): Uploads recording file to server.

File: AudioRecorder.cpp

Implements functions declared in Microphone_h.h.

Function Implementations:

  • SPIFFSInit(): Initializes SPIFFS, opens recording file, and writes WAV header.
  • i2sInit(): Initializes I2S configuration.
  • i2s_adc_data_scale(): Scales ADC data for writing to file.
  • i2s_adc(): Task function for reading ADC data and writing to file.
  • example_disp_buf(): Displays buffer content for debugging.
  • wavHeader(): Generates WAV header.
  • listSPIFFS(): Lists files in SPIFFS.
  • uploadFile(): Uploads recording file to server using HTTP POST request.

Additional Notes:

  • Error handling is included for file operations and HTTP requests.
  • WAV header generation ensures proper formatting for WAV files.
  • uploadFile() sends recording file to server using Wit.ai API for speech recognition.

File: SD_READ_WRITE_H

Defines a class SDManager and declares functions for reading from and writing to an SD card using the SD_MMC library.

Constants and Macros:

  • SD_MMC_CMD, SD_MMC_CLK, SD_MMC_D0: Pins for SD_MMC communication.

Class SDManager:

  • initSD(): Initializes SD card and creates file if it doesn't exist.
  • listDir(): Lists contents of directory on SD card.
  • createDir(): Creates new directory on SD card.
  • removeDir(): Removes directory from SD card.
  • readFile(): Reads content of file from SD card.
  • writeFile(): Writes data to file on SD card.
  • appendFile(): Appends data to existing file on SD card.
  • renameFile(): Renames file on SD card.
  • deleteFile(): Deletes file from SD card.
  • testFileIO(): Performs file I/O operations for testing.
  • findSSIDInFile(): Searches for specific SSID in file on SD card.
  • existFile(): Checks if file exists on SD card.

File: sd_read_write.cpp

Implements functions declared in SD_READ_WRITE_H.

Static Member Initialization:

  • DEBUG_TAG: Debug tag for logging.

Function Implementations:

  • Each function defined in the header file is implemented here, handling various file operations such as reading, writing, listing directories, creating directories, removing directories, renaming files, appending data to files, and more.
  • Error handling and logging are included for feedback on success or failure of each operation.

Additional Notes:

  • SDManager class provides interface for interacting with SD card.
  • Error handling ensures robustness when accessing SD card.
  • Code is well-commented for easy understanding of functionality and usage.

File: CameraUploader.h

Defines a class CameraUploader for uploading images captured from a camera module to a server over Wi-Fi.

Dependencies:

  • Arduino.h: Arduino core library.
  • esp_camera.h: ESP32 camera library.
  • WiFiManager.h: Library for handling Wi-Fi connection.

Class CameraUploader:

  • Constructor: Initializes a CameraUploader object with server IP address and port.
    • Parameters:
      • serverIp: Server IP address.
      • serverPort: Server port number.
  • begin(): Initializes serial communication and connects to Wi-Fi using WiFiManager.
  • uploadImage(): Uploads image to server if client is connected.
  • Private Members:
    • serverIp: Server IP address.
    • serverPort: Server port number.
    • client: WiFiClient object for handling Wi-Fi client connection.
  • Private Methods:
    • cameraSetup(): Sets up camera module with specified configurations.

Implementation Notes:

  • CameraUploader class manages process of capturing images from camera module and uploading to server over Wi-Fi.
  • begin() method initiates Wi-Fi connection using WiFiManager, allowing device to connect to Wi-Fi network.
  • uploadImage() method attempts to upload image to server if client connection is available.
  • cameraSetup() method configures various parameters of camera module such as brightness, contrast, saturation, special effects, white balance, exposure control, etc.
  • Error handling is included for cases where HTTP request fails, returning appropriate error message.

File: ChatGPT_h.h

Defines a class ChatGPT for interacting with OpenAI GPT (Generative Pre-trained Transformer) model for generating text-based responses.

Dependencies:

  • Arduino.h: Arduino core library.
  • WiFiManager.h: Library for handling Wi-Fi connection.
  • HTTPClient.h: Library for making HTTP requests.

Class ChatGPT:

  • Constructor: Initializes a ChatGPT object.
  • begin(): Begins Wi-Fi connection using WiFiManager.
  • setApiKey(String apiKey): Sets API key required for accessing OpenAI API.
  • setModel(String modelName): Sets model to be used for generating responses.
  • generateResponse(String input): Generates response

based on input text.

  • Private Members:
    • apiKey: API key for accessing OpenAI API.
    • selectedModel: Selected model for generating responses.
  • Private Methods:
    • sendRequest(String input): Sends HTTP request to OpenAI API with input text and retrieves response.

Implementation Notes:

  • ChatGPT class provides interface for interacting with OpenAI GPT model to generate text responses based on user input.
  • begin() method initiates Wi-Fi connection using WiFiManager.
  • setApiKey() method allows user to set API key required for accessing OpenAI API.
  • setModel() method allows user to specify model to be used for generating responses (e.g., MODEL_DAVINCI or MODEL_CURIE).
  • generateResponse() method takes user input as prompt and sends HTTP request to OpenAI API to generate response based on selected model.
  • Error handling is included to handle cases where HTTP request fails, returning appropriate error message.
  • Users can use provided example code to integrate ChatGPT class into their Arduino projects and interact with GPT model to generate text responses.

Creating Plugins

Overview

Plugin Structure

Integration Guide

Tools

ImageConverter565

ImageConverter565 is a tool used for converting images to a format suitable for display on a screen.

Usage

  1. Download ImageConverter565.
  2. Open the tool.
  3. Select the image you want to convert.
  4. Choose "Save As" and select ".c" as the file format.
  5. Set the target board to "chipKIT / Arduino Due".
  6. Adjust the name and resize the image as needed.
  7. Save the image.
  8. Change the file extension to ".h" to use it in your project.

Convert Font vlw File to Byte Array

To convert a font vlw file to a byte array, follow these steps:

  1. Use the online tool File to Hex Converter.

  2. Paste the contents of the font vlw file into the converter.

  3. Copy the generated byte array.

  4. In your Arduino sketch, create a new tab and paste the byte array.

  5. Add two lines at the beginning of the byte array with a unique font name:

    const uint8_t fontName[] PROGMEM = {
  6. At the end of the byte array, add:

    };
  7. Include the tab in your main sketch using #include "FileName.h".