// ESP32-S3 - MASTER (Thermistor, LCD, ESP-NOW Send/Recv) - Revised Serial Output #include #include #include #include "DFRobot_RGBLCD1602.h" #include #include // For esp_now_recv_info_t // --- LCD Configuration --- const int LCD_I2C_SDA_PIN = 5; const int LCD_I2C_SCL_PIN = 6; const uint8_t LCD_RGB_ADDRESS = 0x60; DFRobot_RGBLCD1602 lcd(LCD_RGB_ADDRESS, 16, 2); // --- Thermistor Configuration --- const int THERMISTOR_PIN = 1; const float NOMINAL_RESISTANCE = 10000.0; const float NOMINAL_TEMPERATURE = 25.0; const float B_COEFFICIENT = 3950.0; const float SERIES_RESISTOR = 10000.0; const float V_IN = 3.3; const int ADC_MAX_S3 = 4095; const int NUM_SAMPLES_THERMISTOR = 5; const int SAMPLE_DELAY_MS_THERMISTOR = 5; // --- ESP-NOW Configuration --- uint8_t slaveMacAddress[] = {0x34, 0x85, 0x18, 0x03, 0x38, 0xD4}; // YOUR C3 SLAVE MAC typedef struct struct_temp_message { float temperature; int id; } struct_temp_message; typedef struct struct_reply_message { char status_text[20]; int original_id; } struct_reply_message; struct_temp_message tempToSend; struct_reply_message replyReceived; esp_now_peer_info_t peerInfo; volatile bool newReplyArrived = false; int messageCounter = 0; float currentTemperatureCelsius = -999.0; float temperatureAtLastSend = -999.0; // --- ESP-NOW Callbacks --- void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { // This confirms the ESP-NOW packet was queued or sent at a low level // Serial.print("S3_MASTER: Low-level send status to C3: "); // Serial.println(status == ESP_NOW_SEND_SUCCESS ? "OK" : "Fail"); } void OnDataRecv(const esp_now_recv_info_t *recv_info, const uint8_t *incomingData, int len) { if (len == sizeof(replyReceived)) { memcpy(&replyReceived, incomingData, sizeof(replyReceived)); newReplyArrived = true; } } // --- Thermistor Reading Function (no changes) --- void readThermistor() { float totalAdcValue = 0; for (int i = 0; i < NUM_SAMPLES_THERMISTOR; i++) { totalAdcValue += analogRead(THERMISTOR_PIN); delay(SAMPLE_DELAY_MS_THERMISTOR); } float averageAdcValue = totalAdcValue / NUM_SAMPLES_THERMISTOR; float vOut = (averageAdcValue / ADC_MAX_S3) * V_IN; float thermistorResistance; if (abs(V_IN - vOut) < 1e-6) thermistorResistance = 1e9; else if (vOut < 1e-6) thermistorResistance = 1e-9; else thermistorResistance = SERIES_RESISTOR * (vOut / (V_IN - vOut)); if (thermistorResistance > 0 && NOMINAL_RESISTANCE > 0 && B_COEFFICIENT > 0) { float t0_kelvin = NOMINAL_TEMPERATURE + 273.15; float steinhart_val = thermistorResistance / NOMINAL_RESISTANCE; if (steinhart_val > 0) { steinhart_val = log(steinhart_val) / B_COEFFICIENT + (1.0 / t0_kelvin); if (abs(steinhart_val) > 1e-9) currentTemperatureCelsius = (1.0 / steinhart_val) - 273.15; else currentTemperatureCelsius = -998.0; } else currentTemperatureCelsius = -997.0; } else currentTemperatureCelsius = -999.0; } // --- LCD Update Function (no changes) --- void updateDisplay() { lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.setCursor(6, 0); if (currentTemperatureCelsius > -270.0) { char tempStr[7]; dtostrf(currentTemperatureCelsius, 5, 1, tempStr); lcd.print(tempStr); lcd.print((char)223); lcd.print("C"); } else { lcd.print("Err"); } lcd.setCursor(0, 1); lcd.print("S.Reply: "); lcd.setCursor(10, 1); if (replyReceived.status_text[0] != '\0') { lcd.print(replyReceived.status_text); } else { lcd.print("Wait..."); } } void setup() { delay(500); Serial.begin(115200); unsigned long startWait = millis(); while (!Serial && (millis() - startWait < 2000)) { delay(10); } Serial.println("\n--- S3 MASTER Initializing ---"); Wire.begin(LCD_I2C_SDA_PIN, LCD_I2C_SCL_PIN); lcd.init(); lcd.setRGB(50, 50, 150); lcd.clear(); lcd.setCursor(0,0); lcd.print("S3 Master Boot"); replyReceived.status_text[0] = '\0'; WiFi.mode(WIFI_STA); Serial.print("S3_MASTER MAC: "); Serial.println(WiFi.macAddress()); if (esp_now_init() != ESP_OK) { Serial.println("S3_MASTER: ESP-NOW Init FAILED"); lcd.setCursor(0,1); lcd.print("ESP-NOW ERR"); while(1) delay(100); } esp_now_register_send_cb(OnDataSent); esp_now_register_recv_cb(OnDataRecv); memcpy(peerInfo.peer_addr, slaveMacAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("S3_MASTER: Peer Add FAILED"); lcd.setCursor(0,1); lcd.print("Peer Add ERR"); while(1) delay(100); } lcd.setCursor(0,1); lcd.print("Ready"); Serial.println("--- S3 MASTER Ready ---"); delay(1000); } unsigned long lastEspNowSendTime = 0; const long espNowSendInterval = 10000; // Send ESP-NOW message every 10 seconds unsigned long lastSensorReadTime = 0; const long sensorReadInterval = 1000; // Read sensor & update LCD every 1 second void loop() { unsigned long currentTime = millis(); if (currentTime - lastSensorReadTime >= sensorReadInterval) { lastSensorReadTime = currentTime; readThermistor(); updateDisplay(); } if (currentTime - lastEspNowSendTime >= espNowSendInterval) { lastEspNowSendTime = currentTime; if (currentTemperatureCelsius > -270.0) { tempToSend.temperature = currentTemperatureCelsius; temperatureAtLastSend = currentTemperatureCelsius; tempToSend.id = messageCounter++; // Acknowledge sending temperature Serial.printf("S3_MASTER: Sending Temp to C3: %.1f%cC (ID: %d)\n", tempToSend.temperature, (char)223, tempToSend.id); esp_now_send(slaveMacAddress, (uint8_t *) &tempToSend, sizeof(tempToSend)); } else { Serial.println("S3_MASTER: Invalid temp, not sending."); } } if (newReplyArrived) { newReplyArrived = false; // Acknowledge reply received and print formatted message Serial.printf("S3_MASTER: Reply received from C3 for ID %d.\n", replyReceived.original_id); Serial.printf(">>> Ohh %.1f%cC, that's %s!\n\n", temperatureAtLastSend, (char)223, replyReceived.status_text); updateDisplay(); } delay(10); }