ESP32 can't resolve DNS

smprajkta
Posts: 1
Joined: Tue Jun 18, 2019 10:07 am

ESP32 can't resolve DNS

Postby smprajkta » Tue Jun 18, 2019 10:25 am

I am trying to send data to raspberrypi3 from my DOIT ESP32 DEVKIT V1 using DNS of raspberry pi using mqtt.
MQTT server is up and running on raspberry pi,
when I do a code to send data through mqtt on esp32 by addressing the raspberry pi by its IP address, its working fine, but when I put the DNS of raspberry pi which is "rpi.local", the same code doesn't work and gives the error
Serial terminal output:- Attempting MQTT connection...failed, rc=-2 try again in 5 seconds)

DNS on pi is working because the same code posted below is working on esp8266, means DNS resolving is working on esp8266 but not on esp32. Any help is highly appreciated.
Thank you

Code: Select all

#include <WiFi.h>

#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "Robotics_Lab";
const char* password = "vidya123";
const char* mqtt_server = "rpi.local"; // the newly created DNS by me

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int relay = 15;

/*************************************************************************/
#define ANALOG_PIN_0 36
int currentPin = 36;              //Assign CT input to pin 1
double kilos = 0;
int peakPower = 0;

int mVperAmp = 185;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
/****************************************************************************/
unsigned long currentMillis;
unsigned long previousMillis = 0;
unsigned long interval = 2000;
void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(2, HIGH);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
    digitalWrite(relay, HIGH);
    Serial.println("relay1 on");
  } else {
    digitalWrite(2, LOW);  // Turn the LED off by making the voltage HIGH
    digitalWrite(relay, LOW);
    Serial.println("relay1 off");
  }
  if ((char)payload[1] == '1') {
    //digitalWrite(2, HIGH);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
    //digitalWrite(relay, HIGH);
    Serial.println("relay2 on");
  } else {
    //digitalWrite(2, LOW);  // Turn the LED off by making the voltage HIGH
    //digitalWrite(relay, LOW);
    Serial.println("relay2 off");
  }
  if ((char)payload[2] == '1') {
    digitalWrite(2, HIGH);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
    //digitalWrite(relay, HIGH);
    Serial.println("relay3 on");
  } else {
    digitalWrite(2, LOW);  // Turn the LED off by making the voltage HIGH
    //digitalWrite(relay, LOW);
    Serial.println("relay3 off");
  }
  if ((char)payload[3] == '1') {
    digitalWrite(2, HIGH);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
    //digitalWrite(relay, HIGH);
    Serial.println("relay4 on");
  } else {
    digitalWrite(2, LOW);  // Turn the LED off by making the voltage HIGH
    //digitalWrite(relay, LOW);
    Serial.println("relay4 off");
  }

  /*
    if ((char)payload[3] == '1') {
    digitalWrite(2, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
    //digitalWrite(relay, HIGH);
    Serial.println("relay4");
    } else {
    digitalWrite(2, HIGH);  // Turn the LED off by making the voltage HIGH
    //digitalWrite(relay, LOW);
    }*/

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("power", "hello world");
      // ... and resubscribe
      client.subscribe("ESP32-1");

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(currentPin, INPUT);     // Initialize the 2 pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}


float getVPP()
{
  float result;

  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 4096;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 1000) //sample for 1 Sec
  {
    readValue = analogRead(ANALOG_PIN_0);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the maximum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  result = ((maxValue - minValue) * 3.3) / 4096.0;

  return result;
}
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  /*
    if (now - lastMsg > 2000) {
    lastMsg = now;
    value=!value;
    // Serial.println(value);
    if(value == 1)
    snprintf (msg, 50, "%s","ON");
    if(value == 0)
    snprintf (msg, 50, "%s","OFF");
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("state",msg);
    delay(2000);
    }*/
  /**************************************************************/
  /*******************************************************************************/
  Voltage = getVPP();
  VRMS = (Voltage / 2.0) * 0.707;
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  AmpsRMS = AmpsRMS - 0.13;
  if (AmpsRMS < 0.35) AmpsRMS = 0;
  //Serial.print(AmpsRMS);
  //Serial.println(" Amps RMS");
  //Serial.println(" *************************************************");
  int RMSPower = 220 * AmpsRMS;  //Calculates RMS Power Assuming Voltage 220VAC, change to 110VAC accordingly
  if (RMSPower > peakPower)
  {
    peakPower = RMSPower;
  }
  kilos = kilos + (RMSPower * (2.05 / 60 / 60 / 1000)); //Calculate kilowatt hours used



  /**********************************************************/



  Serial.print("RMS Current = ");
  Serial.print(AmpsRMS);
  Serial.println("A");

  Serial.print("RMSPower = ");
  Serial.print(RMSPower);
  Serial.println("W");

  Serial.print("Total Units = ");
  Serial.print(kilos);
  Serial.println("kWh");
  /*********************************************************************/
  Serial.println("here");
  currentMillis = millis();
  if (currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    //lastMsg = now;
    // Serial.println(value);
    sprintf (msg, "%f", kilos);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("state", msg);
    delay(2000);
  }

  // kilos = 6.9;
}

rsssrinivas
Posts: 3
Joined: Sat Aug 03, 2019 8:14 am

Re: ESP32 can't resolve DNS

Postby rsssrinivas » Sat Aug 03, 2019 10:51 am

Did you solve your issue?

Who is online

Users browsing this forum: No registered users and 24 guests