Go Down

Topic: pubsubclient communication (Read 233 times) previous topic - next topic

rajesh1129

how can i publish RSSI() and SSID() values by using pubsubclient.

 float n = WiFi.scanNetworks();
  Serial.printf("%d network(s) found\n", n);  //Scan start ... 5 network(s) found
  if (n == 0) {
    Serial.println("no wifi`s are connected");
  }
  else {
    for (float i = 0; i < n; i++)
    {
      Serial.printf("%d: %s, Ch:%d (%ddBm)\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i));  //1: Tech_D005107, Ch:6 (-72dBm)

      mqttclient.publish("ssid",WiFi.SSID(i).c_str());
      mqttclient.publish("rssi",WiFi.RSSI(i));


//i need to rewrite the above two lines
    }

i am getting error friends can you help me

pert


rajesh1129







C:\Users\extrcha\Documents\Arduino\client_tag\client_tag.ino: In function 'void ScanAndConnect()':

client_tag:104:43: error: no matching function for call to 'PubSubClient::publish(int [50], int32_t)'

       mqttclient.publish(msg1,WiFi.RSSI(i));

                                           ^

C:\Users\extrcha\Documents\Arduino\client_tag\client_tag.ino:104:43: note: candidates are:

In file included from C:\Users\extrcha\Documents\Arduino\client_tag\client_tag.ino:3:0:

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:141:12: note: boolean PubSubClient::publish(const char*, const char*)

    boolean publish(const char* topic, const char* payload);

            ^

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:141:12: note:   no known conversion for argument 1 from 'int [50]' to 'const char*'

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:142:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)

    boolean publish(const char* topic, const char* payload, boolean retained);

            ^

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:142:12: note:   candidate expects 3 arguments, 2 provided

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:143:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)

    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);

            ^

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:143:12: note:   candidate expects 3 arguments, 2 provided

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:144:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)

    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);


            ^

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:144:12: note:   candidate expects 4 arguments, 2 provided

exit status 1
no matching function for call to 'PubSubClient::publish(int [50], int32_t)'

pert

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:
  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.


This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

rajesh1129

#4
Apr 08, 2019, 04:48 am Last Edit: Apr 08, 2019, 04:52 am by rajesh1129
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>

const char* msg;
const char* msg1;

#define ssid "ssid"
#define password "pass"
#define mqtt_server "broker_ip"

#define client_name "NodeMCU 8CH"

const char* intopic = "home/esp";
const char* outtopic = "home/msg";

const char* outtopic1 = "home/msg1";


WiFiClient espClient;
PubSubClient mqttclient(espClient);

int status = WL_IDLE_STATUS;
//WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin()
//is called and remains active until the number of attempts expires
//(resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED);

void setup(void)
{
  // Start Serial
  Serial.begin(115200);//bits per secound
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");

  // Connect to WiFi
  status = WiFi.begin(ssid, password);
  // WiFi.mode(WIFI_STA);
  if (status != WL_CONNECTED) //WL_CONNECTED: assigned when connected to a WiFi network;
  {
    delay(500);
    Serial.print(".");
  }
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
  mqttclient.setServer(mqtt_server, 1883);
  mqttclient.setCallback(callback);
}

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

void loop() {
  // put your main code here, to run repeatedly:
  if (!mqttclient.connected()) {
    reconnect();
  }
  mqttclient.loop();

  delay(100);

  float n = WiFi.scanNetworks();
  Serial.printf("%d network(s) found\n", n);  //Scan start ... 5 network(s) found

  if (n == 0) {
    Serial.println("no wifi`s are connected");
  }
  else {
    int a[20];
    float array1[20];
    int j = 0;
    for (float i = 0; i < n; i++)
    {
      Serial.printf("%d: %d, Ch:%d (%ddBm)\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i));  //1: Tech_D005107, Ch:6 (-72dBm)
    }

    for (int i = 0; i < n; i++) {
      a[j] = WiFi.RSSI(i);
      j++;
    }

    for (j = 0; j < n; j++) {
      int d = j;
      while ( d > 0 && array1[d - 1] < array1[d]) {
        //swapping
        int  t      = array1[d];
        array1[d]   = array1[d - 1];
        array1[d - 1] = t;
        d--;
      }
    }

    //top 3
    Serial.println("Sorted order:\n");
    for (j = 0; j < 2; j++) {
      Serial.println(array1[j]);//top three rssi values
    }

    //we need to send rssi,ssid,tagid at a time...!
    for (float i = 0; i < n; i++)
    {
      Serial.printf("%d: %d, Ch:%d (%ddBm)\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i));  //1: Tech_D005107, Ch:6 (-72dBm)
      Serial.printf("sending messages:");
      msg = WiFi.SSID(i).c_str();
      Serial.printf("sending message");
      Serial.println(msg);
      mqttclient.publish(outtopic, msg);

      msg1 = WiFi.RSSI(i);
      char msg2 = msg1;
      Serial.printf("sending message");
      Serial.println(msg1);
      mqttclient.publish(outtopic1, msg2);

    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!mqttclient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttclient.connect(client_name)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //mqttclient.publish(msgTopic, "MQTT is connected");
      // ... and resubscribe
      mqttclient.subscribe(intopic);
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(mqttclient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}



errors:
C:\Users\extrcha\Desktop\project_demo\tag4\tag4.ino: In function 'void loop()':

tag4:121:25: error: invalid conversion from 'int32_t {aka int}' to 'const char*' [-fpermissive]

       msg1 = WiFi.RSSI(i);

                         ^

tag4:122:19: error: invalid conversion from 'const char*' to 'char' [-fpermissive]

       char msg2 = msg1;

                   ^

tag4:125:41: error: invalid conversion from 'char' to 'const char*' [-fpermissive]

       mqttclient.publish(outtopic1, msg2);

                                         ^

In file included from C:\Users\extrcha\Desktop\project_demo\tag4\tag4.ino:3:0:

C:\Users\extrcha\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:141:12: error:   initializing argument 2 of 'boolean PubSubClient::publish(const char*, const char*)' [-fpermissive]

    boolean publish(const char* topic, const char* payload);

            ^

exit status 1
invalid conversion from 'int32_t {aka int}' to 'const char*' [-fpermissive]

the above errors

pert

#5
Apr 08, 2019, 11:37 am Last Edit: Apr 08, 2019, 11:38 am by pert
C:\Users\extrcha\Desktop\project_demo\tag4\tag4.ino: In function 'void loop()':

tag4:121:25: error: invalid conversion from 'int32_t {aka int}' to 'const char*' [-fpermissive]

       msg1 = WiFi.RSSI(i);
The error is fairly self explanatory. WiFi.RSSI(i) returns an int, but for some reason you are trying to put it in a char*. You need to convert the int to a string before you can do that. For that purpose, you might find itoa() useful:
http://www.cplusplus.com/reference/cstdlib/itoa/

You also might like to study the reference page for the RSSI() function (the ESP8266WiFi library follows the API of the official Arduino WiFi library):
https://www.arduino.cc/en/Reference/WiFiRSSI

tag4:122:19: error: invalid conversion from 'const char*' to 'char' [-fpermissive]

       char msg2 = msg1;
Now this code is just mystifying. I don't know why you would do something like that. Best thing to do is to remove it, since it serves no purpose.

rajesh1129


pert

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

Go Up