Arduino Forum

Using Arduino => Programming Questions => Topic started by: Markov on Dec 19, 2017, 01:43 pm

Title: Is it possible to convert MAC address to a string?
Post by: Markov on Dec 19, 2017, 01:43 pm
Like the title says.

What my code does: it looks up temperature from a sensor and displays it on my webserver, also every hour it sends the temperature to the database.
Pretty simple. But I would like to send my sensor's MAC address as well. I don't care if it's format is XX:XX:XX:XX:XX:XX:XX:XX or like below, I'd just like it to send it.
PS! I didn't copy-paste all of the code. Don't yell at me for asking such a simple question, I'm still a beginner  :smiley-confuse:

Code: [Select]

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
char temperatureCString[6];
DeviceAddress tempSensor = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX};

const char *ssid = "XXXXXX";
const char *password = "XXXXXX";

long interval = 3600000;
long previousMillis = 0;

WiFiClient client;
MySQL_Connection conn((Client *)&client);

char INSERT_SQL[] = "USE temperature; INSERT INTO temperature(temperature, time, date, MAC) VALUES (%s, CURRENT_TIME, CURRENT_DATE, ??)";  //Have to change ??
char query[128];


ESP8266WebServer server ( 80 );

IPAddress server_addr(XXX.XXX.XXX.XXX);   
char user2[] = "XXXX";                                       
char password2[] = "XXXXX";                             

void setup ( void ) {
  DS18B20.begin();
Serial.begin ( 115200 );
WiFi.begin ( ssid, password );
Serial.println ( "" );

// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
  Serial.println ( "" );
  Serial.print ( "Connected to " );
  Serial.println ( ssid );
  Serial.print ( "IP address: " );
  Serial.println ( WiFi.localIP() );
 
  Serial.println("Connecting to database");

   while (conn.connect(server_addr, 3306, user2, password2) != true) { //Connect to database
     delay(200);
     Serial.print ( "." );
   }

  Serial.println("");
  Serial.println("Connected to SQL Server!");   

if ( MDNS.begin ( "esp8266" ) ) {
Serial.println ( "MDNS responder started" );
}
}

void loop ( void ) {
server.handleClient();
  long currentMillis = millis();
  if (currentMillis - previousMillis > interval) { //Timer
      while (conn.connect(server_addr, 3306, user2, password2) != true) {
        delay(200);
      }
      sprintf(query, INSERT_SQL, temperatureCString, ??); // Have to change ??
      MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
      cur_mem->execute(query);
      delete cur_mem;                 
      previousMillis = currentMillis;
  }
  getTemperature();
Title: Re: Is it possible to convert MAC address to a string?
Post by: horace on Dec 19, 2017, 01:54 pm
if you have a look at
https://www.arduino.cc/en/Reference/WiFi

there is a method WiFi.macAddress()
https://www.arduino.cc/en/Reference/WiFiMACAddress
Title: Re: Is it possible to convert MAC address to a string?
Post by: DKWatson on Dec 19, 2017, 01:56 pm
What format are they in now?

Use itoa or ltoa to covert numbers into an ASCII char array.

You can concatenate arrays or on a character by character basis append them to a String.

If you don't mind unsolicited advice, stick to the character array, avoid String unless you absolutely need it.
Title: Re: Is it possible to convert MAC address to a string?
Post by: PaulS on Dec 19, 2017, 02:12 pm
Quote
But I would like to send my sensor's MAC address as well.
Sensors do NOT have MAC addresses.
Title: Re: Is it possible to convert MAC address to a string?
Post by: Markov on Dec 19, 2017, 02:36 pm
I don't need to print MAC address, I need to send it to the database.
if you have a look at
https://www.arduino.cc/en/Reference/WiFi

there is a method WiFi.macAddress()
https://www.arduino.cc/en/Reference/WiFiMACAddress

I can't convert with itoa, because: invalid conversion from 'char*' to 'int' [-fpermissive]


What format are they in now?

Use itoa or ltoa to covert numbers into an ASCII char array.

You can concatenate arrays or on a character by character basis append them to a String.

If you don't mind unsolicited advice, stick to the character array, avoid String unless you absolutely need it.
Title: Re: Is it possible to convert MAC address to a string?
Post by: DKWatson on Dec 19, 2017, 02:56 pm
So that says that they're already ASCII characters. Are they character arrays or strings?

If arrays, concat.

If strings, build a bigger string by appending all the little bits.

Ultimately, for serial transmission the data goes out byte by byte so character arrays are always better.
Title: Re: Is it possible to convert MAC address to a string?
Post by: horace on Dec 19, 2017, 03:25 pm
are you looking to do something along the lines of this code which scans for DS18S20 devices and converts the device byte addr[8] to a char array
Code: [Select]
// DS18S20 Temperature chip - scan for address
#include <OneWire.h>
OneWire  ds(2);  // on pin 2

void setup(void) {
  Serial.begin(115200);
  delay(1000);
  ds.reset();
  byte addr[8];
  while(ds.search(addr)) {
    char address[30]={0};
    Serial.print("R=");
    for(int i = 0; i < 8; i++) {
      Serial.print(addr[i], HEX);
      // address next part of address
      itoa(addr[i], &address[strlen(address)], 16);
      if(i<7)address[strlen(address)]=':';
      Serial.print(" ");
     }
    Serial.print("\naddress ");   // print address
    Serial.println(address);
    }
  }
void loop(void) {
 }

a run gives
Code: [Select]
R=28 FF FC 2A 85 16 5 8A
address 28:ff:fc:2a:85:16:5:8a