├── release ├── utils │ ├── esp-flasher.cmd │ ├── esp-terminal.cmd │ ├── esp-flasher │ └── esp-terminal ├── md.css ├── README.md └── gen_release.sh ├── sdk ├── License ├── lib │ ├── libat.a │ ├── libpp.a │ ├── libgcc.a │ ├── libhal.a │ ├── libjson.a │ ├── liblwip.a │ ├── libmain.a │ ├── libmesh.a │ ├── libphy.a │ ├── libpwm.a │ ├── libssl.a │ ├── libwpa.a │ ├── libwpa2.a │ ├── libwps.a │ ├── libairkiss.a │ ├── libcrypto.a │ ├── libdriver.a │ ├── libespnow.a │ ├── libmbedtls.a │ ├── libupgrade.a │ ├── liblwip_536.a │ ├── libnet80211.a │ ├── libsmartconfig.a │ ├── strip_libc_funcs.txt │ ├── strip_libgcc_funcs.txt │ └── readme.md └── include │ ├── version.h │ ├── ets_forward.h │ ├── os_type.h │ ├── smartconfig.h │ ├── sntp.h │ ├── ping.h │ └── pwm.h ├── images ├── conf.png ├── edge.png ├── term.png ├── web.png ├── phone1.jpg ├── phone2.jpg ├── dh-logo.png ├── sample1.jpg ├── sample2.jpg ├── sample3.jpg ├── sample4.jpg ├── sample5.jpg └── demo-device.jpg ├── examples-cloud ├── demo128x64.png └── simple-gpio-set.html ├── firmware-src ├── pages │ ├── favicon.ico │ ├── gen_pages.sh │ ├── ds18b20.html │ ├── libs.js │ ├── pwm.html │ └── hd44780.html ├── sources │ ├── dhzc_web.h │ ├── webserver.h │ ├── dhnotification.h │ ├── custom_firmware.c │ ├── dhzc_dnsd.h │ ├── dhterminal_configure.h │ ├── crc32.h │ ├── dhzc_post.h │ ├── devices │ │ ├── mhz19.h │ │ ├── ds18b20.h │ │ ├── max6675.h │ │ ├── max31855.h │ │ ├── bh1750.h │ │ ├── tm1637.h │ │ ├── lm75.h │ │ ├── ads1115.h │ │ ├── pcf8574_hd44780.h │ │ ├── mlx90614.h │ │ ├── bmp180.h │ │ ├── bmp280.h │ │ ├── si7021.h │ │ ├── mcp4725.h │ │ ├── mhz19.c │ │ ├── ds18b20.c │ │ ├── ina219.h │ │ ├── hmc5883l.h │ │ ├── max6675.c │ │ ├── max31855.c │ │ ├── mpu6050.h │ │ ├── pca9685.h │ │ ├── pcf8591.h │ │ ├── bh1750.c │ │ ├── lm75.c │ │ ├── dht.h │ │ ├── mcp4725.c │ │ ├── pcf8574.h │ │ ├── mlx90614.c │ │ ├── si7021.c │ │ └── hmc5883l.c │ ├── commands │ │ ├── pwm_cmd.h │ │ ├── lm75_cmd.h │ │ ├── mhz19_cmd.h │ │ ├── bh1750_cmd.h │ │ ├── bmp180_cmd.h │ │ ├── bmp280_cmd.h │ │ ├── ina219_cmd.h │ │ ├── si7021_cmd.h │ │ ├── tm1637_cmd.h │ │ ├── ads1115_cmd.h │ │ ├── ds18b20_cmd.h │ │ ├── max6675_cmd.h │ │ ├── mpu6050_cmd.h │ │ ├── mcp4725_cmd.h │ │ ├── pca9685_cmd.h │ │ ├── hmc5883l_cmd.h │ │ ├── max31855_cmd.h │ │ ├── mlx90614_cmd.h │ │ ├── ws2812b_cmd.h │ │ ├── pcf8574_hd44780_cmd.h │ │ ├── adc_cmd.h │ │ ├── spi_cmd.h │ │ ├── pcf8574_cmd.h │ │ ├── pcf8591_cmd.h │ │ ├── mfrc522_cmd.h │ │ ├── gpio_cmd.h │ │ ├── mhz19_cmd.c │ │ ├── i2c_cmd.h │ │ ├── pwm_cmd.c │ │ ├── dht_cmd.h │ │ ├── max6675_cmd.c │ │ ├── max31855_cmd.c │ │ ├── uart_cmd.h │ │ ├── ds18b20_cmd.c │ │ ├── tm1637_cmd.c │ │ ├── lm75_cmd.c │ │ ├── pca9685_cmd.c │ │ ├── bh1750_cmd.c │ │ ├── ads1115_cmd.c │ │ ├── mlx90614_cmd.c │ │ ├── si7021_cmd.c │ │ ├── bmp180_cmd.c │ │ ├── bmp280_cmd.c │ │ ├── onewire_cmd.h │ │ ├── pcf8574_hd44780_cmd.c │ │ ├── mpu6050_cmd.c │ │ ├── mcp4725_cmd.c │ │ ├── ina219_cmd.c │ │ └── hmc5883l_cmd.c │ ├── dhap.h │ ├── rest.h │ ├── dhcommands.h │ ├── uploadable_api.h │ ├── dhmem.c │ ├── mdnsd.h │ ├── dhdebug.h │ ├── swab.c │ ├── DH │ │ ├── adc.h │ │ ├── adc.c │ │ ├── pwm.h │ │ ├── spi.h │ │ └── i2c.h │ ├── dhesperrors.h │ ├── dhmem.h │ ├── dhterminal_commandline.h │ ├── dhzc_pages.h │ ├── rand.h │ ├── dhconnector_websocket.h │ ├── snprintf.h │ ├── dhdata.h │ ├── dhconnector.h │ ├── rand.c │ ├── dhconnector_websocket_api.h │ ├── dhdata.c │ ├── dhdebug.c │ ├── dhrequest.h │ ├── uploadable_api.c │ ├── dhnotification.c │ ├── dhsender_queue.h │ ├── uploadable_page.h │ ├── dhsender.h │ └── httpd.h ├── .project ├── LICENSE └── README.md ├── .gitignore ├── firmware-tests └── README.md ├── esp-utils ├── common │ ├── terminal.h │ ├── terminal_posix.cpp │ ├── serialport.cpp │ └── serialport.h ├── .project ├── Makefile ├── LICENSE └── README.md ├── LICENSE ├── examples └── chip_configure_script.sh └── changelog.txt /release/utils/esp-flasher.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | %~dp0\esp-flasher-win.exe %* -------------------------------------------------------------------------------- /release/utils/esp-terminal.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | %~dp0\esp-terminal-win.exe %* -------------------------------------------------------------------------------- /sdk/License: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/License -------------------------------------------------------------------------------- /images/conf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/conf.png -------------------------------------------------------------------------------- /images/edge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/edge.png -------------------------------------------------------------------------------- /images/term.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/term.png -------------------------------------------------------------------------------- /images/web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/web.png -------------------------------------------------------------------------------- /sdk/lib/libat.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libat.a -------------------------------------------------------------------------------- /sdk/lib/libpp.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libpp.a -------------------------------------------------------------------------------- /images/phone1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/phone1.jpg -------------------------------------------------------------------------------- /images/phone2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/phone2.jpg -------------------------------------------------------------------------------- /sdk/lib/libgcc.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libgcc.a -------------------------------------------------------------------------------- /sdk/lib/libhal.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libhal.a -------------------------------------------------------------------------------- /sdk/lib/libjson.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libjson.a -------------------------------------------------------------------------------- /sdk/lib/liblwip.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/liblwip.a -------------------------------------------------------------------------------- /sdk/lib/libmain.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libmain.a -------------------------------------------------------------------------------- /sdk/lib/libmesh.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libmesh.a -------------------------------------------------------------------------------- /sdk/lib/libphy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libphy.a -------------------------------------------------------------------------------- /sdk/lib/libpwm.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libpwm.a -------------------------------------------------------------------------------- /sdk/lib/libssl.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libssl.a -------------------------------------------------------------------------------- /sdk/lib/libwpa.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libwpa.a -------------------------------------------------------------------------------- /sdk/lib/libwpa2.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libwpa2.a -------------------------------------------------------------------------------- /sdk/lib/libwps.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libwps.a -------------------------------------------------------------------------------- /images/dh-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/dh-logo.png -------------------------------------------------------------------------------- /images/sample1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/sample1.jpg -------------------------------------------------------------------------------- /images/sample2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/sample2.jpg -------------------------------------------------------------------------------- /images/sample3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/sample3.jpg -------------------------------------------------------------------------------- /images/sample4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/sample4.jpg -------------------------------------------------------------------------------- /images/sample5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/sample5.jpg -------------------------------------------------------------------------------- /sdk/lib/libairkiss.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libairkiss.a -------------------------------------------------------------------------------- /sdk/lib/libcrypto.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libcrypto.a -------------------------------------------------------------------------------- /sdk/lib/libdriver.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libdriver.a -------------------------------------------------------------------------------- /sdk/lib/libespnow.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libespnow.a -------------------------------------------------------------------------------- /sdk/lib/libmbedtls.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libmbedtls.a -------------------------------------------------------------------------------- /sdk/lib/libupgrade.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libupgrade.a -------------------------------------------------------------------------------- /images/demo-device.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/images/demo-device.jpg -------------------------------------------------------------------------------- /sdk/lib/liblwip_536.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/liblwip_536.a -------------------------------------------------------------------------------- /sdk/lib/libnet80211.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libnet80211.a -------------------------------------------------------------------------------- /release/md.css: -------------------------------------------------------------------------------- 1 | a[href]:after { 2 | content: "" !important; 3 | } 4 | body { 5 | font-size: 0.6em; 6 | } 7 | -------------------------------------------------------------------------------- /sdk/lib/libsmartconfig.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/sdk/lib/libsmartconfig.a -------------------------------------------------------------------------------- /examples-cloud/demo128x64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/examples-cloud/demo128x64.png -------------------------------------------------------------------------------- /firmware-src/pages/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devicehive/esp8266-firmware/HEAD/firmware-src/pages/favicon.ico -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | firmware-src/firmware 2 | firmware-src/build 3 | firmware-src/.settings 4 | firmware-src/pages/pages.h 5 | esp-utils/build 6 | esp-utils/.settings 7 | sdk/lib/libc.a 8 | release/build 9 | release/*.zip 10 | release/*.tar.gz 11 | firmware-src/pages/debug.log 12 | .DS_Store 13 | 14 | -------------------------------------------------------------------------------- /release/utils/esp-flasher: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 3 | if [[ $(uname) == *"MINGW"* ]]; then 4 | $DIR/esp-flasher-win.exe $@ 5 | elif [[ $(uname) == *"Darwin"* ]]; then 6 | $DIR/esp-flasher-osx $@ 7 | else 8 | $DIR/esp-flasher-linux $@ 9 | fi 10 | -------------------------------------------------------------------------------- /release/utils/esp-terminal: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 3 | if [[ $(uname) == *"MINGW"* ]]; then 4 | $DIR/esp-terminal-win.exe $@ 5 | elif [[ $(uname) == *"Darwin"* ]]; then 6 | $DIR/esp-terminal-osx $@ 7 | else 8 | $DIR/esp-terminal-linux $@ 9 | fi 10 | -------------------------------------------------------------------------------- /sdk/include/version.h: -------------------------------------------------------------------------------- 1 | #ifndef ESP_SDK_VERSION_H 2 | #define ESP_SDK_VERSION_H 3 | 4 | #define ESP_SDK_VERSION_MAJOR 2 5 | #define ESP_SDK_VERSION_MINOR 2 6 | #define ESP_SDK_VERSION_PATCH 0 7 | 8 | #define ESP_SDK_VERSION_NUMBER 0x020200 9 | #define ESP_SDK_VERSION_STRING "2.2.0" 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /sdk/lib/strip_libc_funcs.txt: -------------------------------------------------------------------------------- 1 | OPEN libc.a 2 | DELETE lib_a-bzero.o 3 | DELETE lib_a-memcmp.o 4 | DELETE lib_a-memcpy.o 5 | DELETE lib_a-memmove.o 6 | DELETE lib_a-memset.o 7 | DELETE lib_a-rand.o 8 | DELETE lib_a-strcmp.o 9 | DELETE lib_a-strcpy.o 10 | DELETE lib_a-strlen.o 11 | DELETE lib_a-strncmp.o 12 | DELETE lib_a-strncpy.o 13 | DELETE lib_a-strstr.o 14 | SAVE 15 | END -------------------------------------------------------------------------------- /firmware-src/sources/dhzc_web.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhzc_web.h 3 | * \brief Web configuration server. 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHZC_WEB_H_ 10 | #define _DHZC_WEB_H_ 11 | 12 | /** 13 | * \brief Initialize HTTP daemon 14 | */ 15 | void dhzc_web_init(void); 16 | 17 | #endif /* _DHZC_WEB_H_ */ 18 | -------------------------------------------------------------------------------- /firmware-src/sources/webserver.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file webserver.h 3 | * \brief Web server for DeviceHive firmware. 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef WEBSERVER_H_ 10 | #define WEBSERVER_H_ 11 | 12 | /** 13 | * \brief Initialize web server 14 | */ 15 | void webserver_init(void); 16 | 17 | #endif /* WEBSERVER_H_ */ 18 | -------------------------------------------------------------------------------- /firmware-src/sources/dhnotification.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhnotification.h 3 | * \brief Notification help manager. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | * \details Collect callbacks from HAL modules and sends notifications. 8 | */ 9 | 10 | #ifndef _DHNOTIFICATION_H_ 11 | #define _DHNOTIFICATION_H_ 12 | 13 | 14 | #endif /* _DHNOTIFICATION_H_ */ 15 | -------------------------------------------------------------------------------- /firmware-src/sources/custom_firmware.c: -------------------------------------------------------------------------------- 1 | /* 2 | * custom_firmware.c 3 | * 4 | * Copyright 2016 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | */ 9 | #include "dhrequest.h" 10 | #include "dhdebug.h" 11 | 12 | #include 13 | 14 | HTTP_REQUEST * ICACHE_FLASH_ATTR custom_firmware_request(void) { 15 | // reimplement this method to return actual HTTP_REQUEST to make custom notification firmware 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /firmware-src/sources/dhzc_dnsd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhzc_dnsd.h 3 | * \brief DNS server for zero configuration. 4 | * \details Dummy, always return AP IP address. 5 | * \author Nikolay Khabarov 6 | * \date 2015 7 | * \copyright DeviceHive MIT 8 | */ 9 | 10 | #ifndef _DHAP_DNS_H_ 11 | #define _DHZC_DNS_H_ 12 | 13 | /** 14 | * \brief Initialize DNSD daemon. 15 | */ 16 | void dhzc_dnsd_init(void); 17 | 18 | #endif /* _DHAP_DNS_H_ */ 19 | -------------------------------------------------------------------------------- /firmware-src/sources/dhterminal_configure.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhterminal_configure.h 3 | * \brief Configure util for firmware. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHTERMINAL_CONFIGURE_H_ 10 | #define _DHTERMINAL_CONFIGURE_H_ 11 | 12 | /** 13 | * \brief Start configuration util in terminal. 14 | */ 15 | void dhterminal_configure_start(void); 16 | 17 | #endif /* _DHSERIAL_CONFIGURE_H_ */ 18 | -------------------------------------------------------------------------------- /firmware-src/sources/crc32.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file crc32.h 3 | * \brief CRC32 calculation module 4 | * \author Gary S. Brown 5 | * \date 1986 6 | * \copyright Public Domain 7 | */ 8 | 9 | #ifndef _CRC32_H_ 10 | #define _CRC32_H_ 11 | 12 | #include 13 | 14 | /** 15 | * \brief Calculate CRC32. 16 | * \param[in] buf Point to data. 17 | * \param[in] size Data size in bytes. 18 | * \return Calculated CRC32 value. 19 | */ 20 | uint32_t crc32(const void *buf, size_t size); 21 | 22 | #endif /* _CRC32_H_ */ 23 | -------------------------------------------------------------------------------- /firmware-tests/README.md: -------------------------------------------------------------------------------- 1 | # DeviceHive ESP8266 Firmware Test 2 | Special html pages with javascript that suppose to test firmware. 3 | 4 | # requests.html 5 | Tests command and their syntax. Run this test and wait green final report. 6 | Or red, if it has failure. 7 | 8 | # notification-stress.html 9 | Tests firmware under very high load. This test forces firmware to generate 10 | as much notiations as it can. Run this test and wait as long as you can or 11 | until notification stops come in (you may see notification id on page during 12 | test). 13 | -------------------------------------------------------------------------------- /sdk/lib/strip_libgcc_funcs.txt: -------------------------------------------------------------------------------- 1 | OPEN libgcc.a 2 | DELETE _addsubdf3.o 3 | DELETE _addsubsf3.o 4 | DELETE _divdf3.o 5 | DELETE _divdi3.o 6 | DELETE _divsi3.o 7 | DELETE _extendsfdf2.o 8 | DELETE _fixdfsi.o 9 | DELETE _fixunsdfsi.o 10 | DELETE _fixunssfsi.o 11 | DELETE _floatsidf.o 12 | DELETE _floatsisf.o 13 | DELETE _floatunsidf.o 14 | DELETE _floatunsisf.o 15 | DELETE _muldf3.o 16 | DELETE _muldi3.o 17 | DELETE _mulsf3.o 18 | DELETE _truncdfsf2.o 19 | DELETE _udivdi3.o 20 | DELETE _udivsi3.o 21 | DELETE _umoddi3.o 22 | DELETE _umodsi3.o 23 | DELETE _umulsidi3.o 24 | SAVE 25 | END -------------------------------------------------------------------------------- /firmware-src/sources/dhzc_post.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhzc_post.h 3 | * \brief POST data parser. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHAP_POST_H_ 10 | #define _DHZC_POST_H_ 11 | 12 | /** 13 | * \brief Parse and apply POST data from HTTP request. 14 | * \param[in] data Point to data. 15 | * \param[in] size Data size in bytes. 16 | * \return Zero on success or pointer to constant string with error description on error. 17 | */ 18 | char *dhzc_post_parse(const char *data, unsigned int len); 19 | 20 | #endif /* _DHAP_POST_H_ */ 21 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mhz19.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MHZ-19 CO2 sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_MHZ19_H_ 8 | #define _DEVICES_MHZ19_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_MHZ19) 12 | 13 | /** 14 | * @brief Read CO2 sensor value. 15 | * @param[out] co2 Pointer to store measure result in ppm(parts-per-million). 16 | * @return NULL on success, or string with error description. 17 | */ 18 | const char* mhz19_read(int *co2); 19 | 20 | #endif /* DH_DEVICE_MHZ19 */ 21 | #endif /* _DEVICES_MHZ19_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pwm_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief PWM command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_PWM_CMD_H_ 8 | #define _COMMANDS_PWM_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_PWM) // PWM command handlers 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "pwm/control" command. 16 | */ 17 | void dh_handle_pwm_control(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_PWM */ 21 | #endif /* _COMMANDS_PWM_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/dhap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhap.h 3 | * \brief Wireless firmware configuration util 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHAP_H_ 10 | #define _DHAP_H_ 11 | 12 | /** 13 | * \brief Initialize access point. 14 | * \param[in] ssid Wireless network name. 15 | * \param[in] password Wireless network password. Can be NULL or empty string for open network. 16 | */ 17 | void dhap_init(const char *ssid, const char *password); 18 | 19 | /** 20 | * \brief Return AP ipinfo. 21 | * \return Pointer to ip_info struct. 22 | */ 23 | const struct ip_info * dhap_get_ip_info(); 24 | 25 | #endif /* SOURCES_DHAP_H_ */ 26 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/lm75_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief LM75 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_LM75_CMD_H_ 8 | #define _COMMANDS_LM75_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_LM75) && defined(DH_DEVICE_LM75) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/lm75/read" command. 16 | */ 17 | void dh_handle_devices_lm75_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_LM75 && DH_DEVICE_LM75 */ 21 | #endif /* _COMMANDS_LM75_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mhz19_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MHZ19 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MHZ19_CMD_H_ 8 | #define _COMMANDS_MHZ19_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MHZ19) && defined(DH_DEVICE_MHZ19) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/mhz19/read" command. 16 | */ 17 | void dh_handle_devices_mhz19_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_MHZ19 && DH_DEVICE_MHZ19 */ 21 | #endif /* _COMMANDS_MHZ19_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /esp-utils/common/terminal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * main.cpp 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: simple VT100 terminal implementation 9 | * 10 | */ 11 | 12 | #ifndef TERMINAL_H_ 13 | #define TERMINAL_H_ 14 | 15 | #if ( defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) ) 16 | #define TERMINALWINDOWS 17 | #else 18 | #define TERMINALPOSIX 19 | #endif 20 | 21 | class Terminal { 22 | public: 23 | static Terminal *open(); 24 | virtual ~Terminal(); 25 | void get(char *buf); 26 | void put(char c); 27 | void put(const char *str); 28 | private: 29 | Terminal(); 30 | }; 31 | 32 | #endif /* TERMINAL_H_ */ 33 | -------------------------------------------------------------------------------- /firmware-src/sources/rest.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file rest.h 3 | * \brief Local RESTful service 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _REST_H_ 10 | #define _REST_H_ 11 | 12 | #include "httpd.h" 13 | 14 | /** 15 | * \brief Handle rest request. 16 | * \param[in] path Url path. 17 | * \param[in] key Access key for API which was given in request. 18 | * \param[in] content_in Request body, typically json. 19 | * \param[out] answer Data for response. 20 | * \return One of httpd statuses. 21 | */ 22 | 23 | HTTP_RESPONSE_STATUS rest_handle(const char *path, const char *key, 24 | HTTP_CONTENT *content_in, HTTP_ANSWER *answer); 25 | 26 | #endif /* _REST_H_ */ 27 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/bh1750_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief BH1750 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_BH1750_CMD_H_ 8 | #define _COMMANDS_BH1750_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_BH1750) && defined(DH_DEVICE_BH1750) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/bh1750/read" command. 16 | */ 17 | void dh_handle_devices_bh1750_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_BH1750 && DH_DEVICE_BH1750 */ 21 | #endif /* _COMMANDS_BH1750_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/bmp180_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief BMP180 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_BMP180_CMD_H_ 8 | #define _COMMANDS_BMP180_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_BMP180) && defined(DH_DEVICE_BMP180) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/bmp180/read" command. 16 | */ 17 | void dh_handle_devices_bmp180_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_BMP180 && DH_DEVICE_BMP180 */ 21 | #endif /* _COMMANDS_BMP180_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/bmp280_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief BMP280 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_BMP280_CMD_H_ 8 | #define _COMMANDS_BMP280_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_BMP280) && defined(DH_DEVICE_BMP280) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/bmp280/read" command. 16 | */ 17 | void dh_handle_devices_bmp280_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_BMP280 && DH_DEVICE_BMP280 */ 21 | #endif /* _COMMANDS_BMP280_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ina219_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief INA219 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_INA219_CMD_H_ 8 | #define _COMMANDS_INA219_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_INA219) && defined(DH_DEVICE_INA219) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/ina219/read" command. 16 | */ 17 | void dh_handle_devices_ina219_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_INA219 && DH_DEVICE_INA219 */ 21 | #endif /* _COMMANDS_INA219_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/si7021_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief SI7021 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_SI7021_CMD_H_ 8 | #define _COMMANDS_SI7021_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_SI7021) && defined(DH_DEVICE_SI7021) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/si7021/read" command. 16 | */ 17 | void dh_handle_devices_si7021_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_SI7021 && DH_DEVICE_SI7021 */ 21 | #endif /* _COMMANDS_SI7021_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/tm1637_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief TM1637 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_TM1637_CMD_H_ 8 | #define _COMMANDS_TM1637_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_TM1637) && defined(DH_DEVICE_TM1637) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/tm1637/write" command. 16 | */ 17 | void dh_handle_devices_tm1637_write(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_TM1637 && DH_DEVICE_TM1637 */ 21 | #endif /* _COMMANDS_TM1637_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ads1115_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief ADS1115 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_ADS1115_CMD_H_ 8 | #define _COMMANDS_ADS1115_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_ADS1115) && defined(DH_DEVICE_ADS1115) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/ads1115/read" command. 16 | */ 17 | void dh_handle_devices_ads1115_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_ADS1115 && DH_DEVICE_ADS1115 */ 21 | #endif /* _COMMANDS_ADS1115_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ds18b20_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief DS18B20 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_DS18B20_CMD_H_ 8 | #define _COMMANDS_DS18B20_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_DS18B20) && defined(DH_DEVICE_DS18B20) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/ds18b20/read" command. 16 | */ 17 | void dh_handle_devices_ds18b20_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_DS18B20 && DH_DEVICE_DS18B20 */ 21 | #endif /* _COMMANDS_DS18B20_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/max6675_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MAX6675 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MAX6675_CMD_H_ 8 | #define _COMMANDS_MAX6675_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MAX6675) && defined(DH_DEVICE_MAX6675) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/max6675/read" command. 16 | */ 17 | void dh_handle_devices_max6675_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_MAX6675 && DH_DEVICE_MAX6675 */ 21 | #endif /* _COMMANDS_MAX6675_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mpu6050_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MPU6050 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MPU6050_CMD_H_ 8 | #define _COMMANDS_MPU6050_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MPU6050) && defined(DH_DEVICE_MPU6050) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/mpu6050/read" command. 16 | */ 17 | void dh_handle_devices_mpu6050_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_MPU6050 && DH_DEVICE_MPU6050 */ 21 | #endif /* _COMMANDS_MPU6050_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mcp4725_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MCP4725 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MCP4725_CMD_H_ 8 | #define _COMMANDS_MCP4725_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MCP4725) && defined(DH_DEVICE_MCP4725) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/mcp4725/write" command. 16 | */ 17 | void dh_handle_devices_mcp4725_write(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_MCP4725 && DH_DEVICE_MCP4725 */ 21 | #endif /* _COMMANDS_MCP4725_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/dhcommands.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhcommands.h 3 | * \brief Handle commands from remote server. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHCOMMANDS_H_ 10 | #define _DHCOMMANDS_H_ 11 | 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * \brief Handle remote command. 16 | * \param[in] cb Callback descriptor for result. 17 | * \param[in] command Null terminated string with command. 18 | * \param[in] params Pointer to JSON with parameters. 19 | * \param[in] paramslen JSON parameters length in bytes. 20 | */ 21 | void dhcommands_do(COMMAND_RESULT *cb, const char *command, const char *params, 22 | unsigned int paramslen); 23 | 24 | #endif /* _DHCOMMANDS_H_ */ 25 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pca9685_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief PCA9685 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_PCA9685_CMD_H_ 8 | #define _COMMANDS_PCA9685_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_PCA9685) && defined(DH_DEVICE_PCA9685) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/pca9685/control" command. 16 | */ 17 | void dh_handle_devices_pca9685_control(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_PCA9685 && DH_DEVICE_PCA9685 */ 21 | #endif /* _COMMANDS_PCA9685_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/uploadable_api.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file httpd.h 3 | * \brief Module for handling uploadable HTTP API calls. 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | #ifndef _UPLOADABLE_API_H_ 9 | #define _UPLOADABLE_API_H_ 10 | 11 | #include "httpd.h" 12 | 13 | /** 14 | * \brief Handle rest request. 15 | * \param[in] path Url path. 16 | * \param[in] key Access key for API which was given in request. 17 | * \param[in] content_in Request body, typically json. 18 | * \return One of httpd statuses. 19 | */ 20 | 21 | HTTP_RESPONSE_STATUS uploadable_api_handle(const char *path, const char *key, 22 | HTTP_CONTENT *content_in, HTTP_ANSWER *answer); 23 | 24 | #endif /* _UPLOADABLE_API_H_ */ 25 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/hmc5883l_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief HMC5883L command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_HMC5883L_CMD_H_ 8 | #define _COMMANDS_HMC5883L_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_HMC5883L) && defined(DH_DEVICE_HMC5883L) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/hmc5883l/read" command. 16 | */ 17 | void dh_handle_devices_hmc5883l_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_HMC5883L && DH_DEVICE_HMC5883L */ 21 | #endif /* _COMMANDS_HMC5883L_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/max31855_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MAX31855 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MAX31855_CMD_H_ 8 | #define _COMMANDS_MAX31855_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MAX31855) && defined(DH_DEVICE_MAX31855) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/max31855/read" command. 16 | */ 17 | void dh_handle_devices_max31855_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_MAX31855 && DH_DEVICE_MAX31855 */ 21 | #endif /* _COMMANDS_MAX31855_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mlx90614_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MLX90614 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MLX90614_CMD_H_ 8 | #define _COMMANDS_MLX90614_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MLX90614) && defined(DH_DEVICE_MLX90614) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/mlx90614/read" command. 16 | */ 17 | void dh_handle_devices_mlx90614_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_MLX90614 && DH_DEVICE_MLX90614 */ 21 | #endif /* _COMMANDS_MLX90614_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/ds18b20.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with DS18B20 temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_DS18B20_H_ 8 | #define _DEVICES_DS18B20_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_DS18B20) 12 | 13 | /** 14 | * @brief Measure temperature one time. 15 | * @param[in] pin 1-wire pin for communication. Can be DH_ONEWIRE_NO_PIN. 16 | * @param[out] temperature Pointer to store measure result in degree Celsius. 17 | * @return NULL on success, text description on error. 18 | */ 19 | const char* ds18b20_read(int pin, float *temperature); 20 | 21 | #endif /* DH_DEVICE_DS18B20 */ 22 | #endif /* _DEVICES_DS18B20_H_ */ 23 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ws2812b_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Onewire WS2812B command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_WS2812B_CMD_H_ 8 | #define _COMMANDS_WS2812B_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_ONEWIRE) // onewire WS2812B command handlers 12 | #include "dhsender_data.h" 13 | #include "dhcommand_parser.h" 14 | 15 | /** 16 | * @brief Handle "onewire/ws2812b/write" command. 17 | */ 18 | void dh_handle_onewire_ws2812b_write(COMMAND_RESULT *cmd_res, const char *command, 19 | const char *params, unsigned int params_len); 20 | 21 | #endif /* DH_COMMANDS_ONEWIRE */ 22 | #endif /* _COMMANDS_WS2812B_CMD_H_ */ 23 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/max6675.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MAX6675 K-thermocouple temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_MAX6675_H_ 8 | #define _DEVICES_MAX6675_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_MAX6675) 12 | 13 | /** 14 | * @brief Measure temperature one time. 15 | * @param[in] cs Chip select pin. Can be DH_SPI_NO_PIN to disable CS. 16 | * @param[out] temperature Pointer to store measure result in degree Celsius. 17 | * @return NULL on success, text description on error. 18 | */ 19 | const char* max6675_read(int pin, float *temperature); 20 | 21 | #endif /* DH_DEVICE_MAX6675 */ 22 | #endif /* SOURCES_DEVICES_MAX6675_H_ */ 23 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/max31855.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file max31855.h 3 | * @brief Simple communication with MAX31855 thermocouple temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_MAX31855_H_ 8 | #define _DEVICES_MAX31855_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_MAX31855) 12 | 13 | /** 14 | * @brief Measure temperature one time. 15 | * @param[in] cs Chip select pin. Can be DH_SPI_NO_PIN to disable CS. 16 | * @param[out] temperature Pointer to store measure result in degree Celsius. 17 | * @return NULL on success, text description on error. 18 | */ 19 | const char* max31855_read(int pin, float *temperature); 20 | 21 | #endif /* DH_DEVICE_MAX31855 */ 22 | #endif /* _DEVICES_MAX31855_H_ */ 23 | -------------------------------------------------------------------------------- /firmware-src/sources/dhmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dhmem.c 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: Memory observer module 9 | * 10 | */ 11 | #include "dhmem.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | static int mGlobalBlock = 0; 18 | 19 | void ICACHE_FLASH_ATTR dhmem_block(void) { 20 | mGlobalBlock = 1; 21 | ETS_GPIO_INTR_DISABLE(); 22 | } 23 | 24 | void ICACHE_FLASH_ATTR dhmem_unblock(void) { 25 | mGlobalBlock = 0; 26 | dhmem_unblock_cb(); 27 | ETS_GPIO_INTR_ENABLE(); 28 | } 29 | 30 | int ICACHE_FLASH_ATTR dhmem_isblock(void) { 31 | return mGlobalBlock; 32 | } 33 | 34 | void ICACHE_FLASH_ATTR dhmem_unblock_cb(void) { 35 | // do nothing, move this method to any module if needed 36 | } 37 | -------------------------------------------------------------------------------- /firmware-src/sources/mdnsd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file mdnsd.h 3 | * \brief Simple mDNS implementation 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _MDNS_H_ 10 | #define _MDNS_H_ 11 | 12 | /** 13 | * \brief Initialize mDNS daemon. 14 | * \details Must be called after getting IP address. 1st level domain is always 'local'. 15 | * Max length is 64 bytes including zero terminated char. 16 | * \param[in] name Pointer to static string with 2nd level domain name. 17 | * \param[in] addr Corresponding IP address. 18 | * \return Zero on error, non-zero on success. 19 | */ 20 | int mdnsd_start(const char *name, unsigned long addr); 21 | 22 | /** 23 | * \brief Stops mDNS daemon. 24 | */ 25 | void mdnsd_stop(void); 26 | 27 | #endif /* _MDNS_H_ */ 28 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pcf8574_hd44780_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief HD44780 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_PCF8574_HD44780_CMD_H_ 8 | #define _COMMANDS_PCF8574_HD44780_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_PCF8574_HD44780) && defined(DH_DEVICE_PCF8574_HD44780) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/pcf8574/hd44780/write" command. 16 | */ 17 | void dh_handle_devices_pcf8574_hd44780_write(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | #endif /* DH_COMMANDS_PCF8574_HD44780 && DH_DEVICE_PCF8574_HD44780 */ 21 | #endif /* _COMMANDS_PCF8574_HD44780_CMD_H_ */ 22 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/adc_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief ADC command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_ADC_CMD_H_ 8 | #define _COMMANDS_ADC_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_ADC) // ADC command handlers 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "adc/read" command. 16 | */ 17 | void dh_handle_adc_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "adc/int" command. 23 | */ 24 | void dh_handle_adc_int(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | #endif /* DH_COMMANDS_ADC */ 28 | #endif /* _COMMANDS_ADC_CMD_H_ */ 29 | -------------------------------------------------------------------------------- /firmware-src/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | DeviceHive 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /firmware-src/sources/dhdebug.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhdebug.h 3 | * \brief Print debug text. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHDEBUG_H_ 10 | #define _DHDEBUG_H_ 11 | 12 | #include "irom.h" 13 | 14 | /** 15 | * \brief Print to debug output. 16 | * \param[in] fmt Format. 17 | * \param[in] ... Additional arguments specified by format 18 | */ 19 | void dhdebug_ram(const char *fmt, ...); 20 | 21 | /** The same as dhdebug_ram, but forces to store format string in rom */ 22 | #define dhdebug(fmt, ...) do { \ 23 | RO_DATA char str[] = fmt; \ 24 | dhdebug_ram(str, ##__VA_ARGS__); } while(0) 25 | 26 | /** 27 | * \brief Print dumped data in hex. 28 | * \param[in] data Data for dump. 29 | * \param[in] len Data size in bytes. 30 | */ 31 | void dhdebug_dump(const char *data, unsigned int len); 32 | 33 | #endif /* _DHDEBUG_H_ */ 34 | -------------------------------------------------------------------------------- /firmware-src/sources/swab.c: -------------------------------------------------------------------------------- 1 | /** @file 2 | * @brief Helper module to work with big-endian/little-endian numbers. 3 | * @copyright 2017 [DeviceHive](http://devicehive.com) 4 | * @author Sergey Polichnoy 5 | */ 6 | #include "swab.h" 7 | 8 | 9 | /** 10 | * @brief Swap byte-order of 32-bits value. 11 | * @param[in] x Input value: `0xAABBCCDD`. 12 | * @return Output value: `0xDDCCBBAA`. 13 | */ 14 | uint32_t ICACHE_FLASH_ATTR swab_u32(uint32_t x) 15 | { 16 | return (((x>>24)&0xFF) << 0) 17 | | (((x>>16)&0xFF) << 8) 18 | | (((x>>8)&0xFF) << 16) 19 | | (((x>>0)&0xFF) << 24); 20 | } 21 | 22 | 23 | /** 24 | * @brief Swap byte-order of 16-bits value. 25 | * @param[in] x Input value: `0xAABB`. 26 | * @return Output value: `0xBBAA`. 27 | */ 28 | uint16_t ICACHE_FLASH_ATTR swab_u16(uint16_t x) 29 | { 30 | return (((x>>8)&0xFF) << 0) 31 | | (((x>>0)&0xFF) << 8); 32 | } 33 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/spi_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief SPI command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_SPI_CMD_H_ 8 | #define _COMMANDS_SPI_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_SPI) // SPI command handlers 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "spi/master/read" command. 16 | */ 17 | void dh_handle_spi_master_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "spi/master/write" command. 23 | */ 24 | void dh_handle_spi_master_write(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | #endif /* DH_COMMANDS_SPI */ 28 | #endif /* _COMMANDS_SPI_CMD_H_ */ 29 | -------------------------------------------------------------------------------- /esp-utils/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | esp-utils 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /firmware-src/sources/DH/adc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief ADC hardware access layer for ESP8266 firmware. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DH_ADC_H_ 8 | #define _DH_ADC_H_ 9 | 10 | /** 11 | * @brief ADC suitable channels. 12 | */ 13 | #define DH_ADC_SUITABLE_PINS 0x01 // ADC0 14 | 15 | 16 | /** 17 | * @brief Get ADC value. 18 | * @return ADC voltage in volts. 19 | */ 20 | float dh_adc_get_value(void); 21 | 22 | 23 | /** 24 | * @brief Start ADC loop measurement. 25 | * @param[in] period_ms Measurement interval in milliseconds. 26 | */ 27 | void dh_adc_loop(unsigned int period_ms); 28 | 29 | 30 | /** 31 | * @brief Callback function for loop measurement. 32 | * @param[in] value ADC voltage in volts. 33 | */ 34 | // TODO: consider to use callback function pointer 35 | extern void dh_adc_loop_value_cb(float value); 36 | 37 | #endif /* _DH_ADC_H_ */ 38 | -------------------------------------------------------------------------------- /firmware-src/sources/dhesperrors.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhesperrors.h 3 | * \brief Print human readable error in debug. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHESPERRORS_H_ 10 | #define _DHESPERRORS_H_ 11 | 12 | #include 13 | 14 | /** 15 | * \brief Print disconnect reason. 16 | * \param[in] descrption Text that will be printed before error description. 17 | * \param[in] reason Disconnect reason. 18 | */ 19 | void dhesperrors_disconnect_reason(const char *descrption, uint8_t reason); 20 | 21 | /** 22 | * \brief Print espconn result. 23 | * \param[in] descrption Text that will be printed before error description. 24 | * \param[in] reason espconn result. 25 | */ 26 | void dhesperrors_espconn_result(const char *descrption, int reason); 27 | 28 | void dhesperrors_wifi_state(const char *descrption, uint8_t reason); 29 | 30 | #endif /* _DHESPERRORS_H_ */ 31 | -------------------------------------------------------------------------------- /firmware-src/sources/dhmem.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhmem.h 3 | * \brief Memory manager helper. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | * \details Observer current memory status, and blocks firmware from generating new objects until memory will be free. 8 | */ 9 | 10 | #ifndef _DHMEM_H_ 11 | #define _DHMEM_H_ 12 | 13 | /** 14 | * \brief Activate memory save mode 15 | */ 16 | void dhmem_block(void); 17 | 18 | /** 19 | * \brief Deactivate memory save mode 20 | */ 21 | void dhmem_unblock(void); 22 | 23 | /** 24 | * \brief Check memory status good and new request allocation is possible. 25 | * \return Non zero if memory status is bad and allocation of new requests should be blocked. Zero otherwise. 26 | */ 27 | int dhmem_isblock(void); 28 | 29 | /** 30 | * \brief Callback that will be called when allocation is possible again. 31 | */ 32 | void dhmem_unblock_cb(void); 33 | 34 | #endif /* _DHMEM_H_ */ 35 | -------------------------------------------------------------------------------- /firmware-src/sources/dhterminal_commandline.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhterminal_commandline.h 3 | * \brief Commands interpreter for terminal. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHTERMINAL_COMMANDLINE_H_ 10 | #define _DHTERMINAL_COMMANDLINE_H_ 11 | 12 | /** 13 | * \brief Perform command from terminal 14 | * \param[in] command String with command. 15 | */ 16 | void dhterminal_commandline_do(const char *command); 17 | 18 | /** 19 | * \brief Terminal commands autocompleter. 20 | * \details Functions remember previous return value and next call will happen with the same value, function will continue search with original pattern. 21 | * \param[in] pattern Beginning of command. 22 | * \return String with matching command or zero if no commands found. 23 | */ 24 | char *dhterminal_commandline_autocompleter(const char *pattern); 25 | 26 | #endif /* _DHTERMINAL_COMMANDLINE_H_ */ 27 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/bh1750.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with BH1750 illuminance sensor 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_BH1750_H_ 8 | #define _DEVICES_BH1750_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_BH1750) 12 | 13 | /** 14 | * @brief Measure illuminance one time. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] illuminance Illuminance in lux. 18 | * @return Status value, one of DH_I2C_Status enum. 19 | */ 20 | int bh1750_read(int sda, int scl, float illuminance[1]); 21 | 22 | 23 | /** 24 | * @brief Set sensor address which should be used while reading. 25 | * @param[in] address I2C end device address. 26 | */ 27 | void bh1750_set_address(int address); 28 | 29 | #endif /* DH_DEVICE_BH1750 */ 30 | #endif /* _DEVICES_BH1750_H_ */ 31 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/tm1637.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with TM1637 LED 8 segment driver. 4 | * @copyright 2017 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_TM1637_H_ 8 | #define _DEVICES_TM1637_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_TM1637) 12 | 13 | #include 14 | 15 | /** 16 | * @brief Set text on screen. 17 | * 18 | * Old text will be erased. 19 | * 20 | * @param[in] sda Pin for I2C's SDA. Use DH_I2C_NO_PIN to prevent initialization. 21 | * @param[in] scl Pin for I2C's SCL. Use DH_I2C_NO_PIN to prevent initialization. 22 | * @param[in] text Chars to write on display. Only digits and colon are allowed. 23 | * @param[in] len Number of chars to write. 24 | * @return Status value, one of DH_I2C_Status enum. 25 | */ 26 | int tm1636_write(int sda, int scl, const char *text, size_t len); 27 | 28 | #endif /* DH_DEVICE_TM1637 */ 29 | #endif /* _DEVICES_TM1637_H_ */ 30 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/lm75.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with LM75 temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_LM75_H_ 8 | #define _DEVICES_LM75_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_LM75) 12 | 13 | /** 14 | * @brief Measure temperature one time. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] temperature Pointer for storing temperature result measure in Celsius. 18 | * @return Status value, one of DH_I2C_Status enum. 19 | */ 20 | int lm75_read(int sda, int scl, float *temperature); 21 | 22 | 23 | /** 24 | * @brief Set sensor address which should be used while reading. 25 | * @param[in] address I2C end device address. 26 | */ 27 | void lm75_set_address(int address); 28 | 29 | #endif /* DH_DEVICE_LM75 */ 30 | #endif /* _DEVICES_LM75_H_ */ 31 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/ads1115.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with ADS1115 ADC. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _SOURCES_DEVICES_ADS1115_H_ 8 | #define _SOURCES_DEVICES_ADS1115_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_ADS1115) 12 | 13 | /** 14 | * @brief Get ADC voltages. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] values Pointer to four float values to store result in Volts. 18 | * @return Status value, one of DH_I2C_Status enum. 19 | */ 20 | int ads1115_read(int sda, int scl, float values[4]); 21 | 22 | 23 | /** 24 | * @brief Set sensor address which should be used while reading. 25 | * @param[in] address I2C end device address. 26 | */ 27 | void ads1115_set_address(int address); 28 | 29 | #endif /* DH_DEVICE_ADS1115 */ 30 | #endif /* _SOURCES_DEVICES_ADS1115_H_ */ 31 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pcf8574_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief PCF8574 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_PCF8574_CMD_H_ 8 | #define _COMMANDS_PCF8574_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_PCF8574) && defined(DH_DEVICE_PCF8574) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/pcf8574/read" command. 16 | */ 17 | void dh_handle_devices_pcf8574_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "devices/pcf8574/write" command. 23 | */ 24 | void dh_handle_devices_pcf8574_write(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | #endif /* DH_COMMANDS_PCF8574 && DH_DEVICE_PCF8574 */ 28 | #endif /* _COMMANDS_PCF8574_CMD_H_ */ 29 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pcf8591_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief PCF8591 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_PCF8591_CMD_H_ 8 | #define _COMMANDS_PCF8591_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_PCF8591) && defined(DH_DEVICE_PCF8591) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/pcf8591/read" command. 16 | */ 17 | void dh_handle_devices_pcf8591_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "devices/pcf8591/write" command. 23 | */ 24 | void dh_handle_devices_pcf8591_write(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | #endif /* DH_COMMANDS_PCF8591 && DH_DEVICE_PCF8591 */ 28 | #endif /* _COMMANDS_PCF8591_CMD_H_ */ 29 | -------------------------------------------------------------------------------- /esp-utils/Makefile: -------------------------------------------------------------------------------- 1 | CXX ?= g++ 2 | CFLAGS += -c -Wall 3 | LDFLAGS += -Os 4 | LIBS = 5 | OBJDIR = build 6 | SOURCES = $(wildcard common/*.cpp) 7 | COMMONOBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:%.cpp=%.o)) 8 | MAINS = $(wildcard *.cpp) 9 | ALLOBJECTS = $(COMMONOBJECTS) $(addprefix $(OBJDIR)/, $(MAINS:%.cpp=%.o)) 10 | ESPTERMINAL = $(OBJDIR)/esp-terminal 11 | ESPFLASHER = $(OBJDIR)/esp-flasher 12 | ifeq ($(OS),Windows_NT) 13 | LDFLAGS += -static -s 14 | else 15 | ifneq ($(shell uname -s),Darwin) 16 | LDFLAGS += -s 17 | LIBS = -pthread 18 | endif 19 | endif 20 | 21 | all: $(SOURCES) $(ESPTERMINAL) $(ESPFLASHER) 22 | 23 | $(ESPTERMINAL): $(ALLOBJECTS) 24 | @echo "LD $@" 25 | @$(CXX) $(LDFLAGS) $(LIBS) $(COMMONOBJECTS) $@.o -o $@ 26 | 27 | $(ESPFLASHER): $(ALLOBJECTS) 28 | @echo "LD $@" 29 | @$(CXX) $(LDFLAGS) $(LIBS) $(COMMONOBJECTS) $@.o -o $@ 30 | 31 | $(OBJDIR)/%.o: %.cpp 32 | @echo "CXX $<" 33 | @mkdir -p $(dir $@) 34 | @$(CXX) $(CFLAGS) $< -o $@ 35 | 36 | rebuild: clean all 37 | 38 | clean: 39 | @rm -rf $(OBJDIR) 40 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mfrc522_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MFRC522 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_MFRC522_CMD_H_ 8 | #define _COMMANDS_MFRC522_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_MFRC522) && defined(DH_DEVICE_MFRC522) 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "devices/mfrc522/read" command. 16 | */ 17 | void dh_handle_devices_mfrc522_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "devices/mfrc522/mifare/read" and "devices/mfrc522/mifare/write" commands. 23 | */ 24 | void dh_handle_devices_mfrc522_mifare_read_write(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | #endif /* DH_COMMANDS_MFRC522 && DH_DEVICE_MFRC522 */ 28 | #endif /* _COMMANDS_MFRC522_CMD_H_ */ 29 | -------------------------------------------------------------------------------- /firmware-src/sources/DH/adc.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief ADC hardware access layer for ESP8266 firmware. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "DH/adc.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // module variables 16 | static os_timer_t mTimer; 17 | 18 | /* 19 | * dh_adc_get_value() implementation. 20 | */ 21 | float ICACHE_FLASH_ATTR dh_adc_get_value(void) 22 | { 23 | return system_adc_read() / 1024.0f; 24 | } 25 | 26 | 27 | /** 28 | * @brief Timeout callback. 29 | */ 30 | static void ICACHE_FLASH_ATTR timeout_cb(void *arg) 31 | { 32 | // call external handler 33 | dh_adc_loop_value_cb(dh_adc_get_value()); 34 | } 35 | 36 | 37 | /* 38 | * dh_adc_loop() implementation. 39 | */ 40 | void ICACHE_FLASH_ATTR dh_adc_loop(unsigned int period_ms) 41 | { 42 | os_timer_disarm(&mTimer); 43 | if (period_ms) { 44 | os_timer_setfn(&mTimer, timeout_cb, NULL); 45 | os_timer_arm(&mTimer, period_ms, 1); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/pcf8574_hd44780.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with HD44780 like displays via PCF8574 GPIO extender with I2C bus. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_PCF8574_HD44780_H_ 8 | #define _DEVICES_PCF8574_HD44780_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_PCF8574_HD44780) 12 | 13 | #include 14 | 15 | /** 16 | * @brief Set text on screen. 17 | * 18 | * Old text will be erased. '\n'(0x0A) char is supported. 19 | * 20 | * @param[in] sda Pin for I2C's SDA. Use DH_I2C_NO_PIN to prevent initialization. 21 | * @param[in] scl Pin for I2C's SCL. Use DH_I2C_NO_PIN to prevent initialization. 22 | * @param[in] text Chars to write on display. 23 | * @param[in] len Number of chars to write. Data more then 80 bytes is ignored. 24 | * @return Status value, one of DH_I2C_Status enum. 25 | */ 26 | int pcf8574_hd44780_write(int sda, int scl, const char *text, int len); 27 | 28 | #endif /* DH_DEVICE_PCF8574_HD44780 */ 29 | #endif /* _DEVICES_PCF8574_HD44780_H_ */ 30 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/gpio_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief GPIO command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_GPIO_CMD_H_ 8 | #define _COMMANDS_GPIO_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_GPIO) // GPIO command handlers 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "gpio/write" command. 16 | */ 17 | void dh_handle_gpio_write(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "gpio/read" command. 23 | */ 24 | void dh_handle_gpio_read(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | 28 | /** 29 | * @brief Handle "gpio/int" command. 30 | */ 31 | void dh_handle_gpio_int(COMMAND_RESULT *cmd_res, const char *command, 32 | const char *params, unsigned int params_len); 33 | 34 | #endif /* DH_COMMANDS_GPIO */ 35 | #endif /* _COMMANDS_GPIO_CMD_H_ */ 36 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mhz19_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MHZ19 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/mhz19_cmd.h" 8 | #include "devices/mhz19.h" 9 | 10 | #include "dhcommand_parser.h" 11 | #include 12 | 13 | #if defined(DH_COMMANDS_MHZ19) && defined(DH_DEVICE_MHZ19) 14 | 15 | /* 16 | * dh_handle_devices_mhz19_read() implementation. 17 | */ 18 | void ICACHE_FLASH_ATTR dh_handle_devices_mhz19_read(COMMAND_RESULT *cmd_res, const char *command, 19 | const char *params, unsigned int params_len) 20 | { 21 | if (params_len) { 22 | dh_command_fail(cmd_res, "Command does not have parameters"); 23 | return; // FAILED 24 | } 25 | 26 | int co2; 27 | const char *err_msg = mhz19_read(&co2); 28 | if (err_msg != 0) { 29 | dh_command_fail(cmd_res, err_msg); 30 | } else { 31 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, 32 | RDT_FORMAT_JSON, "{\"co2\":%d}", co2); 33 | } 34 | } 35 | 36 | #endif /* DH_COMMANDS_MHZ19 && DH_DEVICE_MHZ19 */ 37 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mlx90614.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MLX90614 contactless IR temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_MLX90614_H_ 8 | #define _DEVICES_MLX90614_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_MLX90614) 12 | 13 | /** 14 | * @brief Measure temperature one time. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] ambient Pointer for storing ambient temperature result measure in Celsius. 18 | * @param[out] object Pointer for storing object temperature result measure in Celsius. 19 | * @return Status value, one of DH_I2C_Status enum. 20 | */ 21 | int mlx90614_read(int sda, int scl, float *ambient, float *object); 22 | 23 | 24 | /** 25 | * @brief Set sensor address which should be used while reading. 26 | * @param[in] address I2C end device address. 27 | */ 28 | void mlx90614_set_address(int address); 29 | 30 | #endif /* DH_DEVICE_MLX90614 */ 31 | #endif /* _DEVICES_MLX90614_H_ */ 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 DeviceHive 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /firmware-src/sources/dhzc_pages.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhzc_pages.h 3 | * \brief HTML pages for zero configuration. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHZC_PAGES_H_ 10 | #define _DHZC_PAGES_H_ 11 | 12 | /** 13 | * \brief Get page HTML with error. 14 | * \param[out] error Pointer to string with error description 15 | * \param[out] len Pointer to page size in bytes. 16 | * \return Pointer to page or zero if there is not enough memory. 17 | */ 18 | const char * dhzc_pages_error(const char *error, unsigned int *len); 19 | 20 | /** 21 | * \brief Get page HTML with data accepted result. 22 | * \param[out] len Pointer to page size in bytes. 23 | * \return Pointer to page or zero if there is not enough memory. 24 | */ 25 | const char * dhzc_pages_ok(unsigned int *len); 26 | 27 | /** 28 | * \brief Get page HTML with configuration form. 29 | * \param[out] size Pointer to page size in bytes. 30 | * \return Pointer to page or zero if there is not enough memory. 31 | */ 32 | const char * dhzc_pages_form(unsigned int *len); 33 | 34 | #endif /* _DHZC_PAGES_H_ */ 35 | -------------------------------------------------------------------------------- /esp-utils/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 DeviceHive 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /firmware-src/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 DeviceHive 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/bmp180.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with BMP180 pressure sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_BMP180_H_ 8 | #define _DEVICES_BMP180_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_BMP180) 12 | 13 | /** 14 | * @brief Measure pressure one time. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] pressure Pointer for storing pressure result measure in Pascals. 18 | * @param[out] temperature Pointer for storing temperature result measure in degree Celsius. Can be NULL. 19 | * @return Status value, one of DH_I2C_Status enum. 20 | */ 21 | int bmp180_read(int sda, int scl, 22 | int *pressure, 23 | float *temperature); 24 | 25 | 26 | /** 27 | * @brief Set sensor address which should be used while reading. 28 | * @param[in] address I2C end device address. 29 | */ 30 | void bmp180_set_address(int address); 31 | 32 | #endif /* DH_DEVICE_BMP180 */ 33 | #endif /* _DEVICES_BMP180_H_ */ 34 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/bmp280.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with BMP280 pressure sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_BMP280_H_ 8 | #define _DEVICES_BMP280_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_BMP280) 12 | 13 | /** 14 | * @brief Measure pressure one time. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] pressure Pointer for storing pressure result measure in Pascals. 18 | * @param[out] temperature Pointer for storing temperature result measure in degree Celsius. Can be NULL. 19 | * @return Status value, one of DH_I2C_Status enum. 20 | */ 21 | int bmp280_read(int sda, int scl, 22 | float *pressure, 23 | float *temperature); 24 | 25 | 26 | /** 27 | * @brief Set sensor address which should be used while reading. 28 | * @param[in] address I2C end device address. 29 | */ 30 | void bmp280_set_address(int address); 31 | 32 | #endif /* DH_DEVICE_BMP280 */ 33 | #endif /* _DEVICES_BMP280_H_ */ 34 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/si7021.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with SI7021 relative humidity and temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_SI7021_H_ 8 | #define _DEVICES_SI7021_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_SI7021) 12 | 13 | /** 14 | * @brief Measure temperature and relative humidity one time. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] humidity Pointer for storing relative humidity result measure in percents. 18 | * @param[out] temperature Pointer for storing temperature result measure in degree Celsius. Can be NULL. 19 | * @return Status value, one of DH_I2C_Status enum. 20 | */ 21 | int si7021_read(int sda, int scl, float *humidity, float *temperature); 22 | 23 | 24 | /** 25 | * @brief Set sensor address which should be used while reading. 26 | * @param[in] address I2C end device address. 27 | */ 28 | void si7021_set_address(int address); 29 | 30 | #endif /* DH_DEVICE_SI7021 */ 31 | #endif /* _DEVICES_SI7021_H_ */ 32 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mcp4725.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MCP4725 DAC. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_MCP4725_H_ 8 | #define _DEVICES_MCP4725_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_MCP4725) 12 | 13 | /** 14 | * @brief Set DAC voltages. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] value Voltage in Volts. 18 | * @return Status value, one of DH_I2C_Status enum. 19 | */ 20 | int mcp4725_write(int sda, int scl, float value); 21 | 22 | 23 | /** 24 | * @brief Set sensor address which should be used while reading. 25 | * @param[in] address I2C end device address. 26 | */ 27 | void mcp4725_set_address(int address); 28 | 29 | 30 | /** 31 | * @brief Set reference voltage in volts. 32 | * 33 | * Default is 3.3V. 34 | * 35 | * @param[in] voltage Voltage in Volts. 36 | * @return Status value, one of DH_I2C_Status enum. 37 | */ 38 | int mcp4725_set_vref(float voltage); 39 | 40 | #endif /* DH_DEVICE_MCP4725 */ 41 | #endif /* _DEVICES_MCP4725_H_ */ 42 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/i2c_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief I2C command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_I2C_CMD_H_ 8 | #define _COMMANDS_I2C_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_I2C) // I2C command handlers 12 | #include "dhcommand_parser.h" 13 | #include "dhsender_data.h" 14 | 15 | /** 16 | * @brief Helper function to initialize I2C bus. 17 | * @return Non-zero if I2C was initialized. Zero otherwise. 18 | */ 19 | int dh_i2c_init_helper(COMMAND_RESULT *cmd_res, ALLOWED_FIELDS fields, 20 | const gpio_command_params *params); 21 | 22 | 23 | /** 24 | * @brief Handle "i2c/master/read" command. 25 | */ 26 | void dh_handle_i2c_master_read(COMMAND_RESULT *cmd_res, const char *command, 27 | const char *params, unsigned int params_len); 28 | 29 | 30 | /** 31 | * @brief Handle "i2c/master/write" command. 32 | */ 33 | void dh_handle_i2c_master_write(COMMAND_RESULT *cmd_res, const char *command, 34 | const char *params, unsigned int params_len); 35 | 36 | #endif /* DH_COMMANDS_I2C */ 37 | #endif /* _COMMANDS_I2C_CMD_H_ */ 38 | -------------------------------------------------------------------------------- /sdk/lib/readme.md: -------------------------------------------------------------------------------- 1 | # About libc.a and libgcc.a 2 | 3 | In order to reduce the size of **.text**, we remove the functions which already in rom code. 4 | 5 | The removed functions in libgcc.a: 6 | 7 | ``` 8 | __addsubdf3 9 | __addsubsf3 10 | __divdf3 11 | __divdi3 12 | __divsi3 13 | __extendsfdf2 14 | __fixdfsi 15 | __fixunsdfsi 16 | __fixunssfsi 17 | __floatsidf 18 | __floatsisf 19 | __floatunsidf 20 | __floatunsisf 21 | __muldf3 22 | __muldi3 23 | __mulsf3 24 | __truncdfsf2 25 | __udivdi3 26 | __udivsi3 27 | __umoddi3 28 | __umodsi3 29 | __umulsidi3 30 | ``` 31 | 32 | The removed functions in libc.a: 33 | 34 | ``` 35 | bzero 36 | memcmp 37 | memcpy 38 | memmove 39 | memset 40 | strcmp 41 | strcpy 42 | strlen 43 | strncmp 44 | strncpy 45 | strstr 46 | ``` 47 | 48 | ## How to remove the functions in those two lib. 49 | 50 | The libc.a in SDK is compiled from newlib v2.0.0, libgcc.a is compiled from gcc v4.8.5. 51 | If you use other version gcc and newlib, you can follow those commands to strip the functions. 52 | 53 | ``` 54 | cp $(TOOLCHAIN)/lib/gcc/xtensa-lx106-elf//libgcc.a . 55 | xtensa-lx106-elf-ar -M < strip_libgcc_funcs.txt 56 | 57 | cp $(TOOLCHAIN)/xtensa-lx106-elf/lib/libc.a . 58 | xtensa-lx106-elf-ar -M < strip_libc_funcs.txt 59 | ``` -------------------------------------------------------------------------------- /firmware-src/pages/gen_pages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Util for generating .h file from html fils for ESP8266 Device Hive firmware 4 | 5 | set -e 6 | 7 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 8 | 9 | TARGETFILE="$DIR/pages.h" 10 | 11 | print() { 12 | echo "$@" >> $TARGETFILE 13 | } 14 | 15 | echo "/* This is autogenerated file. Do not modify directly. */" > $TARGETFILE 16 | print "#ifndef _PAGES_H_" 17 | print "#define _PAGES_H_" 18 | print '#include "../sources/irom.h"' 19 | print "typedef struct {const char *path; const char *data; unsigned int data_len;} WEBPAGE;" 20 | 21 | index="WEBPAGE web_pages[] = { " 22 | comma="" 23 | FILE_LIST=$(find $DIR -name \*.html -o -name \*.css -o -name \*.js) 24 | FILE_LIST="$FILE_LIST $DIR/favicon.ico" 25 | for file in $FILE_LIST; do 26 | filename=$(basename "$file") 27 | echo "Parsing $filename ..." 28 | name=${filename/./_} 29 | data=$(gzip -c $file | od -An -v -t x1 | tr -d '\n' | sed -E "s/( +)([[:xdigit:]]{2})/\\\x\2/g") 30 | print "RO_DATA char $name[] = \"$data\";" 31 | index="$index$comma {\"$filename\", $name, sizeof($name) - 1}" 32 | comma=", " 33 | done 34 | print "$index };" 35 | print "#endif /* _PAGES_H_ */" 36 | echo "$(basename $TARGETFILE) successfully generated." 37 | -------------------------------------------------------------------------------- /firmware-src/sources/rand.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file rand.h 3 | * \brief Random implementation. 4 | * \details Pseudo-randomized numbers generator based on linear congruential generator. 5 | * \author Nikolay Khabarov 6 | * \date 2015 7 | * \copyright Public Domain 8 | */ 9 | 10 | #ifndef _RAND_H_ 11 | #define _RAND_H_ 12 | 13 | /** Maximum random values count.*/ 14 | #define RAND_MAX 32767 15 | 16 | /** 17 | * \brief Generate random value. 18 | * \return Value in range of 0..(RAND_MAX - 1). 19 | */ 20 | int rand(void); 21 | 22 | /** 23 | * \brief Generate random device key. 24 | * \details Key length is variable. 25 | * \param[out] buf Pointer to buf where data will be copied. Should be at least 17 bytes. Pass zero to emulate and find out max size. 26 | * \return Number of char was copied to buf, excluding null terminated. 27 | */ 28 | unsigned int rand_generate_key(char *buf); 29 | 30 | /** 31 | * \brief Generate random device id. 32 | * \param[out] buf Pointer to buf where data will be copied. Should be at least 20 bytes. Pass zero to emulate and find out max size. 33 | * \return Number of char was copied to buf, excluding null terminated, always 19. 34 | */ 35 | unsigned int rand_generate_deviceid(char *buf); 36 | 37 | #endif /* _RAND_H_ */ 38 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mhz19.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MHZ-19 CO2 sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/mhz19.h" 8 | #include "DH/uart.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_MHZ19) 15 | 16 | /* 17 | * mhz19_read() implementation. 18 | */ 19 | const char* ICACHE_FLASH_ATTR mhz19_read(int *co2) 20 | { 21 | dh_uart_set_mode(DH_UART_MODE_PER_BUF); 22 | if (!!dh_uart_init(9600, 8, 'N', 1)) 23 | return "failed to init UART"; 24 | 25 | const uint8_t request[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; 26 | dh_uart_send_buf(request, sizeof(request)); 27 | delay_ms(20); 28 | 29 | uint8_t *result = 0; 30 | size_t len = dh_uart_get_buf((void**)&result); 31 | if (len != 9){ 32 | return len ? "Response length mismatch" 33 | : "No response"; 34 | } 35 | 36 | int i; 37 | uint8_t cs = 0; 38 | for(i = 1; i < 8; i++) { 39 | cs += result[i]; 40 | } 41 | cs = ~cs; 42 | cs++; 43 | if (cs != result[8]) { 44 | return "Checksum mismatch"; 45 | } 46 | 47 | *co2 = unsignedInt16be((const char*)result, 2); 48 | return NULL; // OK 49 | } 50 | 51 | #endif /* DH_DEVICE_MHZ19 */ 52 | -------------------------------------------------------------------------------- /firmware-src/sources/dhconnector_websocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \file dhconnector_websocket.h 3 | * \brief Main connectivity to DeviceHive with WebSocket 4 | * \author Nikolay Khabarov 5 | * \date 2017 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHCONNECTOR_WEBSOCKET_H_ 10 | #define _DHCONNECTOR_WEBSOCKET_H_ 11 | 12 | /** Function prototype for sending data. */ 13 | typedef int (*dhconnector_websocket_send_proto)(const char *data, unsigned int len); 14 | /** Function prototype for error callback. */ 15 | typedef void (*dhconnector_websocket_error)(void); 16 | 17 | /** 18 | * \brief Initialize devicehive WebSocket protocol exchange 19 | * \param[in] send_func Pointer to function to call to send data. 20 | * \param[in] err_func Pointer to function to call on error. 21 | */ 22 | void dhconnector_websocket_start(dhconnector_websocket_send_proto send_func, dhconnector_websocket_error err_func); 23 | 24 | /** 25 | * \brief Stop any protocol activities. 26 | */ 27 | void dhconnector_websocket_stop(); 28 | 29 | /** 30 | * \brief Parse received from server data. 31 | * \param[in] data Pointer to data. 32 | * \param[in] len Number of bytes in data. 33 | */ 34 | void dhconnector_websocket_parse(const char *data, unsigned int len); 35 | 36 | #endif /* _DHCONNECTOR_WEBSOCKET_H_ */ 37 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/ds18b20.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with DS18B20 temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/ds18b20.h" 8 | #include "DH/onewire.h" 9 | #include "dhutils.h" 10 | 11 | #include 12 | 13 | #if defined(DH_DEVICE_DS18B20) 14 | 15 | /* 16 | * ds18b20_read() implementation. 17 | */ 18 | const char* ICACHE_FLASH_ATTR ds18b20_read(int pin, float *temperature) 19 | { 20 | if (pin != DH_ONEWIRE_NO_PIN) { 21 | if(!!dh_onewire_set_pin(pin)) { 22 | return "Failed to set up onewire pin"; 23 | } 24 | } 25 | 26 | uint8_t buf[8]; 27 | buf[0] = 0xCC; 28 | buf[1] = 0x44; // start measure, use defaults 29 | if (!!dh_onewire_write(buf, 2)) { 30 | return "No response"; 31 | } 32 | 33 | delay_ms(750); // maximum possible time for measure 34 | 35 | // buf[0] = 0xCC; 36 | buf[1] = 0xBE; // read memory 37 | if (!!dh_onewire_write(buf, 2)) { 38 | return "Failed to read"; 39 | } 40 | if (!!dh_onewire_read(buf, sizeof(buf))) { 41 | return "Failed to read data"; 42 | } 43 | 44 | const int16_t *t = (const int16_t *)&buf[0]; 45 | *temperature = (t[0] / 16.0f); // default precision 46 | 47 | return NULL; // OK 48 | } 49 | 50 | #endif /* DH_DEVICE_DS18B20 */ 51 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/ina219.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with INA219 power monitor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_INA219_H_ 8 | #define _DEVICES_INA219_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_INA219) 12 | 13 | /** 14 | * @brief Get measurements. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] voltage Input voltage in Volts. 18 | * @param[out] current Current in Amperes. 19 | * @param[out] power Total power since first call. 20 | * @return Status value, one of DH_I2C_Status enum. 21 | */ 22 | int ina219_read(int sda, int scl, float *voltage, float *current, float *power); 23 | 24 | 25 | /** 26 | * @brief Set sensor address which should be used while reading. 27 | * @param[in] address I2C end device address. 28 | */ 29 | void ina219_set_address(int address); 30 | 31 | /** 32 | * @brief Set reference shunt resistance. 33 | * 34 | * Default is 0.1 Ohm. 35 | * 36 | * @param[in] resistance Resistance in Ohms. 37 | * @return Status value, one of DH_I2C_Status enum. 38 | */ 39 | int ina219_set_shunt(float resistance); 40 | 41 | #endif /* DH_DEVICE_INA219 */ 42 | #endif /* _DEVICES_INA219_H_ */ 43 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/hmc5883l.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with HMC5883L compass sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_HMC5883L_H_ 8 | #define _DEVICES_HMC5883L_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_HMC5883L) 12 | 13 | /** Measurements in three dimensions. */ 14 | typedef struct { 15 | float X; ///< @brief X axis. 16 | float Y; ///< @brief Y axis. 17 | float Z; ///< @brief Z axis. 18 | } HMC5883L_XYZ; 19 | 20 | 21 | /** @brief Axis overflow */ 22 | #define HMC5883l_OVERFLOWED -1.0e8f 23 | 24 | 25 | /** 26 | * @brief Measure compass data. 27 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 28 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 29 | * @param[out] compass Compass data in normalized vector. If axis is overflowed during measure, HMC5883l_OVERFLOWED is a value. 30 | * @return NULL on success, text description on error. 31 | */ 32 | int hmc5883l_read(int sda, int scl, HMC5883L_XYZ *compass); 33 | 34 | 35 | /** 36 | * @brief Set sensor address which should be used while reading. 37 | * @param[in] address I2C end device address. 38 | */ 39 | void hmc5883l_set_address(int address); 40 | 41 | #endif /* DH_DEVICE_HMC5883L */ 42 | #endif /* _DEVICES_HMC5883L_H_ */ 43 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/max6675.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MAX6675 K-thermocouple temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/max6675.h" 8 | #include "DH/gpio.h" 9 | #include "DH/spi.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_MAX6675) 15 | 16 | // module variables 17 | static int mCSPin = 15; 18 | 19 | /* 20 | * max6675_read() implementation. 21 | */ 22 | const char* ICACHE_FLASH_ATTR max6675_read(int pin, float *temperature) 23 | { 24 | if (pin == DH_SPI_NO_PIN) { 25 | dh_spi_set_cs_pin(mCSPin); 26 | } else { 27 | int ok = (pin != DH_SPI_NO_CS); 28 | if (ok) 29 | ok = (0 == dh_spi_set_cs_pin(pin)); 30 | if (!ok) 31 | return "Wrong CS pin"; 32 | mCSPin = pin; 33 | } 34 | dh_spi_set_mode(DH_SPI_CPOL0CPHA0); 35 | 36 | // start converting 37 | dh_gpio_write(DH_GPIO_PIN(mCSPin), 0); 38 | delay_ms(250); 39 | 40 | uint8_t buf[2]; 41 | dh_spi_read(buf, sizeof(buf)); 42 | 43 | if ((buf[0] & 0x80) || (buf[1] & 0x02)) 44 | return "Protocol error"; 45 | if (buf[1] & 0x04) 46 | return "Thermocouple is not connected"; 47 | 48 | buf[1] &= 0xF8; 49 | int v = signedInt16be((const char*)buf, 0); 50 | *temperature = v / 32.0f; 51 | 52 | return NULL; // OK 53 | } 54 | 55 | #endif /* DH_DEVICE_MAX6675 */ 56 | -------------------------------------------------------------------------------- /firmware-src/sources/snprintf.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file snprintf.h 3 | * \brief Tiny snprintf implementation. 4 | * \author Nikolay Khabarov 5 | * \date 2014 6 | * \copyright Public Domain 7 | */ 8 | 9 | #ifndef _SNPRINTF_H_ 10 | #define _SNPRINTF_H_ 11 | 12 | #include 13 | #include 14 | 15 | /** 16 | * \brief Write formatted data to char array. 17 | * \details Pointers to ROM is supported. 18 | * \param[out] pString Output buffer. 19 | * \param[in] length Output buffer maximum size(including space for null terminated char). 20 | * \param[in] pFormat Format. 21 | * \param[in] ... Additional variables, specified by format. 22 | * \return Number of character that was printed in buffer(excluding null terminated char). 23 | */ 24 | int snprintf(char *pString, size_t length, const char *pFormat, ...); 25 | 26 | /** 27 | * \brief Write formatted data to char array. 28 | * \details Pointers to ROM is supported. 29 | * \param[out] pString Output buffer. 30 | * \param[in] length Output buffer maximum size(including space for null terminated char). 31 | * \param[in] pFormat Format. 32 | * \param[in] ap Additional variables, specified by format. 33 | * \return Number of character that was printed in buffer(excluding null terminated char). 34 | */ 35 | int vsnprintf(char *pString, size_t length, const char *pFormat, va_list ap); 36 | 37 | #endif /* _SNPRINTF_H_ */ 38 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pwm_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief PWM command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/pwm_cmd.h" 8 | #include "DH/pwm.h" 9 | #include "DH/adc.h" 10 | 11 | #ifdef DH_COMMANDS_PWM // PWM command handlers 12 | #include "dhcommand_parser.h" 13 | #include 14 | 15 | /* 16 | * dh_handle_pwm_control() implementation. 17 | */ 18 | void ICACHE_FLASH_ATTR dh_handle_pwm_control(COMMAND_RESULT *cmd_res, const char *command, 19 | const char *params, unsigned int params_len) 20 | { 21 | gpio_command_params info; 22 | ALLOWED_FIELDS fields = 0; 23 | const char *err_msg = parse_params_pins_set(params, params_len, 24 | &info, DH_GPIO_SUITABLE_PINS, 0, 25 | AF_VALUES | AF_PERIOD | AF_COUNT, &fields); 26 | 27 | if (err_msg != 0) { 28 | dh_command_fail(cmd_res, err_msg); 29 | } else if (!!dh_pwm_start(info.storage.uint_values, 30 | info.pin_value_readed, 31 | (fields & AF_PERIOD) ? info.periodus 32 | : dh_pwm_get_period_us(), 33 | info.count)) { 34 | dh_command_fail(cmd_res, "Wrong parameters"); 35 | } else { 36 | dh_command_done(cmd_res, ""); // OK 37 | } 38 | } 39 | 40 | #endif /* DH_COMMANDS_PWM */ 41 | -------------------------------------------------------------------------------- /firmware-src/sources/dhdata.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhdata.h 3 | * \brief Encode binary data to text. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHDATA_H_ 10 | #define _DHDATA_H_ 11 | 12 | /** 13 | * \brief Encode binary data to text. 14 | * \details Function check if output buffer enough for text and start writing to it only this check. Return zero immediately otherwise. 15 | * \param[in] data Pointer to binary data. 16 | * \param[in] datalen Data size in bytes. 17 | * \param[out] out Pointer to output buffer. 18 | * \param[in] outlen Output buffer size in bytes. 19 | * \return Number of chars that was copied to output buffer. 20 | */ 21 | int dhdata_encode(const char *data, unsigned int datalen, char *out, unsigned int outlen); 22 | 23 | /** 24 | * \brief Decode text to binary data. 25 | * \details Function check if output buffer enough for data and start writing to it only this check. Return zero immediately otherwise. 26 | * \param[in] data Pointer to encoded text. 27 | * \param[in] datalen Encoded text size in bytes. 28 | * \param[out] out Pointer to output buffer. 29 | * \param[in] outlen Output buffer size in bytes. 30 | * \return Number of bytes that was copied to output buffer. 31 | */ 32 | int dhdata_decode(const char *data, unsigned int datalen, char *out, unsigned int outlen); 33 | 34 | #endif /* _DHDATA_H_ */ 35 | -------------------------------------------------------------------------------- /firmware-src/sources/dhconnector.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhconnector.h 3 | * \brief Main connectivity to DeviceHive server for receiving commands. 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHCONNECTOR_H_ 10 | #define _DHCONNECTOR_H_ 11 | 12 | #include "dhrequest.h" 13 | #include "dhsender_data.h" 14 | 15 | /** Current connection state. */ 16 | typedef enum { 17 | CS_DISCONNECT, ///< Disconnected from DeviceHive server. 18 | CS_RESOLVEHTTP, ///< Resolve HTTP API url. 19 | CS_GETINFO, ///< Getting info from DeviceHive server with WebSocket url. 20 | CS_RESOLVEWEBSOCKET,///< Resolve WebSocket url. 21 | CS_WEBSOCKET, ///< Request to switch protocol to WebSocket. 22 | CS_OPERATE ///< Normal operational state. 23 | } CONNECTION_STATE; 24 | 25 | /** 26 | * \brief Initializes connector, parameters are taken from permanent storage. 27 | */ 28 | void dhconnector_init(void); 29 | 30 | /** 31 | * \brief Get connector current state. 32 | * \return Current state value from CONNECTION_STATE enum. 33 | */ 34 | CONNECTION_STATE dhconnector_get_state(void); 35 | 36 | /** 37 | * \brief Callback for custom firmware. If callback returns non null request, it will be sent instead poll request. 38 | * \return HTTP request for DH server or null to keep normal mode. 39 | */ 40 | HTTP_REQUEST *custom_firmware_request(void); 41 | 42 | #endif /* _DHCONNECTOR_H_ */ 43 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/max31855.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file max31855.h 3 | * @brief Simple communication with MAX31855 thermocouple temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/max31855.h" 8 | #include "DH/gpio.h" 9 | #include "DH/spi.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_MAX31855) 15 | 16 | // module variables 17 | static int mCSPin = 15; 18 | 19 | 20 | /* 21 | * max31855_read() implementation. 22 | */ 23 | const char* ICACHE_FLASH_ATTR max31855_read(int pin, float *temperature) 24 | { 25 | if (pin == DH_SPI_NO_PIN) { 26 | dh_spi_set_cs_pin(mCSPin); 27 | } else { 28 | int ok = (pin != DH_SPI_NO_CS); 29 | if (ok) 30 | ok = (0 == dh_spi_set_cs_pin(pin)); 31 | if (!ok) 32 | return "Wrong CS pin"; 33 | mCSPin = pin; 34 | } 35 | dh_spi_set_mode(DH_SPI_CPOL0CPHA0); 36 | 37 | // start converting 38 | dh_gpio_write(DH_GPIO_PIN(mCSPin), 0); 39 | delay_ms(100); 40 | 41 | uint8_t buf[4]; 42 | dh_spi_read(buf, sizeof(buf)); 43 | 44 | if (buf[3] & 0x01) 45 | return "Open circuit"; 46 | if (buf[3] & 0x02) 47 | return "Short to GND"; 48 | if (buf[3] & 0x03) 49 | return "Short to Vcc"; 50 | 51 | buf[1] &= 0xFC; 52 | int v = signedInt16be((const char*)buf, 0); 53 | *temperature = v / 16.0f; 54 | 55 | return NULL; // OK 56 | } 57 | 58 | #endif /* DH_DEVICE_MAX31855 */ 59 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/dht_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief DHT command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_DHT_CMD_H_ 8 | #define _COMMANDS_DHT_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #include "dhsender_data.h" 12 | #if defined(DH_COMMANDS_ONEWIRE) 13 | 14 | /** 15 | * @brief Handle "onewire/dht/read" command. 16 | */ 17 | void dh_handle_onewire_dht_read(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | #endif /* DH_COMMANDS_ONEWIRE */ 20 | 21 | 22 | #if defined(DH_COMMANDS_DHT11) && defined(DH_DEVICE_DHT11) 23 | /** 24 | * @brief Handle "devices/dht11/read" command. 25 | */ 26 | void ICACHE_FLASH_ATTR dh_handle_devices_dht11_read(COMMAND_RESULT *cmd_res, const char *command, 27 | const char *params, unsigned int params_len); 28 | #endif /* DH_COMMANDS_DHT11 && DH_DEVICE_DHT11 */ 29 | 30 | 31 | #if defined(DH_COMMANDS_DHT22) && defined(DH_DEVICE_DHT22) 32 | /** 33 | * @brief Handle "devices/dht22/read" command. 34 | */ 35 | void ICACHE_FLASH_ATTR dh_handle_devices_dht22_read(COMMAND_RESULT *cmd_res, const char *command, 36 | const char *params, unsigned int params_len); 37 | #endif /* DH_COMMANDS_DHT22 || DH_DEVICE_DHT22 */ 38 | 39 | #endif /* _COMMANDS_DHT_CMD_H_ */ 40 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mpu6050.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MPU6050 accelerometer and gyroscope sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_MPU6050_H_ 8 | #define _DEVICES_MPU6050_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_MPU6050) 12 | 13 | /** @brief Measurements in three dimensions. */ 14 | typedef struct { 15 | float X; ///< @brief X axis. 16 | float Y; ///< @brief Y axis. 17 | float Z; ///< @brief Z axis. 18 | } MPU6050_XYZ; 19 | 20 | 21 | /** 22 | * @brief Measure accelerometer and gyroscope data. 23 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 24 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 25 | * @param[out] acceleromter Accelerometer data in metre per second squared. Can be NULL. 26 | * @param[out] gyroscope Gyroscope data in degree per second. Can be NULL. 27 | * @param[out] temparature Gyroscope data in degree per second. Can be NULL. 28 | * @return Status value, one of DH_I2C_Status enum. 29 | */ 30 | int mpu6050_read(int sda, int scl, MPU6050_XYZ *acceleromter, MPU6050_XYZ *gyroscope, float *temparature); 31 | 32 | 33 | /** 34 | * @brief Set sensor address which should be used while reading. 35 | * @param[in] address I2C end device address. 36 | */ 37 | void mpu6050_set_address(int address); 38 | 39 | #endif /* DH_DEVICE_MPU6050 */ 40 | #endif /* _DEVICES_MPU6050_H_ */ 41 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/max6675_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MAX6675 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/max6675_cmd.h" 8 | #include "devices/max6675.h" 9 | #include "DH/spi.h" 10 | #include "DH/adc.h" 11 | 12 | #include "dhcommand_parser.h" 13 | #include 14 | 15 | #if defined(DH_COMMANDS_MAX6675) && defined(DH_DEVICE_MAX6675) 16 | 17 | /* 18 | * dh_handle_devices_max6675_read() implementation. 19 | */ 20 | void ICACHE_FLASH_ATTR dh_handle_devices_max6675_read(COMMAND_RESULT *cmd_res, const char *command, 21 | const char *params, unsigned int params_len) 22 | { 23 | gpio_command_params info; 24 | ALLOWED_FIELDS fields = 0; 25 | if (params_len) { 26 | const char *err_msg = parse_params_pins_set(params, params_len, 27 | &info, DH_ADC_SUITABLE_PINS, 0, AF_CS, &fields); 28 | if (err_msg != 0) { 29 | dh_command_fail(cmd_res, err_msg); 30 | return; // FAILED 31 | } 32 | } 33 | 34 | float temperature; 35 | const char* err_msg = max6675_read((fields & AF_CS) ? info.CS : DH_SPI_NO_PIN, &temperature); 36 | if (err_msg != 0) { 37 | dh_command_fail(cmd_res, err_msg); 38 | } else { 39 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 40 | "{\"temperature\":%f}", temperature); 41 | } 42 | } 43 | 44 | #endif /* DH_COMMANDS_MAX6675 && DH_DEVICE_MAX6675 */ 45 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/max31855_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MAX31855 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/max31855_cmd.h" 8 | #include "devices/max31855.h" 9 | #include "DH/spi.h" 10 | #include "DH/adc.h" 11 | 12 | #include "dhcommand_parser.h" 13 | #include 14 | 15 | #if defined(DH_COMMANDS_MAX31855) && defined(DH_DEVICE_MAX31855) 16 | 17 | /* 18 | * dh_handle_devices_max31855_read() implementation. 19 | */ 20 | void ICACHE_FLASH_ATTR dh_handle_devices_max31855_read(COMMAND_RESULT *cmd_res, const char *command, 21 | const char *params, unsigned int params_len) 22 | { 23 | gpio_command_params info; 24 | ALLOWED_FIELDS fields = 0; 25 | if (params_len) { 26 | const char *err_msg = parse_params_pins_set(params, params_len, 27 | &info, DH_ADC_SUITABLE_PINS, 0, AF_CS, &fields); 28 | if (err_msg != 0) { 29 | dh_command_fail(cmd_res, err_msg); 30 | return; // FAILED 31 | } 32 | } 33 | 34 | float temperature; 35 | const char *err_msg = max31855_read((fields & AF_CS) ? info.CS : DH_SPI_NO_PIN, &temperature); 36 | if (err_msg != 0) { 37 | dh_command_fail(cmd_res, err_msg); 38 | } else { 39 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 40 | "{\"temperature\":%f}", temperature); 41 | } 42 | } 43 | 44 | #endif /* DH_COMMANDS_MAX31855 && DH_DEVICE_MAX31855 */ 45 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/uart_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief UART command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_UART_CMD_H_ 8 | #define _COMMANDS_UART_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_UART) // UART command handlers 12 | #include "dhsender_data.h" 13 | 14 | /** 15 | * @brief Handle "uart/write" command. 16 | */ 17 | void ICACHE_FLASH_ATTR dh_handle_uart_write(COMMAND_RESULT *cmd_res, const char *command, 18 | const char *params, unsigned int params_len); 19 | 20 | 21 | /** 22 | * @brief Handle "uart/read" command. 23 | */ 24 | void ICACHE_FLASH_ATTR dh_handle_uart_read(COMMAND_RESULT *cmd_res, const char *command, 25 | const char *params, unsigned int params_len); 26 | 27 | 28 | /** 29 | * @brief Handle "uart/int" command. 30 | */ 31 | void ICACHE_FLASH_ATTR dh_handle_uart_int(COMMAND_RESULT *cmd_res, const char *command, 32 | const char *params, unsigned int params_len); 33 | 34 | 35 | /** 36 | * @brief Handle "uart/terminal" command. 37 | */ 38 | void ICACHE_FLASH_ATTR dh_handle_uart_terminal(COMMAND_RESULT *cmd_res, const char *command, 39 | const char *params, unsigned int params_len); 40 | 41 | #endif /* DH_COMMANDS_UART */ 42 | #endif /* _COMMANDS_UART_CMD_H_ */ 43 | -------------------------------------------------------------------------------- /firmware-src/sources/rand.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dhrand.c 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: Typical random implementation 9 | * 10 | */ 11 | #include "rand.h" 12 | #include "snprintf.h" 13 | 14 | #include 15 | 16 | 17 | static unsigned long seed = 0; 18 | 19 | int ICACHE_FLASH_ATTR rand(void) { 20 | if(seed == 0) { 21 | seed = system_get_time(); 22 | } 23 | seed = seed * 1103515245 + 12345; 24 | return (unsigned int)(seed/65536) % RAND_MAX; 25 | } 26 | 27 | unsigned int ICACHE_FLASH_ATTR rand_generate_key(char *buf) { 28 | const int minlen = 8; 29 | const int maxlen = 16; 30 | if(buf == 0) 31 | return maxlen; 32 | int num = minlen + rand() % (maxlen - minlen + 1); 33 | unsigned int bufpos = 0; 34 | while(num--) { 35 | char c = 0x21 + rand() % 0x5C; // 0x21 - 0x7C 36 | // removing unsuitable chars 37 | if(c == '"') 38 | c = '}'; // 0x7D 39 | else if(c == '\\') 40 | c = '~'; // 0x7E 41 | buf[bufpos++] = c; 42 | } 43 | buf[bufpos] = 0; 44 | return bufpos; 45 | } 46 | 47 | unsigned int ICACHE_FLASH_ATTR rand_generate_deviceid(char *buf) { 48 | const char prefix[] = "esp-device-"; 49 | const unsigned int len = 16; 50 | if(buf == 0) 51 | return len + sizeof(prefix) - 1; 52 | unsigned int bufpos = snprintf(buf, sizeof(prefix), prefix); 53 | int i; 54 | for(i = 0; i < len; i++) { 55 | bufpos += snprintf(&buf[bufpos], 2, "%x", rand() % 16); 56 | } 57 | return bufpos; 58 | } 59 | -------------------------------------------------------------------------------- /sdk/include/ets_forward.h: -------------------------------------------------------------------------------- 1 | #ifndef _ETS_FORWARD_H_ 2 | #define _ETS_FORWARD_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | void ets_isr_mask(uint32_t); 11 | void ets_isr_unmask(uint32_t); 12 | void ets_intr_lock(void); 13 | void ets_intr_unlock(void); 14 | 15 | // memory 16 | int ets_memcmp(const void*, const void*, size_t); 17 | void *ets_memcpy(void*, const void*, size_t); 18 | void *ets_memset(void*, int, size_t); 19 | void *ets_memmove(void*, const void*, size_t); 20 | void ets_bzero(void*, size_t); 21 | 22 | // sprintf 23 | int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); 24 | int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4))); 25 | int os_printf_plus(const char *format, ...) __attribute__ ((format (printf, 1, 2))); 26 | 27 | // strings 28 | int ets_strcmp(const char*, const char*); 29 | int ets_strncmp(const char*, const char*, size_t); 30 | char* ets_strcpy(char*, const char*); 31 | char* ets_strncpy(char*, const char*, size_t); 32 | char *ets_strstr(const char*, const char*); 33 | int ets_str2macaddr(void *, void *); 34 | // int atoi(const char*); 35 | 36 | // timer 37 | void ets_timer_disarm(os_timer_t*); 38 | void ets_timer_setfn(os_timer_t*, ETSTimerFunc*, void*); 39 | 40 | void ets_update_cpu_frequency(int freqmhz); 41 | uint8 wifi_get_opmode(void); 42 | int rand(void); 43 | 44 | #endif // _ETS_FORWARD_H_ 45 | -------------------------------------------------------------------------------- /esp-utils/common/terminal_posix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * main.cpp 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: simple VT100 terminal implementation 9 | * 10 | */ 11 | 12 | #include "terminal.h" 13 | 14 | #ifdef TERMINALPOSIX 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | static struct termios mOldStdinAttr; 21 | 22 | void Terminal::put(char c) { 23 | putchar(c); 24 | fflush( stdout ); 25 | } 26 | 27 | void Terminal::put(const char *str) { 28 | while(*str) 29 | put(*str++); 30 | } 31 | 32 | Terminal *Terminal::open() { 33 | static Terminal *term = 0; 34 | if(!term) 35 | term = new Terminal(); 36 | return term; 37 | } 38 | 39 | Terminal::Terminal() { 40 | memset(&mOldStdinAttr, 0, sizeof(mOldStdinAttr)); 41 | tcgetattr(STDIN_FILENO, &mOldStdinAttr); 42 | struct termios stdinattr; 43 | tcgetattr(STDIN_FILENO, &stdinattr); 44 | stdinattr.c_lflag &= ~ICANON; 45 | stdinattr.c_lflag &= ~ECHO; 46 | stdinattr.c_cc[VMIN] = 1; 47 | stdinattr.c_cc[VTIME] = 0; 48 | stdinattr.c_cc[VEOF] = 3; 49 | stdinattr.c_cc[VINTR] = 0; 50 | stdinattr.c_cc[VSTART] = 0; 51 | tcsetattr(0, TCSANOW, &stdinattr); 52 | tcsetattr(STDIN_FILENO, TCSANOW, &stdinattr); 53 | } 54 | 55 | Terminal::~Terminal() { 56 | tcsetattr(STDIN_FILENO, TCSANOW, &mOldStdinAttr); 57 | } 58 | 59 | void Terminal::get(char *buf) { 60 | if(read(STDIN_FILENO, buf, 1)) 61 | buf[1] = 0; 62 | else 63 | buf[0] = 0; 64 | } 65 | 66 | #endif // TERMINALPOSIX 67 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/pca9685.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with PCA9685 PWM LED controller 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_PCA9685_H_ 8 | #define _DEVICES_PCA9685_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_PCA9685) 12 | 13 | #include "DH/gpio.h" 14 | 15 | /** @brief Do not change frequency */ 16 | #define PCA9685_NO_PERIOD 0 17 | 18 | /** Pins which are allowed for this chip */ 19 | #define PCA9685_SUITABLE_PINS 0xFFFF 20 | 21 | 22 | /** 23 | * @brief Measure pressure one time. 24 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN to use current. 25 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN to use current. 26 | * @param[in] pins_duty Array with pins duty cycles for each pin. 27 | * @param[in] pins Bitwise pins mask with pins that should be enabled, 28 | * this pins have to have correct value in pins_duty array. 29 | * @param[in] period_us PWM period. Can be PCA9685_NO_PERIOD to use current. 30 | * @return Status value, one of DH_I2C_Status enum. 31 | */ 32 | int pca9685_control(int sda, int scl, const float pins_duty[DH_GPIO_PIN_COUNT], 33 | DHGpioPinMask pins, unsigned int period_us); 34 | 35 | 36 | /** 37 | * @brief Set sensor address which should be used while reading. 38 | * @param[in] address I2C end device address. 39 | */ 40 | void pca9685_set_address(int address); 41 | 42 | #endif /* DH_DEVICE_PCA9685 */ 43 | #endif /* _DEVICES_PCA9685_H_ */ 44 | -------------------------------------------------------------------------------- /firmware-src/sources/dhconnector_websocket_api.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \file dhconnector_websocket_api.h 3 | * \brief Main connectivity to DeviceHive via WebSocket 4 | * \author Nikolay Khabarov 5 | * \date 2017 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHCONNECTOR_WEBSOCKET_API_H_ 10 | #define _DHCONNECTOR_WEBSOCKET_API_H_ 11 | 12 | #define DHCONNECT_WEBSOCKET_API_ERROR -1 13 | 14 | /** 15 | * \brief Initialize communication with DeviceHive server 16 | * \param[out] data Pointer to store data which should be sent to server. 17 | * \param[in] maxlen Maximum size of specified buffer in bytes. 18 | * \return Number of copied bytes. 19 | */ 20 | int dhconnector_websocket_api_start(char *buf, unsigned int maxlen); 21 | 22 | /** 23 | * \brief Exchange data with server. 24 | * \param[in] in Data received from server. 25 | * \param[in] inlen Number of bytes received from server. 26 | * \param[in] out Pointer to store data which should be sent to server. 27 | * \param[in] outmaxlen Maximum size of specified buffer in bytes. 28 | * \return Number of copied bytes or DHCONNECT_WEBSOCKET_API_ERROR on error. 29 | */ 30 | int dhconnector_websocket_api_communicate(const char *in, unsigned int inlen, char *out, unsigned int outmaxlen); 31 | 32 | /** 33 | * \brief Check if connection is established and command subscription is successfully set. 34 | * \return Non zero value if successfully connected and zero otherwise. 35 | */ 36 | int dhconnector_websocket_api_check(); 37 | 38 | #endif /* _DHCONNECTOR_WEBSOCKET_API_H_ */ 39 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ds18b20_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief DS18B20 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/ds18b20_cmd.h" 8 | #include "commands/onewire_cmd.h" 9 | #include "devices/ds18b20.h" 10 | #include "DH/onewire.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_DS18B20) && defined(DH_DEVICE_DS18B20) 17 | 18 | /* 19 | * dh_handle_devices_ds18b20_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_ds18b20_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | if (params_len) { 25 | gpio_command_params info; 26 | ALLOWED_FIELDS fields = 0; 27 | const char *err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, AF_PIN, &fields); 29 | if (err_msg != 0) { 30 | dh_command_fail(cmd_res, err_msg); 31 | return; // FAILED 32 | } 33 | if (dh_onewire_init_helper(cmd_res, fields, &info)) 34 | return; // FAILED 35 | } 36 | 37 | float temperature; 38 | const char *err_msg = ds18b20_read(DH_ONEWIRE_NO_PIN, &temperature); 39 | if (err_msg != 0) { 40 | dh_command_fail(cmd_res, err_msg); 41 | } else { 42 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, "{\"temperature\":%f}", temperature); 43 | } 44 | } 45 | 46 | #endif /* DH_COMMANDS_DS18B20 && DH_DEVICE_DS18B20 */ 47 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/pcf8591.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with PCF8591 ADC/DAC. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_PCF8591_H_ 8 | #define _DEVICES_PCF8591_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_PCF8591) 12 | 13 | /** 14 | * @brief Get ADC voltages. 15 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 16 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 17 | * @param[out] values Pointer to four float values to store result in Volts. 18 | * @return Status value, one of DH_I2C_Status enum. 19 | */ 20 | int pcf8591_read(int sda, int scl, float values[4]); 21 | 22 | 23 | /** 24 | * @brief Set DAC voltages. 25 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 26 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 27 | * @param[in] value Voltage in Volts. 28 | * @return Status value, one of DH_I2C_Status enum. 29 | */ 30 | int pcf8591_write(int sda, int scl, float value); 31 | 32 | 33 | /** 34 | * @brief Set sensor address which should be used while reading. 35 | * @param[in] address I2C end device address. 36 | */ 37 | void pcf8591_set_address(int address); 38 | 39 | 40 | /** 41 | * @brief Set reference voltage in volts. 42 | * 43 | * Default is 3.3V. 44 | * 45 | * @param[in] voltage Reference voltage in Volts. 46 | * @return Status value, one of DH_I2C_Status enum. 47 | */ 48 | int pcf8591_set_vref(float voltage); 49 | 50 | #endif /* DH_DEVICE_PCF8591 */ 51 | #endif /* _DEVICES_PCF8591_H_ */ 52 | -------------------------------------------------------------------------------- /release/README.md: -------------------------------------------------------------------------------- 1 | Binary fimrware release in repository is deprecated. To download firmware binaries follow 2 | [this link](https://github.com/devicehive/esp8266-firmware/releases). 3 | 4 | This directory contains script for generating releases. There are some tools which should be installed. 5 | 6 | # win32 build tools 7 | ``` 8 | sudo apt install binutils-mingw-w64-i686 gcc-mingw-w64-i686-dev gcc-mingw-w64-i686 g++-mingw-w64-i686 9 | ``` 10 | 11 | # osx build tools 12 | ``` 13 | sudo apt install ccache 14 | wget http://security.ubuntu.com/ubuntu/pool/universe/o/openssl098/libssl0.9.8_0.9.8o-7ubuntu3.2_amd64.deb 15 | wget https://launchpad.net/~flosoft/+archive/ubuntu/cross-apple/+files/apple-x86-odcctools_758.159-0flosoft11_amd64.deb 16 | wget https://launchpad.net/~flosoft/+archive/ubuntu/cross-apple/+files/ccache-lipo_1.0-0flosoft3_amd64.deb 17 | wget https://launchpad.net/~flosoft/+archive/ubuntu/cross-apple/+files/apple-x86-gcc_4.2.1~5646.1flosoft2_amd64.deb 18 | wget https://launchpad.net/~flosoft/+archive/ubuntu/cross-apple/+files/apple-uni-sdk-10.5_20110407-0.flosoft1_amd64.deb 19 | sudo dpkg -i libssl0.9.8_0.9.8o-7ubuntu3.2_amd64.deb 20 | sudo dpkg -i apple-x86-odcctools_758.159-0flosoft11_amd64.deb 21 | sudo dpkg -i ccache-lipo_1.0-0flosoft3_amd64.deb 22 | sudo dpkg -i apple-uni-sdk-10.5_20110407-0.flosoft1_amd64.deb 23 | sudo dpkg -i apple-x86-gcc_4.2.1~5646.1flosoft2_amd64.deb 24 | ``` 25 | 26 | # linux build tools 27 | ``` 28 | sudo apt install linux-libc-dev:i386 29 | ``` 30 | 31 | # converting markdown to pdf 32 | ``` 33 | sudo apt install npm nodejs-legacy 34 | sudo npm install -g markdown-pdf 35 | ``` 36 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/bh1750.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with BH1750 illuminance sensor 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/bh1750.h" 8 | #include "DH/i2c.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_BH1750) 15 | 16 | /** Default sensor i2c address*/ 17 | #define BH1750_DEFAULT_ADDRESS 0x46 18 | 19 | // module variables 20 | static int mAddress = BH1750_DEFAULT_ADDRESS; 21 | 22 | 23 | /* 24 | * bh1750_read() implementation. 25 | */ 26 | int ICACHE_FLASH_ATTR bh1750_read(int sda, int scl, float illuminance[1]) 27 | { 28 | int status; 29 | if (sda != DH_I2C_NO_PIN && scl != DH_I2C_NO_PIN) { 30 | if ((status = dh_i2c_init(sda, scl)) != DH_I2C_OK) { 31 | dhdebug("bh1750: failed to set up pins"); 32 | return status; 33 | } 34 | } 35 | 36 | char buf[2]; 37 | buf[0] = 0x21; // One Time High Resolution (0.5 lx) 38 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 39 | dhdebug("bh1750: failed to measure"); 40 | return status; 41 | } 42 | 43 | delay_ms(180); 44 | if ((status = dh_i2c_read(mAddress, buf, 2)) != DH_I2C_OK) { 45 | dhdebug("bh1750: failed to read"); 46 | return status; 47 | } 48 | 49 | *illuminance = (float)unsignedInt16be(buf, 0) / 1.2f / 2.0f; 50 | return DH_I2C_OK; 51 | } 52 | 53 | 54 | /* 55 | * bh1750_set_address() implementation. 56 | */ 57 | void ICACHE_FLASH_ATTR bh1750_set_address(int address) 58 | { 59 | mAddress = address; 60 | } 61 | 62 | #endif /* DH_DEVICE_BH1750 */ 63 | -------------------------------------------------------------------------------- /sdk/include/os_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ESPRESSIF MIT License 3 | * 4 | * Copyright (c) 2016 5 | * 6 | * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, 7 | * it is free of charge, to any person obtaining a copy of this software and associated 8 | * documentation files (the "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the Software is furnished 11 | * to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all copies or 14 | * substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | * 23 | */ 24 | 25 | #ifndef _OS_TYPES_H_ 26 | #define _OS_TYPES_H_ 27 | 28 | #include "ets_sys.h" 29 | 30 | #define os_signal_t ETSSignal 31 | #define os_param_t ETSParam 32 | #define os_event_t ETSEvent 33 | #define os_task_t ETSTask 34 | #define os_timer_t ETSTimer 35 | #define os_timer_func_t ETSTimerFunc 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/lm75.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with LM75 temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/lm75.h" 8 | #include "DH/i2c.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | #include 14 | 15 | #if defined(DH_DEVICE_LM75) 16 | 17 | /** @brief Default sensor i2c address*/ 18 | #define LM75_DEFAULT_ADDRESS 0x90 19 | 20 | // module variables 21 | static int mAddress = LM75_DEFAULT_ADDRESS; 22 | 23 | 24 | /* 25 | * lm75_read() implementation. 26 | */ 27 | int ICACHE_FLASH_ATTR lm75_read(int sda, int scl, float *temperature) 28 | { 29 | int status; 30 | if (sda != DH_I2C_NO_PIN && scl != DH_I2C_NO_PIN) { 31 | if ((status = dh_i2c_init(sda, scl)) != DH_I2C_OK) { 32 | dhdebug("lm75: failed to set up pins"); 33 | return status; 34 | } 35 | } 36 | 37 | uint8_t buf[2]; 38 | buf[0] = 0x0; // get temperature 39 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 40 | dhdebug("lm75: failed to write get temperature command"); 41 | return status; 42 | } 43 | if ((status = dh_i2c_read(mAddress, buf, 2)) != DH_I2C_OK) { 44 | dhdebug("lm75: failed to read temperature"); 45 | return status; 46 | } 47 | 48 | const int raw = signedInt16be((const char*)buf, 0); 49 | *temperature = (float)raw / 256.0f; 50 | return DH_I2C_OK; 51 | } 52 | 53 | 54 | /* 55 | * lm75_set_address() implementation. 56 | */ 57 | void ICACHE_FLASH_ATTR lm75_set_address(int address) 58 | { 59 | mAddress = address; 60 | } 61 | 62 | #endif /* DH_DEVICE_LM75 */ 63 | -------------------------------------------------------------------------------- /firmware-src/sources/dhdata.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dhdata.c 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: Data coder/encoder 9 | * 10 | */ 11 | #include "dhdata.h" 12 | #include "user_config.h" 13 | #include "dhutils.h" 14 | #include "base64.h" 15 | 16 | #if defined DATAENCODEBASE64 && defined DATAENCODEHEX 17 | #error Only one data encode method are allowed 18 | #elif !defined DATAENCODEBASE64 && !defined DATAENCODEHEX 19 | #error No data encode method specified. Please define DATAENCODEBASE64 or DATAENCODEHEX 20 | #endif 21 | 22 | 23 | int ICACHE_FLASH_ATTR dhdata_encode(const char *data, unsigned int datalen, char *out, unsigned int outlen) { 24 | #ifdef DATAENCODEBASE64 25 | return esp_base64_encode(data, datalen, out, outlen); 26 | #else 27 | if(datalen*2 > outlen || datalen == 0) 28 | return 0; 29 | unsigned int datapos = 0; 30 | unsigned int outpos = 0; 31 | while(datalen--) { 32 | outpos += byteToHex(data[datapos], (char *)&out[outpos]); 33 | datapos++; 34 | } 35 | return outpos; 36 | #endif // DATAENCODEBASE64 37 | } 38 | 39 | int ICACHE_FLASH_ATTR dhdata_decode(const char *data, unsigned int datalen, char *out, unsigned int outlen) { 40 | #ifdef DATAENCODEBASE64 41 | return esp_base64_decode(data, datalen, out, outlen); 42 | #else 43 | if(datalen % 2 || outlen < datalen / 2 || datalen == 0) 44 | return 0; 45 | char c; 46 | unsigned int datapos = 0; 47 | unsigned int outpos = 0; 48 | while(datalen) { 49 | if(hexToByte(&data[datapos], &c) != 2) 50 | return 0; 51 | datapos += 2; 52 | out[outpos++] = c; 53 | datalen -= 2; 54 | } 55 | return outpos; 56 | #endif // DATAENCODEBASE64 57 | } 58 | -------------------------------------------------------------------------------- /release/gen_release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | DIR=$(realpath $(dirname $0)) 6 | BUILD=$DIR/build 7 | VER=$(grep FIRMWARE_VERSION $DIR/../firmware-src/sources/user_config.h | cut -d'"' -f2) 8 | 9 | mkdir -p $BUILD 10 | rm -rf $BUILD/* 11 | rm -f $BUILD/../dh-esp-firmware-v$VER.zip 12 | rm -f $BUILD/../dh-esp-firmware-v$VER.tar.gz 13 | (cd $DIR/../esp-utils && CXX=x86_64-linux-gnu-g++ CFLAGS=-m32 LDFLAGS=-m32 make rebuild) 14 | cp $DIR/../esp-utils/build/esp-terminal $BUILD/esp-terminal-linux 15 | cp $DIR/../esp-utils/build/esp-flasher $BUILD/esp-flasher-linux 16 | (cd $DIR/../esp-utils && CXX=i686-apple-darwin10-g++ make rebuild) 17 | cp $DIR/../esp-utils/build/esp-terminal $BUILD/esp-terminal-osx 18 | cp $DIR/../esp-utils/build/esp-flasher $BUILD/esp-flasher-osx 19 | (cd $DIR/../esp-utils && CXX=i686-w64-mingw32-g++ LDFLAGS=-static make rebuild) 20 | cp $DIR/../esp-utils/build/esp-terminal $BUILD/esp-terminal-win.exe 21 | cp $DIR/../esp-utils/build/esp-flasher $BUILD/esp-flasher-win.exe 22 | cp $DIR/utils/* $BUILD/ 23 | 24 | # since an issue - https://github.com/ariya/phantomjs/issues/13959 (markdown-pdf uses phantomjs) 25 | # just remove the local links from document. 26 | sed $DIR/../DeviceHiveESP8266.md -e 's/\[\([^]].*\)\](#.*)/\1/g' > $DIR/../DeviceHiveESP8266_hack.md 27 | (cd $DIR/.. && markdown-pdf DeviceHiveESP8266_hack.md -o $BUILD/DeviceHiveESP8266.pdf --css-path $DIR/md.css) 28 | rm $DIR/../DeviceHiveESP8266_hack.md 29 | 30 | (cd $DIR/../firmware-src && make rebuild) && cp $DIR/../firmware-src/firmware/* $BUILD 31 | 32 | (cd $BUILD && zip -r -q ../dh-esp-firmware-v$VER.zip *) 33 | (cd $BUILD && tar -czf ../dh-esp-firmware-v$VER.tar.gz *) 34 | 35 | 36 | -------------------------------------------------------------------------------- /firmware-src/pages/ds18b20.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DS18B20 5 | 6 | 24 | 25 | 26 |
27 |
28 |
29 | 30 | 31 |
32 |
33 |
34 |
This sample reads temperature from DS18B20 sensor every 1 second. Click on buttons below to start or stop. 35 |
36 |
37 | 40 | 41 | 42 |
43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/tm1637_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief TM1637 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/tm1637_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/tm1637.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_TM1637) && defined(DH_DEVICE_TM1637) 17 | 18 | /* 19 | * dh_handle_devices_tm1637_write() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_tm1637_write(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | const char *err_msg = parse_params_pins_set(params, params_len, 27 | &info, DH_ADC_SUITABLE_PINS, 0, 28 | AF_SDA | AF_SCL | AF_DATA | AF_TEXT_DATA, &fields); 29 | if (err_msg != 0) { 30 | dh_command_fail(cmd_res, err_msg); 31 | return; // FAILED 32 | } 33 | if ((fields & (AF_DATA | AF_TEXT_DATA)) == 0 || info.data_len == 0) { 34 | dh_command_fail(cmd_res, "Text not specified"); 35 | return; // FAILED 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; // FAILED 41 | 42 | const int status = tm1636_write(DH_I2C_NO_PIN, DH_I2C_NO_PIN, info.data, info.data_len); 43 | err_msg = dh_i2c_error_string(status); 44 | if (err_msg != 0) { 45 | dh_command_fail(cmd_res, err_msg); 46 | } else { 47 | dh_command_done(cmd_res, ""); 48 | } 49 | } 50 | 51 | #endif /* DH_COMMANDS_TM1637 && DH_DEVICE_TM1637 */ 52 | -------------------------------------------------------------------------------- /esp-utils/README.md: -------------------------------------------------------------------------------- 1 | # DeviceHive ESP8266 Simple utils 2 | `esp-terminal`: 3 | Simple tool for easy access DeviceHive ESP8266 firmware terminal in production 4 | release with simple support of ansi escape codes 5 | 6 | `esp-flasher`: 7 | Simple tool for flashing DeviceHive firmware in ESP8266 8 | 9 | # How To Build 10 | Run `make`. 11 | All binary files will be generated in `build` directory. 12 | 13 | # esp-terminal usage 14 | Run application and it will try to detect device automatically. You also can 15 | specify device manually by passing port name in command line. 16 | To quit from terminal press `Ctrl+Q`. 17 | 18 | # esp-flasher usage 19 | Run application and it will try to detect device automatically. If no parameters 20 | were specified it also will try to open files `devicehive.bin` in current directory 21 | and directory with its binary and flash them to corresponding addresses. 22 | You can specify port name in first argument if you want to specify it manually. 23 | You also can specify which files have to be written in devices in arguments by 24 | pairs `hex address file name`. For exmaple: 25 | 26 | ``` 27 | esp-flasher COM2 0x00000 boot.img 0x40000 spi.img 28 | esp-flasher 0x40000 myimagefile.bin 29 | ``` 30 | 31 | There also `--developer` and `--reboot` arguments which supposed to be used by 32 | developers only. First enables incremental flash mode, it compares previosuly 33 | flashed file (should be saved as `devicehive.bin.prev`) and if differences are 34 | minimal it will flash only them to save time on flashing. `--reboot` argument 35 | simply reboot chip (serial adapter `RTS` should be connected to `GPIO0`, `DTR` to `RTS` 36 | pin). 37 | 38 | # License 39 | See [LICENSE](./LICENSE) file. 40 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/lm75_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief LM75 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/lm75_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/lm75.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_LM75) && defined(DH_DEVICE_LM75) 17 | 18 | /* 19 | * dh_handle_devices_lm75_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_lm75_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char* err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | if (fields & AF_ADDRESS) 35 | lm75_set_address(info.address); 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; 41 | 42 | float temperature; 43 | const int status = lm75_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &temperature); 44 | const char *err_msg = dh_i2c_error_string(status); 45 | if (err_msg != 0) { 46 | dh_command_fail(cmd_res, err_msg); 47 | } else { 48 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 49 | "{\"temperature\":%f}", temperature); 50 | } 51 | } 52 | 53 | #endif /* DH_COMMANDS_LM75 && DH_DEVICE_LM75 */ 54 | -------------------------------------------------------------------------------- /firmware-src/sources/DH/pwm.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Software PWM implementation for ESP8266 firmware. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | * 7 | * This module uses ESP8266 hardware timer, so any other module 8 | * with hardware timer requirement will be incompatible. 9 | */ 10 | #ifndef _DH_PWM_H_ 11 | #define _DH_PWM_H_ 12 | 13 | #include "DH/gpio.h" 14 | 15 | /** 16 | * @brief Default PWM frequency in microseconds. 17 | */ 18 | #define DH_PWM_DEFAULT_PERIOD_US 1000 19 | 20 | /** 21 | * @brief PWM depth, high value may cause very high CPU load. 22 | */ 23 | #define DH_PWM_DEPTH 100 24 | 25 | 26 | /** 27 | * @brief Start PWM for specified pins. 28 | * @param[in] duty Array with pins duty cycles for each pin. 29 | * @param[in] pins Bitwise pin mask with pins that should be enabled, 30 | * these pins must have correct values in `duty` array. 31 | * @param[in] period_us PWM period, microseconds. 32 | * @param[in] count Number of tacts for PWM. If zero PWM will not stop automatically. 33 | * @return Zero on success. 34 | */ 35 | int dh_pwm_start(uint32_t duty[DH_GPIO_PIN_COUNT], 36 | DHGpioPinMask pins, 37 | unsigned int period_us, 38 | unsigned int count); 39 | 40 | 41 | /** 42 | * @brief Get current PWM period. 43 | * @return PWM period in microseconds. 44 | */ 45 | unsigned int dh_pwm_get_period_us(void); 46 | 47 | 48 | /** 49 | * @brief Stops PWM for specified pins. 50 | * 51 | * PWM timer will stop automatically if no pins left. 52 | * 53 | * @param[in] pins Bitwise pins mask. 54 | */ 55 | void dh_pwm_disable(DHGpioPinMask pins); 56 | 57 | #endif /* _DH_PWM_H_ */ 58 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pca9685_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief PCA9685 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/pca9685_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/pca9685.h" 10 | #include "DH/i2c.h" 11 | 12 | #include "dhcommand_parser.h" 13 | #include 14 | 15 | #if defined(DH_COMMANDS_PCA9685) && defined(DH_DEVICE_PCA9685) 16 | 17 | /* 18 | * dh_handle_devices_pca9685_control() implementation. 19 | */ 20 | void ICACHE_FLASH_ATTR dh_handle_devices_pca9685_control(COMMAND_RESULT *cmd_res, const char *command, 21 | const char *params, unsigned int params_len) 22 | { 23 | gpio_command_params info; 24 | ALLOWED_FIELDS fields = 0; 25 | const char *err_msg = parse_params_pins_set(params, params_len, 26 | &info, PCA9685_SUITABLE_PINS, 0, 27 | AF_SDA | AF_SCL | AF_ADDRESS | AF_FLOATVALUES | AF_PERIOD, &fields); 28 | if (err_msg != 0) { 29 | dh_command_fail(cmd_res, err_msg); 30 | return; // FAILED 31 | } 32 | if (fields & AF_ADDRESS) 33 | pca9685_set_address(info.address); 34 | 35 | fields |= AF_ADDRESS; 36 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 37 | return; // FAILED 38 | 39 | const int status = pca9685_control(DH_I2C_NO_PIN, DH_I2C_NO_PIN, 40 | info.storage.float_values, info.pin_value_readed, 41 | (fields & AF_PERIOD) ? info.periodus : PCA9685_NO_PERIOD); 42 | err_msg = dh_i2c_error_string(status); 43 | if (err_msg != 0) { 44 | dh_command_fail(cmd_res, err_msg); 45 | } else { 46 | dh_command_done(cmd_res, ""); 47 | } 48 | } 49 | 50 | #endif /* DH_COMMANDS_PCA9685 && DH_DEVICE_PCA9685 */ 51 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/bh1750_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief BH1750 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/bh1750_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/bh1750.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_BH1750) && defined(DH_DEVICE_BH1750) 17 | 18 | /* 19 | * dh_handle_devices_bh1750_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_bh1750_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char *err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | if (fields & AF_ADDRESS) 35 | bh1750_set_address(info.address); 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; // FAILED 41 | 42 | float illuminance; 43 | const int status = bh1750_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &illuminance); 44 | const char *err_msg = dh_i2c_error_string(status); 45 | if (err_msg != 0) { 46 | dh_command_fail(cmd_res, err_msg); 47 | } else { 48 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 49 | "{\"illuminance\":%f}", illuminance); 50 | } 51 | } 52 | 53 | #endif /* DH_COMMANDS_BH1750 && DH_DEVICE_BH1750 */ 54 | -------------------------------------------------------------------------------- /firmware-src/sources/dhdebug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dhdebug.c 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: Module for debug output 9 | * 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "dhdebug.h" 18 | #include "dhterminal.h" 19 | #include "user_config.h" 20 | #include "dhutils.h" 21 | 22 | void dhdebug_ram(const char *fmt, ...) { 23 | va_list ap; 24 | va_start(ap, fmt); 25 | dhterminal_debug(fmt, ap); 26 | va_end(ap); 27 | } 28 | 29 | void ICACHE_FLASH_ATTR dhdebug_dump(const char *data, unsigned int len) { 30 | if(len == 0) 31 | return; 32 | const unsigned char byte_per_line = 16; 33 | char textbuf[byte_per_line + 1]; 34 | char hexbuf[byte_per_line * 3 + 1]; 35 | char address[9] = "00000000"; 36 | int pos = 0; 37 | dhdebug("Dump at 0x%X, length %u", (unsigned int)data, len); 38 | do { 39 | byteToHex((pos / 0x1000000) & 0xFF, &address[0]); 40 | byteToHex((pos / 0x10000) & 0xFF, &address[2]); 41 | byteToHex((pos / 0x100) & 0xFF, &address[4]); 42 | byteToHex(pos & 0xFF, &address[6]); 43 | unsigned int i; 44 | for(i = 0; i < byte_per_line; i++) { 45 | if(pos < len) { 46 | char c = data[pos++]; 47 | byteToHex(c, &hexbuf[i * 3]); 48 | hexbuf[i * 3 + 2] = ' '; 49 | if(c < 0x20 || c >= 0x7F) 50 | textbuf[i] = '.'; 51 | else 52 | textbuf[i] = c; 53 | textbuf[i + 1] = 0; 54 | } else { 55 | hexbuf[i * 3] = ' '; 56 | hexbuf[i * 3 + 1] = ' '; 57 | hexbuf[i * 3 + 2] = ' '; 58 | } 59 | hexbuf[i * 3 + 3] = 0; 60 | } 61 | dhdebug("%s: %s %s", address, hexbuf, textbuf); 62 | } while (pos < len); 63 | } 64 | -------------------------------------------------------------------------------- /firmware-src/sources/dhrequest.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhrequest.h 3 | * \brief Generating HTTP requests for DeviceHive server. 4 | * \author Nikolay Khabarov 5 | * \date 2017 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHREQUEST_H_ 10 | #define _DHREQUEST_H_ 11 | 12 | #include "dhsettings.h" 13 | #include "user_config.h" 14 | 15 | /** Maximum length of host in urls */ 16 | #define DHREQUEST_HOST_MAX_BUF_LEN 256 17 | 18 | /** Structure for HTTP request */ 19 | typedef struct { 20 | unsigned int len; 21 | char data[]; 22 | } HTTP_REQUEST; 23 | 24 | /** 25 | * \brief Extract host, port, path from URL 26 | * \param[in] url URL to parse. 27 | * \param[out] host Pointer with bugger where to store host. Should be at least DHREQUEST_HOST_MAX_BUF_LEN bytes. Cannot extract more then DHREQUEST_HOST_MAX_BUF_LEN - 1 chars. 28 | * \param[out] port Pointer to int value to store port. 29 | * \return Pointer to url's path or NULL on error. 30 | */ 31 | const char *dhrequest_parse_url(const char *url, char *host, int *port); 32 | 33 | /** 34 | * \brief Create info request. 35 | * \details Return valuse is single instance. Each call return pointer to the same buffer. 36 | * \param[in] api DeviceHive API url. 37 | * \return Pointer to HTTP_REQUEST struct. 38 | */ 39 | HTTP_REQUEST *dhrequest_create_info(const char *api); 40 | 41 | /** 42 | * \brief Create WebSocket upgrade request. 43 | * \details Return value is single instance. Each call return pointer to the same buffer. 44 | * \param[in] api DeviceHive API url. 45 | * \param[in] url WebSocket API url. 46 | * \return Pointer to HTTP_REQUEST struct. 47 | */ 48 | HTTP_REQUEST *dhrequest_create_wsrequest(const char *api, const char *url); 49 | 50 | #endif /* _DHREQUEST_H_ */ 51 | -------------------------------------------------------------------------------- /firmware-src/sources/uploadable_api.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rest.c 3 | * 4 | * Copyright 2016 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | */ 9 | 10 | #include "uploadable_api.h" 11 | #include "dhsettings.h" 12 | #include "uploadable_page.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | HTTP_RESPONSE_STATUS ICACHE_FLASH_ATTR uploadable_api_handle(const char *path, const char *key, 19 | HTTP_CONTENT *content_in, HTTP_ANSWER *answer) { 20 | static const char flash[] = "/flash/page/"; 21 | answer->content.len = 0; 22 | if(os_strncmp(path, flash, sizeof(flash) - 1) == 0) { 23 | if(dhsettings_get_devicehive_key()[0]) { 24 | if(key == 0) { 25 | return HRCS_UNAUTHORIZED; 26 | } 27 | if(os_strcmp(key, dhsettings_get_devicehive_key())) { 28 | return HRCS_UNAUTHORIZED; 29 | } 30 | } 31 | const char *p = &path[sizeof(flash) - 1]; 32 | UP_STATUS res = UP_STATUS_WRONG_CALL; 33 | if(os_strcmp(p, "begin") == 0) { 34 | if(content_in->len == 0) 35 | res = uploadable_page_begin(); 36 | } else if(os_strcmp(p, "finish") == 0) { 37 | if(content_in->len == 0) 38 | res = uploadable_page_finish(); 39 | } else if(os_strcmp(p, "put") == 0) { 40 | if(content_in->len) 41 | res = uploadable_page_put(content_in->data, content_in->len); 42 | } else { 43 | return HRCS_NOT_FOUND; 44 | } 45 | switch(res) { 46 | case UP_STATUS_OK: 47 | return HRCS_ANSWERED_PLAIN; 48 | case UP_STATUS_INTERNAL_ERROR: 49 | return HRCS_INTERNAL_ERROR; 50 | case UP_STATUS_WRONG_CALL: 51 | answer->ok = 0; 52 | return HRCS_ANSWERED_PLAIN; 53 | case UP_STATUS_OVERFLOW: 54 | return HRCS_TOO_MANY_REQUESTS; 55 | } 56 | return HRCS_ANSWERED_PLAIN; 57 | } 58 | return HRCS_NOT_FOUND; 59 | } 60 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ads1115_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief ADS1115 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/ads1115_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/ads1115.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_ADS1115) && defined(DH_DEVICE_ADS1115) 17 | 18 | /* 19 | * dh_handle_devices_ads1115_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_ads1115_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char *err_msg = parse_params_pins_set(params, params_len, &info, 28 | DH_ADC_SUITABLE_PINS, 0, AF_SDA | AF_SCL | AF_ADDRESS, &fields); 29 | if (err_msg != 0) { 30 | dh_command_fail(cmd_res, err_msg); 31 | return; // FAILED 32 | } 33 | if (fields & AF_ADDRESS) 34 | ads1115_set_address(info.address); 35 | } 36 | fields |= AF_ADDRESS; 37 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 38 | return; // FAILED 39 | 40 | float values[4]; 41 | const int status = ads1115_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, values); 42 | const char *err_msg = dh_i2c_error_string(status); 43 | if (err_msg != 0) { 44 | dh_command_fail(cmd_res, err_msg); 45 | } else { 46 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 47 | "{\"0\":%f, \"1\":%f, \"2\":%f, \"3\":%f}", 48 | values[0], values[1], values[2], values[3]); 49 | } 50 | } 51 | 52 | #endif /* DH_COMMANDS_ADS1115 && DH_DEVICE_ADS1115 */ 53 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mlx90614_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MLX90614 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/mlx90614_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/mlx90614.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_MLX90614) && defined(DH_DEVICE_MLX90614) 17 | 18 | /* 19 | * dh_handle_devices_mlx90614_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_mlx90614_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char *err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | if (fields & AF_ADDRESS) 35 | mlx90614_set_address(info.address); 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; // FAILED 41 | 42 | float ambient, object; 43 | const int status = mlx90614_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &ambient, &object); 44 | const char *err_msg = dh_i2c_error_string(status); 45 | if (err_msg != 0) { 46 | dh_command_fail(cmd_res, err_msg); 47 | } else { 48 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 49 | "{\"ambient\":%f, \"object\":%f}", ambient, object); 50 | } 51 | } 52 | 53 | #endif /* DH_COMMANDS_MLX90614 && DH_DEVICE_MLX90614 */ 54 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/si7021_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief SI7021 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/si7021_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/si7021.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_SI7021) && defined(DH_DEVICE_SI7021) 17 | 18 | /* 19 | * dh_handle_devices_si7021_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_si7021_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char *err_msg = parse_params_pins_set(params, params_len, &info, 28 | DH_ADC_SUITABLE_PINS, 0, AF_SDA | AF_SCL | AF_ADDRESS, &fields); 29 | if (err_msg != 0) { 30 | dh_command_fail(cmd_res, err_msg); 31 | return; // FAILED 32 | } 33 | if (fields & AF_ADDRESS) 34 | si7021_set_address(info.address); 35 | } 36 | 37 | fields |= AF_ADDRESS; 38 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 39 | return; // FAILED 40 | 41 | float temperature; 42 | float humidity; 43 | const int status = si7021_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &humidity, &temperature); 44 | const char *err_msg = dh_i2c_error_string(status); 45 | if (err_msg != 0) { 46 | dh_command_fail(cmd_res, err_msg); 47 | } else { 48 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 49 | "{\"temperature\":%f, \"humidity\":%f}", temperature, humidity); 50 | } 51 | } 52 | 53 | #endif /* DH_COMMANDS_SI7021 && DH_DEVICE_SI7021 */ 54 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/bmp180_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief BMP180 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/bmp180_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/bmp180.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_BMP180) && defined(DH_DEVICE_BMP180) 17 | 18 | /* 19 | * dh_handle_devices_bmp180_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_bmp180_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char *err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | if (fields & AF_ADDRESS) 35 | bmp180_set_address(info.address); 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; // FAILED 41 | 42 | float temperature; 43 | int pressure; 44 | const int status = bmp180_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &pressure, &temperature); 45 | const char *err_msg = dh_i2c_error_string(status); 46 | if (err_msg != 0) { 47 | dh_command_fail(cmd_res, err_msg); 48 | } else { 49 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 50 | "{\"temperature\":%f, \"pressure\":%d}", temperature, pressure); 51 | } 52 | } 53 | 54 | #endif /* DH_COMMANDS_BMP180 && DH_DEVICE_BMP180 */ 55 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/bmp280_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief BMP280 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/bmp280_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/bmp280.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_BMP280) && defined(DH_DEVICE_BMP280) 17 | 18 | /* 19 | * dh_handle_devices_bmp280_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_bmp280_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char *err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | if (fields & AF_ADDRESS) 35 | bmp280_set_address(info.address); 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; // FAILED 41 | 42 | float temperature; 43 | float pressure; 44 | const int status = bmp280_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &pressure, &temperature); 45 | const char *err_msg = dh_i2c_error_string(status); 46 | if (err_msg != 0) { 47 | dh_command_fail(cmd_res, err_msg); 48 | } else { 49 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 50 | "{\"temperature\":%f, \"pressure\":%f}", temperature, pressure); 51 | } 52 | } 53 | 54 | #endif /* DH_COMMANDS_BMP280 && DH_DEVICE_BMP280 */ 55 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/onewire_cmd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Onewire command handlers. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _COMMANDS_ONEWIRE_CMD_H_ 8 | #define _COMMANDS_ONEWIRE_CMD_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_COMMANDS_ONEWIRE) // onewire command handlers 12 | #include "dhsender_data.h" 13 | #include "dhcommand_parser.h" 14 | 15 | /** 16 | * @brief Initialization helper. 17 | * @return Non-zero if onewire was initialized. Zero otherwise. 18 | */ 19 | int dh_onewire_init_helper(COMMAND_RESULT *cmd_res, ALLOWED_FIELDS fields, 20 | const gpio_command_params *params); 21 | 22 | 23 | /** 24 | * @brief Handle "onewire/master/read" command. 25 | */ 26 | void dh_handle_onewire_master_read(COMMAND_RESULT *cmd_res, const char *command, 27 | const char *params, unsigned int params_len); 28 | 29 | 30 | /** 31 | * @brief Handle "onewire/master/write" commands. 32 | */ 33 | void dh_handle_onewire_master_write(COMMAND_RESULT *cmd_res, const char *command, 34 | const char *params, unsigned int params_len); 35 | 36 | 37 | /** 38 | * Handle "onewire/master/search" or "onewire/master/alarm" commands. 39 | */ 40 | void dh_handle_onewire_master_search(COMMAND_RESULT *cmd_res, const char *command, 41 | const char *params, unsigned int params_len); 42 | 43 | 44 | /** 45 | * @brief Handle "onewire/master/int" command. 46 | */ 47 | void dh_handle_onewire_master_int(COMMAND_RESULT *cmd_res, const char *command, 48 | const char *params, unsigned int params_len); 49 | 50 | #endif /* DH_COMMANDS_ONEWIRE */ 51 | #endif /* _COMMANDS_ONEWIRE_CMD_H_ */ 52 | -------------------------------------------------------------------------------- /firmware-src/sources/dhnotification.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dhnotification.c 3 | * 4 | * Copyright 2015 DeviceHive 5 | * 6 | * Author: Nikolay Khabarov 7 | * 8 | * Description: Module for catching hardware events and preparing notification to DeviceHive server 9 | * 10 | */ 11 | #include "dhnotification.h" 12 | #include "dhsender.h" 13 | #include "DH/gpio.h" 14 | #include "DH/adc.h" 15 | #include "user_config.h" 16 | #include "snprintf.h" 17 | #include "dhdata.h" 18 | #include "dhdebug.h" 19 | #include "dhstatistic.h" 20 | #include "dhmem.h" 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | void ICACHE_FLASH_ATTR dh_gpio_int_cb(DHGpioPinMask caused_pins) { 28 | if(dhmem_isblock()) { 29 | dhstat_got_notification_dropped(); 30 | return; 31 | } 32 | dhsender_notification(RNT_NOTIFICATION_GPIO, RDT_GPIO, caused_pins, dh_gpio_read(), system_get_time(), DH_GPIO_SUITABLE_PINS); 33 | } 34 | 35 | void ICACHE_FLASH_ATTR dh_adc_loop_value_cb(float value){ 36 | if(dhmem_isblock()) { 37 | dhstat_got_notification_dropped(); 38 | return; 39 | } 40 | dhsender_notification(RNT_NOTIFICATION_ADC, RDT_FLOAT, value); 41 | } 42 | 43 | void ICACHE_FLASH_ATTR dh_uart_buf_rcv_cb(const void *buf, size_t len) { 44 | if(dhmem_isblock()) { 45 | dhstat_got_notification_dropped(); 46 | return; 47 | } 48 | 49 | dhsender_notification(RNT_NOTIFICATION_UART, RDT_DATA_WITH_LEN, buf, len); 50 | } 51 | 52 | 53 | /* 54 | * dh_onewire_search_result() implementation. 55 | */ 56 | void ICACHE_FLASH_ATTR dh_onewire_search_result(unsigned int pin, const void *buf, size_t len) 57 | { 58 | if (dhmem_isblock()) { 59 | dhstat_got_notification_dropped(); 60 | return; 61 | } 62 | dhsender_notification(RNT_NOTIFICATION_ONEWIRE, RDT_SEARCH64, pin, buf, len); 63 | } 64 | -------------------------------------------------------------------------------- /examples/chip_configure_script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This is a sample script which can be used for automated chips configuring. 4 | # Connect your fresh flashed chip to your computer and run this script to configure chip. 5 | 6 | # System serial port device. 7 | PORT=/dev/ttyUSB0 8 | # WiFi network credentials. 9 | WIFI_SSID="WiFiNetwork" 10 | WIFI_PASSWORD="WiFiPassword" 11 | # Server credentials. In this sample data, use script argument as suffix for deviceid. 12 | SERVER_URL="http://playground.devicehive.com/api/rest" 13 | DEVICEID="esp-name$1" 14 | KEY="Abracadabra=" 15 | 16 | # Function for erasing input data. 17 | function clean { 18 | echo -e -n "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" > $PORT 19 | } 20 | 21 | # Configure port. 22 | stty -F $PORT 115200 23 | 24 | # Connect to port output stream, just to see chip output in the terminal. 25 | cat $PORT & 26 | pid=$! 27 | 28 | # If something was entered in chip terminal before, make sure that we start with 29 | # new line by sending unexsting command. 30 | echo "nop" > $PORT 31 | sleep 0.2 32 | # Send configure command. 33 | echo "configure" > $PORT 34 | sleep 0.2 35 | # Send WiFi mode. 36 | clean 37 | echo "0" > $PORT 38 | sleep 0.2 39 | # Send WiFi network SSID. 40 | clean 41 | echo "$WIFI_SSID" > $PORT 42 | sleep 0.2 43 | # Send WiFi network password, no need to clean, it's always blank. 44 | echo "$WIFI_PASSWORD" > $PORT 45 | sleep 0.2 46 | # Send cloud server api endpoint. 47 | clean 48 | echo "$SERVER_URL" > $PORT 49 | sleep 0.2 50 | # Send deviceid. 51 | clean 52 | echo "$DEVICEID" > $PORT 53 | sleep 0.2 54 | # Send key. 55 | echo "$KEY" > $PORT 56 | sleep 0.2 57 | # Terminate reading process. 58 | kill $pid 59 | echo -e "\nDone." 60 | 61 | -------------------------------------------------------------------------------- /firmware-src/sources/dhsender_queue.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhsender_queue.h 3 | * \brief Module for storing request that should be sent 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _DHSENDER_QUEUE_H_ 10 | #define _DHSENDER_QUEUE_H_ 11 | 12 | #include "dhsender_data.h" 13 | 14 | #include 15 | 16 | /** 17 | * \brief Add new request for dhsender in queue. 18 | * \param[in] type Request type, see REQUEST_TYPE enum. 19 | * \param[in] notification_type If it is notification, this parameter should contain notification type. Ignore for responses. 20 | * \param[in] data_type Type of data that passed to function. 21 | * \param[in] id CommandId for response. Ignore for notifications. 22 | * \param[in] ap Request data. 23 | * \return Non zero value on success, zero on error. 24 | */ 25 | int dhsender_queue_add(REQUEST_TYPE type, REQUEST_NOTIFICATION_TYPE notification_type, REQUEST_DATA_TYPE data_type, unsigned int id, va_list ap); 26 | 27 | /** 28 | * \brief Take one item from queue. 29 | * \details Taken item will be deleted from queue. 30 | * \param[out] out Pointer to SENDER_JSON_DATA that should be created. 31 | * \param[out] is_notification Pointer to variable that will receive non zero value if taken request is notification, zero value otherwise. 32 | * \return Non zero value on success, zero on error. 33 | */ 34 | int dhsender_queue_take(SENDER_JSON_DATA *out, unsigned int *is_notification); 35 | 36 | /** 37 | * \brief Getting current queue size. 38 | * \return Number of item currently in queue. 39 | */ 40 | unsigned int dhsender_queue_length(void); 41 | 42 | /** 43 | * \brief Initialize queue. 44 | */ 45 | void dhsender_queue_init(void); 46 | 47 | #endif /* _DHSENDER_QUEUE_H_ */ 48 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/dht.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with DHT11 humidity sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_DHT_H_ 8 | #define _DEVICES_DHT_H_ 9 | 10 | #include "user_config.h" 11 | #include 12 | 13 | #if defined(DH_DEVICE_DHT11) 14 | /** 15 | * @brief Measure relative humidity with DHT11 sensor one time. 16 | * @param[in] pin 1-wire pin for communication. Can be DH_ONEWIRE_NO_PIN. 17 | * @param[out] humidity Pointer for storing relative humidity result measure in percents. 18 | * @param[out] temperature Pointer for storing temperature result measure in degree Celsius. Can be NULL. 19 | * @return NULL on success, text description on error. 20 | */ 21 | const char* dht11_read(int pin, int *humidity, int *temperature); 22 | #endif /* DH_DEVICE_DHT11 */ 23 | 24 | 25 | #if defined(DH_DEVICE_DHT22) 26 | /** 27 | * @brief Measure relative humidity with DHT22 sensor one time. 28 | * @param[in] pin 1-wire pin for communication. Can be DH_ONEWIRE_NO_PIN. 29 | * @param[out] humidity Pointer for storing relative humidity result measure in percents. 30 | * @param[out] temperature Pointer for storing temperature result measure in degree Celsius. Can be NULL. 31 | * @return NULL on success, text description on error. 32 | */ 33 | const char* dht22_read(int pin, float *humidity, float *temperature); 34 | #endif /* DH_DEVICE_DHT22 */ 35 | 36 | 37 | #if defined(DH_COMMANDS_ONEWIRE) 38 | /** 39 | * @brief Read data from DHT like devices. 40 | * 41 | * Checksum will not be checked. 42 | * 43 | * @param[out] buf Buffer to read to. 44 | * @param[in] len Buffer length in bytes. 45 | * @return Number of read bytes on success, zero on error. 46 | */ 47 | int dht_read(void *buf, size_t len); 48 | 49 | #endif /* DH_COMMANDS_ONEWIRE */ 50 | #endif /* _DEVICES_DHT_H_ */ 51 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/pcf8574_hd44780_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief HD44780 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/pcf8574_hd44780_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/pcf8574_hd44780.h" 10 | #include "devices/pcf8574.h" 11 | #include "DH/i2c.h" 12 | #include "DH/adc.h" 13 | 14 | #include "dhcommand_parser.h" 15 | #include 16 | 17 | #if defined(DH_COMMANDS_PCF8574_HD44780) && defined(DH_DEVICE_PCF8574_HD44780) 18 | 19 | /* 20 | * dh_handle_devices_pcf8574_hd44780_write() implementation. 21 | */ 22 | void ICACHE_FLASH_ATTR dh_handle_devices_pcf8574_hd44780_write(COMMAND_RESULT *cmd_res, const char *command, 23 | const char *params, unsigned int params_len) 24 | { 25 | gpio_command_params info; 26 | ALLOWED_FIELDS fields = 0; 27 | const char *err_msg = parse_params_pins_set(params, params_len, 28 | &info, PCF8574_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS | AF_DATA | AF_TEXT_DATA, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | 35 | if ((fields & (AF_DATA | AF_TEXT_DATA)) == 0 || info.data_len == 0) { 36 | dh_command_fail(cmd_res, "Text not specified"); 37 | return; // FAILED 38 | } 39 | 40 | if (fields & AF_ADDRESS) 41 | pcf8574_set_address(info.address); 42 | 43 | fields |= AF_ADDRESS; 44 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 45 | return; // FAILED 46 | 47 | const int status = pcf8574_hd44780_write(DH_I2C_NO_PIN, DH_I2C_NO_PIN, info.data, info.data_len); 48 | err_msg = dh_i2c_error_string(status); 49 | if (err_msg != 0) { 50 | dh_command_fail(cmd_res, err_msg); 51 | } else { 52 | dh_command_done(cmd_res, ""); 53 | } 54 | } 55 | 56 | #endif /* DH_COMMANDS_PCF8574_HD44780 && DH_DEVICE_PCF8574_HD44780 */ 57 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mpu6050_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MPU6050 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/mpu6050_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/mpu6050.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_MPU6050) && defined(DH_DEVICE_MPU6050) 17 | 18 | /* 19 | * dh_handle_devices_mpu6050_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_mpu6050_read(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | if (params_len) { 27 | const char* err_msg = parse_params_pins_set(params, params_len, 28 | &info, DH_ADC_SUITABLE_PINS, 0, 29 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 30 | if (err_msg != 0) { 31 | dh_command_fail(cmd_res, err_msg); 32 | return; // FAILED 33 | } 34 | if (fields & AF_ADDRESS) 35 | mpu6050_set_address(info.address); 36 | } 37 | 38 | fields |= AF_ADDRESS; 39 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 40 | return; // FAILED 41 | 42 | MPU6050_XYZ acc; 43 | MPU6050_XYZ gyro; 44 | float temperature; 45 | const int status = mpu6050_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &acc, &gyro, &temperature); 46 | const char *err_msg = dh_i2c_error_string(status); 47 | if (err_msg != 0) { 48 | dh_command_fail(cmd_res, err_msg); 49 | } else { 50 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 51 | "{\"temperature\":%f, \"acceleration\":{\"X\":%f, \"Y\":%f, \"Z\":%f}, \"rotation\":{\"X\":%f, \"Y\":%f, \"Z\":%f}}", 52 | temperature, acc.X, acc.Y, acc.Z, gyro.X, gyro.Y, gyro.Z); 53 | } 54 | } 55 | 56 | #endif /* DH_COMMANDS_MPU6050 && DH_DEVICE_MPU6050 */ 57 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mcp4725.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MCP4725 DAC. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/mcp4725.h" 8 | #include "DH/i2c.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_MCP4725) 15 | 16 | /** @brief Default sensor i2c address*/ 17 | #define MCP4725_DEFAULT_ADDRESS 0xC0 18 | 19 | // module variables 20 | static int mAddress = MCP4725_DEFAULT_ADDRESS; 21 | static float mVoltage = 3.3f; 22 | 23 | 24 | /* 25 | * mcp4725_write() implementation. 26 | */ 27 | int ICACHE_FLASH_ATTR mcp4725_write(int sda, int scl, float value) 28 | { 29 | if (value < 0.0f || value > mVoltage) 30 | return DH_I2C_WRONG_PARAMETERS; 31 | 32 | int status; 33 | if (sda != DH_I2C_NO_PIN && scl != DH_I2C_NO_PIN) { 34 | if ((status = dh_i2c_init(sda, scl)) != DH_I2C_OK) { 35 | dhdebug("mcp4725: failed to set up pins"); 36 | return status; 37 | } 38 | } 39 | 40 | const uint16_t v = (uint16_t)(value / mVoltage * 4095.0f); // TODO: check 4096 scale, 4095 looks suspicious 41 | 42 | uint8_t buf[3]; 43 | buf[0] = 0x40; // Config(write DAC register) 44 | buf[1] = ((v >> 4) & 0xFF); 45 | buf[2] = ((v & 0xF) << 4); // LSB bits 46 | if ((status = dh_i2c_write(mAddress, buf, 3, 1)) != DH_I2C_OK) { 47 | dhdebug("mcp4725: failed to write"); 48 | return status; 49 | } 50 | 51 | return DH_I2C_OK; 52 | } 53 | 54 | 55 | /* 56 | * mcp4725_set_address() implementation. 57 | */ 58 | void ICACHE_FLASH_ATTR mcp4725_set_address(int address) 59 | { 60 | mAddress = address; 61 | } 62 | 63 | 64 | /* 65 | * mcp4725_set_vref() implementation. 66 | */ 67 | int ICACHE_FLASH_ATTR mcp4725_set_vref(float voltage) 68 | { 69 | if (mVoltage <= 0.0f) 70 | return DH_I2C_WRONG_PARAMETERS; 71 | 72 | mVoltage = voltage; 73 | return DH_I2C_OK; 74 | } 75 | 76 | #endif /* DH_DEVICE_MCP4725 */ 77 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/pcf8574.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with PCF8574 GPIO extender. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DEVICES_PCF8574_H_ 8 | #define _DEVICES_PCF8574_H_ 9 | 10 | #include "user_config.h" 11 | #if defined(DH_DEVICE_PCF8574) 12 | 13 | /** @brief Pins which can be used with extender. */ 14 | #define PCF8574_SUITABLE_PINS 0b11111111 15 | 16 | 17 | /** 18 | * @brief Read data on extender pins. 19 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 20 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 21 | * @param[out] pins Pointer where pin mask will be stored. 22 | * @return Status value, one of DH_I2C_Status enum. 23 | */ 24 | int pcf8574_read(int sda, int scl, unsigned int *pins); 25 | 26 | 27 | /** 28 | * @brief Write data on extender pins. 29 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 30 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 31 | * @param[in] pins_to_set Pin mask to set up high level. 32 | * @param[in] pins_to_clear Pin mask to set up low level. 33 | * @return Status value, one of DH_I2C_Status enum. 34 | */ 35 | int pcf8574_write(int sda, int scl, 36 | unsigned int pins_to_set, 37 | unsigned int pins_to_clear); 38 | 39 | 40 | /** 41 | * @brief Set all pins. 42 | * @param[in] sda Pin for I2C's SDA. Can be DH_I2C_NO_PIN. 43 | * @param[in] scl Pin for I2C's SCL. Can be DH_I2C_NO_PIN. 44 | * @param[in] pins Pin mask of pins state. 45 | * @return Status value, one of DH_I2C_Status enum. 46 | */ 47 | int pcf8574_set(int sda, int scl, 48 | unsigned int pins); 49 | 50 | 51 | /** 52 | * @brief Set extender address which should be used while reading. 53 | * @param[in] address I2C end device address. 54 | */ 55 | void pcf8574_set_address(int address); 56 | 57 | #endif /* DH_DEVICE_PCF8574 */ 58 | #endif /* _DEVICES_PCF8574_H_ */ 59 | -------------------------------------------------------------------------------- /esp-utils/common/serialport.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * serialport_win.cpp 3 | * 4 | * Cross-platform serial port implementation 5 | * 6 | * Created on: 2014 7 | * Author: Nikolay Khabarov 8 | * License: You can do whatever you want. Author doesn't provide warranty of any kind. 9 | * 10 | */ 11 | 12 | #include 13 | #include "serialport.h" 14 | 15 | SerialPort::SerialPort(COM comport, const char *name) { 16 | mCom = comport; 17 | mTreadFlag = true; 18 | mReadFlag = false; 19 | mReadError = false; 20 | mBytesRecivedSinceLastSend = 0; 21 | mThread = 0; 22 | mLastReceived = 0; 23 | snprintf(mName, sizeof(mName), "%s", name); 24 | } 25 | 26 | COM SerialPort::get_com() { 27 | return mCom; 28 | } 29 | 30 | bool SerialPort::isReadError() { 31 | return mReadError; 32 | } 33 | 34 | bool SerialPort::waitAnswer(unsigned int len, unsigned int timeOutMs) { 35 | for(unsigned int i = 0; i < timeOutMs/10 + 1; i++) { 36 | if(mBytesRecivedSinceLastSend >= len) 37 | return true; 38 | sleep(10); 39 | } 40 | return false; 41 | } 42 | 43 | void SerialPort::waitTransmitionEnd(unsigned int timeOutMs) { 44 | while(getTick() - mLastReceived < timeOutMs) 45 | sleep(10); 46 | } 47 | 48 | void SerialPort::send(const char *text) { 49 | mBytesRecivedSinceLastSend = 0; 50 | unsigned int tlen = strlen(text); 51 | unsigned int bw = write_native(text, tlen); 52 | if(bw != tlen) 53 | SerialPortError(this, ERROR_WRITE_STRING); 54 | } 55 | 56 | void SerialPort::send(char c) { 57 | mBytesRecivedSinceLastSend = 0; 58 | unsigned int bw = write_native(&c, 1); 59 | if(bw <= 0) 60 | SerialPortError(this, ERROR_WRITE_STRING); 61 | } 62 | 63 | void SerialPort::send(const void *data, unsigned int len) { 64 | mBytesRecivedSinceLastSend = 0; 65 | unsigned int bw = write_native(data, len); 66 | if(bw != len) 67 | SerialPortError(this, ERROR_WRITE_STRING); 68 | } 69 | 70 | const char *SerialPort::getName() { 71 | return mName; 72 | } 73 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/mcp4725_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief MCP4725 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/mcp4725_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/mcp4725.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | 13 | #include "dhcommand_parser.h" 14 | #include 15 | 16 | #if defined(DH_COMMANDS_MCP4725) && defined(DH_DEVICE_MCP4725) 17 | 18 | /* 19 | * dh_handle_devices_mcp4725_read() implementation. 20 | */ 21 | void ICACHE_FLASH_ATTR dh_handle_devices_mcp4725_write(COMMAND_RESULT *cmd_res, const char *command, 22 | const char *params, unsigned int params_len) 23 | { 24 | gpio_command_params info; 25 | ALLOWED_FIELDS fields = 0; 26 | const char *err_msg = parse_params_pins_set(params, params_len, 27 | &info, DH_ADC_SUITABLE_PINS, 0, 28 | AF_SDA | AF_SCL | AF_ADDRESS | AF_REF | AF_FLOATVALUES, &fields); 29 | if (err_msg != 0) { 30 | dh_command_fail(cmd_res, err_msg); 31 | return; // FAILED 32 | } 33 | if (info.pin_value_readed != 1) { 34 | dh_command_fail(cmd_res, "Unsuitable pin"); 35 | return; // FAILED 36 | } 37 | if (fields & AF_ADDRESS) 38 | mcp4725_set_address(info.address); 39 | if (fields & AF_REF) { 40 | const int status = mcp4725_set_vref(info.ref); 41 | err_msg = dh_i2c_error_string(status); 42 | if (err_msg != 0) { 43 | dh_command_fail(cmd_res, err_msg); 44 | return; // FAILED 45 | } 46 | } 47 | 48 | fields |= AF_ADDRESS; 49 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 50 | return; // FAILED 51 | 52 | const int status = mcp4725_write(DH_I2C_NO_PIN, DH_I2C_NO_PIN, info.storage.float_values[0]); 53 | const char *res = dh_i2c_error_string(status); 54 | if (res != 0) { 55 | dh_command_fail(cmd_res, res); 56 | } else { 57 | dh_command_done(cmd_res, ""); 58 | } 59 | } 60 | 61 | #endif /* DH_COMMANDS_MCP4725 && DH_DEVICE_MCP4725 */ 62 | -------------------------------------------------------------------------------- /firmware-src/pages/libs.js: -------------------------------------------------------------------------------- 1 | // ajax functions 2 | function ajax(verb, url, params, key, cb) { 3 | var xmlhttp = new XMLHttpRequest(); 4 | xmlhttp.open(verb, url); 5 | xmlhttp.setRequestHeader('Authorization', 'Bearer ' + key); 6 | xmlhttp.onreadystatechange = function () { 7 | if (xmlhttp.readyState == 4) 8 | cb(xmlhttp); 9 | }; 10 | if(params == null || verb.match(/^get$/i)) 11 | xmlhttp.send(); 12 | else if(params.length == 0) 13 | xmlhttp.send(); 14 | else { 15 | if (typeof params == 'object' && !(params instanceof ArrayBuffer)) 16 | params = JSON.stringify(params); 17 | xmlhttp.send(params); 18 | } 19 | } 20 | 21 | function send_command(command, params, key, cb) { 22 | ajax('POST', '/api/' + command, params, key, function(xmlhttp) { 23 | var res = xmlhttp.responseText; 24 | try { 25 | res = JSON.parse(res); 26 | } catch (e) { } 27 | if (xmlhttp.status < 200 || xmlhttp.status > 299) 28 | cb(true, res); 29 | else 30 | cb(false, res); 31 | }); 32 | } 33 | 34 | // dom helpers 35 | function byId(id) { 36 | return document.getElementById(id); 37 | } 38 | 39 | function print(text, err, id) { 40 | var out = byId(id || 'output'); 41 | out.style.color = (!!err) ? 'red' : 'green'; 42 | out.innerHTML = ''; 43 | out.appendChild(document.createTextNode(text)); 44 | } 45 | function replaceDom(target, obj) { 46 | target.innerHTML = ''; 47 | target.appendChild(renderDom(obj)); 48 | } 49 | 50 | function renderDom(obj) { 51 | if (typeof obj == 'string') { 52 | return document.createTextNode(obj); 53 | } else if (Array.isArray(obj)) { 54 | var dom = document.createElement(obj[0]); 55 | var attrs = obj[1]; 56 | // doesn't work for style attribute in IE 57 | Object.keys(attrs).forEach( 58 | function (key) { dom.setAttribute(key, attrs[key]); }); 59 | obj.slice(2).forEach( 60 | function (child) { dom.appendChild(renderDom(child)); }); 61 | return dom; 62 | } else 63 | throw 'Cannot make dom of: ' + obj; 64 | } 65 | 66 | function getKey() { 67 | return byId('key').value; 68 | } 69 | -------------------------------------------------------------------------------- /firmware-src/sources/DH/spi.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief SPI bus implementation for ESP8266 firmware. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DH_SPI_H_ 8 | #define _DH_SPI_H_ 9 | 10 | #include 11 | 12 | /** 13 | * @brief SPI operation mode. 14 | */ 15 | typedef enum { 16 | DH_SPI_CPOL0CPHA0 = 0, ///< @brief Low clock polarity, front edge. 17 | DH_SPI_CPOL0CPHA1 = 1, ///< @brief Low clock polarity, rear edge. 18 | DH_SPI_CPOL1CPHA0 = 2, ///< @brief High clock polarity, front edge. 19 | DH_SPI_CPOL1CPHA1 = 3, ///< @brief High clock polarity, rear edge. 20 | } DHSpiMode; 21 | 22 | 23 | /** 24 | * @brief Virtual pin for CS. 25 | * 26 | * It means that module will not set up CS line automatically. 27 | */ 28 | #define DH_SPI_NO_CS (~0U) 29 | 30 | 31 | /** 32 | * @brief Do not initialize pin indicator. 33 | */ 34 | #define DH_SPI_NO_PIN (-2) 35 | 36 | 37 | /** 38 | * @brief Set SPI mode. 39 | * @param[in] mode SPI mode. 40 | * @return Zero on success. 41 | */ 42 | int dh_spi_set_mode(DHSpiMode mode); 43 | 44 | 45 | /** 46 | * @brief Set CS pin 47 | * @param[in] pin Pin number. Can be DH_SPI_NO_CS. 48 | * @return Zero on success. 49 | */ 50 | int dh_spi_set_cs_pin(unsigned int pin); 51 | 52 | 53 | /** 54 | * @brief Write data to SPI bus. 55 | * 56 | * Pins will be initialized automatically. 57 | * 58 | * @param[in] buf Buffer to write. 59 | * @param[in] len Buffer length in bytes. 60 | * @param[in] disable_cs Non zero value means that CS should be disabled after operation, zero value means not. 61 | */ 62 | void dh_spi_write(const void *buf, size_t len, int disable_cs); 63 | 64 | 65 | /** 66 | * @brief Read data from SPI bus. 67 | * 68 | * Pins will be initialized automatically. 69 | * 70 | * CS will be disabled after operation. 71 | * 72 | * @param[out] buf Buffer to read data to. 73 | * @param[in] len Buffer length in bytes. 74 | */ 75 | void dh_spi_read(void *buf, size_t len); 76 | 77 | #endif /* _DH_SPI_H_ */ 78 | -------------------------------------------------------------------------------- /sdk/include/smartconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ESPRESSIF MIT License 3 | * 4 | * Copyright (c) 2016 5 | * 6 | * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, 7 | * it is free of charge, to any person obtaining a copy of this software and associated 8 | * documentation files (the "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the Software is furnished 11 | * to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all copies or 14 | * substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | * 23 | */ 24 | 25 | #ifndef __SMARTCONFIG_H__ 26 | #define __SMARTCONFIG_H__ 27 | 28 | typedef enum { 29 | SC_STATUS_WAIT = 0, 30 | SC_STATUS_FIND_CHANNEL, 31 | SC_STATUS_GETTING_SSID_PSWD, 32 | SC_STATUS_LINK, 33 | SC_STATUS_LINK_OVER, 34 | } sc_status; 35 | 36 | typedef enum { 37 | SC_TYPE_ESPTOUCH = 0, 38 | SC_TYPE_AIRKISS, 39 | SC_TYPE_ESPTOUCH_AIRKISS, 40 | } sc_type; 41 | 42 | typedef void (*sc_callback_t)(sc_status status, void *pdata); 43 | 44 | const char *smartconfig_get_version(void); 45 | bool smartconfig_start(sc_callback_t cb, ...); 46 | bool smartconfig_stop(void); 47 | bool esptouch_set_timeout(uint8 time_s); //15s~255s, offset:45s 48 | bool smartconfig_set_type(sc_type type); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /firmware-src/README.md: -------------------------------------------------------------------------------- 1 | # DeviceHive ESP8266 Firmware 2 | Special firmware to use ESP8266 in DeviceHive clouds. 3 | 4 | # Building 5 | Project can be opened in Eclipse. To compile it you would need to install 6 | toolchain. See instructions [here](https://github.com/esp8266/esp8266-wiki/wiki/Toolchain). 7 | Actually we need just: [Xtensa crosstool-NG](https://github.com/jcmvbkbc/crosstool-NG). 8 | SDK is already included in this repo. 9 | 10 | # Firmware usage 11 | Flash firmware (`devicehive.bin` firmware directory) to the device with 12 | `esp-flasher` util (see `esp-util` project on top of the repo). You can also use any other 13 | flasher for `esp8266` ([esptool](https://github.com/themadinventor/esptool) for example). 14 | 15 | Firmware should be flashed at `0x0` SPI chip address. Connect to device terminal 16 | via serial port with `esp-terminal` (see `esp-util` project). You can also use any 17 | other serial port terminal with escape sequences support. [PuTTY](http://www.putty.org/) 18 | for Windows OS or `screen` util under Linux for example. This 19 | firmware has quite regular unix like terminal via UART. Autocomplete and command 20 | history are supported. 21 | Type `help` in terminal to see all aviable commands. At first you need to configure 22 | device. Type `configure` in terminal to run configuration util on it. Enter network 23 | and server parameters, device will be rebooted. Now you can send command via 24 | DeviceHive cloud to device. Please refer to [ESP8266](../DeviceHiveESP8266.md) firmware 25 | commands documentation to find which commands are supported. 26 | 27 | # genbin.sh 28 | Small util to extract binary images for flashing for compiled file. Might be useful 29 | for other projects. Usage: 30 | 31 | ``` 32 | genbin.sh [] 33 | ``` 34 | 35 | # pages 36 | This directory contains web pages which will be embedded into firmware. Rebuild 37 | (`make rebuild`) sources after changing content of this pages or run mannually 38 | `gen_pages.sh` script to update it. 39 | 40 | # License 41 | MIT. See [LICENSE](./LICENSE) file. 42 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/mlx90614.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with MLX90614 contactless IR temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/mlx90614.h" 8 | #include "DH/i2c.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_MLX90614) 15 | 16 | /** @brief Default sensor i2c address*/ 17 | #define MLX90614_DEFAULT_ADDRESS 0xB4 18 | 19 | // module variable 20 | static int mAddress = MLX90614_DEFAULT_ADDRESS; 21 | 22 | 23 | /* 24 | * mlx90614_read() implementation. 25 | */ 26 | int ICACHE_FLASH_ATTR mlx90614_read(int sda, int scl, float *ambient, float *object) 27 | { 28 | int status; 29 | if (sda != DH_I2C_NO_PIN && scl != DH_I2C_NO_PIN) { 30 | if ((status = dh_i2c_init(sda, scl)) != DH_I2C_OK) { 31 | dhdebug("mlx90614: failed to set up pins"); 32 | return status; 33 | } 34 | } 35 | 36 | uint8_t buf[2]; 37 | buf[0] = 0x06; // Ta register 38 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 39 | dhdebug("mlx90614: failed to get Ta register"); 40 | return status; 41 | } 42 | if ((status = dh_i2c_read(mAddress, buf, 2)) != DH_I2C_OK) { 43 | dhdebug("mlx90614: failed to read Ta"); 44 | return status; 45 | } 46 | 47 | int raw = signedInt16le((const char*)buf, 0); 48 | *ambient = raw * 0.02f - 273.15f; 49 | 50 | buf[0] = 0x07; // Tobj1 register 51 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 52 | dhdebug("mlx90614: failed to get Tobj1 register"); 53 | return status; 54 | } 55 | if ((status = dh_i2c_read(mAddress, buf, 2)) != DH_I2C_OK) { 56 | dhdebug("mlx90614: failed to read Tobj1"); 57 | return status; 58 | } 59 | 60 | raw = signedInt16le((const char*)buf, 0); 61 | *object = raw * 0.02f - 273.15f; 62 | 63 | return DH_I2C_OK; 64 | } 65 | 66 | 67 | /* 68 | * mlx90614_set_address() implementation. 69 | */ 70 | void ICACHE_FLASH_ATTR mlx90614_set_address(int address) 71 | { 72 | mAddress = address; 73 | } 74 | 75 | #endif /* DH_DEVICE_MLX90614 */ 76 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/ina219_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief INA219 command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/ina219_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/ina219.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | #include "snprintf.h" 13 | 14 | #include "dhcommand_parser.h" 15 | #include 16 | 17 | #if defined(DH_COMMANDS_INA219) && defined(DH_DEVICE_INA219) 18 | 19 | /* 20 | * dh_handle_devices_ina219_read() implementation. 21 | */ 22 | void ICACHE_FLASH_ATTR dh_handle_devices_ina219_read(COMMAND_RESULT *cmd_res, const char *command, 23 | const char *params, unsigned int params_len) 24 | { 25 | gpio_command_params info; 26 | ALLOWED_FIELDS fields = 0; 27 | if (params_len) { 28 | const char* err_msg = parse_params_pins_set(params, params_len, 29 | &info, DH_ADC_SUITABLE_PINS, 0, 30 | AF_SDA | AF_SCL | AF_ADDRESS | AF_REF, &fields); 31 | if (err_msg != 0) { 32 | dh_command_fail(cmd_res, err_msg); 33 | return; // FAILED 34 | } 35 | if (fields & AF_ADDRESS) 36 | ina219_set_address(info.address); 37 | if (fields & AF_REF) { 38 | const int status = ina219_set_shunt(info.ref); 39 | err_msg = dh_i2c_error_string(status); 40 | if (err_msg != 0) { 41 | dh_command_fail(cmd_res, err_msg); 42 | return; // FAILED 43 | } 44 | } 45 | } 46 | 47 | fields |= AF_ADDRESS; 48 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 49 | return; // FAILED 50 | 51 | float voltage; 52 | float current; 53 | float power; 54 | 55 | const int status = ina219_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &voltage, ¤t, &power); 56 | const char *err_msg = dh_i2c_error_string(status); 57 | if (err_msg != 0) { 58 | dh_command_fail(cmd_res, err_msg); 59 | } else { 60 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 61 | "{\"voltage\":%f, \"current\":%f, \"power\":%f}", 62 | voltage, current, power); 63 | } 64 | } 65 | 66 | #endif /* DH_COMMANDS_INA219 && DH_DEVICE_INA219 */ 67 | -------------------------------------------------------------------------------- /examples-cloud/simple-gpio-set.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DeviceHive - Esp8266 test 6 | 22 | 23 | 24 |

Beeper: 25 | 26 | 27 |


LED: 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /firmware-src/sources/uploadable_page.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file httpd.h 3 | * \brief Module for handling uploadable web page. 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _UPLOADABLE_PAGE_H_ 10 | #define _UPLOADABLE_PAGE_H_ 11 | 12 | /** Uploadable page functions return status. */ 13 | typedef enum { 14 | UP_STATUS_OK, ///< Successfully done. 15 | UP_STATUS_INTERNAL_ERROR, ///< Error while saving in ROM / no memory. 16 | UP_STATUS_WRONG_CALL, ///< Method is not allowed to call at this point. 17 | UP_STATUS_OVERFLOW ///< ROM storage is overflowed. 18 | } UP_STATUS; 19 | 20 | /** 21 | * \brief Get the content of uploadable page. 22 | * \details Content is stored in ROM, it can be read only per 4 bytes. 23 | * \param[in] data Pointer where to store data length in bytes. 24 | * \return Pointer in ROM memory. 25 | */ 26 | const char *uploadable_page_get(unsigned int *len); 27 | 28 | /** 29 | * \brief Destroy page data. 30 | * \details Physically page will be kept without first byte. 31 | * \return One of UP_STATUS statuses. 32 | */ 33 | UP_STATUS uploadable_page_delete(); 34 | 35 | /** 36 | * \brief Initialize flashing new web page procedure. 37 | * \details Pointer for writing will be moved to very beginning. This 38 | * mode will be exited if no writes operation happen in last 39 | * UPLOADABLE_PAGE_TIMEOUT_MS. 40 | * \return One of UP_STATUS statuses. 41 | */ 42 | UP_STATUS uploadable_page_begin(); 43 | 44 | /** 45 | * \brief Write piece of data. Address increments internally. 46 | * \details Data is saved per 4 KiB blocks. Smaller buffer will be saved 47 | * internally and will be written when 4 KiB is collected. 48 | * \param[in] data Pointer to data. 49 | * \param[in] data_len Data length. 50 | * \return One of UP_STATUS statuses. 51 | */ 52 | UP_STATUS uploadable_page_put(const char *data, unsigned int data_len); 53 | 54 | /** 55 | * \brief Flash all remains data and leave out flashing procedure. 56 | * \return One of UP_STATUS statuses. 57 | */ 58 | UP_STATUS uploadable_page_finish(); 59 | 60 | #endif /* _UPLOADABLE_PAGE_H_ */ 61 | -------------------------------------------------------------------------------- /sdk/include/sntp.h: -------------------------------------------------------------------------------- 1 | #ifndef __SNTP_H__ 2 | #define __SNTP_H__ 3 | 4 | #include "os_type.h" 5 | #ifdef LWIP_OPEN_SRC 6 | #include "lwip/ip_addr.h" 7 | #else 8 | #include "ip_addr.h" 9 | #endif 10 | /** 11 | * get the seconds since Jan 01, 1970, 00:00 (GMT + 8) 12 | */ 13 | uint32 sntp_get_current_timestamp(); 14 | /** 15 | * get real time (GTM + 8 time zone) 16 | */ 17 | char* sntp_get_real_time(long t); 18 | /** 19 | * SNTP get time_zone default GMT + 8 20 | */ 21 | sint8 sntp_get_timezone(void); 22 | /** 23 | * SNTP set time_zone (default GMT + 8) 24 | */ 25 | bool sntp_set_timezone(sint8 timezone); 26 | /** 27 | * Initialize this module. 28 | * Send out request instantly or after SNTP_STARTUP_DELAY(_FUNC). 29 | */ 30 | void sntp_init(void); 31 | /** 32 | * Stop this module. 33 | */ 34 | void sntp_stop(void); 35 | /** 36 | * Initialize one of the NTP servers by IP address 37 | * 38 | * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS 39 | * @param dnsserver IP address of the NTP server to set 40 | */ 41 | void sntp_setserver(unsigned char idx, ip_addr_t *addr); 42 | /** 43 | * Obtain one of the currently configured by IP address (or DHCP) NTP servers 44 | * 45 | * @param numdns the index of the NTP server 46 | * @return IP address of the indexed NTP server or "ip_addr_any" if the NTP 47 | * server has not been configured by address (or at all). 48 | */ 49 | ip_addr_t sntp_getserver(unsigned char idx); 50 | /** 51 | * Initialize one of the NTP servers by name 52 | * 53 | * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS,now sdk support SNTP_MAX_SERVERS = 3 54 | * @param dnsserver DNS name of the NTP server to set, to be resolved at contact time 55 | */ 56 | void sntp_setservername(unsigned char idx, char *server); 57 | /** 58 | * Obtain one of the currently configured by name NTP servers. 59 | * 60 | * @param numdns the index of the NTP server 61 | * @return IP address of the indexed NTP server or NULL if the NTP 62 | * server has not been configured by name (or at all) 63 | */ 64 | char *sntp_getservername(unsigned char idx); 65 | 66 | #define sntp_servermode_dhcp(x) 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/si7021.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with SI7021 relative humidity and temperature sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/si7021.h" 8 | #include "DH/i2c.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_SI7021) 15 | 16 | /** @brief Default sensor i2c address*/ 17 | #define SI7021_DEFAULT_ADDRESS 0x80 18 | 19 | // module variables 20 | static int mAddress = SI7021_DEFAULT_ADDRESS; 21 | 22 | 23 | /* 24 | * si7021_read() implementation. 25 | */ 26 | int ICACHE_FLASH_ATTR si7021_read(int sda, int scl, float *humidity, float *temperature) 27 | { 28 | int status; 29 | if(sda != DH_I2C_NO_PIN && scl != DH_I2C_NO_PIN) { 30 | if ((status = dh_i2c_init(sda, scl)) != DH_I2C_OK) { 31 | dhdebug("si7021: failed to set up pins"); 32 | return status; 33 | } 34 | } 35 | 36 | uint8_t buf[2]; 37 | buf[0] = 0xE5; // read humidity, hold master 38 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 39 | dhdebug("si7021: failed to write get humidity"); 40 | return status; 41 | } 42 | if ((status = dh_i2c_read(mAddress, buf, 2)) != DH_I2C_OK) { 43 | dhdebug("si7021: failed to read humidity"); 44 | return status; 45 | } 46 | *humidity = ((float)unsignedInt16be((const char*)buf, 0)) * 125.0f / 65536.0f - 6.0f; 47 | 48 | if (temperature) { 49 | buf[0] = 0xE0; // read temperature from humidity measurement 50 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 51 | dhdebug("si7021: failed to write get temperature"); 52 | return status; 53 | } 54 | if ((status = dh_i2c_read(mAddress, buf, 2)) != DH_I2C_OK) { 55 | dhdebug("si7021: failed to read temperature"); 56 | return status; 57 | } 58 | *temperature = ((float)unsignedInt16be((const char *)buf, 0)) * 175.72f / 65536.0f - 46.85f; 59 | } 60 | 61 | return DH_I2C_OK; 62 | } 63 | 64 | 65 | /* 66 | * si7021_set_address() implementation. 67 | */ 68 | void ICACHE_FLASH_ATTR si7021_set_address(int address) 69 | { 70 | mAddress = address; 71 | } 72 | 73 | #endif /* DH_DEVICE_SI7021 */ 74 | -------------------------------------------------------------------------------- /sdk/include/ping.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ESPRESSIF MIT License 3 | * 4 | * Copyright (c) 2016 5 | * 6 | * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, 7 | * it is free of charge, to any person obtaining a copy of this software and associated 8 | * documentation files (the "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the Software is furnished 11 | * to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all copies or 14 | * substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | * 23 | */ 24 | 25 | #ifndef __PING_H__ 26 | #define __PING_H__ 27 | 28 | 29 | typedef void (* ping_recv_function)(void* arg, void *pdata); 30 | typedef void (* ping_sent_function)(void* arg, void *pdata); 31 | 32 | struct ping_option{ 33 | uint32 count; 34 | uint32 ip; 35 | uint32 coarse_time; 36 | ping_recv_function recv_function; 37 | ping_sent_function sent_function; 38 | void* reverse; 39 | }; 40 | 41 | struct ping_resp{ 42 | uint32 total_count; 43 | uint32 resp_time; 44 | uint32 seqno; 45 | uint32 timeout_count; 46 | uint32 bytes; 47 | uint32 total_bytes; 48 | uint32 total_time; 49 | sint8 ping_err; 50 | }; 51 | 52 | bool ping_start(struct ping_option *ping_opt); 53 | bool ping_regist_recv(struct ping_option *ping_opt, ping_recv_function ping_recv); 54 | bool ping_regist_sent(struct ping_option *ping_opt, ping_sent_function ping_sent); 55 | 56 | #endif /* __PING_H__ */ 57 | -------------------------------------------------------------------------------- /firmware-src/sources/dhsender.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file dhsender.h 3 | * \brief Module for sending remote command results and notifications 4 | * \author Nikolay Khabarov 5 | * \date 2015 6 | * \copyright DeviceHive MIT 7 | * \details Responses and notifications will not be sent immediately, module has queue that use dynamic memory. 8 | * Having this queue module can collect lots of responses or notifications that will be sent when it is 9 | * possible. But if no dynamic memory enough data will be lost, notification about it will print in debug. 10 | */ 11 | 12 | #ifndef _DHSENDER_H_ 13 | #define _DHSENDER_H_ 14 | 15 | #include "dhsender_data.h" 16 | 17 | #include 18 | 19 | /** Function prototype for new item in queue callback. */ 20 | typedef void (*dhsender_new_item_cb)(void); 21 | 22 | /** 23 | * \brief Notify that current data was failed to send. 24 | */ 25 | void dhsender_current_fail(void); 26 | 27 | /** 28 | * \brief Notify that current data was sent. 29 | */ 30 | void dhsender_current_success(void); 31 | 32 | /** 33 | * \brief Get next struct SENDER_JSON_DATA which should be sent. 34 | * \return Pointer to SENDER_JSON_DATA or NULL if there is no data to send. 35 | */ 36 | SENDER_JSON_DATA *dhsender_next(void); 37 | 38 | /** 39 | * \brief Set callbacks. 40 | * \param[in] new_item Pointer to a function which should be called on adding new item to queue. 41 | */ 42 | void dhsender_set_cb(dhsender_new_item_cb new_item); 43 | 44 | /** 45 | * \brief Send command response. 46 | * \param[in] cid Command id that response should be sent. 47 | * \param[in] status Command status. 48 | * \param[in] data_type Data type for response. 49 | * \param[in] ... Data according to the type. 50 | */ 51 | void dhsender_response(CommandResultArgument cid, RESPONCE_STATUS status, REQUEST_DATA_TYPE data_type, ...); 52 | 53 | /** 54 | * \brief Send notification. 55 | * \param[in] type Type of module that sends notification. 56 | * \param[in] data_type Data type for notification. 57 | * \param[in] ... Data according to the type. 58 | */ 59 | void dhsender_notification(REQUEST_NOTIFICATION_TYPE type, REQUEST_DATA_TYPE data_type, ...); 60 | 61 | #endif /* _DHSENDER_H_ */ 62 | -------------------------------------------------------------------------------- /firmware-src/pages/pwm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PWM 5 | 6 | 28 | 29 | 30 |
31 |
32 |
33 | 34 | 35 |
36 |
37 |
38 |
39 | 42 |
This sample shows how to use PWM. Connect LED and specified pin. Click buttons below. 43 |

44 | Some boards have pin connected to GPIO2(TX) port and they mostly light up with '0' port state. 45 |
46 | 47 | 48 | 49 | 51 |
Brightness control, slide to light up
52 | 53 |
54 |
55 |
56 | 57 | -------------------------------------------------------------------------------- /firmware-src/sources/httpd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file httpd.h 3 | * \brief Simple HTTP server. 4 | * \author Nikolay Khabarov 5 | * \date 2016 6 | * \copyright DeviceHive MIT 7 | */ 8 | 9 | #ifndef _HTTPD_H_ 10 | #define _HTTPD_H_ 11 | 12 | /** Callback return code */ 13 | typedef enum { 14 | HRCS_ANSWERED_HTML, ///< Response was sent with html, content field is set. 15 | HRCS_ANSWERED_JSON, ///< Response was sent with json, content field is set. 16 | HRCS_ANSWERED_JS, ///< Response was sent with js, content field is set. 17 | HRCS_ANSWERED_CSS, ///< Response was sent with css, content field is set. 18 | HRCS_ANSWERED_PLAIN, ///< Response was sent with plain text, content field is set. 19 | HRCS_ANSWERED_XICON, ///< Response was sent with ico format icon. 20 | HRCS_NOT_FINISHED, ///< Request is not finished. 21 | HRCS_NOT_FOUND, ///< Path not found. 22 | HRCS_INTERNAL_ERROR, ///< Internal error happens. 23 | HRCS_BAD_REQUEST, ///< Request is not correct. 24 | HRCS_NOT_IMPLEMENTED, ///< Method is not implemented. 25 | HRCS_UNAUTHORIZED, ///< Unauthorized. 26 | HRCS_TOO_MANY_REQUESTS, ///< Too many requests on server. 27 | HRCS_OPTIONS ///< Options. 28 | } HTTP_RESPONSE_STATUS; 29 | 30 | /** Content descriptor */ 31 | typedef struct { 32 | const char *data; ///< Data to data. 33 | unsigned int len; ///< Data length. 34 | } HTTP_CONTENT; 35 | 36 | /** Struct for HRCS_ANSWERED data */ 37 | typedef struct { 38 | HTTP_CONTENT content; ///< Data to return. 39 | unsigned ok : 1; ///< Is response with 2xx code? True by default. 40 | unsigned free_content : 1; ///< Is data was malloced, need to be free? False by default. 41 | unsigned gzip : 1; ///< Is data gzip compressed. 42 | } HTTP_ANSWER; 43 | 44 | /** Callback prototype for requests. */ 45 | typedef HTTP_RESPONSE_STATUS (*HttpRequestCb)(const char *path, const char *key, 46 | HTTP_CONTENT *content_in, HTTP_ANSWER *answer); 47 | 48 | /** 49 | * \brief Initialize HTTP daemon 50 | */ 51 | void httpd_init(HttpRequestCb get_cb, HttpRequestCb post_cb); 52 | 53 | /** 54 | * \brief Force server to redirect and answer only to single host 55 | * \param[in] host Pointer to host name, or zero to disable redirect. 56 | */ 57 | void httpd_redirect(const char *host); 58 | 59 | #endif /* _HTTPD_H_ */ 60 | -------------------------------------------------------------------------------- /changelog.txt: -------------------------------------------------------------------------------- 1 | v0.1 Initial version 2 | 3 | v0.2 4 | - Minor terminal improvment 5 | - PWM added 6 | - UART added 7 | - I2C added 8 | - DeviceName sends to server as the same string as DeviceId 9 | - Online status showing now works on server 10 | - timeout added for GPIO interruption 11 | - OS X flasher fix 12 | 13 | v0.3 14 | - wireless zero configuration mode 15 | - timestamp for gpio notifications 16 | - SPI added 17 | - onewire added 18 | - statistics in 'status' terminal command 19 | - stability improvement 20 | 21 | v0.4 22 | - new playgrounds support 23 | 24 | v0.5 25 | - uploadable web page 26 | - mDNS 27 | - local embedded web server with tools and samples 28 | - local RESTful API 29 | - support for some popular sensor devices 30 | - bug fixes 31 | 32 | v0.6 33 | - fixes and improvements 34 | - new devices support. Overall firmware supports: 35 | - DS18B20, LM75 - temperature sensors, 36 | - DHT11, DHT22, SI7021 - humidity sensors 37 | - BMP180, BMP280 - barometric pressure sensor 38 | - BH1750 - light sensor 39 | - MPU6050 - 3-axis gyroscope and a 3-axis accelerometer 40 | - HMC5883L - magnetometer 41 | - PCF8574 - GPIO extender 42 | - HD44780 - 2x16 4x20 displays(with PCF8574 adapter) 43 | - MH-Z19 - CO2 sensor 44 | - MCP4725 - digital to analog convertor 45 | - PCF8591 - analog to digital and digital to analog converter 46 | - ADS1115 - analog to digital converter 47 | - INA219 - power monitor 48 | - MFRC522 - RFID tag reader 49 | - PCA9685 - PWM controller 50 | - MLX90614 - contactless IR thermometer 51 | - MAX6675, MAX31855 - thermocouple thermometers 52 | - TM1637 - 8 segments LED display controller 53 | - WS2812B - LED strip controller 54 | - access point mode 55 | - embedded pages redesign 56 | - sample of using D3.js for plotting graphs from different sensors on embedded web server 57 | 58 | v0.7 59 | - new command 'command/list' 60 | - connectino to server now is implemeted with WebSocket 61 | - latest DeviceHive server v3.x and playground support 62 | - compile time ability to disable some devices 63 | 64 | v0.8 65 | - TLS connection with server. 66 | - fix dht11/dht22 support 67 | - improve stability of esp-flasher/esp-terminal for mac os 68 | - latest server connectivity protocol fixes 69 | 70 | -------------------------------------------------------------------------------- /firmware-src/pages/hd44780.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LCD 1602A, 2004A etc 5 | 6 | 29 | 30 | 31 |
32 |
33 |
34 | 35 | 36 |
37 |
38 |
39 |
    40 | This sample displays text on connected via PCF8574 adapter HD44870 screen like 1602A, 2004A etc. 41 |

    Pinmap for adapter should be (PCF8574 pin -> Display pin): 42 |

  • P0->RS
  • 43 |
  • P1->RW
  • 44 |
  • P2->E
  • 45 |
  • P3->Backligth control or not connected
  • 46 |
  • P4->D4
  • 47 |
  • P5->D5
  • 48 |
  • P6->D6
  • 49 |
  • P7->D7
  • 50 |
51 |
52 | 55 | 58 | 61 | 64 | 65 |
66 |
67 |
68 | 69 | 70 | -------------------------------------------------------------------------------- /esp-utils/common/serialport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * serialport.h 3 | * 4 | * Cross-platform serial port implementation 5 | * 6 | * Created on: 2014 7 | * Author: Nikolay Khabarov 8 | * License: You can do whatever you want. Author doesn't provide warranty of any kind. 9 | * 10 | */ 11 | 12 | #ifndef SERIALPORT_H 13 | #define SERIALPORT_H 14 | #include 15 | 16 | #if ( defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) ) 17 | #define COMWINDOWS 18 | typedef void *HANDLE; 19 | #define COM HANDLE 20 | typedef unsigned long DWORD; 21 | typedef void *PVOID,*LPVOID; 22 | #else 23 | #define COMPOSIX 24 | #define COM int 25 | #endif 26 | 27 | #define ERROR_WRITE_STRING (char*)"ERROR ---> CAN'T WRITE TO PORT\n" 28 | #define ERROR_READ_STRING (char*)"ERROR ---> CAN'T READ FROM PORT\n" 29 | 30 | class SerialPort 31 | { 32 | public: 33 | static SerialPort *open(const char *port); 34 | virtual ~SerialPort(); 35 | void send(const char *text); 36 | void send(char c); 37 | void send(const void *data, unsigned int len); 38 | bool waitAnswer(unsigned int len, unsigned int timeOutMs); 39 | void waitTransmitionEnd(unsigned int timeOutMs); 40 | bool isReadError(); 41 | static const char *findNextPort(bool finish); 42 | void setRts(bool val); 43 | void setDtr(bool val); 44 | void sleep(unsigned int ms); 45 | const char *getName(); 46 | private: 47 | #ifdef COMWINDOWS 48 | static DWORD ThreadProc (LPVOID lpdwThreadParam); 49 | #else 50 | static void * thread_start(void *arg); 51 | #endif 52 | SerialPort(COM comport, const char *name); 53 | unsigned int write_native(const void *data, unsigned int len); 54 | bool read_native(const void *data, unsigned int len, unsigned int *rb); 55 | static unsigned int getTick(); 56 | COM get_com(); 57 | COM mCom; 58 | bool mTreadFlag; 59 | bool mReadFlag; 60 | void *mThread; 61 | unsigned int mBytesRecivedSinceLastSend; 62 | bool mReadError; 63 | unsigned int mLastReceived; 64 | char mName[256]; 65 | }; 66 | 67 | extern void SerialPortRecieved(SerialPort *port, const char *text, unsigned int len); 68 | extern void SerialPortError(SerialPort *port, const char *text); 69 | 70 | #endif // SERIALPORT_H 71 | -------------------------------------------------------------------------------- /firmware-src/sources/commands/hmc5883l_cmd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief HMC5883L command handlers. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "commands/hmc5883l_cmd.h" 8 | #include "commands/i2c_cmd.h" 9 | #include "devices/hmc5883l.h" 10 | #include "DH/i2c.h" 11 | #include "DH/adc.h" 12 | #include "snprintf.h" 13 | 14 | #include "dhcommand_parser.h" 15 | #include 16 | 17 | #if defined(DH_COMMANDS_HMC5883L) && defined(DH_DEVICE_HMC5883L) 18 | 19 | /* 20 | * dh_handle_devices_hmc5883l_read() implementation. 21 | */ 22 | void ICACHE_FLASH_ATTR dh_handle_devices_hmc5883l_read(COMMAND_RESULT *cmd_res, const char *command, 23 | const char *params, unsigned int params_len) 24 | { 25 | gpio_command_params info; 26 | ALLOWED_FIELDS fields = 0; 27 | if (params_len) { 28 | const char *err_msg = parse_params_pins_set(params, params_len, 29 | &info, DH_ADC_SUITABLE_PINS, 0, 30 | AF_SDA | AF_SCL | AF_ADDRESS, &fields); 31 | if (err_msg != 0) { 32 | dh_command_fail(cmd_res, err_msg); 33 | return; // FAILED 34 | } 35 | if (fields & AF_ADDRESS) 36 | hmc5883l_set_address(info.address); 37 | } 38 | 39 | fields |= AF_ADDRESS; 40 | if (dh_i2c_init_helper(cmd_res, fields, &info)) 41 | return; // FAILED 42 | 43 | HMC5883L_XYZ compass; 44 | const int status = hmc5883l_read(DH_I2C_NO_PIN, DH_I2C_NO_PIN, &compass); 45 | const char *err_msg = dh_i2c_error_string(status); 46 | if (err_msg != 0) { 47 | dh_command_fail(cmd_res, err_msg); 48 | return; // FAILED 49 | } 50 | 51 | // TODO: support for NaN in snprintf! 52 | char floatbufx[10] = "NaN"; 53 | char floatbufy[10] = "NaN"; 54 | char floatbufz[10] = "NaN"; 55 | if (compass.X != HMC5883l_OVERFLOWED) 56 | snprintf(floatbufx, sizeof(floatbufx), "%f", compass.X); 57 | if (compass.Y != HMC5883l_OVERFLOWED) 58 | snprintf(floatbufy, sizeof(floatbufy), "%f", compass.Y); 59 | if (compass.Z != HMC5883l_OVERFLOWED) 60 | snprintf(floatbufz, sizeof(floatbufz), "%f", compass.Z); 61 | cmd_res->callback(cmd_res->data, DHSTATUS_OK, RDT_FORMAT_JSON, 62 | "{\"magnetometer\":{\"X\":%s, \"Y\":%s, \"Z\":%s}}", 63 | floatbufx, floatbufy, floatbufz); 64 | } 65 | 66 | #endif /* DH_COMMANDS_HMC5883L && DH_DEVICE_HMC5883L */ 67 | -------------------------------------------------------------------------------- /sdk/include/pwm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ESPRESSIF MIT License 3 | * 4 | * Copyright (c) 2016 5 | * 6 | * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, 7 | * it is free of charge, to any person obtaining a copy of this software and associated 8 | * documentation files (the "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | * and/or sell copies of the Software, and to permit persons to whom the Software is furnished 11 | * to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all copies or 14 | * substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | * 23 | */ 24 | 25 | #ifndef __PWM_H__ 26 | #define __PWM_H__ 27 | 28 | /*pwm.h: function and macro definition of PWM API , driver level */ 29 | /*user_light.h: user interface for light API, user level*/ 30 | /*user_light_adj: API for color changing and lighting effects, user level*/ 31 | 32 | 33 | /*NOTE!! : DO NOT CHANGE THIS FILE*/ 34 | 35 | /*SUPPORT UP TO 8 PWM CHANNEL*/ 36 | #define PWM_CHANNEL_NUM_MAX 8 37 | 38 | struct pwm_param { 39 | uint32 period; 40 | uint32 freq; 41 | uint32 duty[PWM_CHANNEL_NUM_MAX]; //PWM_CHANNEL<=8 42 | }; 43 | 44 | 45 | /* pwm_init should be called only once, for now */ 46 | void pwm_init(uint32 period, uint32 *duty,uint32 pwm_channel_num,uint32 (*pin_info_list)[3]); 47 | void pwm_start(void); 48 | 49 | void pwm_set_duty(uint32 duty, uint8 channel); 50 | uint32 pwm_get_duty(uint8 channel); 51 | void pwm_set_period(uint32 period); 52 | uint32 pwm_get_period(void); 53 | 54 | uint32 get_pwm_version(void); 55 | void set_pwm_debug_en(uint8 print_en); 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /firmware-src/sources/DH/i2c.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Software I2C implementation for ESP8266 firmware. 4 | * @copyright 2015 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #ifndef _DH_I2C_H_ 8 | #define _DH_I2C_H_ 9 | 10 | #include 11 | 12 | /** 13 | * @brief I2C functions return status. 14 | */ 15 | typedef enum { 16 | DH_I2C_OK, ///< @brief Success. 17 | DH_I2C_NOACK, ///< @brief ACK response was not received on bus. 18 | DH_I2C_WRONG_PARAMETERS, ///< @brief Wrong parameters. 19 | DH_I2C_BUS_BUSY, ///< @brief Module wasn't able to set desired level on bus during specified timeout. 20 | DH_I2C_DEVICE_ERROR ///< @brief Error in device. 21 | } DH_I2C_Status; 22 | 23 | 24 | /** 25 | * @brief Do not initialize pin indicator. 26 | */ 27 | #define DH_I2C_NO_PIN (-1) 28 | 29 | 30 | /** 31 | * @brief Get error message based on status. 32 | * @return NULL if status is OK, error message otherwise. 33 | */ 34 | const char* dh_i2c_error_string(int status); 35 | 36 | 37 | /** 38 | * @brief Prepare pins for usage with I2C. 39 | * @param[in] sda_pin Pin number for SDA line. 40 | * @param[in] scl_pin Pin number for SCL line. 41 | * @return Status value, one of DH_I2C_Status value. 42 | */ 43 | int dh_i2c_init(unsigned int sda_pin, 44 | unsigned int scl_pin); 45 | 46 | 47 | /** 48 | * @brief Prepare last successfully prepared pins for usage with I2C. 49 | */ 50 | void dh_i2c_reinit(void); 51 | 52 | 53 | /** 54 | * @brief Write data to I2C bus. 55 | * @param[in] address Device address. 56 | * @param[in] buf Buffer to write. 57 | * @param[in] len Buffer length in bytes. 58 | * @param[in] send_stop Send STOP after writing if this parameter has non zero value, do not send if parameter is zero. 59 | * @return Status value, one of DH_I2C_Status value. 60 | */ 61 | int dh_i2c_write(unsigned int address, 62 | const void *buf, size_t len, 63 | int send_stop); 64 | 65 | 66 | /** 67 | * @brief Read data from I2C bus. 68 | * @param[in] address Device address. 69 | * @param[out] buf Buffer to read data to. 70 | * @param[in] len Buffer length in bytes. 71 | * @return Status value, one of DH_I2C_Status value. 72 | */ 73 | int dh_i2c_read(unsigned int address, 74 | void *buf, size_t len); 75 | 76 | #endif /* _DH_I2C_H_ */ 77 | -------------------------------------------------------------------------------- /firmware-src/sources/devices/hmc5883l.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * @brief Simple communication with HMC5883L compass sensor. 4 | * @copyright 2016 [DeviceHive](http://devicehive.com) 5 | * @author Nikolay Khabarov 6 | */ 7 | #include "devices/hmc5883l.h" 8 | #include "DH/i2c.h" 9 | #include "dhdebug.h" 10 | #include "dhutils.h" 11 | 12 | #include 13 | 14 | #if defined(DH_DEVICE_HMC5883L) 15 | 16 | /** @brief Default sensor i2c address*/ 17 | #define HMC5883L_DEFAULT_ADDRESS 0x3C 18 | 19 | #define HMC5883l_OVERFLOWED_RAW -4096 20 | 21 | // module variables 22 | static int mAddress = HMC5883L_DEFAULT_ADDRESS; 23 | 24 | 25 | /* 26 | * hmc5883l_read() implementation. 27 | */ 28 | int ICACHE_FLASH_ATTR hmc5883l_read(int sda, int scl, HMC5883L_XYZ *compass) 29 | { 30 | int status; 31 | if (sda != DH_I2C_NO_PIN && scl != DH_I2C_NO_PIN) { 32 | if ((status = dh_i2c_init(sda, scl)) != DH_I2C_OK) { 33 | dhdebug("hmc5883l: failed to set up pins"); 34 | return status; 35 | } 36 | } 37 | 38 | uint8_t buf[7]; 39 | buf[0] = 0x02; // mode register 40 | buf[1] = BIT(0); // single run mode 41 | if ((status = dh_i2c_write(mAddress, buf, 2, 1)) != DH_I2C_OK) { 42 | dhdebug("hmc5883l: failed to power up"); 43 | return status; 44 | } 45 | 46 | delay_ms(80); 47 | 48 | buf[0] = 0x03; // get data 49 | if ((status = dh_i2c_write(mAddress, buf, 1, 0)) != DH_I2C_OK) { 50 | dhdebug("hmc5883l: failed to set read register"); 51 | return status; 52 | } 53 | if ((status = dh_i2c_read(mAddress, buf, 7)) != DH_I2C_OK) { 54 | dhdebug("hmc5883l: failed to read"); 55 | return status; 56 | } 57 | 58 | const int x = signedInt16be((const char*)buf, 0); 59 | const int y = signedInt16be((const char*)buf, 4); 60 | const int z = signedInt16be((const char*)buf, 2); 61 | // note: 2048 is a range of possible values, 1.3f is a default sensor field range 62 | compass->X = (x == HMC5883l_OVERFLOWED_RAW) ? HMC5883l_OVERFLOWED : (x * 1.3f / 2048.0f); 63 | compass->Y = (y == HMC5883l_OVERFLOWED_RAW) ? HMC5883l_OVERFLOWED : (y * 1.3f / 2048.0f); 64 | compass->Z = (z == HMC5883l_OVERFLOWED_RAW) ? HMC5883l_OVERFLOWED : (z * 1.3f / 2048.0f); 65 | 66 | return DH_I2C_OK; 67 | } 68 | 69 | 70 | /* 71 | * hmc5883l_set_address() implementation. 72 | */ 73 | void ICACHE_FLASH_ATTR hmc5883l_set_address(int address) 74 | { 75 | mAddress = address; 76 | } 77 | 78 | #endif /* DH_DEVICE_HMC5883L */ 79 | --------------------------------------------------------------------------------