Advertisement
pleasedontcode

Network Operations rev_05

Jun 20th, 2025
378
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 Operations
  13.     - Source Code NOT compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-06-20 08:02:48
  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 updateOutputs(void);
  43. void initializeEthernet();
  44. void readAllChannels();
  45.  
  46. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  47. const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
  48.  
  49. /***** DEFINITION OF I2C PINS *****/
  50. const uint8_t pdf_PCF8575_I2C_PIN_SDA_A4 = A4;
  51. const uint8_t pdf_PCF8575_I2C_PIN_SCL_A5 = A5;
  52. const uint8_t pdf_PCF8575_I2C_SLAVE_ADDRESS = 32;
  53. const uint8_t adc_ADS1115_I2C_PIN_SDA_A4 = A4;
  54. const uint8_t adc_ADS1115_I2C_PIN_SCL_A5 = A5;
  55. const uint8_t adc_ADS1115_I2C_SLAVE_ADDRESS = 72;
  56.  
  57. /***** DEFINITION OF SPI PINS *****/
  58. const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
  59. const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
  60. const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
  61. const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
  62.  
  63. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  64. /***** used to store raw data *****/
  65. bool ethernet_W5500_RST_PIN_D2_rawData = 0;
  66.  
  67. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  68. /***** used to store data after characteristic curve transformation *****/
  69. float ethernet_W5500_RST_PIN_D2_phyData = 0.0;
  70.  
  71. /****** LIBRARY CLASS INSTANCES *****/
  72. EthernetClass Ethernet; // Ethernet object
  73. PCF8575 pcf8575(pdf_PCF8575_I2C_SLAVE_ADDRESS); // PCF8575 expander
  74. // Add ADS1115 object if needed
  75. DFRobot_ADS1115 ads1115; // ADS1115 ADC
  76.  
  77. unsigned long previousMillis = 0;
  78. const long interval = 1000; // 1 second interval for periodic reading
  79.  
  80. void setup(void)
  81. {
  82.   // Initialize serial communication for debugging
  83.   Serial.begin(9600);
  84.   while (!Serial) {
  85.     ; // wait for serial port to connect. Needed for native USB port only
  86.   }
  87.  
  88.   // Initialize Ethernet
  89.   initializeEthernet();
  90.  
  91.   // Initialize pins
  92.   pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
  93.   pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
  94.   // start the SPI library:
  95.   SPI.begin();
  96.  
  97.   // Initialize I2C
  98.   Wire.begin();
  99.  
  100.   // Initialize PCF8575
  101.   pcf8575.begin();
  102.  
  103.   // Initialize ADS1115 if needed
  104.   // ads1115.initialize(); // Uncomment if ADS1115 is used
  105.  
  106. }
  107.  
  108. void loop(void)
  109. {
  110.   // Maintain Ethernet connection
  111.   Ethernet.maintain();
  112.  
  113.   // Periodically read all channels
  114.   unsigned long currentMillis = millis();
  115.   if (currentMillis - previousMillis >= interval) {
  116.     previousMillis = currentMillis;
  117.     readAllChannels();
  118.   }
  119.  
  120.   // Update outputs
  121.   updateOutputs();
  122. }
  123.  
  124. void updateOutputs()
  125. {
  126.   digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
  127. }
  128.  
  129. void initializeEthernet()
  130. {
  131.   // Use DHCP to get IP address
  132.   int result = Ethernet.begin();
  133.   if (result == 0) {
  134.     Serial.println("Failed to configure Ethernet using DHCP");
  135.     // fallback or error handling
  136.   } else {
  137.     Serial.print("Ethernet IP Address: ");
  138.     Serial.println(Ethernet.localIP());
  139.   }
  140. }
  141.  
  142. void readAllChannels()
  143. {
  144.   // Read all digital inputs from PCF8575
  145.   uint16_t inputStates = pcf8575.read16();
  146.   Serial.print("PCF8575 Inputs: ");
  147.   Serial.println(inputStates, BIN);
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement