Advertisement
pleasedontcode

Network Configuration rev_09

Jul 4th, 2025
240
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: Network Configuration
  13.     - Source Code NOT compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-07-05 00:32:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Detect and initialize the Ethernet connection */
  21.     /* using the Ethernet2 library and the W5500 module. */
  22.     /* Retrieve and display the assigned IP address to */
  23.     /* confirm successful connection. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* Monitor and read all channels connected via the */
  26.     /* PCF8575 I2C expander periodically, ensuring real- */
  27.     /* time data acquisition from the digital inputs. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <SPI.h>
  34. #include <Wire.h>
  35. #include <Ethernet2.h>    // https://github.com/adafruit/Ethernet2
  36. #include <PCF8575.h>      // https://github.com/RobTillaart/PCF8575.git
  37. #include <DFRobot_ADS1115.h> // https://github.com/DFRobot/DFRobot_ADS1115
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void monitorAndReadChannels(void);
  43. void updateOutputs(void);
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
  47.  
  48. /***** DEFINITION OF I2C PINS *****/
  49. const uint8_t pdf_PCF8575_I2C_PIN_SDA_A4 = A4;
  50. const uint8_t pdf_PCF8575_I2C_PIN_SCL_A5 = A5;
  51. const uint8_t pdf_PCF8575_I2C_SLAVE_ADDRESS = 0x20; // 32 decimal
  52. const uint8_t adc_ADS1115_I2C_SLAVE_ADDRESS = 0x48; // 72 decimal
  53.  
  54. /***** DEFINITION OF SPI PINS *****/
  55. const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
  56. const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
  57. const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
  58. const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
  59.  
  60. /***** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  61. Ethernet2 Ethernet; // Ethernet instance
  62. PCF8575 pcf8575(pdf_PCF8575_I2C_SLAVE_ADDRESS); // PCF8575 instance
  63. DFRobot_ADS1115 ads1115(&Wire); // ADS1115 instance
  64.  
  65. /***** VARIABLES *****/
  66. // Raw data and physical data for the W5500 reset pin
  67. bool ethernet_W5500_RST_PIN_D2_rawData = false;
  68.  
  69. // Timing variables for periodic monitoring
  70. unsigned long previousMonitorMillis = 0;
  71. const unsigned long monitorInterval = 1000; // 1 second
  72.  
  73. void setup(void)
  74. {
  75.   // Initialize serial for debugging
  76.   Serial.begin(115200);
  77.   while (!Serial); // Wait for serial port
  78.  
  79.   // Initialize reset pin
  80.   pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
  81.   // Initialize SPI
  82.   pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
  83.   SPI.begin();
  84.  
  85.   // Reset W5500 module
  86.   digitalWrite(ethernet_W5500_RST_PIN_D2, LOW);
  87.   delay(10);
  88.   digitalWrite(ethernet_W5500_RST_PIN_D2, HIGH);
  89.   delay(100);
  90.  
  91.   // Detect and initialize Ethernet connection
  92.   Serial.println("Initializing Ethernet...");
  93.   if (Ethernet.begin()) {
  94.     Serial.println("Ethernet initialized successfully.");
  95.   } else {
  96.     Serial.println("Ethernet initialization failed.");
  97.   }
  98.  
  99.   // Retrieve and display IP address
  100.   IPAddress ip = Ethernet.localIP();
  101.   Serial.print("Assigned IP Address: ");
  102.   Serial.println(ip);
  103.  
  104.   // Initialize PCF8575
  105.   if (pcf8575.begin()) {
  106.     Serial.println("PCF8575 initialized successfully.");
  107.   } else {
  108.     Serial.println("PCF8575 initialization failed.");
  109.   }
  110.  
  111.   // Initialize ADS1115
  112.   ads1115.init();
  113.   if (ads1115.checkADS1115()) {
  114.     Serial.println("ADS1115 initialized successfully.");
  115.   } else {
  116.     Serial.println("ADS1115 initialization failed.");
  117.   }
  118. }
  119.  
  120. void loop(void)
  121. {
  122.   unsigned long currentMillis = millis();
  123.  
  124.   // Periodically monitor and read all channels
  125.   if (currentMillis - previousMonitorMillis >= monitorInterval)
  126.   {
  127.     previousMonitorMillis = currentMillis;
  128.     monitorAndReadChannels();
  129.   }
  130.  
  131.   // Refresh output data
  132.   updateOutputs();
  133. }
  134.  
  135. void monitorAndReadChannels()
  136. {
  137.   // Read all 16 channels from PCF8575
  138.   uint16_t channelStates = pcf8575.read16();
  139.  
  140.   // Display the states
  141.   Serial.print("PCF8575 Channel States: 0x");
  142.   Serial.println(channelStates, HEX);
  143.  
  144.   // Optional: process individual pins if needed
  145.   for (uint8_t pin = 0; pin < 16; pin++)
  146.   {
  147.     uint8_t pinState = (channelStates & (1 << pin)) ? HIGH : LOW;
  148.     Serial.print("Pin ");
  149.     Serial.print(pin);
  150.     Serial.print(": ");
  151.     Serial.println(pinState == HIGH ? "HIGH" : "LOW");
  152.   }
  153. }
  154.  
  155. void updateOutputs()
  156. {
  157.   // Set the W5500 reset pin according to rawData
  158.   digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
  159. }
  160.  
  161. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement