2018/2/22..Webserver, EEPROM,SD on ESP..
Passing few days in Kaushong lib, I managed to get some very slow progress in my porgramming;
1. Webserver
I have first took a example on esp32 with BME280, work note:
http://ldsrc.blogspot.tw/2018/02/esp32-web-server-http-learning-with.html
and fall into trouble with bme280 lib for esp32, since the adafruit lib is for it's own board, so there is no way to assigned IIC pins. With my heltec wemos esp32 boards, I do need this function to assigned the IIC pins to cowork with other device. After alomost one day of work, I found out that the example use an modified lib of adafruit for BME280, which is able to assign pins.
As to web server, I have to attack the knowage of HTML from zero, there are several stuff on html, after some basic knowage, I took the lib from bbx10 to modify it, so that I can show data, base on some parameter to change the html page,get input (i.e. device_ID), and possible to save it to EEPROM.
The web server lib is working on both 8266 and 32. the wifi lib of 8266 and 32 are stupidly different, so you have to use ifdefine xxxxx to auto switch between these 2 boards.
modified lib:
https://github.com/Takatsuki0204/BME280-I2C-ESP32
web server lib:
https://github.com/bbx10/WebServer_tng
The works are in Arduino_projects\0_ESP\ESP32_8266_web_server
2.EEPROM
ESP 32 and 8266 has both flash on board, and can be used as EEPROM on arduino, the lib is identical for both boards.you do:
and that would be good for both.
read/write is similar to arduino, but for writing you need to add commit(), otherwise it would not write..
the eeprom is working on bytes, so for writing there are some works on string, float to bytes, and for reading, it would need some work on bytes to string...
I did not have much experience on this, so it took me again some more time.
example1: String to byte array:
to write string into EEPROM, you need to convert a string into a byte array.
There is a function call string.getBytes() to get a string into a byte array, following is an example:
SSID_AP.getBytes(AP_SSID,11);
AP_SSID[] is a byte array, SSID_AP is a string of 10 char, (in C, a string is terminated with "/0", NULL, it does not appear anyway, even you use arduino SSID_AP.length() it will only return 10.)
So to get it work, you need to declair AP_SSID as 11 bytes:
byte AP_SSID[11];
and then use getBytes command
SSID_AP.getBytes(AP_SSID,11);
so that you can have the correct byte array.
It took me a good afternoon to find this.
Then to make the byte array dynamic with string.length is some how not easy, since the declamation of array need a const int you can't just declair like this:
byte AP_SSID[SSID_AP.length()+1];
I tried, it does not work
example2: byte array to string:
to get a bytes from eeprom and put them into string,this is relative easy, you just need to convert the byte to char and append them all together.
for (int i = 1; i < 11; i++)
{
SSID_AP += (char(EEPROM.read(i)));
}
and you don't need to add a NULL...
the work is here : Arduino_projects\0_ESP\EEprom_ESP32_8266
3.SD card
SD card on esp32 can have 2 kind of interfaces, I decided to go for SPI interface. The I have to deal with 2 SPI devices in my project, the other one is lora.
Well, it also took me a while, since I'm not so familiar with SPI.The study is here:
http://ldsrc.blogspot.tw/2018/02/sd-card-on-esp32-with-radiohead.html
In short, CS of each device has to be different, first, you need to make them work seperatly before put them to work together. To make them work together, there are some SPI parameters to be work out, such as SPI frequency, mode, bit order.Good explanation is here:
https://www.circuitsathome.com/mcu/running-multiple-slave-devices-on-arduino-spi-bus/
The difficulty is that most SPI devices has already lib on it, you don't want to write your own. But take time to understand them about: if there is a way to specify the frequency or set different mode or bit order? as well as assign SPI pins. and the most importantly if the lib handle CS and MISO with good care.
In my case of RadioHead lora(RFM95) and sd lib of esp32(not the same for 8266, 8266 sd lib and lora has to be test again).
RadioHead lib handles every thing very good, no wonder it's the most popular ones in arduino world. and I found that the lib use 1M as spi bus speed.
And the sd lib of esp32 has the parameter to select spi bus speed. I did the following:
a. init radiohard
b. manually write high the CS of RFM95
c. init the sd card with assigned spi frequency 1M
I'm then able to send out lora message and write a message to SD.
The works is here:
\Arduino_projects\0_ESP\ESP32_lora_and_SD
1. Webserver
I have first took a example on esp32 with BME280, work note:
http://ldsrc.blogspot.tw/2018/02/esp32-web-server-http-learning-with.html
and fall into trouble with bme280 lib for esp32, since the adafruit lib is for it's own board, so there is no way to assigned IIC pins. With my heltec wemos esp32 boards, I do need this function to assigned the IIC pins to cowork with other device. After alomost one day of work, I found out that the example use an modified lib of adafruit for BME280, which is able to assign pins.
As to web server, I have to attack the knowage of HTML from zero, there are several stuff on html, after some basic knowage, I took the lib from bbx10 to modify it, so that I can show data, base on some parameter to change the html page,get input (i.e. device_ID), and possible to save it to EEPROM.
The web server lib is working on both 8266 and 32. the wifi lib of 8266 and 32 are stupidly different, so you have to use ifdefine xxxxx to auto switch between these 2 boards.
modified lib:
https://github.com/Takatsuki0204/BME280-I2C-ESP32
web server lib:
https://github.com/bbx10/WebServer_tng
The works are in Arduino_projects\0_ESP\ESP32_8266_web_server
2.EEPROM
ESP 32 and 8266 has both flash on board, and can be used as EEPROM on arduino, the lib is identical for both boards.you do:
#include "EEPROM.h"
#define EEPROM_SIZE 64
and that would be good for both.
read/write is similar to arduino, but for writing you need to add commit(), otherwise it would not write..
EEPROM.read(i);
EEPROM.write(i,byte);
EEPROM.commit();
the eeprom is working on bytes, so for writing there are some works on string, float to bytes, and for reading, it would need some work on bytes to string...
I did not have much experience on this, so it took me again some more time.
example1: String to byte array:
to write string into EEPROM, you need to convert a string into a byte array.
There is a function call string.getBytes() to get a string into a byte array, following is an example:
SSID_AP.getBytes(AP_SSID,11);
AP_SSID[] is a byte array, SSID_AP is a string of 10 char, (in C, a string is terminated with "/0", NULL, it does not appear anyway, even you use arduino SSID_AP.length() it will only return 10.)
So to get it work, you need to declair AP_SSID as 11 bytes:
byte AP_SSID[11];
and then use getBytes command
SSID_AP.getBytes(AP_SSID,11);
so that you can have the correct byte array.
It took me a good afternoon to find this.
Then to make the byte array dynamic with string.length is some how not easy, since the declamation of array need a const int you can't just declair like this:
byte AP_SSID[SSID_AP.length()+1];
I tried, it does not work
example2: byte array to string:
to get a bytes from eeprom and put them into string,this is relative easy, you just need to convert the byte to char and append them all together.
for (int i = 1; i < 11; i++)
{
SSID_AP += (char(EEPROM.read(i)));
}
and you don't need to add a NULL...
the work is here : Arduino_projects\0_ESP\EEprom_ESP32_8266
3.SD card
SD card on esp32 can have 2 kind of interfaces, I decided to go for SPI interface. The I have to deal with 2 SPI devices in my project, the other one is lora.
Well, it also took me a while, since I'm not so familiar with SPI.The study is here:
http://ldsrc.blogspot.tw/2018/02/sd-card-on-esp32-with-radiohead.html
In short, CS of each device has to be different, first, you need to make them work seperatly before put them to work together. To make them work together, there are some SPI parameters to be work out, such as SPI frequency, mode, bit order.Good explanation is here:
https://www.circuitsathome.com/mcu/running-multiple-slave-devices-on-arduino-spi-bus/
The difficulty is that most SPI devices has already lib on it, you don't want to write your own. But take time to understand them about: if there is a way to specify the frequency or set different mode or bit order? as well as assign SPI pins. and the most importantly if the lib handle CS and MISO with good care.
In my case of RadioHead lora(RFM95) and sd lib of esp32(not the same for 8266, 8266 sd lib and lora has to be test again).
RadioHead lib handles every thing very good, no wonder it's the most popular ones in arduino world. and I found that the lib use 1M as spi bus speed.
And the sd lib of esp32 has the parameter to select spi bus speed. I did the following:
a. init radiohard
b. manually write high the CS of RFM95
c. init the sd card with assigned spi frequency 1M
I'm then able to send out lora message and write a message to SD.
The works is here:
\Arduino_projects\0_ESP\ESP32_lora_and_SD
留言
張貼留言