Thank you a lot for your answer, wvmarle! That helps a lot ![]()
but why would you switch the master/slave roles?
I have been reading deeper on i2c and you are definitely right, in this case it wouldn't be necessary to switch the roles.
The Mega can just stream (no need to locally buffer beyond the I2C buffers) the whole thing to the NodeMCU in one go.
That's a great point! I couldn't find any example with a similar implementation. Could you please show me a pseudo-code about how the thing would be done?
My guess in a rough coding:
#Slave (Arduino Mega)
void setup(){
Wire.begin(8);
Wire.onRequest(streamLastPicture);}
void streamLastPicture(){
File lastPicture = SD.open("lastpicture.jpg");
if (lastPicture){
Wire.write(lastPicture); //Just like that?
}else{
Wire.write(-1);
}
lastPicture.close();
}
#Master (NodeMCU)
File streamedImage;
void Loop(){
if (HttpGetRequest(request)){
httpResponse(request));
}
}
httpResponse(request){
Wire.requestFrom(8,Bytes); //How many Bytes to request without knowing Image size??
while (Wire.available()){
File streamedImage = Wire.read(); //How to buffer here?
}
imageJPG = streamedImage.renderAsJpg(streamedImage); //How to treat the streamed Bytes as Image?
httpResponse(httpHeaders + "<html><body><img src ="+imageJPG+ "/></body></html>);
}
Everything can be summarized now in one more question: how can I get from the i2c wire the bytes of a JPG file in order to serve it immediately in a valid HTTP Response??