Hi all. I am creating a wireless sensor device. The idea is that its a generic sketch that makes it unique based on the mac address of the device. Ive used a previous project that is successfully making use of the pubsubclient library to sent data from a BME280 sensor to a local MQTT server. So really I know that this does already work, but getting the mac address is where my lack of knowledge is tripping me up.
According to the info here
I will be receiving a byte array.
Id like to concat my other variables with this. in order to get something like clientName + macAddress (bob_00:11:22:33:44:55)
But char, char*, String etc. This is where I dont know enough. Im using char* mostly for the other items as you can see, and it works, but Ill be honest, I dont really know why Im using that over char[] other than it works, I havent tried changing it and I dont know if there is a benefit this way)
Other than the I assume I could have a char[] of the correct size and fill it with all the two char* items maybe?
Ive created a function using strcpy as you can see which works fine assuming everything passed in is the right type, which the mac address isnt.
Ive tried a few variations of the mac data type but I think I just to to convert it correctly. I think this is simple but beyond my knowledge.
I should also mention, Im only compiling this at the minute and havent tried to load it onto any device yet.
Any advice appreciated
Thanks
/*to do list
set deepsleep time correctly
ensure mac address is used to keep IDs unique
*/
/* Board: wemos D1 mini ( LOLIN D1 R2 & Mini)
Upload: 921600
CPU frequency:80Mhz
Flash 4MB
Debug port: disabled
Debug Level: none
lwlp variant: v2 lower memory
vtables: flash
exception: legacy
erase flash: sketch and wifi settings
ssl: all ciphers
*/
//pubsubclient.h file edited, max packet size from 128 to 256
#include <BME280I2C.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <ArduinoJson.h>
const char* wifi_ssid = "ssid";
const char* wifi_password = "password";
const char* mqtt_server = "192.168.0.100";
const char* mqtt_user = "esp";
const char* mqtt_password = "password";
char* mqtt_publish_topic = "home/temperature/";
char* clientName = "espSensor_";
byte macAdd[6];
char* clientNameMac;
char* topicNameMac;
float temp(NAN), hum(NAN), pres(NAN);
float tempCalibration = 0;
unsigned long DEEP_SLEEP_TIME = 50000; //set this value after testing
int PORT_SPEED = 9600;
BME280I2C::Settings settings(
BME280::OSR_X1,
BME280::OSR_X1,
BME280::OSR_X1,
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
// BME280::Filter_Off,
BME280::Filter_4,
BME280::SpiEnable_False,
0x76 // I2C address
);
BME280I2C bme(settings);
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, espClient);
void setup() {
Serial.begin(PORT_SPEED);
Serial.println("");
Serial.print("Serial port opened @ ");
Serial.println(PORT_SPEED);
//SDA = D2 GPIO4, SCL = D1 GPIO5
Wire.begin();
Serial.println("i2c started");
bool state = bme.begin();
delay(1000);
int BME_retries = 0;
while (!state && BME_retries < 5) {
Serial.println("unable to connect to BME1 sensor at 0x76");
delay(5000);
BME_retries++;
}
if (WiFi.status() != WL_CONNECTED) {
setup_wifi();
}
Serial.println("connecting to mqtt server");
client.setServer(mqtt_server, 1883);
delay(10);
macAdd = WiFi.macAddress();
clientNameMac = concatFunction(clientName, macAdd, clientNameMac);
topicNameMac = concatFunction(mqtt_publish_topic, macAdd, topicNameMac);
if (client.connect(clientNameMac, mqtt_user, mqtt_password)) {
Serial.print("connected to MQTT server");
//prepare mqtt payload
Serial.println("");
Serial.println("preparing MQTT payload...");
Serial.println("");
const size_t capacity = JSON_OBJECT_SIZE(3);
DynamicJsonDocument doc(capacity);
doc["temperature"] = temp;
doc["humidity"] = hum;
doc["pressure"] = pres;
char buffer[capacity];
serializeJson(doc, buffer, sizeof(buffer));
sendValues(buffer, topicNameMac);
} else {
Serial.println("unable to connect to MQTT server");
ESP.deepSleep(DEEP_SLEEP_TIME);
}
delay(1000); //requires some time for MQTT publish
ESP.deepSleep(DEEP_SLEEP_TIME);
}
void setup_wifi() {
int wifi_retry_count = 0;
Serial.println("setup wifi");
WiFi.begin(wifi_ssid, wifi_password);
delay(10);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
wifi_retry_count++;
if (wifi_retry_count > 60) {
Serial.println("unable to connect to WIFI...!!!");
ESP.deepSleep(DEEP_SLEEP_TIME);
}
}
Serial.println("connected to WIFI");
}
char* concatFunction(char* firstChar, char* secondChar, char* resultChar) {
strcpy(resultChar, firstChar);
strcat(resultChar, secondChar);
return resultChar;
}
float getValues() {
Serial.println("getting sensor values");
//get values from sensor
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pres, temp, hum, tempUnit, presUnit);
temp = temp + tempCalibration;
pres = pres / 100;
Serial.print("temp ");
Serial.print(temp);
Serial.print(" - ");
Serial.print("pressure ");
Serial.print(pres);
Serial.print(" - ");
Serial.print("humidity ");
Serial.println(hum);
}
void sendValues(char* str, char* topic) {
if (WiFi.status() == WL_CONNECTED && client.connected()) {
if (client.publish(topic, str)) {
Serial.println("publish succsessful");
} else {
Serial.println("connected to MQTT but publish was unsuccsessful");
}
delay(10);
} else {
Serial.println("not connected to wifi or MQTT server");
}
}
void loop() {
}