Repo for ESP32 Weather Station Development
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.2KB

  1. //#include "freertos/FreeRTOS.h"
  2. //#include "esp_wifi.h"
  3. //#include "esp_system.h"
  4. //#include "esp_event.h"
  5. //#include "esp_event_loop.h"
  6. //#include "nvs_flash.h"
  7. //#include "driver/gpio.h"
  8. #include "bme280.h"
  9. #include "bme280_defs.h"
  10. //#include "ssd1366.h"
  11. #include "driver/i2c.h"
  12. #include <u8g2.h>
  13. #include "u8g2_esp32_hal.h"
  14. #include <esp_log.h>
  15. void i2c_setup()
  16. {
  17. printf("Setting up I²C driver... ");
  18. //i2c_driver_install(0, I2C_MODE_MASTER, 0, 0, 0);
  19. i2c_config_t config;
  20. config.mode = I2C_MODE_MASTER;
  21. config.sda_io_num = 18;
  22. config.sda_pullup_en = GPIO_PULLUP_ENABLE;
  23. config.scl_io_num = 19;
  24. config.scl_pullup_en = GPIO_PULLUP_ENABLE;
  25. config.master.clk_speed = 100000;
  26. i2c_param_config(I2C_NUM_0, &config);
  27. printf("Set driver parameters... ");
  28. esp_err_t err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
  29. if (err == ESP_OK)
  30. printf("Driver installed!\n");
  31. else if (err == ESP_ERR_INVALID_ARG)
  32. printf("Driver install failed, invalid arguments!\n");
  33. else
  34. printf("Driver install failed!\n");
  35. }
  36. void i2c_detect()
  37. {
  38. printf("Scanning I²C bus:\n");
  39. uint8_t address;
  40. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  41. for (int i = 0; i < 128; i += 16) {
  42. printf("%02x: ", i);
  43. for (int j = 0; j < 16; j++) {
  44. fflush(stdout);
  45. address = i + j;
  46. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  47. i2c_master_start(cmd);
  48. i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 0x1);
  49. i2c_master_stop(cmd);
  50. esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS);
  51. i2c_cmd_link_delete(cmd);
  52. if (ret == ESP_OK) {
  53. printf("%02x ", address);
  54. } else if (ret == ESP_ERR_TIMEOUT) {
  55. printf("UU ");
  56. } else {
  57. printf("-- ");
  58. }
  59. }
  60. printf("\r\n");
  61. }
  62. }
  63. int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) {
  64. //printf("Reading from bus: dev_id=%x, reg_addr=%x, data=%p, length=%u\n", dev_id, reg_addr, data, len);
  65. /*i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  66. i2c_master_start(cmd);
  67. if (data_addr != -1) {
  68. i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
  69. i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
  70. i2c_master_start(cmd);
  71. }
  72. i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
  73. if (len > 1) {
  74. i2c_master_read(cmd, data, len - 1, ACK_VAL);
  75. }
  76. i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
  77. i2c_master_stop(cmd);
  78. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  79. i2c_cmd_link_delete(cmd);*/
  80. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  81. i2c_master_start(cmd);
  82. i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_WRITE, 1);
  83. i2c_master_write_byte(cmd, reg_addr, 1);
  84. i2c_master_start(cmd);
  85. i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_READ, 1);
  86. if (len > 1) {
  87. i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK);
  88. }
  89. i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK);
  90. i2c_master_stop(cmd);
  91. i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS);
  92. i2c_cmd_link_delete(cmd);
  93. return 0;
  94. }
  95. int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) {
  96. //printf("Writing to bus: dev_id=%x, reg_addr=%x, data=%p, length=%u\n", dev_id, reg_addr, data, len);
  97. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  98. i2c_master_start(cmd);
  99. i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_WRITE, 1);
  100. i2c_master_write_byte(cmd, reg_addr, 1);
  101. i2c_master_write(cmd, data, len, 1);
  102. i2c_master_stop(cmd);
  103. i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS);
  104. i2c_cmd_link_delete(cmd);
  105. return 0;
  106. }
  107. void i2c_delay(uint32_t period) {
  108. vTaskDelay(period / portTICK_PERIOD_MS);
  109. }
  110. void i2c_shutdown()
  111. {
  112. printf("Shutting down I²C bus... ");
  113. esp_err_t err = i2c_driver_delete(I2C_NUM_0);
  114. if (err == ESP_ERR_INVALID_ARG)
  115. printf("Failed, invalid arguments!\n");
  116. else
  117. printf("Success!\n");
  118. }
  119. void app_main(void)
  120. {
  121. i2c_setup();
  122. struct bme280_dev dev;
  123. int8_t rslt = BME280_OK;
  124. dev.dev_id = 0x76;
  125. dev.intf = BME280_I2C_INTF;
  126. dev.read = i2c_read;
  127. dev.write = i2c_write;
  128. dev.delay_ms = i2c_delay;
  129. rslt = bme280_init(&dev);
  130. uint8_t settings_sel;
  131. uint32_t req_delay;
  132. struct bme280_data comp_data;
  133. /* Recommended mode of operation: Indoor navigation */
  134. dev.settings.osr_h = BME280_OVERSAMPLING_1X;
  135. dev.settings.osr_p = BME280_OVERSAMPLING_16X;
  136. dev.settings.osr_t = BME280_OVERSAMPLING_2X;
  137. dev.settings.filter = BME280_FILTER_COEFF_16;
  138. settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
  139. rslt = bme280_set_sensor_settings(settings_sel, &dev);
  140. /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled
  141. * and the oversampling configuration. */
  142. req_delay = bme280_cal_meas_delay(&(dev.settings));
  143. printf("Temperature, Pressure, Humidity\r\n");
  144. /* Continuously stream sensor data */
  145. while (1) {
  146. rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &dev);
  147. /* Wait for the measurement to complete and print data @25Hz */
  148. dev.delay_ms(req_delay);
  149. rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &dev);
  150. printf("%i °c, %i hPa, %i %%\r\n", comp_data.temperature/100, comp_data.pressure/100, comp_data.humidity/1024);
  151. vTaskDelay(50 / portTICK_PERIOD_MS);
  152. }
  153. i2c_shutdown();
  154. u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
  155. u8g2_esp32_hal.sda = 18;
  156. u8g2_esp32_hal.scl = 19;
  157. u8g2_esp32_hal_init(u8g2_esp32_hal);
  158. u8g2_t u8g2;
  159. u8g2_Setup_ssd1306_i2c_128x64_vcomh0_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb,
  160. u8g2_esp32_gpio_and_delay_cb);
  161. //u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
  162. u8x8_SetI2CAddress(&u8g2.u8x8,0x3C << 1);
  163. u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
  164. u8g2_SetPowerSave(&u8g2, 0); // wake up display
  165. u8g2_SetContrast(&u8g2, 50);
  166. u8g2_uint_t r = 10;
  167. u8g2_uint_t x = r+1;
  168. u8g2_uint_t y = r+1;
  169. int8_t dx = 2;
  170. int8_t dy = 5;
  171. while (1) {
  172. u8g2_ClearBuffer(&u8g2);
  173. u8g2_DrawDisc(&u8g2, x, y, r, U8G2_DRAW_ALL);
  174. u8g2_DrawFrame(&u8g2, 0, 0, 128, 64);
  175. u8g2_SendBuffer(&u8g2);
  176. x = x + dx;
  177. y = y + dy;
  178. if (x <= 0+r+1 || x >= 127-r-1) {
  179. dx = dx * -1;
  180. }
  181. if (y <= 0+r+1 || y >= 63-r-1) {
  182. dy = dy * -1;
  183. vTaskDelay(10 / portTICK_PERIOD_MS);
  184. }
  185. }
  186. }