将 HTML 页面转换为十六进制值数组以在 Arduino 服务器中使用

标签 c bash arduino webserver

在 Arduino(或一般微 Controller )上运行 Web 服务器的许多示例和教程都使用十六进制值数组形式的 gzip 压缩网页

#define index_ov2640_html_gz_len 4316
const uint8_t index_ov2640_html_gz[] = {
  0x1F, 0x8B, 0x08, 0x08, 0x50, 0x5C, 0xAE, 0x5C, 0x00, 0x03, 0x69, 0x6E, 0x64, 0x65, 0x78, 0x5F,
  ...
  0x7F, 0x22, 0xF6, 0x5F, 0x04, 0x9C, 0x39, 0x76, 0x5C, 0x6C, 0x00, 0x00
};

我找到了一些手动将 HTML 转换为此类数组的方法,但我正在寻找一种自动执行转换的方法,例如使用 bash 脚本。

我找到了 hexdump 工具,并能够使用它产生一些有希望的结果

hexdump -e '16/1 "0x%02X, " "\n"' ~/Dev/Arduino/CameraWebServer/www/index_ov2640.html.gz > test.txt

但有一些问题:

  1. 生成的文件(有时)包含一个或多个星号
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
*
0x3C, 0x69, 0x6E, 0x70, 0x75, 0x74, 0x20, 0x69, 0x64, 0x3D, 0x22, 0x61, 0x65, 0x63, 0x32, 0x22,
  • 最后一行看起来像这样
  • 0x6D, 0x6C, 0x3E, 0x0A, 0x  , 0x  , 0x  , 0x  , 0x  , 0x  , 0x  , 0x  , 0x  , 0x  , 0x  , 0x  ,
    

    最佳答案

    感谢我与 Owen 的对话我了解到,unix xxd 工具准确地提供了我正在搜索的内容。我之前介绍的脚本可以简化为:

    #!/bin/bash
    
    OUTPUT_FILE=camera_index_new.h
    
    # clear the output file
    > $OUTPUT_FILE
    
    for file in `ls *.html`; do
        echo "Compressing: $file"
        gzip -kf "$file" && \
        echo "Converting $file to byte array"
        echo "/* Contents of file "$file".gz */" >> $OUTPUT_FILE
        xxd -i -u $file".gz" >> $OUTPUT_FILE
        echo "" >> $OUTPUT_FILE
    done
    

    以前的解决方案:

    基于P__J__建议我根据 bin2c 制作 bash 脚本项目产生的输出几乎与 espressif CameraWebServer exmple 中使用的输出相同

    #!/bin/bash
    
    OUTPUT_FILE=camera_index.h
    
    convert_to_byte_array() {
        echo "Converting $@ to byte arrays"
        ./bin2c -m -o $OUTPUT_FILE $@
    }
    
    for file in `ls *.html`; do
        echo "Compressing: $file"
        cp "$file" "copy_$file" && \
        gzip -f "$file" && \
        mv "copy_$file" "$file"
        compressed_files=( "${compressed_files[@]}" $file".gz")
    done
    if test -f "./bin2c"; then
        convert_to_byte_array ${compressed_files[@]}
    elif test -f "./bin2c.c"; then
        echo "Compiling bin2c from the source"
        gcc -o bin2c bin2c.c
        convert_to_byte_array ${compressed_files[@]}
    else
        echo "Conversion of ${compressed_files[@]} to byte arrays not possilbe, missing bin2c.
    Download bin2c source (bin2c.c) fom https://sourceforge.net/projects/bin2c/ and put it in this folder."
    fi
    

    关于将 HTML 页面转换为十六进制值数组以在 Arduino 服务器中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59866735/

    相关文章:

    bash - 有没有一种简单的方法可以为一个 glob 设置 nullglob

    bash - 如何在 bash 脚本中循环?

    linux - 创建一个分离 screen ,向它发送命令

    c - 在 C 中显示相同数字的简单链表

    c - 双 "for"循环中的 fgets 和 strtok

    c - 在这两个函数中为什么需要执行WATCHDOG_RESET()?

    macos - 使用 Xcode 5 在 Mac OS X 上进行 Arduino 编程?

    c - 链表-插入函数

    c++ - 在 Platformio 上使用串行监视器不显示 tty.usbmodem

    python - 处理 Arduino-Python 通信中的异常