Bestand naar C, C++ variable converten.

Om een bestand als variable in C of C++ te gebruiken kun je de tool xxd gebruiken.
xxd is standaard aanwezig op de meeste linux Mint machines.

cellie@cellie-Mint-64 ~ $ echo Hello World\! > temp
cellie@cellie-Mint-64 ~ $ xxd -i temp
unsigned char temp[] = {
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21,
0x0a
};
unsigned int temp_len = 13;

Om de variable niet in RAM maar in ROM terecht te laten komen verander je de code als volgt:

const char temp[] = {
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21,
0x0a
};
const int temp_len = 13;

De link:

Natuurlijk niet zelf bedacht maar gevonden op stackoverflow.

ESP8266 – .softAP() en .softAPConfig in Arduino IDE

De config komt na de init. Dus:

WiFi.softAPConfig( myIPAdress, myGateway, mySubnet );
WiFi.softAP( "myAP" );

is fout en

WiFi.softAP( "myAP" );
WiFi.softAPConfig( myIPAdress, myGateway, mySubnet );

is goed.

Same goes for WiFi.config() en WiFi.begin().
Eerst de init dan de config.
https://github.com/esp8266/Arduino/issues/128#issuecomment-96787709

Plasmabol ESP8266 PWM frequentie.

byte outputPin = D4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
//todo: make a blacklist of misbehaving frequecies
long newFreq = random( 9, 20 );
long newDuration = random( 2048 );
long newDutyCycle = random( 6, 6 + PWMRANGE / 2 ) ;

analogWrite( outputPin, newDutyCycle );
analogWriteFreq( newFreq );
Serial.println( "duty: " + (String)newDutyCycle + " freq: " + (String)newFreq );
delay( newDuration );
}

ESP8266 – Gebufferd binair SPIFFS lezen. #2

270 KB/s.

De int bufferSize = 1460 is cruciaal, dit is nl de TCP hw buffer size.


void fileSend(String fileName) {
  if ( !SPIFFS.begin() ) {
    Serial.println("Fatal error: no SPIFFS disk present!");
    analogWrite(redLED, PWMRANGE);     
    while (true) {}   //wait forever
  }
  File thisFile = SPIFFS.open(fileName, "r");
  if (!thisFile) {
    webClient.print("HTTP/1.1 404 Not found\r\n\r\n");
    webClient.print("ERROR 404 - '" + fileName + "' was not found.");
    return;
  }
  //check the file's extention and tailor the header for that
  String httpHeader = "HTTP/1.1 200 \r\nContent-Type: text/plain\r\n";
  
  if ( fileName.substring(  fileName.length() - 4 ) == ".css" ) {
    httpHeader = "HTTP/1.1 200 \r\nContent-Type: text/css\r\n";
  }

  if ( fileName.substring(  fileName.length() - 4 ) == ".htm" || 
       fileName.substring(  fileName.length() - 4 ) == ".php" ) {
    httpHeader = "HTTP/1.1 200 \r\nContent-Type: text/html\r\n";
  }
  
  httpHeader += "Content-Length: " + (String)thisFile.size() + "\r\n\r\n" ;
  webClient.print(httpHeader);

  int bufferSize = 1460;
  char rwBuffer[bufferSize];
  while ( thisFile.available() > bufferSize ) {
    thisFile.readBytes(rwBuffer,bufferSize);  
    webClient.write(rwBuffer, bufferSize);
  }
  //send last chunk
  bufferSize = thisFile.available();
  thisFile.readBytes(rwBuffer,bufferSize);  
  webClient.write(rwBuffer, bufferSize);
  thisFile.close();
}//done

ESP8266 – Gebufferd binair SPIFFS lezen.

Deze routine word gebruikt in de Aquacontroller.
Haalt ongeveer 14kB/s. Maar kan wel 1.3MB grote bestanden aan.

void fileSend(String fileName) {
 SPIFFS.begin();
 File thisFile = SPIFFS.open(fileName, "r");
 if (!thisFile) {
   webClient.println("HTTP/1.1 404 Not found\r\n");
   webClient.println("ERROR 404 - '" + fileName + "' was not found.");
   return;
 }
 //check the file's extention and tailor the header for that
 String httpHeader;
 if ( fileName.substring( fileName.length() - 4 ) == ".css" ) {
   httpHeader = "HTTP/1.1 200 \r\nContent-Type: text/css\r\n";
 } 
 else {
   httpHeader = "HTTP/1.1 200 \r\nContent-Type: text/html\r\n";
 }
 httpHeader += "Content-Length: " + (String)thisFile.size() + "\r\n" ;
 webClient.println(httpHeader);

 int bufferSize = 512;
 char rwBuffer[bufferSize];
 while ( thisFile.available() > bufferSize ) {
   for ( int bufferPos = 0; bufferPos < bufferSize; bufferPos++ ) {
     rwBuffer[bufferPos] = thisFile.read();
   }
   webClient.write(rwBuffer, bufferSize);
 }
 //send last chunk
 int leftOver = thisFile.available();
 for ( int bufferPos = 0; bufferPos < leftOver; bufferPos++ ) {
   rwBuffer[bufferPos] = thisFile.read();
 }
 webClient.write(rwBuffer, leftOver);
 thisFile.close();
}