These are chat archives for esp8266/Arduino

1st
May 2017
Steve Nelson
@zenmanenergy
May 01 2017 12:54
@riataman did you make any progress?
riataman
@riataman
May 01 2017 14:02
I did make software spi work
but is not fast enought for continous recording
its good enought for about 20seconds of recording
at 44100 sample rate
Steve Nelson
@zenmanenergy
May 01 2017 14:39
can someone help me figure out how to write a struct to a binary file? binary files have always baffled me. Here's a short gist of what I'm trying to do. https://gist.github.com/zenmanenergy/8dba0f792370f39316f72d559e366af6#file-writing-struct-to-file-L25
It doesn't like line 25 as the print() method doesn't know what the hell a "Person" is. So how do I convert the "Person" to something print() understands?
Steve Nelson
@zenmanenergy
May 01 2017 14:50
do I need to use the write() method instead of print()?
Steve Nelson
@zenmanenergy
May 01 2017 15:11

hmmm, made a little progress... i think this is writing it! woowoo!

char buffer[sizeof(me)];
  memcpy(buffer, &me, sizeof(Person));
  File writefile = SPIFFS.open(fileName,"w");
  writefile.write((uint8_t *)buffer, sizeof(me));
  writefile.close();

If there is a better way to do it that memcpy(), I'd love to hear how. Now I need to figure out how to go the other direction and read the file back into a struct.

CodingCreature
@CodingCreate101
May 01 2017 15:11
print is built on top of write.. i don't think it would change anything
Steve Nelson
@zenmanenergy
May 01 2017 15:11
ha, funny timing
CodingCreature
@CodingCreate101
May 01 2017 15:11
It did??
Lol
That's great
Steve Nelson
@zenmanenergy
May 01 2017 15:13
is memcpy() a good way to do this? or is there a better way?
CodingCreature
@CodingCreate101
May 01 2017 15:14
Uhh, for now, I can't think of anything better :|
Steve Nelson
@zenmanenergy
May 01 2017 15:18
@zenmanenergy
What is seriously embarrassing is that after googling for about 15 minutes, I came across a github repository that has almost exactly what I need (except it's for reading/writing text)... guess who posted it 2 years ago? me! sigh https://github.com/zenmanenergy/ESP8266-Arduino-Examples/blob/master/helloworld_read_write_text_file/file.ino
Steve Nelson
@zenmanenergy
May 01 2017 15:36

dang it... i'm SO close!

char oldMeBuffer[fileSize];
  readfile.read((uint8_t *)oldMeBuffer, fileSize);
  readfile.close();
  Serial.println(sizeof(oldMeBuffer));
  Person oldMe;
  memcpy(&oldMe, oldMeBuffer, sizeof(oldMeBuffer));

it crashes on the last line. but it is displaying the correct sizeof() value. what am i doing wrong witht he memcpy()?

oops left out some code.
File readfile = SPIFFS.open(fileName, "r");
  int fileSize = readfile.size();
  Serial.println("Please be 36! please be 36!");
  Serial.println(fileSize);
  char oldMeBuffer[fileSize];
  readfile.read((uint8_t *)oldMeBuffer, fileSize);
  readfile.close();
  Serial.println(sizeof(oldMeBuffer));
  Person oldMe;
  memcpy(&oldMe, &oldMeBuffer, sizeof(oldMeBuffer));
riataman
@riataman
May 01 2017 15:51
you can't do that to objcets
C++ objects aren't simply formed by their contents bytes
you could do that if Person was a simple struct
C struct, not C++
Steve Nelson
@zenmanenergy
May 01 2017 15:55
Is there a way to do this with a c++ struct? I suspect I'll ultimately have things like vectors and maps in my structs. I would guess those won't work in a c struct
riataman
@riataman
May 01 2017 15:56
no
that would never work
specially if you have maps and vectors
since those are pointers
Steve Nelson
@zenmanenergy
May 01 2017 15:56
darn.
riataman
@riataman
May 01 2017 15:57
I mean you can store the data
but have to write code to serialize and deserialize it
Steve Nelson
@zenmanenergy
May 01 2017 15:57
do i have do that for each item in the struct? or can I serialize/deserialize the whole struct?
i was doing this before with arduinoJson, which was working, but it was quickly becoming a pain in the ass to do it for every item in the structs.
riataman
@riataman
May 01 2017 15:59
well, there are strategies you can follow
I guess google for "serializing a C++ object to disk"
and see what comes up
Steve Nelson
@zenmanenergy
May 01 2017 16:00
in a nutshell, I'm trying to make it so that if power is lost in the nodemcu, the objects are pulled from SPIFFS and recreated.
sweet. i'll look that up
that's probably a good place to start to understand what is involved
Steve Nelson
@zenmanenergy
May 01 2017 16:01
that sounds perfect. i'll give that a read, thanks!
brutzler
@brutzler
May 01 2017 16:53

Hi, can somebody help me with a little C++-problem?
As you know a DS18B20 address is defined like this:

  DeviceAddress Sensor  = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

How to make a reference to this address for a function???

void getTemp( float &_temp, ......... &_sensor)
{
   _temp = sensors.getTempC(_sensor); 
}
riataman
@riataman
May 01 2017 16:54
void getTemp( float &_temp, DeviceAddress &_sensor)
and you call as getTemp(temp, Sensor);
brutzler
@brutzler
May 01 2017 16:56
"DeviceAddress"......OMG, hahaha. Was too easy to see it without help.
Thx a lot
Michael Miller
@Makuna
May 01 2017 17:11
@riataman You are incorrect. Classes and Structs are only different in a few minor ways; and their contents are formed in the same way.
riataman
@riataman
May 01 2017 17:11
I'm talking about plain C structs vs C++ struct/classes
Michael Miller
@Makuna
May 01 2017 17:11
Or do you mean, when the contents are complex objects, they are not stored within it as somple bytes?
riataman
@riataman
May 01 2017 17:11
they are not the same thing
C++ can generate plain C structs if you stick to some rules
but its not probably what @zenmanenergy wnats
Michael Miller
@Makuna
May 01 2017 17:14
class MyClass {
  int v;
  int a;
}

struct MyStruct {
  int v;
  int a;
}
if you take an pointer to an instance to each of these and use the sizeof(); they will be the same.
Steve Nelson
@zenmanenergy
May 01 2017 17:14
I was looking for a simple way to take a struct and store it in a binary file, then upon reboot, read the binary file and dump it back into the struct. I seem to be able to store it in the file fairly easily, but not dump it back. My solution, which works, is to create an json packet, store the text in the file, then recreate the object by parsing the json. it works, but I was wondering if there was a binary way to acheive the same thing
riataman
@riataman
May 01 2017 17:15
@Makuna yeah, those generate what I would call plain "C" struct
for serealization you only want structs
Michael Miller
@Makuna
May 01 2017 17:16
You keep using that term, it has no meaning.
riataman
@riataman
May 01 2017 17:16
and then you want the pack hint on them
It does have meaning
stick a virtual method in any of those
now you have a virtual table
and its not a plain struct anymore
you can't just dump the object to disk anymore
Michael Miller
@Makuna
May 01 2017 17:17
Yes you can?!
(hold a second for me to type an example)
Steve Nelson
@zenmanenergy
May 01 2017 17:19
@Makuna did you see the gist i posted earlier? That's what I was starting with. i got as far as reading the data back from the file, then it crashed when I tried to call memcpy().
riataman
@riataman
May 01 2017 17:20
@Makuna read about POD types in C++
Michael Miller
@Makuna
May 01 2017 17:20
class myClass {
  int a;
  int b;
  public:
  virtual int GiveMeA() {
    return a;
  }
};

myClass anInstance;

SomeApiWriteBytes((byte*)&anInstance, sizeof(anInstance));

SomeApiReadBytes((byte*)&anInstance, sizeof(anInstance));
riataman
@riataman
May 01 2017 17:21
Makuna: now try to do this:
myClass Object2;
memcpy (Object2, anInstance, sizeof(anInstance);
Michael Miller
@Makuna
May 01 2017 17:22
sizeof will not include vtable, and taking the address will point to the front of the member variables.
Steve Nelson
@zenmanenergy
May 01 2017 17:22
oooh! that's exactly what i was trying to do, it kept crashing though
Michael Miller
@Makuna
May 01 2017 17:24
There is no need for the extra memory cache in your example above, simplify it.
Steve Nelson
@zenmanenergy
May 01 2017 17:26
here's my latest version, but it crashes on line 42. what would i do to simplify it?
Michael Miller
@Makuna
May 01 2017 17:26
did you look up what the crash information is telling you?
Steve Nelson
@zenmanenergy
May 01 2017 17:27
whoops. that's the old one. here's the new one
no, i've never known how to do that.
f'ing gitter is not updating it. not sure why.
Michael Miller
@Makuna
May 01 2017 17:28
search, its out there, search for something like "ESP8266 exception meaning"
Steve Nelson
@zenmanenergy
May 01 2017 17:28
cool. i definitely will!
Michael Miller
@Makuna
May 01 2017 17:29
when you reference code from github. select the line by clicking on it, the browser URL will reference the line, so you can just paste it here like
https://gist.github.com/zenmanenergy/8dba0f792370f39316f72d559e366af6#file-writing-struct-to-file-L42
Steve Nelson
@zenmanenergy
May 01 2017 17:31
it's not that... this is the first revision... i was trying to post the second revision of the gist. if you click the link, it'll show you different code than what is here
Michael Miller
@Makuna
May 01 2017 17:31
  Person oldMe;
  readfile.read((uint8_t *)&oldMe, sizeof(oldMe));

  readfile.close();

  Serial.println(sizeof(oldMe));
This is what I meant by simplify
AHHH, the issue is your struct
it contains complex types, so this technique can't be used.
Steve Nelson
@zenmanenergy
May 01 2017 17:32
because they are Strings?
Michael Miller
@Makuna
May 01 2017 17:33
yes
Steve Nelson
@zenmanenergy
May 01 2017 17:33
dang. i think that's what essentially @riataman was saying too.
Michael Miller
@Makuna
May 01 2017 17:33
change those to arrays of chars and it will work.
Steve Nelson
@zenmanenergy
May 01 2017 17:34
yeah, i may... but ultimately I'm going to end up doing stuff with vectors and maps. i guess serializing it through json may still be the simplest way
i'm going to try the chars right now though...
Michael Miller
@Makuna
May 01 2017 17:35
yeah, the other option is to expose a serialize/deserialize method on your struct, that walks on your complex types and writes/reads them.
Steve Nelson
@zenmanenergy
May 01 2017 17:36
is there some library that would do that easily?
Stavros Korokithakis
@skorokithakis
May 01 2017 17:37
Is there a way to get a numeric code for the reason of the last reset?
Michael Miller
@Makuna
May 01 2017 17:37
There is no real way to put that stuff in a library; it has to know the details of all your members and all the types.
Steve Nelson
@zenmanenergy
May 01 2017 17:37
ok.
Michael Miller
@Makuna
May 01 2017 17:38
yeah, have a serial debug attached, it will be spewed, and if not, inside the ESP class there are methods to retrieve the reset reason.
It sounds daunting, but binary serialization and deserialiation isn't hard once you attempt it. As long as you don't have to worry about cross platform compatibility (endianness)
brutzler
@brutzler
May 01 2017 17:40
@skorokithakis
try this:
ESP.getResetReason()
Michael Miller
@Makuna
May 01 2017 17:40
yup
Stavros Korokithakis
@skorokithakis
May 01 2017 17:41
I saw in the source that that returns a character array, doesn't it? I'll try it soon, as I'm not at the computer now
Michael Miller
@Makuna
May 01 2017 17:42
@riataman Sorry, I think I may have misunderstood what you were trying to get across, I thought you meant that its not possible at all, but I stated as long as you don't have complex types it works.
Stavros Korokithakis
@skorokithakis
May 01 2017 17:42
I would like to get resetInfo.reason, to match on
Michael Miller
@Makuna
May 01 2017 17:43
getResetInfoPtr will return the struct and then you can get just the value
Stavros Korokithakis
@skorokithakis
May 01 2017 17:47
Ah, thank you
Steve Nelson
@zenmanenergy
May 01 2017 17:51
well, this is not nearly as exciting as I was hoping for, but it does work when it's simplified to char arrays:
struct Person{
    char personId[10]="1234abc";
    char firstName[10]="Steve";
    char lastName[10]="Nelson";
  };

  Person me;
  Serial.println(me.personId);
  Serial.println(me.firstName);
  Serial.println(me.lastName);

  SPIFFS.begin();
  File writefile = SPIFFS.open("test","w");
  writefile.write((uint8_t *)&me, sizeof(me));
  writefile.close();

  Serial.println("file written!");

  Person oldMe;
  File readfile = SPIFFS.open("test","w");
  readfile.read((uint8_t *)&oldMe, readfile.size());
  readfile.close();

  Serial.println("file read");

  Serial.println(oldMe.personId);
  Serial.println(oldMe.firstName);
  Serial.println(oldMe.lastName);
thanks for the help @Makuna and @riataman
Michael Miller
@Makuna
May 01 2017 17:55
search for C++ Binary Serializing Deserialing to learn more the topic; it can be a deep hole ;-) While Json will provide an excellent cross compatible file format and that can be easily transmitted across the internet, it can also be very fat if you are storing in a limited space (if you are already including the code for other reasons, at least your code size won't get fatter).
Steve Nelson
@zenmanenergy
May 01 2017 17:56
Oh boy...every time i learn something new I seem to dig myself deeper.
have you used Boost on an esp board? is that the direction I should start digging? that seems to come up on the first few pages.
Stavros Korokithakis
@skorokithakis
May 01 2017 18:24
@Makuna err, do you remember, off the top of your head, how i could use getResetInfoPtr?
i'm not sure what type the returned object should h ave
oh never mind, looks like i can just do ESP.getResetInfoPtr()->reason
riataman
@riataman
May 01 2017 19:03
oh why, oh why this doesn't work :(
!""#$%#"$% sd cards
Want to share a SD card with the flash spi bus
but the card never responds
it does work fine in HSPI
even with hw cs
brutzler
@brutzler
May 01 2017 21:26

Is there a chance to expand the Hardware-serial-Rx-buffer?
I found this:

Serial.setRxBufferSize(1024);

but it causes a

'class HardwareSerial' has no member named 'setRxBufferSize'
riataman
@riataman
May 01 2017 21:26
the uart buffer is not in a release
you have go to the git version
brutzler
@brutzler
May 01 2017 21:32
Hmmm... found a post about one year ago. Looks like this was removed already in 1.6.9
And still only in git? Why?
riataman
@riataman
May 01 2017 21:34
what do you REALLY want to do?
why do you need to increase the buffer?
brutzler
@brutzler
May 01 2017 21:36
"Found" a software that makes a serial->telnet bridge. And on bigger RX-packages I loose data.
And to find out why, i thought as first step to increase the RX buffer.
riataman
@riataman
May 01 2017 21:38
I guess install the git version and see what happens
the arduino esp8266 hasn't got a release in quite a bit
espressif probably focusing in esp32
brutzler
@brutzler
May 01 2017 21:46
Think I will read in the data into an own buffer and send after a linefeed....