├── .gitattributes ├── EthernetPrototype └── EthernetPrototype.ino └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /EthernetPrototype/EthernetPrototype.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT 8 | #define ETH_PHY_POWER 12 9 | 10 | int request = 0; 11 | char *payloadString; 12 | 13 | #define inTopic "Ethernet/command" 14 | #define outTopic "Ethernet/status" 15 | 16 | #define SEALEVELPRESSURE_HPA (1013.25) 17 | Adafruit_BME280 bme; // I2C 18 | 19 | int led1 = 32; 20 | int led2 = 33; 21 | unsigned long entry; 22 | 23 | 24 | int LED = 02; 25 | 26 | // #define ETH_CLK_MODE ETH_CLOCK_GPIO0_OUT // Version with PSRAM 27 | #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT // Version with not PSRAM 28 | 29 | // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source) 30 | #define ETH_POWER_PIN -1 31 | 32 | // Type of the Ethernet PHY (LAN8720 or TLK110) 33 | #define ETH_TYPE ETH_PHY_LAN8720 34 | 35 | // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110) 36 | #define ETH_ADDR 0 37 | 38 | // Pin# of the I²C clock signal for the Ethernet PHY 39 | #define ETH_MDC_PIN 23 40 | 41 | // Pin# of the I²C IO signal for the Ethernet PHY 42 | #define ETH_MDIO_PIN 18 43 | 44 | #define NRST 5 45 | 46 | // CHANGE THESE SETTINGS FOR YOUR APPLICATION 47 | const char* mqttServerIp = "192.168.0.203"; // IP address of the MQTT broker 48 | const short mqttServerPort = 1883; // IP port of the MQTT broker 49 | const char* mqttClientName = "ESP32-POE"; 50 | const char* mqttUsername = "admin"; 51 | const char* mqttPassword = "admin"; 52 | 53 | // Initializations of network clients 54 | WiFiClient espClient; 55 | PubSubClient mqttClient(espClient); 56 | static bool eth_connected = false; 57 | uint64_t chipid; 58 | 59 | //ESP32 Specific Ethernet 60 | //Yes, I know it says wifi. Trust me. 61 | void WiFiEvent(WiFiEvent_t event) 62 | { 63 | switch (event) { 64 | case SYSTEM_EVENT_ETH_START: 65 | Serial.println("ETH Started"); 66 | //set eth hostname here 67 | ETH.setHostname("esp32-ethernet"); 68 | break; 69 | case SYSTEM_EVENT_ETH_CONNECTED: 70 | Serial.println("ETH Connected"); 71 | break; 72 | case SYSTEM_EVENT_ETH_GOT_IP: 73 | Serial.print("ETH MAC: "); 74 | Serial.print(ETH.macAddress()); 75 | Serial.print(", IPv4: "); 76 | Serial.print(ETH.localIP()); 77 | if (ETH.fullDuplex()) { 78 | Serial.print(", FULL_DUPLEX"); 79 | } 80 | Serial.print(", "); 81 | Serial.print(ETH.linkSpeed()); 82 | Serial.println("Mbps"); 83 | eth_connected = true; 84 | break; 85 | case SYSTEM_EVENT_ETH_DISCONNECTED: 86 | Serial.println("ETH Disconnected"); 87 | eth_connected = false; 88 | break; 89 | case SYSTEM_EVENT_ETH_STOP: 90 | Serial.println("ETH Stopped"); 91 | eth_connected = false; 92 | break; 93 | default: 94 | break; 95 | } 96 | } 97 | 98 | void callback(char* topic, byte* payload, unsigned int length) { 99 | for (int i = 0; i < length; i++) { 100 | payload[length] = '\0'; 101 | } 102 | Serial.println(payloadString); 103 | Serial.println(request); 104 | Serial.println(topic); 105 | 106 | 107 | /* // This snippet sets all the setpoints at once for the whole arduino 108 | if (strstr(topic, "setall") != NULL) { 109 | 110 | } 111 | */ 112 | if (strstr(topic, "command") != NULL) { 113 | payloadString = (char *) payload; 114 | Serial.println("The request is..."); 115 | Serial.println(payloadString); 116 | if (strstr(payloadString, "1on") != NULL) { 117 | Serial.println("Turning led 1 on"); 118 | digitalWrite(led1, HIGH); 119 | } 120 | if (strstr(payloadString, "1off") != NULL) { 121 | Serial.println("Turning led 1 off"); 122 | digitalWrite(led1, LOW); 123 | } 124 | if (strstr(payloadString, "2on") != NULL) { 125 | Serial.println("Turning led 2 on"); 126 | digitalWrite(led2, HIGH); 127 | } 128 | if (strstr(payloadString, "2off") != NULL) { 129 | Serial.println("Turning led 2 off"); 130 | digitalWrite(led2, LOW); 131 | } 132 | 133 | } 134 | } 135 | 136 | void reconnect() { 137 | // Loop until we're reconnected 138 | while (!mqttClient.connected()) { 139 | Serial.print("Attempting MQTT connection..."); 140 | // Attempt to connect 141 | if (mqttClient.connect(mqttClientName)) { 142 | //if (mqttClient.connect(mqttClientName, mqttUsername, mqttPassword)) { // if credentials is nedded 143 | Serial.println("connected"); 144 | // Once connected, publish an announcement... 145 | mqttClient.publish("random/test", "hello world"); 146 | // ... and resubscribe 147 | mqttClient.subscribe("leds/#"); 148 | } else { 149 | Serial.print("failed, rc="); 150 | Serial.print(mqttClient.state()); 151 | Serial.println(" try again in 5 seconds"); 152 | // Wait 5 seconds before retrying 153 | delay(5000); 154 | } 155 | mqttClient.publish(outTopic, "connected to MQTT"); 156 | mqttClient.subscribe(inTopic); 157 | } 158 | } 159 | 160 | void setup() 161 | { 162 | Serial.begin(115200); 163 | 164 | pinMode(led1, OUTPUT); 165 | pinMode(led2, OUTPUT); 166 | digitalWrite(led1, LOW); 167 | digitalWrite(led2, LOW); 168 | 169 | mqttClient.setServer(mqttServerIp, mqttServerPort); 170 | mqttClient.setCallback(callback); 171 | 172 | chipid = ESP.getEfuseMac(); //The chip ID is essentially its MAC address(length: 6 bytes). 173 | Serial.printf("ESP32 Chip ID = %04X", (uint16_t)(chipid >> 32)); //print High 2 bytes 174 | Serial.printf("%08X\n", (uint32_t)chipid); //print Low 4bytes. 175 | 176 | // Init sensor 177 | Wire.begin (14, 15); // init I2C on the respective pins 178 | unsigned status = bme.begin(0x76); 179 | if (!status) { 180 | Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); 181 | Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16); 182 | Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); 183 | Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); 184 | Serial.print(" ID of 0x60 represents a BME 280.\n"); 185 | Serial.print(" ID of 0x61 represents a BME 680.\n"); 186 | while (1) delay(10); 187 | } 188 | 189 | WiFi.onEvent(WiFiEvent); 190 | ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); 191 | //uncomment to set static ip, (ip address, gateway, subnetmask) 192 | //ETH.config(IPAddress(192, 168, 0, 90),IPAddress(192, 168, 0, 1),IPAddress(255, 255, 255, 0)); 193 | } 194 | 195 | void loop() { 196 | // check if ethernet is connected 197 | if (eth_connected) { 198 | // now take care of MQTT client... 199 | if (!mqttClient.connected()) { 200 | reconnect(); 201 | } else { 202 | mqttClient.loop(); 203 | 204 | } 205 | } 206 | if (millis() > entry + 5000) { 207 | float temp = bme.readTemperature(); 208 | Serial.print("Temperature = "); 209 | Serial.print(temp); 210 | Serial.println(" *C"); 211 | 212 | Serial.print("Pressure = "); 213 | float pressure = bme.readPressure() / 100.0F; 214 | Serial.print(pressure); 215 | Serial.println(" hPa"); 216 | 217 | float alti = bme.readAltitude(SEALEVELPRESSURE_HPA); 218 | Serial.print("Approx. Altitude = "); 219 | Serial.print(alti); 220 | Serial.println(" m"); 221 | 222 | float hum = bme.readHumidity(); 223 | Serial.print("Humidity = "); 224 | Serial.print(hum); 225 | Serial.println(" %"); 226 | 227 | Serial.println(); 228 | char message_buff[50]; 229 | String payloadStr = "T " + String(temp, 1) + " H " + String(hum, 1) + " P " + pressure + " A " + String(alti, 1); 230 | payloadStr.toCharArray(message_buff, payloadStr.length() + 1); 231 | 232 | mqttClient.publish (outTopic, message_buff); 233 | entry = millis(); 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32 Ethernet 2 | Example File for Ethernet 3 | --------------------------------------------------------------------------------