Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Network Operations
- - Source Code NOT compiled for: Arduino Nano 33 BLE
- - Source Code created on: 2025-06-20 08:02:48
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Detect and initialize the Ethernet connection */
- /* using the Ethernet2 library and the W5500 module. */
- /* Retrieve and display the assigned IP address to */
- /* confirm successful connection. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Monitor and read all channels connected via the */
- /* PCF8575 I2C expander periodically, ensuring real- */
- /* time data acquisition from the digital inputs. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <Wire.h>
- #include <Ethernet2.h> //https://github.com/adafruit/Ethernet2
- #include <PCF8575.h> //https://github.com/RobTillaart/PCF8575.git
- #include <DFRobot_ADS1115.h> //https://github.com/DFRobot/DFRobot_ADS1115
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- void initializeEthernet();
- void readAllChannels();
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ethernet_W5500_RST_PIN_D2 = 2;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t pdf_PCF8575_I2C_PIN_SDA_A4 = A4;
- const uint8_t pdf_PCF8575_I2C_PIN_SCL_A5 = A5;
- const uint8_t pdf_PCF8575_I2C_SLAVE_ADDRESS = 32;
- const uint8_t adc_ADS1115_I2C_PIN_SDA_A4 = A4;
- const uint8_t adc_ADS1115_I2C_PIN_SCL_A5 = A5;
- const uint8_t adc_ADS1115_I2C_SLAVE_ADDRESS = 72;
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t ethernet_W5500_SPI_PIN_MOSI_D11 = 11;
- const uint8_t ethernet_W5500_SPI_PIN_MISO_D12 = 12;
- const uint8_t ethernet_W5500_SPI_PIN_SCLK_D13 = 13;
- const uint8_t ethernet_W5500_SPI_PIN_CS_D10 = 10;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ethernet_W5500_RST_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ethernet_W5500_RST_PIN_D2_phyData = 0.0;
- /****** LIBRARY CLASS INSTANCES *****/
- EthernetClass Ethernet; // Ethernet object
- PCF8575 pcf8575(pdf_PCF8575_I2C_SLAVE_ADDRESS); // PCF8575 expander
- // Add ADS1115 object if needed
- DFRobot_ADS1115 ads1115; // ADS1115 ADC
- unsigned long previousMillis = 0;
- const long interval = 1000; // 1 second interval for periodic reading
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- // Initialize Ethernet
- initializeEthernet();
- // Initialize pins
- pinMode(ethernet_W5500_RST_PIN_D2, OUTPUT);
- pinMode(ethernet_W5500_SPI_PIN_CS_D10, OUTPUT);
- // start the SPI library:
- SPI.begin();
- // Initialize I2C
- Wire.begin();
- // Initialize PCF8575
- pcf8575.begin();
- // Initialize ADS1115 if needed
- // ads1115.initialize(); // Uncomment if ADS1115 is used
- }
- void loop(void)
- {
- // Maintain Ethernet connection
- Ethernet.maintain();
- // Periodically read all channels
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- readAllChannels();
- }
- // Update outputs
- updateOutputs();
- }
- void updateOutputs()
- {
- digitalWrite(ethernet_W5500_RST_PIN_D2, ethernet_W5500_RST_PIN_D2_rawData);
- }
- void initializeEthernet()
- {
- // Use DHCP to get IP address
- int result = Ethernet.begin();
- if (result == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- // fallback or error handling
- } else {
- Serial.print("Ethernet IP Address: ");
- Serial.println(Ethernet.localIP());
- }
- }
- void readAllChannels()
- {
- // Read all digital inputs from PCF8575
- uint16_t inputStates = pcf8575.read16();
- Serial.print("PCF8575 Inputs: ");
- Serial.println(inputStates, BIN);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement