· last year · Aug 03, 2024, 04:55 PM
1/********* Pleasedontcode.com **********
2
3 Pleasedontcode thanks you for automatic code generation! Enjoy your code!
4
5 - Terms and Conditions:
6 You have a non-exclusive, revocable, worldwide, royalty-free license
7 for personal and commercial use. Attribution is optional; modifications
8 are allowed, but you're responsible for code maintenance. We're not
9 liable for any loss or damage. For full terms,
10 please visit pleasedontcode.com/termsandconditions.
11
12 - Project: GSM Interface
13 - Source Code NOT compiled for: ESP32 DevKit V1
14 - Source Code created on: 2024-08-03 16:49:11
15
16********* Pleasedontcode.com **********/
17
18/****** SYSTEM REQUIREMENTS *****/
19/****** SYSTEM REQUIREMENT 1 *****/
20 /* Use TinyGSM library to initialize modem with APN */
21 /* "airtelgprs.com", establish GSM connection, enable */
22 /* GNSS module for location data capture, and display */
23 /* all activity logs on Serial Monitor. */
24/****** SYSTEM REQUIREMENT 2 *****/
25 /* add #define TINY_GSM_MODEM_SIM7600 */
26/****** END SYSTEM REQUIREMENTS *****/
27
28/****** DEFINITION OF LIBRARIES *****/
29#include <Wire.h>
30#include <cohereclient.hpp> // Cohere Client library for API interaction
31#include <Adafruit_SSD1306.h> // Adafruit SSD1306 OLED display library
32#include <U8g2_for_Adafruit_GFX.h> // U8g2 library for Adafruit GFX support
33#include <TinyGsmClient.h> // TinyGSM library for GSM communication
34
35/****** FUNCTION PROTOTYPES *****/
36void setup(void);
37void loop(void);
38
39/***** DEFINITION OF Hardware Serial *****/
40#define modem Serial2 // TX_PIN: D17, RX_PIN: D16
41
42/***** DEFINITION OF I2C PINS *****/
43const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21; // I2C SDA pin
44const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22; // I2C SCL pin
45const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C; // I2C Slave address
46
47/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
48WiFiClientSecure client; // Secure WiFi client for HTTPS connections
49CohereClient cohereClient(&client, "YOUR_API_KEY"); // Initialize CohereClient with the secure client and API key
50
51// Initialize the display object with I2C address and reset pin
52#define OLED_RESET -1 // No reset pin used for I2C
53Adafruit_SSD1306 display(OLED_RESET); // Create display object
54
55// Initialize U8G2_FOR_ADAFRUIT_GFX object
56U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create U8G2 object
57
58// Initialize TinyGSM modem
59#define TINY_GSM_MODEM_SIM7600 // Define the modem type
60TinyGsmClient gsmClient(modem); // Create GSM client
61TinyGsm modem(gsmClient); // Create modem instance
62
63void setup(void)
64{
65 // Initialize the modem serial communication
66 modem.begin(115200);
67 Serial.begin(115200); // Start serial communication for debugging
68
69 // Initialize the display
70 display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
71 display.clearDisplay(); // Clear the display buffer
72
73 // Connect U8G2 to the display
74 u8g2_for_adafruit_gfx.begin(display); // Connect u8g2 procedures to Adafruit GFX
75
76 // Connect to GSM network
77 connectToGSM(); // Call function to connect to GSM
78}
79
80void loop(void)
81{
82 // Example usage of the CohereClient
83 int max_tokens = 200; // Maximum tokens for the API call
84 String prompt = "Once upon a time in a magical land called"; // Prompt for the API call
85
86 // Make an API call and get the response
87 String response = cohereClient.makeAPICall(max_tokens, prompt);
88
89 // Print the text output from the response
90 String output = cohereClient.text_output(response);
91 Serial.println(output);
92 delay(5000); // Delay for readability
93
94 Serial.println(""); // Print a new line for separation
95
96 // Print the full response for debugging
97 String fullResponse = cohereClient.full_response(response);
98 Serial.println(fullResponse);
99 delay(5000); // Delay for readability
100}
101
102/****** FUNCTION TO CONNECT TO GSM *****/
103void connectToGSM()
104{
105 // Set modem parameters
106 modem.restart(); // Restart the modem
107 Serial.println("Connecting to GSM...");
108
109 // Connect to the GSM network
110 if (!modem.gprsConnect("airtelgprs.com", "", "")) // Replace with your APN, username, and password
111 {
112 Serial.println("GPRS connection failed");
113 return;
114 }
115 Serial.println("Connected to GSM"); // Confirm connection
116}
117
118/****** FUNCTION TO CONNECT TO WiFi *****/
119void connectToWiFi()
120{
121 // Replace with your WiFi credentials
122 const char* ssid = "WIFI_SSID"; // Your WiFi SSID
123 const char* password = "WIFI_PW"; // Your WiFi password
124
125 WiFi.begin(ssid, password); // Start the WiFi connection
126 while (WiFi.status() != WL_CONNECTED) // Wait for connection
127 {
128 delay(500);
129 Serial.println("Connecting to WiFi...");
130 }
131 Serial.println("Connected to WiFi"); // Confirm connection
132}
133
134/* END CODE */