hello Amazing community!
i was just working with Ada fruits MQTT library and trying to publish requests......now there is name of the publishing object ,i was wondering that is it possible i would take this name of object as a wifimanager parameter ,but that parameter happens to be string.....
this is what the publishing object looks like
oops...i am sorry
what i want is like this
i want to send name of topic to my device via wifimanager....
since the name of topic is not string how will i do this
thats it
thanks for reply anyways
i dont think its a string.....but i really am not sure
for its i got it like this #define s "/feeds/photocell"
is "/feeds/photocell" a string here?
thanks for reply
thanks J-M-L
i got it
but when i try to put string a a parameter into the given object of publish it just dosent work
here it is #define AIO_USERNAME "sabishaw"
String s="/feeds/photocell";
Adafruit_MQTT_Publish photocell =Adafruit_MQTT_Publish(&mqtt,AIO_USERNAME +s);
just wont work
... you want me to upload whole code?
Find the signature for Adafruit_MQTT_Publish() to see what parameter type is expected . If it’s a cString (a char * or const char *) then you need to build it using strcpy() and strcat() into a char buffer
So you have
Code: [Select] #define AIO_USERNAME "sabishaw"
String s="/feeds/photocell";
(why would you use the String class is another discussion...)
So you do either
Code: [Select]
String p = String(AIO_USERNAME) + s;
And then call
Code: [Select]
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt,p.c_str());
exit status 1
expected unqualified-id before string constant
and this behaviour made me to question if it is a string or not, anyways
char buffer[50]; // has to be long enough
strcpy(buffer, AIO_USERNAME);
strcat(buffer, s.c_str());
And then call
Code: [Select]
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt,buffer);
error message
exit status 1
expected constructor, destructor, or type conversion before '(' token
But as both are known you can also just call
Code: [Select]
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt,"sabishaw/feeds/photocell");
since i am just exprimenting with Adafruit_MQTT_Publish for now,so these strings are known..what i want is,sending these strings as parameters of wifimanager,so that i wouldn't have to hardcode the topic..
thanks for your prompting replies
sure
please dont pay attention to other parts..... for now i was just dealing with the parts i discussed here so far
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "sabishaw"
#define AIO_KEY "5858613ad24e14c19cc"
String s="/feeds/photocell";
String p = String(AIO_USERNAME) + s;
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
//#define a AIO_USERNAME s;
strcpy(buffer, AIO_USERNAME);
strcat(buffer, s.c_str());
AIO_USERNAME+=s;
Adafruit_MQTT_Publish photocell =Adafruit_MQTT_Publish(&mqtt,buffer);
void MQTT_connect();
uint32_t x=0;
//define your default values here,if there are different values in config.json, they are overwritten.
char mqtt_server[40];
char mqtt_port[6] = "8080";
char blynk_token[34] = "YOUR_BLYNK_TOKEN";
//flag for saving data
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
//clean FS, for testing
//SPIFFS.format();
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//add all your parameters here
wifiManager.addParameter(&custom_mqtt_server);
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
strcpy(mqtt_server, custom_mqtt_server.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["mqtt_server"] = mqtt_server;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
MQTT_connect();
delay(5000);
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
}
else {
Serial.println(F("OK!"));
}
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}