Issues Compiling MQTT code for ESP32

Hello All,

First post here! I am working on a senior project for school and have been trying to put an MQTT and WiFi code onto an ESP 32 to read temperatures through a K-type thermocouple. A lot of this is building of off a project that was done last year and we are continuing there work. So I have been getting errors such as:

(see attached Photo)

Now for the WiFi.h error I went into the one that is used and tried to delete the WiFi folder all together from either location and I run into a system error that won't let me delete either folder. So now there is that road block that I have no idea how to cross. Attached will be the code so anyone can take a look at it. I have also been referencing a website as a general model. I will include this as well. The original model seemed to use libraries to do the temperature reading and I took our already working code that get a temperature reading and replaced the existing code with that. I am not to good with Arduino code and I am trying to learn it quickly so I am hoping someone can help. Thanks!

Website Referenced: ESP32 MQTT - Publish DS18B20 Temperature Readings (Arduino IDE) | Random Nerd Tutorials

When attempting to delete WiFi.h from WindowsApps: (see Attached Photo)

#include <WiFi.h>
extern "C" {
  #include "freertos/FreeRTOS.h"  //unsure if this is needed for our code. refer to artical above.
  #include "freertos/timers.h"
}
#include <AsyncMqttClient.h>

// Our included libraries not original to this code
#include <driver/adc.h>
#include "esp32-hal-cpu.h"

#define WIFI_SSID "jester"
#define WIFI_PASSWORD "oliver2011"

// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 1, 0) //need to adjust per different wifi networks
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST "example.com"
#define MQTT_PORT 1883

// Temperature MQTT Topic
#define MQTT_PUB_TEMP "esp32/model-a/Temperature" //model a is for the first test model, will change to b and c for the remaining two models

// GPIO where the DS18B20 is connected to
const int analogPin = 34;                         // This has been changed to try and reflect the current pin setup for us
float Temp; //Temperature, everything after Temp is added in from previous codes
float Vout; //Voltage after adc
float SenVal; // Analog Sensor value
float Beta = 0.9;//RC factor
float FilteredVal;//voltage after digital filter

void getTemp();//adc & Temp function declaration

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

unsigned long previousMillis = 0;   // Stores last time Temperature was published
const long interval = 1000;         // Interval at which to publish sensor readings (Currently set to 1 second intervals)

void connectToWifi() 
{
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() 
{
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void WiFiEvent(WiFiEvent_t event) 
{
  Serial.printf("[WiFi-event] event: %d\n", event);
  switch(event) {
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      connectToMqtt();
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("WiFi lost connection");
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
      break;
  }
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");
  if (WiFi.isConnected()) {
    xTimerStart(mqttReconnectTimer, 0);
  }
}

void onMqttPublish(uint16_t packetId) {
  Serial.println("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void setup() {
  // Serial.begin(9600); //From original code this is what serial begin was set to
  Serial.begin(115200);
  Serial.println();
  Serial.println();

  mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

  WiFi.onEvent(WiFiEvent);

  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  // If your broker requires authentication (username and password), set them below
  //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
  connectToWifi();
}

void getTemp()
{
  SenVal = analogRead(analogPin);//adc1_get_raw(ADC1_CHANNEL_5); //Analog value from AD8495 output pin
  Vout = (SenVal *3.33)/4095; //Conversion analog to digital for the Temperature read voltage 
  Temp = (211.137*Vout)-221.928;//calibrated conversion of digital voltage level to Temperature 
Serial.println (Vout);
Serial.println (Temp);
}

void loop() 
{
  unsigned long currentMillis = millis();
  // Every X number of seconds (interval = 10 seconds) 
  // it publishes a new MQTT message
  if (currentMillis - previousMillis >= interval)
  {
    // Save the last time a new reading was published
    previousMillis = currentMillis;
    // New Temperature readings
    //sensors.requestTemperatures(); // I believe that in this we can add the getting Temperature process in order to get the Temperature. 
    getTemp(); //proposed solution to line above.
    // Temp1 = getTemp(); //sensors.getTempFByIndex(0);
    
    // Publish an MQTT message on topic esp32/model-a/Temperature
    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(Temp).c_str());                            
    Serial.printf("Publishing on topic %s at QoS 1, packetId: ", MQTT_PUB_TEMP);
    Serial.println(packetIdPub1);
    Serial.printf("Message: %.2f /n", getTemp()); //error here and not sure why 
  }
}

Trying to delete wifi_h.png

Trying to delete wifi_h.png

The ESP32's OS is freeRTOS. The Arduino core runs on top of freeRTOS.

cybercornez:
When attempting to delete WiFi.h from WindowsApps: (see Attached Photo)

Why?

just go in and change the files privilege's, then delete them.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.