The use of the ESP8266 in the world of IoT

User avatar
By berkutta
#42739 Hi,

Have a little Problem with the I2C Driver of Espressif. SDA works, but SCL stays always low. Tried with and without pullups on SDA/SCL.

My Code:
Code: Select all#ifdef __cplusplus
extern "C"
{
#endif
   
#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>
#include <user_interface.h>
#include <espmissingincludes.h>
#include <espconn.h>

#include <i2c_master.h>
//#include "include/driver/spi.h"
   
int user_init();

#ifdef __cplusplus
}
#endif

#ifdef ESP8266_GDBSTUB
#include <gdbstub.h>
#endif


static char s_ReplyFormat[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n<html><body><h1>Hello, World</h1>This page was generated by your ESP8266 device %d times.<br>Current Uptime: %ds<br>Currently no Temp & Humidity available!</body></html>";
static char s_Reply[sizeof(s_ReplyFormat) + 20];
static int s_RequestNumber;

static void httpdConnectCb(void *arg)
{
   struct espconn *pConn = (struct espconn*)arg;
   os_sprintf(s_Reply, s_ReplyFormat, ++s_RequestNumber, system_get_time() / 1000000);
   espconn_sent(pConn, (uint8_t *)s_Reply, strlen(s_Reply));
   
   espconn_disconnect(pConn);
}


static os_timer_t s_Timer;
static os_timer_t sensor_Timer;
int s_Tick = 0;

void TimerFunction(void *arg)
{
   s_Tick++;

   //Uncomment the line below to disable the software watchdog that will restart the ESP8266 system after it spends more than ~1 second stopped at a breakpoint.
   //system_soft_wdt_stop();
   
   if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT2)
   {
      gpio_output_set(0, BIT2, BIT2, 0);
   }
   else
   {
      gpio_output_set(BIT2, 0, BIT2, 0);
   }
}

void read_sensor(void *arg)
{
   char byte1, byte2, byte3;
   i2c_master_start();
   i2c_master_writeByte(0b10000000);
   i2c_master_writeByte(0xE3);
   i2c_master_stop();
   
   i2c_master_start();
   i2c_master_writeByte(0b10000000);
   byte1 = i2c_master_readByte();
   byte2 = i2c_master_readByte();
   byte3 = i2c_master_readByte();
   i2c_master_stop();
}

int user_init()
{
#ifdef ESP8266_GDBSTUB
   //gdbstub_init();
#endif
   gpio_init();

   
   struct ip_info info;   
   struct station_config cfg;
   //Set Wifi to Station (Client) mode
   wifi_set_opmode(STATION_MODE);
   //Get (empty) config
   wifi_station_get_config(&cfg);
   //Set Wi-Fi Credidentals
   strcpy((char *)cfg.ssid, "wifi");
   strcpy((char *)cfg.password, "pass");   
   //Save Wi-Fi config
   wifi_station_set_config(&cfg);
   //Connect with saved config
   wifi_station_connect();
   
   
   static struct espconn httpdConn;
   static esp_tcp httpdTcp;
   httpdConn.type = ESPCONN_TCP;
   httpdConn.state = ESPCONN_NONE;
   httpdTcp.local_port = 80;
   httpdConn.proto.tcp = &httpdTcp;

   espconn_regist_connectcb(&httpdConn, httpdConnectCb);
   espconn_accept(&httpdConn);   
   

   
   //PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
   //gpio_output_set(0, BIT2, BIT2, 0);
   //os_timer_setfn(&s_Timer, TimerFunction, NULL);
   //os_timer_arm(&s_Timer, 300, 1);

   i2c_master_init();   
   os_timer_setfn(&sensor_Timer, read_sensor, NULL);
   os_timer_arm(&sensor_Timer, 500, 1);
      
   return 0;
}


SDA - GPIO2
SCL - GPIO14

Config in i2c_master.h:
Code: Select all#define I2C_MASTER_SDA_MUX PERIPHS_IO_MUX_GPIO2_U
#define I2C_MASTER_SCL_MUX PERIPHS_IO_MUX_MTMS_U
#define I2C_MASTER_SDA_GPIO 2
#define I2C_MASTER_SCL_GPIO 14
#define I2C_MASTER_SDA_FUNC FUNC_GPIO2
#define I2C_MASTER_SCL_FUNC FUNC_GPIO14


Have I forgot something or is this a common issue?

Berkutta