1

Here is the code which writes user input into enduser_custom_parameters.json file. This is part of enduser_setup module of esp8266/nodeMCU.

static int enduser_setup_write_file_with_extra_configuration_data(
char * configuration_string, int configuration_length
)
{
  int p_file = 0;
  lua_State *L = NULL;

  ENDUSER_SETUP_DEBUG("enduser: opening enduser_custom_parameters.json for write");

  // setup the file output
  p_file = vfs_open("enduser_custom_parameters.json", "w");
  if (p_file == 0)
  {
    ENDUSER_SETUP_DEBUG("Can't write to file!");
    return 1;
  }

  /* Not certain what this does */
  L = lua_getstate();

  if (L != NULL) 
  {
    lua_pushlstring(L, configuration_string, configuration_length);
  }

  vfs_write(p_file, configuration_string, configuration_length);
  vfs_close(p_file);
  return 0;
}

How should I modify this code to save the data into separate file each time? (I am modifying the module to act as Captive Portal to collect data from different users) I think I can use GUID, current date/time or user's MAC (ideal option) as filename. But have no idea how to do it with C.

LA_
  • 19,823
  • 58
  • 172
  • 308

3 Answers3

1

First, get the mac address. station_info structure contains the MAC address of client, but you need to pass it to this function to be able to use it, here I use the AP's mac for demonstration purposes:

uint8_t mac[6];
wifi_get_macaddr(SOFTAP_IF, mac);

and now create a filename that cotains this address:

char filename[64];
sprintf(filename, "enduser_custom_parameters_%02x%02x%02x%02x%02x%02x.json", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

and then open the file:

p_file = vfs_open(filename, "w");
gabonator
  • 391
  • 1
  • 9
  • Could you explain how that makes two filenames unique/different, if called twice from the same machine/MAC? – Yunnosch Apr 24 '18 at 18:14
  • @Yunnosch thank you, fixed the code. Yes, it took me some time to understand that this wont help. But at least it shows how to generate filename combined with mac address. SSIDs/MAC addresses could be obtained from ESP8266 with [wifi_softap_get_station_info function](https://stackoverflow.com/questions/42593385/get-mac-address-of-client-connected-with-esp8266) so now it needs a bit of research to find out how to get the right address to this function – gabonator Apr 24 '18 at 18:19
  • I see, answering one aspect of a question, while leaving the rest open is an accepted part of the answering process at StackOverflow. But I recommend to clearly state that this is your intention. – Yunnosch Apr 24 '18 at 18:21
  • Thanks. Here is the code, where `wifi_softap_get_station_info` is used - https://github.com/Jcrash29/nodemcu-firmware/blob/master/app/modules/wifi.c#L1673. But it is not clear how to get MAC of particular station. – LA_ Apr 25 '18 at 19:46
0

There are libraries for GUIDs/UUIDs (e.g. libuuid). The risk for a collission is extremely low with these. If you want to save all the data locally, you can further ensure there's no collission by using OS specific methods of creating the file, for example on a POSIX system, you could use

int fd = open(filename, O_CREAT|O_EXCL, S_IRWXU);

which would only succeed if it indeed creates a new file. On success, get a standard C FILE * from the descriptor with fdopen(). On error, just create a new UUID, incorporate it in the filename, and try again.

-1

prepend or Append the unix time stamp to the file name

likeabbas
  • 115
  • 1
  • 10
  • @Scriptim Your edit looks like a complete rewrite, to the point of not being the original answer at all anymore. While this really greatly improves the answer, it is beyond what an edit to another persons post should do. Make it your own answer, it will fare much better than this original one. – Yunnosch Apr 24 '18 at 18:20
  • The device doesn't know the current time (it is not connected to the internet). Probably, something like time from the device start could help (`system_get_time()`). – LA_ Apr 27 '18 at 18:43