pleasedontcode

Device Setup rev_01

Jul 21st, 2025
255
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: Device Setup
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-07-22 01:08:23
  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. /* START CODE */
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <WiFi.h>
  32. #include <SD.h>
  33. #include <SPI.h>
  34. #include <Nextion.h>
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. // Wi-Fi credentials
  41. const char* ssid = "Your_SSID";         // Replace with your Wi-Fi network SSID
  42. const char* password = "Your_PASSWORD"; // Replace with your Wi-Fi password
  43.  
  44. // Nextion Display Serial connection
  45. #define nexSerial Serial2 // Using Serial2 for Nextion
  46. // If your hardware setup differs, adjust accordingly
  47.  
  48. // SD card chip select pin
  49. const int chipSelect = 5; // GPIO5, adjust if necessary
  50.  
  51. // Nextion object
  52. // Assuming Nextion HMI is a 5-inch model connected via Serial2
  53. // The ID numbers are placeholders, adjust according to your Nextion setup
  54. // For example, if you have a button with ID 1, you can control it as shown
  55.  
  56. // No specific Nextion object instantiation needed unless using a library
  57. // We'll use the Nextion library functions directly
  58.  
  59. void setup_wifi() {
  60.   delay(100);
  61.   Serial.println();
  62.   Serial.print("Connecting to WiFi: ");
  63.   Serial.println(ssid);
  64.  
  65.   WiFi.begin(ssid, password);
  66.  
  67.   int retries = 0;
  68.   while (WiFi.status() != WL_CONNECTED && retries < 20) {
  69.     delay(500);
  70.     Serial.print(".");
  71.     retries++;
  72.   }
  73.  
  74.   if (WiFi.status() == WL_CONNECTED) {
  75.     Serial.println();
  76.     Serial.println("WiFi connected");
  77.     Serial.print("IP address: ");
  78.     Serial.println(WiFi.localIP());
  79.   } else {
  80.     Serial.println();
  81.     Serial.println("Failed to connect WiFi");
  82.   }
  83. }
  84.  
  85. void setup(void) {
  86.   Serial.begin(115200); // Serial for debugging
  87.   nexSerial.begin(9600); // Nextion display baud rate
  88.  
  89.   // Initialize Wi-Fi
  90.   setup_wifi();
  91.  
  92.   // Initialize SD card
  93.   if (!SD.begin(chipSelect)) {
  94.     Serial.println("Card Mount Failed");
  95.     // Handle error if necessary
  96.   } else {
  97.     Serial.println("SD card initialized");
  98.     // Optionally, read/write data
  99.   }
  100.  
  101.   // Initialize Nextion display
  102.   // Optional: send initial commands
  103.   // For example, set brightness or clear screen
  104.   nexSerial.write(0xFF);
  105.   nexSerial.write(0xFF);
  106.   nexSerial.write(0xFF);
  107.   // Add any further initialization commands here
  108. }
  109.  
  110. void loop(void) {
  111.   // Handle Wi-Fi, display, and storage tasks
  112.   // For demonstration, display Wi-Fi status on Nextion
  113.   static unsigned long lastUpdate = 0;
  114.   if (millis() - lastUpdate > 5000) { // Update every 5 seconds
  115.     lastUpdate = millis();
  116.  
  117.     String statusText;
  118.     if (WiFi.status() == WL_CONNECTED) {
  119.       statusText = "WiFi: Connected";
  120.     } else {
  121.       statusText = "WiFi: Disconnected";
  122.     }
  123.  
  124.     // Send status to Nextion
  125.     // Assuming there's a text component with object ID 't0'
  126.     // Format command: t0.txt="Status"
  127.     String cmd = "t0.txt=\"" + statusText + "\"";
  128.     sendNextionCommand(cmd);
  129.   }
  130.  
  131.   // Example: Read and process Nextion events if needed
  132.   // For simplicity, omitted here
  133.  
  134.   // Additional code for handling button presses, data transfer, etc.
  135. }
  136.  
  137. // Helper function to send commands to Nextion
  138. void sendNextionCommand(const String& cmd) {
  139.   String command = cmd + "\xFF\xFF\xFF"; // Append 3 terminator bytes
  140.   nexSerial.print(command);
  141. }
  142.  
  143. // Optional: Function to read from Nextion if necessary
  144. void readNextion() {
  145.   while (nexSerial.available()) {
  146.     char c = nexSerial.read();
  147.     // Process incoming data if needed
  148.   }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment