Initial commit

This commit is contained in:
Rafal Zajac
2017-11-12 22:49:10 +01:00
commit 74d0876c59
39 changed files with 2503 additions and 0 deletions

19
examples/CMakeLists.txt Normal file
View File

@ -0,0 +1,19 @@
# Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
add_subdirectory(ds18b20_search)
add_subdirectory(ds18b20_temp)
add_subdirectory(dht22_temp_hum)
add_subdirectory(sht21_example)

View File

@ -0,0 +1,31 @@
# Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
find_package(esp_sdo REQUIRED)
find_package(esp_util REQUIRED)
add_executable(dht22_temp_hum main.c ${ESP_USER_CONFIG})
target_include_directories(dht22_temp_hum PUBLIC
${ESP_USER_CONFIG_DIR}
${esp_sdo_INCLUDE_DIRS}
${esp_util_INCLUDE_DIRS})
target_link_libraries(dht22_temp_hum
${esp_sdo_LIBRARIES}
${esp_util_LIBRARIES}
esp_dht22)
esp_gen_exec_targets(dht22_temp_hum)

View File

@ -0,0 +1,12 @@
## DHT22 example.
Demonstrates how to get temperature and humidity from DHT22 sensor.
## Flashing
```
$ cd build
$ cmake ..
$ make dht22_temp_hum_flash
$ miniterm.py /dev/ttyUSB0 74880
```

View File

@ -0,0 +1,117 @@
/*
* Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <user_interface.h>
#include <esp_gpio.h>
#include <esp_dht22.h>
#include <mem.h>
#include <esp_sdo.h>
os_timer_t timer;
/**
* Rise base to power of.
*
* From: http://bbs.espressif.com/viewtopic.php?t=246
*
* @param base The number.
* @param exp The exponent.
*
* @return Product.
*/
static int ICACHE_FLASH_ATTR
power(int base, int exp)
{
int result = 1;
while (exp) {
result *= base;
exp--;
}
return result;
}
/**
* Get string representation of float.
*
* From: http://bbs.espressif.com/viewtopic.php?t=246
*
* Warning: limited to 15 chars & non-reentrant.
* e.g., don't use more than once per os_printf call.
*
* @param num The float to convert to string.
* @param decimals The number of decimal places.
*
* @return The float string representation.
*/
static char *ICACHE_FLASH_ATTR
ftoa(float num, uint8_t decimals)
{
static char *buf[16];
int whole = (int) num;
int decimal = (int) ((num - whole) * power(10, decimals));
if (decimal < 0) {
// get rid of sign on decimal portion
decimal -= 2 * decimal;
}
char *pattern[10]; // setup printf pattern for decimal portion
os_sprintf((char *) pattern, "%%d.%%0%dd", decimals);
os_sprintf((char *) buf, (const char *) pattern, whole, decimal);
return (char *) buf;
}
void ICACHE_FLASH_ATTR
sys_init_done(void* arg)
{
esp_dht22_err err;
esp_dht22_dev *dev = esp_dht22_new_dev(GPIO2);
// Get temperature.
err = esp_dht22_get(dev);
if (err != ESP_DHT22_OK) {
os_free(dev);
os_printf("DHT22 error code: %d\n", err);
return;
}
os_printf("Temp: %s\n", ftoa(dev->temp, 2));
os_printf("Hum: %s\n", ftoa(dev->hum, 2));
os_printf("--------------------\n");
os_free(dev);
}
void ICACHE_FLASH_ATTR
user_init()
{
// No need for wifi for this example.
wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
//system_init_done_cb(sys_init_done);
stdout_init(BIT_RATE_74880);
// Initialize DHT22 on GPIO 2.
esp_dht22_init(GPIO2);
// Wait before running main code.
os_printf("Initialized.\n");
os_timer_disarm(&timer);
os_timer_setfn(&timer, sys_init_done, NULL);
os_timer_arm(&timer, 1500, true);
}

View File

@ -0,0 +1,37 @@
# Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
find_package(esp_sdo REQUIRED)
find_package(esp_ow REQUIRED)
find_package(esp_eb REQUIRED)
find_package(esp_util REQUIRED)
add_executable(ds18b20_search main.c ${ESP_USER_CONFIG})
target_include_directories(ds18b20_search PUBLIC
${ESP_USER_CONFIG_DIR}
${esp_sdo_INCLUDE_DIRS}
${esp_ow_INCLUDE_DIRS}
${esp_eb_INCLUDE_DIRS}
${esp_util_INCLUDE_DIRS})
target_link_libraries(ds18b20_search
${esp_sdo_LIBRARIES}
${esp_ow_LIBRARIES}
${esp_eb_LIBRARIES}
${esp_util_LIBRARIES}
esp_ds18b20)
esp_gen_exec_targets(ds18b20_search)

View File

@ -0,0 +1,12 @@
## Find DS18b20 devices.
Demonstrates how to search OneWire bus for DS18b20.
## Flashing
```
$ cd build
$ cmake ..
$ make ds18b20_search_flash
$ miniterm.py /dev/ttyUSB0 74880
```

View File

@ -0,0 +1,55 @@
/*
* Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <user_interface.h>
#include <osapi.h>
#include <esp_ds18b20.h>
#include <esp_gpio.h>
#include <esp_sdo.h>
// List of found devices on the OneWire bus.
static esp_ow_device *root = NULL;
static void ICACHE_FLASH_ATTR
sys_init_done()
{
esp_ow_err err;
if (esp_ds18b20_init(GPIO2) == false) {
os_printf("Error initializing DS18B20.\n");
return;
}
err = esp_ds18b20_search(GPIO2, false, &root);
if (err != ESP_OW_OK) {
os_printf("Search error: %d\n", err);
return;
}
// Log found devices to serial.
esp_ow_dump_found(root);
}
void ICACHE_FLASH_ATTR
user_init()
{
// No need for wifi for this examples.
wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
stdout_init(BIT_RATE_74880);
system_init_done_cb(sys_init_done);
}

View File

@ -0,0 +1,37 @@
# Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
find_package(esp_sdo REQUIRED)
find_package(esp_ow REQUIRED)
find_package(esp_eb REQUIRED)
find_package(esp_util REQUIRED)
add_executable(ds18b20_temp main.c ${ESP_USER_CONFIG})
target_include_directories(ds18b20_temp PUBLIC
${ESP_USER_CONFIG_DIR}
${esp_sdo_INCLUDE_DIRS}
${esp_ow_INCLUDE_DIRS}
${esp_eb_INCLUDE_DIRS}
${esp_util_INCLUDE_DIRS})
target_link_libraries(ds18b20_temp
${esp_sdo_LIBRARIES}
${esp_ow_LIBRARIES}
${esp_eb_LIBRARIES}
${esp_util_LIBRARIES}
esp_ds18b20)
esp_gen_exec_targets(ds18b20_temp)

View File

@ -0,0 +1,12 @@
## Get temperature measured by DS18B20.
Demonstrates how to search OneWire bus for DS18b20.
## Flashing
```
$ cd build
$ cmake ..
$ make ds18b20_temp_flash
$ miniterm.py /dev/ttyUSB0 74880
```

View File

@ -0,0 +1,81 @@
/*
* Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <user_interface.h>
#include <osapi.h>
#include <esp_ds18b20.h>
#include <esp_gpio.h>
#include <esp_eb.h>
#include <esp_sdo.h>
#include <esp_util.h>
// List of found devices on the OneWire bus.
static esp_ow_device *root = NULL;
// Handle temperature conversion callbacks.
static void ICACHE_FLASH_ATTR
temperature(const char *event, void *arg)
{
esp_ow_device *dev = arg;
esp_ds18b20_st *st = dev->custom;
if (st->last_temp == ESP_DS18B20_TEMP_ERR) {
os_printf("Temperature read error.\n");
} else {
os_printf("Temperature: %s\n", esp_util_ftoa(st->last_temp, 4));
}
}
static void ICACHE_FLASH_ATTR
sys_init_done()
{
esp_ow_err err;
if (esp_ds18b20_init(GPIO2) == false) {
os_printf("Error initializing DS18B20.\n");
return;
}
err = esp_ds18b20_search(GPIO2, false, &root);
if (err != ESP_OW_OK) {
os_printf("Search error: %d\n", err);
return;
}
if (root == NULL) {
os_printf("No devices found.\n");
}
// Callbacks will be called when conversion is finished.
esp_eb_attach(ESP_DS18B20_EV_TEMP_READY, temperature);
esp_eb_attach(ESP_DS18B20_EV_TEMP_ERROR, temperature);
// Get temperature from the first DS18B20 sensor.
esp_ds18b20_convert(root);
}
void ICACHE_FLASH_ATTR
user_init()
{
// No need for wifi for this examples.
wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
stdout_init(BIT_RATE_74880);
system_init_done_cb(sys_init_done);
}

View File

@ -0,0 +1 @@
// This is only here to make the API headers happy.

View File

@ -0,0 +1,34 @@
# Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
find_package(esp_sdo REQUIRED)
find_package(esp_i2c REQUIRED)
find_package(esp_util REQUIRED)
add_executable(sht21_example main.c ${ESP_USER_CONFIG})
target_include_directories(sht21_example PUBLIC
${ESP_USER_CONFIG_DIR}
${esp_sdo_INCLUDE_DIRS}
${esp_i2c_INCLUDE_DIRS}
${esp_util_INCLUDE_DIRS})
target_link_libraries(sht21_example
${esp_sdo_LIBRARIES}
${esp_i2c_LIBRARIES}
${esp_util_LIBRARIES}
esp_sht21)
esp_gen_exec_targets(sht21_example)

View File

@ -0,0 +1,15 @@
## SHT21 example.
Demonstrates how to:
- get serial number,
- get firmware version,
- get humidity and temperature.
## Flashing
```
$ cd build
$ cmake ..
$ make sht21_example_flash
$ miniterm.py /dev/ttyUSB0 74880
```

View File

@ -0,0 +1,131 @@
/*
* Copyright 2017 Rafal Zajac <rzajac@gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <esp_sht21.h>
#include <esp_sdo.h>
#include <user_interface.h>
#include <osapi.h>
#define SCL GPIO0
#define SDA GPIO2
os_timer_t timer;
/**
* Rise base to power of.
*
* From: http://bbs.espressif.com/viewtopic.php?t=246
*
* @param base The number.
* @param exp The exponent.
*
* @return Product.
*/
static int ICACHE_FLASH_ATTR
power(int base, int exp)
{
int result = 1;
while (exp) {
result *= base;
exp--;
}
return result;
}
/**
* Get string representation of float.
*
* From: http://bbs.espressif.com/viewtopic.php?t=246
*
* Warning: limited to 15 chars & non-reentrant.
* e.g., don't use more than once per os_printf call.
*
* @param num The float to convert to string.
* @param decimals The number of decimal places.
*
* @return The float string representation.
*/
static char *ICACHE_FLASH_ATTR
ftoa(float num, uint8_t decimals)
{
static char *buf[16];
int whole = (int) num;
int decimal = (int) ((num - whole) * power(10, decimals));
if (decimal < 0) {
// get rid of sign on decimal portion
decimal -= 2 * decimal;
}
char *pattern[10]; // setup printf pattern for decimal portion
os_sprintf((char *) pattern, "%%d.%%0%dd", decimals);
os_sprintf((char *) buf, (const char *) pattern, whole, decimal);
return (char *) buf;
}
void ICACHE_FLASH_ATTR
run_sht21()
{
uint8_t rev;
float value;
uint8_t sn[8];
esp_i2c_err err;
err = esp_sht21_init(SCL, SDA);
if (err != ESP_I2C_OK) {
os_printf("SHT21 init error.\n");
return;
}
err = esp_sht21_get_sn(sn);
if (err != ESP_I2C_OK) os_printf("SHT21 sn error %d.\n", err);
os_printf("SHT21 SN: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
sn[0], sn[1], sn[2], sn[3],
sn[4], sn[5], sn[6], sn[7]);
err = esp_sht21_get_rev(&rev);
if (err != ESP_I2C_OK) os_printf("SHT21 sn error %d.\n", err);
os_printf("Firmware rev: 0x%02X\n", rev);
err = esp_sht21_get_rh(&value);
if (err != ESP_I2C_OK) os_printf("Get RH error: %d\n", err);
os_printf("Humidity: %s%%\n", ftoa(value, 2));
err = esp_sht21_get_temp_last(&value);
if (err != ESP_I2C_OK) os_printf("Get TEMP error: %d\n", err);
os_printf("Temperature: %s deg. C\n", ftoa(value, 2));
}
void ICACHE_FLASH_ATTR
user_init()
{
// We don't need WiFi for this example.
wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);
stdout_init(BIT_RATE_74880);
os_printf("Starting...\n");
os_timer_disarm(&timer);
os_timer_setfn(&timer, (os_timer_func_t *) run_sht21, NULL);
os_timer_arm(&timer, 1500, false);
}