CCCPaste

GSM Interface rev_69

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: GSM Interface
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-08-03 16:49:11

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Use TinyGSM library to initialize modem with APN */
	/* "airtelgprs.com", establish GSM connection, enable */
	/* GNSS module for location data capture, and display */
	/* all activity logs on Serial Monitor. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* add #define TINY_GSM_MODEM_SIM7600 */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include   // Cohere Client library for API interaction
#include   // Adafruit SSD1306 OLED display library
#include   // U8g2 library for Adafruit GFX support
#include   // TinyGSM library for GSM communication

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF Hardware Serial *****/
#define modem Serial2  // TX_PIN: D17, RX_PIN: D16

/***** DEFINITION OF I2C PINS *****/
const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21; // I2C SDA pin
const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22; // I2C SCL pin
const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C; // I2C Slave address

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
WiFiClientSecure client; // Secure WiFi client for HTTPS connections
CohereClient cohereClient(&client, "YOUR_API_KEY"); // Initialize CohereClient with the secure client and API key

// Initialize the display object with I2C address and reset pin
#define OLED_RESET -1  // No reset pin used for I2C
Adafruit_SSD1306 display(OLED_RESET); // Create display object

// Initialize U8G2_FOR_ADAFRUIT_GFX object
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create U8G2 object

// Initialize TinyGSM modem
#define TINY_GSM_MODEM_SIM7600 // Define the modem type
TinyGsmClient gsmClient(modem); // Create GSM client
TinyGsm modem(gsmClient); // Create modem instance

void setup(void)
{
    // Initialize the modem serial communication
    modem.begin(115200);
    Serial.begin(115200); // Start serial communication for debugging

    // Initialize the display
    display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
    display.clearDisplay(); // Clear the display buffer

    // Connect U8G2 to the display
    u8g2_for_adafruit_gfx.begin(display); // Connect u8g2 procedures to Adafruit GFX

    // Connect to GSM network
    connectToGSM(); // Call function to connect to GSM
}

void loop(void)
{
    // Example usage of the CohereClient
    int max_tokens = 200; // Maximum tokens for the API call
    String prompt = "Once upon a time in a magical land called"; // Prompt for the API call

    // Make an API call and get the response
    String response = cohereClient.makeAPICall(max_tokens, prompt);
    
    // Print the text output from the response
    String output = cohereClient.text_output(response);
    Serial.println(output);
    delay(5000); // Delay for readability

    Serial.println(""); // Print a new line for separation

    // Print the full response for debugging
    String fullResponse = cohereClient.full_response(response);
    Serial.println(fullResponse);
    delay(5000); // Delay for readability
}

/****** FUNCTION TO CONNECT TO GSM *****/
void connectToGSM()
{
    // Set modem parameters
    modem.restart(); // Restart the modem
    Serial.println("Connecting to GSM...");

    // Connect to the GSM network
    if (!modem.gprsConnect("airtelgprs.com", "", "")) // Replace with your APN, username, and password
    {
        Serial.println("GPRS connection failed");
        return;
    }
    Serial.println("Connected to GSM"); // Confirm connection
}

/****** FUNCTION TO CONNECT TO WiFi *****/
void connectToWiFi()
{
    // Replace with your WiFi credentials
    const char* ssid = "WIFI_SSID"; // Your WiFi SSID
    const char* password = "WIFI_PW"; // Your WiFi password

    WiFi.begin(ssid, password); // Start the WiFi connection
    while (WiFi.status() != WL_CONNECTED) // Wait for connection
    {
        delay(500);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi"); // Confirm connection
}

/* END CODE */