Open Samuraijack22 opened 7 years ago
I am in no way connected to this project, I just use the library quite a bit... so Not sure if this will work for you: `void setup() { Serial.begin(9600); Serial.println(""); Serial.println(""); Serial.println("Check ID in:"); Serial.println("https://www.wemos.cc/verify_products"); Serial.printf("Chip ID = %08X\n", ESP.getChipId()); Serial.println(ESP.getChipId()); Serial.println(""); Serial.println(""); uint32_t chipid=ESP.getChipId();
char clientid[25];
snprintf(clientid,25,"WIFI-Display-%08X",chipid);
Serial.print("Client ID: ");
Serial.println(clientid);
delay(5000); } `
It's how I create unique Client IDs and seems to work pretty well for me. You might look up snprintf for more information about it.
Hi kd8bxp!
Thanks for your reply. I'll have a look and see if it works for me :)
@kd8bxp
Old post but worked for me, thanks!
Hi everyone!
Big disclaimer, this is my first post and I'm a complete noob to programming and any guidance, tips and wider/deeper explanations will be much appreciated... :)
I'm busy with a small IoT program wherein I'm planning to build many, many little ESP-12E data loggers that publishes to a MQTT broker. To differentiate between each data logger, I've been trying to use the ESP.getChipId() and ESP.getFlashChipId() functions combined to create a unique ID with little success... Something like this: "String deviceID = String( ESP.getChipId()) + "-" + String( ESP.getFlashChipId() ); "
I have successfully connected to the MQTT server and published data to it, but only using manually entered generic feed names like "vcc3".
Basically I'm trying to create an automatic & unique feed name along the lines of: "xxx/feeds/" + a unique ID precursor (deviceID) + a variable, like vcc or tempC, etc.
This way each data loggers' feeds will be grouped together (unique precursor), for example: _"xxx/feeds/1941238-1458400_BatV" "xxx/feeds/1941238-1458400tempC" ...
I have successfully printed out the exact topic name that I want to create & publish to the serial monitor: _"PubSub Topic String: xxx/feeds/1941238-1458400BatV "
The problem comes in when I try to use this unique name as a feed name for a new topic....
This piece of coding works fine: _" if (client.connected() && prevValue != vcc8) { Serial.println("Publish vcc8"); String hi = (String)vcc8; hi.toCharArray(valueStr, 5); client.publish(USERNAME PREAMBLE T_vcc8, valueStr); prevValue = vcc8; delay(1000); "_
And this piece works fine for the Serial.println part: _"Serial.print("deviceID: "); Serial.println(deviceID);
Serial.print("PubSub Topic String: "); Serial.println((USERNAME PREAMBLE) + (deviceID) + T_BatV);"_
But when I stick the unique name from the Serial.println into the client.publish function, then everything bombs out........ _" client.publish((USERNAME PREAMBLE) + (deviceID) + T_BatV, valueStr); "_
Before I removed all my personal details, I checked that the code compiles, publishes to the MQTT broker and serial prints the desired code with the manually entered variable name...
The serial print out: _"Connecting to xxx ......... WiFi connected IP address: xxx.xxx.x.xxx Mode: STA PHY mode: N Channel: 6 AP id: 0 Status: 5 Auto connect: 1 SSID (12): xxx Passphrase (8): xxx BSSID set: 0
deviceID: 1941238-1458400 PubSub Topic String: xxx/feeds/1941238-1458400_BatV
Attempting MQTT connection... connected Measure Publish vcc8 Publish Status Delay for 1 minute"_
My code (I tried uploading a file but it didn't want to for some reason":
"
include
include
include
include
include
//#include
/*****GLOBAL SETUP**/ // MQTT & VCC
// Gainsborough flat WiFi
define ssid "xxx"
define password "xxx"
const int LEDBLUE = 2; ADC_MODE(ADC_VCC); //vcc read-mode
//Unique device identification:
String deviceID = String( ESP.getChipId()) + "-" + String( ESP.getFlashChipId() ); // IoT thing device ID - unique device id in our project
define SERVER "xxx"
const int SERVERPORT = 1883; // Port: 1883 or 8883 (for SSL encrypted connection)
define MQTT_USERNAME "xxx"
define MQTT_KEY "xxx"
define USERNAME "xxx/"
define PREAMBLE "feeds/"
define T_BatV "_BatV"
define T_vcc8 "vcc8"
define T_CLIENTSTATUS "_clientStatus"
define T_CLIENTSTATUS "clientStatus"
define T_COMMAND "command"
define VCC_ADJ 1.35134075711016 // LD33CV Low Voltage Drop-out adjustment constant
unsigned long entry; uint8_t clientStatus, prevClientStatus = 99; char valueStr[5]; float vcc8, prevValue = -1;
WiFiClient WiFiClient; // create MQTT object PubSubClient client(WiFiClient);
/**SETUP/ void setup() {
// MQTT & VCC setup; //pinMode(LEDBLUE, OUTPUT); Serial.begin(115200); delay(100); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); WiFi.printDiag(Serial);
client.setServer(SERVER, SERVERPORT); client.setCallback(callback);
/*Test Print***/ //Test ESP.chipID() funciton: Serial.println(); Serial.println();
Serial.print("deviceID: "); Serial.println(deviceID);
Serial.print("PubSub Topic String: "); Serial.println((USERNAME PREAMBLE) + (deviceID) + T_BatV);
Serial.println(); /****/
}
/**LOOP/ void loop() {
yield();
// MQTT & VCC loop; //Serial.println("loop");
if (!client.connected()) { Serial.println("Attempting MQTT connection..."); // Attempt to connect if (client.connect("", MQTT_USERNAME, MQTT_KEY)) { Serial.println("connected"); // ... and resubscribe client.subscribe(USERNAME PREAMBLE T_COMMAND, 1); client.subscribe(USERNAME PREAMBLE "test", 1); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 2 seconds before retrying delay(2000); } }
if (millis() - entry > 1200) { Serial.println("Measure"); entry = millis(); vcc8 = (float)ESP.getVcc() / 1000 * VCC_ADJ; delay(1000); }
if (client.connected() && prevValue != vcc8) { Serial.println("Publish vcc8"); String hi = (String)vcc8; hi.toCharArray(valueStr, 5); client.publish(USERNAME PREAMBLE T_vcc8, valueStr); prevValue = vcc8; delay(1000); }
/**Test Publish****/ //Test topic with deviceID in if (client.connected() && prevValue != vcc8) { Serial.println("Publish test deviceID topic vcc8"); String hi = (String)vcc8; hi.toCharArray(valueStr, 5); client.publish(USERNAME PREAMBLE T_BatV, valueStr); //client.publish((USERNAME PREAMBLE) + (deviceID) + T_BatV, valueStr); prevValue = vcc8; delay(1000);
}
if (client.connected() && prevClientStatus != clientStatus ) { Serial.println("Publish Status");
} client.loop();
WiFi.forceSleepBegin(); // send wifi to sleep to reduce power consumption
Serial.println("Delay for 1 minute"); delay(1*60000); //Master delay timer
//Wake up WiFi WiFi.forceSleepWake();
//Reconnect WiFi after break // if serial is not initialized all following calls to serial end dead.
ifdef SERIAL_DEBUG
Serial.begin(115200); delay(10); Serial.println(); Serial.println(); Serial.println(F("Started from delay"));
endif
Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); WiFi.printDiag(Serial);
client.setServer(SERVER, SERVERPORT); client.setCallback(callback);
}
//----------------------------------------------------------------------
void callback(char topic, byte data, unsigned int length) { // handle message arrived {
Serial.print(topic); Serial.print(": "); for (int i = 0; i < length; i++) { Serial.print((char)data[i]); } Serial.println(); if (data[1] == 'F') { clientStatus = 0; digitalWrite(LEDBLUE, HIGH); } else { clientStatus = 1; digitalWrite(LEDBLUE, LOW); } }
"
Using: Arduino IDE 1.8.4 ESP-12E esp8266 by ESP community v2.3.0
I'm pretty sure it is something unbelievably stupid, but I can't seem to find it... What am I doing wrong?
Thank you :) ESPgetChipID_PubSub_question_anon.zip