pleasedontcode

Setup Initialization rev_02

Jul 21st, 2025
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: Setup Initialization
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-07-22 01:12:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* قم بتمكين اتصال Wi-Fi على Arduino Nano ESP32 */
  21.     /* للسماح بالتحكم عن بعد ونقل البيانات.  ايضا تشغيل */
  22.     /* شاشه ولورا وبطاقه ذاكرهوشاشه nextion 5in  ومهام */
  23.     /* بحاجه لتفصيل وخصوصا ازرار الشاشه */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* تنفيذ اتصالات Wi-Fi وواجهة عرض Nextion وتخزين */
  26.     /* البيانات على بطاقة SD لأتمتة إنترنت الأشياء. */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29.  
  30. /********* User code review feedback **********
  31. #### Feedback 1 ####
  32. - Users\user\Documents\Arduino\libraries\ITEADLIB_Arduino_Nextion-
  33. master\NexUpload.cpp:17:10: fatal error: SoftwareSerial.h: No su
  34. ch file or directory
  35.    17 | #include <SoftwareSerial.h>
  36.       |
  37.           ^~~~~~~~~~~~~~~~~~
  38. compilation terminated.
  39. Alternative
  40. s for SoftwareSerial.h: []
  41. ResolveLibrary(So
  42. ********* User code review feedback **********/
  43.  
  44. /* START CODE */
  45. /****** DEFINITION OF LIBRARIES *****/
  46. #include <WiFi.h>
  47. #include <SD.h>
  48. #include <SPI.h>
  49. // #include <Nextion.h> // Removed, as no library is used for Nextion communication
  50.  
  51. /****** FUNCTION PROTOTYPES *****/
  52. void setup(void);
  53. void loop(void);
  54. void sendNextionCommand(const String& cmd);
  55. void setup_wifi(void);
  56. void readNextion(); // Optional, if needed
  57.  
  58. // Wi-Fi credentials
  59. const char* ssid = "Your_SSID";         // Replace with your Wi-Fi network SSID
  60. const char* password = "Your_PASSWORD"; // Replace with your Wi-Fi password
  61.  
  62. // Nextion Display Serial connection
  63. #define nexSerial Serial2 // Using Serial2 for Nextion
  64. // If your hardware setup differs, adjust accordingly
  65.  
  66. // SD card chip select pin
  67. const int chipSelect = 5; // GPIO5, adjust if necessary
  68.  
  69. // No specific Nextion object instantiation needed unless using a library
  70. // We'll use the Serial2 directly for communication
  71.  
  72. void setup_wifi() {
  73.   delay(100);
  74.   Serial.println();
  75.   Serial.print("Connecting to WiFi: ");
  76.   Serial.println(ssid);
  77.  
  78.   WiFi.begin(ssid, password);
  79.  
  80.   int retries = 0;
  81.   while (WiFi.status() != WL_CONNECTED && retries < 20) {
  82.     delay(500);
  83.     Serial.print(".");
  84.     retries++;
  85.   }
  86.  
  87.   if (WiFi.status() == WL_CONNECTED) {
  88.     Serial.println();
  89.     Serial.println("WiFi connected");
  90.     Serial.print("IP address: ");
  91.     Serial.println(WiFi.localIP());
  92.   } else {
  93.     Serial.println();
  94.     Serial.println("Failed to connect WiFi");
  95.   }
  96. }
  97.  
  98. void setup(void) {
  99.   Serial.begin(115200); // Serial for debugging
  100.   nexSerial.begin(9600); // Nextion display baud rate
  101.  
  102.   // Initialize Wi-Fi
  103.   setup_wifi();
  104.  
  105.   // Initialize SD card
  106.   if (!SD.begin(chipSelect)) {
  107.     Serial.println("Card Mount Failed");
  108.     // Handle error if necessary
  109.   } else {
  110.     Serial.println("SD card initialized");
  111.     // Optionally, read/write data
  112.   }
  113.  
  114.   // Initialize Nextion display
  115.   // Send initial 3 bytes to start communication
  116.   nexSerial.write(0xFF);
  117.   nexSerial.write(0xFF);
  118.   nexSerial.write(0xFF);
  119.   // Add any further initialization commands here if needed
  120. }
  121.  
  122. void loop(void) {
  123.   // Handle Wi-Fi, display, and storage tasks
  124.   // For demonstration, display Wi-Fi status on Nextion
  125.   static unsigned long lastUpdate = 0;
  126.   if (millis() - lastUpdate > 5000) { // Update every 5 seconds
  127.     lastUpdate = millis();
  128.  
  129.     String statusText;
  130.     if (WiFi.status() == WL_CONNECTED) {
  131.       statusText = "WiFi: Connected";
  132.     } else {
  133.       statusText = "WiFi: Disconnected";
  134.     }
  135.  
  136.     // Send status to Nextion
  137.     // Assuming there's a text component with object ID 't0'
  138.     // Format command: t0.txt="Status"
  139.     String cmd = "t0.txt=\"" + statusText + "\"";
  140.     sendNextionCommand(cmd);
  141.   }
  142.  
  143.   // Example: Read and process Nextion events if needed
  144.   // For simplicity, omitted here
  145.  
  146.   // Additional code for handling button presses, data transfer, etc.
  147. }
  148.  
  149. // Helper function to send commands to Nextion
  150. void sendNextionCommand(const String& cmd) {
  151.   String command = cmd + "\xFF\xFF\xFF"; // Append 3 terminator bytes
  152.   nexSerial.print(command);
  153. }
  154.  
  155. // Optional: Function to read from Nextion if necessary
  156. void readNextion() {
  157.   while (nexSerial.available()) {
  158.     char c = nexSerial.read();
  159.     // Process incoming data if needed
  160.   }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment