[SOLVED] Si7021 Blynk ESP8266 12E

Hi iam trying si7021 sensor. But something is wrong.? In Blynk is temp: 128,8 and hum: 118,9.
Library : https://forum.pjrc.com/threads/29237-Si7021-Humidity-and-Temperature-Sensor-Library?highlight=si7021

Thanks.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <Si7021.h>

SI7021 si7021;





#define I2C_SCL 12      // Barometric Pressure Sensor (BMP085)
#define I2C_SDA 13



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "75a3e91b35eea63e9c1";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{

  
  Wire.begin(I2C_SDA, I2C_SCL);
  delay(10);
si7021.begin();
  while(!Serial);
si7021.setHumidityRes(12); // Humidity = 12-bit / Temperature = 14-bit - See more at: http://www.esp8266.com/viewtopic.php?f=13&t=9563#sthash.wOtGMLB0.dpuf
  
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "", ""); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);

}

void sendUptime()
{



float humin = si7021.readHumidity();
float tempin = si7021.readTempPrev();


  Blynk.virtualWrite(3, tempin); // virtual pin 
  Blynk.virtualWrite(4, humin);


  
}

void loop()
{
  Blynk.run();
  timer.run();
}
1 Like

what do your serial prints say?

EDIT YOUR SSID & PASSWORD

How many Gpio 12 does your board have? :wink:
You are using this pin twice

1 Like

Say:
ets Jan 8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v3de0c112
~ld

Yes but DHT i no used.

remove the define then.

OK i removed. But still: ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v3de0c112
~ld

Maybye bad library…

Thanks.

Try to avoid pins 0, 2 and 15 if at all possible.

The link you provided suggests it hasn’t been ported to ESP.

Ok. Here is example sketch from library:

#include <Wire.h>
#include <Si7021.h>

SI7021 si7021;

// BASIC DEMO
// ----------
// Print humidity and temperature to the serial monitor while toggling the heater on and off
// periodically.

// NOTE ON RESOLUTION:
// -------------------
// Changing the resolution of the temperature will also change the humidity resolution.
// Likewise, changing the resolution of the humidity will also change temperature resolution.
// Two functions are provided to change the resolution, both functions are similar except they have
// different masks to the registers, so setTempRes(14) will change the temp resolution to 14-bit
// but humidity will also be set to 12-bit. Setting the humidity resolution to 8-bit setHumidityRes(8)
// will change the temperature resolution to 12-bit.
// Resolution Table:
//                    14-bit Temp <-> 12-bit Humidity
//                    13-bit Temp <-> 10-bit Humidity
//                    12-bit Temp <->  8-bit Humidity
//                    11-bit Temp <-> 11-bit Humidity

// NOTE ON HEATER:
// ---------------
// The HTRE bit in the user register is what turns the heater on and off. This register
// is stored on non-volatile memory to it will keep its state when power is removed. If
// the heater is enabled and power is removed before the program had chance to turn it off
// then the heater will remain enabled the next time it is powered on. The heater has to
// be explicitly turned off. In the begin() function for this library, a reset procedure
// will take place and reset the user register to default, so the heater will be turned off
// and the resolution will also be set to default, 14-bit temperature and 12-bit humidity.
// Keep this in mind if testing and swapping sensors/libraries.

void setup()
{
Serial.begin(115200);
si7021.begin(); // Runs : Wire.begin() + reset()
  while(!Serial); // Wait for serial monitor to open
Serial.println("BASIC DEMO");
Serial.println("------------------------------------------");
si7021.setHumidityRes(12); // Humidity = 12-bit / Temperature = 14-bit
}

void loop()
{
static uint8_t heaterOnOff; // Create static variable for heater control
si7021.setHeater(heaterOnOff); // Turn heater on or off
Serial.print("Heater Status = ");
Serial.println(si7021.getHeater() ? "ON" : "OFF");

  for(int i = (heaterOnOff ? 20 : 30); i>0; i--)
  {
  Serial.print("Humidity : ");
  Serial.print(si7021.readHumidity()); // Read humidity and print to serial monitor
  Serial.print(" %\t");
  Serial.print("Temp : ");
  Serial.print(si7021.readTemp()); // Read temperature and print to serial monitor
  Serial.print(" C\t");
  Serial.println(i); // Print count down for heater change
  delay(500);
  }

heaterOnOff = !heaterOnOff; // Toggle heater on/off variable
}

It’s really the library that needs to be checked and ported.

Ok im trying SI7021 master library now.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <SI7021.h>

SI7021 sensor;





#define I2C_SCL 12  //D6    // Barometric Pressure Sensor (BMP085)
#define I2C_SDA 13//D7



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{

  
  Wire.begin(I2C_SDA, I2C_SCL);
  delay(10);
SI7021.begin();
  while(!Serial);
SI7021.setHumidityRes(12); // Humidity = 12-bit / Temperature = 14-bit - See more at: http://www.esp8266.com/viewtopic.php?f=13&t=9563#sthash.wOtGMLB0.dpuf
  
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "", ""); //insert here your SSID and password
 
 

}

void sendUptime()
{
  
 
  


float humin = SI7021.readHumidity();
float tempin = SI7021.readTempPrev();


  Blynk.virtualWrite(1, tempin); // virtual pin 
  Blynk.virtualWrite(2, humin);


  
}

void loop()
{
  Blynk.run();
  timer.run();
}

but write wrong:


si7021__blynk.ino: In function 'void setup()':
si7021__blynk:32: error: expected unqualified-id before '.' token
si7021__blynk:34: error: expected unqualified-id before '.' token
si7021__blynk.ino: In function 'void sendUptime()':
si7021__blynk:51: error: expected primary-expression before '.' token
si7021__blynk:52: error: expected primary-expression before '.' token
Multiple libraries were found for "BlynkSimpleEsp8266.h"
 Used: C:\Users\boss\Documents\Arduino\libraries\blynk-library-master
 Not used: C:\Users\boss\Documents\Arduino\libraries\Blynk
expected unqualified-id before '.' token

Library example -

#include <Wire.h>
#include <SI7021.h>


SI7021 sensor;
int led1 = 3;
int led2 = 4;

void setup() {
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    sensor.begin();
}


void loop() {

    // temperature is an integer in hundredths
    int temperature = sensor.getCelsiusHundredths();
    temperature = temperature / 100;
    for (int i = 0; i < temperature; i++) {
        pulse(led1); 
    }
    
    delay(5000);
    
    // humidity is an integer representing percent
    int humidity = sensor.getHumidityPercent();
    for (int i = 0; i < humidity; i++) {
        pulse(led2); 
    }
    
    delay(5000);
    
    // this driver should work for SI7020 and SI7021, this returns 20 or 21
    int deviceid = sensor.getDeviceId();
    for (int i = 0; i < deviceid; i++) {
        pulse(led1); 
    }
    delay(5000);

    // enable internal heater for testing
    sensor.setHeater(true);
    delay(20000);
    sensor.setHeater(false);
    
    // see if heater changed temperature
    temperature = sensor.getCelsiusHundredths();
    temperature = temperature / 100;
    for (int i = 0; i < temperature; i++) {
        pulse(led2); 
    }
    
    //cool down
    delay(20000);

    // get humidity and temperature in one shot, saves power because sensor takes temperature when doing humidity anyway
    si7021_env data = sensor.getHumidityAndTemperature();
    for (int i = 0; i < data.celsiusHundredths/100; i++) {
        pulse(led1); 
    }
    for (int i = 0; i < data.humidityBasisPoints/100; i++) {
        pulse(led2); 
    }
    delay(5000);
}

void pulse(int pin) {
   // software pwm, flash light for ~1 second with short duty cycle
   for (int i = 0; i < 20; i++) {
       digitalWrite(pin, HIGH);
       delay(1);
       digitalWrite(pin,LOW);
       delay(9);
   }
   digitalWrite(pin,LOW);
   delay(300);
}

Or i was try Adafruit Library: example sketch:

#include "Adafruit_Si7021.h"

Adafruit_Si7021 sensor = Adafruit_Si7021();

void setup() {
  Serial.begin(115200);
  Serial.println("Si7021 test");
  sensor.begin();
}

void loop() {
  Serial.print("Humidity:    "); Serial.print(sensor.readHumidity(), 2);
  Serial.print("\tTemperature: "); Serial.println(sensor.readTemperature(), 2);
  delay(100);
}

my sketch:

define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include "Adafruit_Si7021.h"

Adafruit_Si7021 sensor = Adafruit_Si7021();





#define I2C_SCL 12  //D6    // Barometric Pressure Sensor (BMP085)
#define I2C_SDA 13//D7



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{

  
  Wire.begin(I2C_SDA, I2C_SCL);
  sensor.begin(); 
  
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "", ""); //insert here your SSID and password
 
 

}

void sendUptime()
{
  
 
  


float humin = Adafruit_Si7021.readHumidity();
float tempin = Adafruit_Si7021.readTempPrev();


  Blynk.virtualWrite(1, tempin); // virtual pin 
  Blynk.virtualWrite(2, humin);


  
}

void loop()
{
  Blynk.run();
  timer.run();
}

but write mistake:

si7021__blynk.ino: In function 'void sendUptime()':
si7021__blynk:47: error: expected primary-expression before '.' token
si7021__blynk:48: error: expected primary-expression before '.' token
Multiple libraries were found for "BlynkSimpleEsp8266.h"
 Used: C:\Users\boss\Documents\Arduino\libraries\blynk-library-master
 Not used: C:\Users\boss\Documents\Arduino\libraries\Blynk
expected primary-expression before '.' token

#include <Adafruit_Si7021.h>

Not

#include "Adafruit_Si7021.h"

I was try it:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <Adafruit_Si7021.h>

Adafruit_Si7021 sensor = Adafruit_Si7021();





#define I2C_SCL 12  //D6    // Barometric Pressure Sensor (BMP085)
#define I2C_SDA 13//D7



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

float tempin, humin;

void setup()
{

  
  Wire.begin(I2C_SDA, I2C_SCL);
  sensor.begin(); 
  
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "", ""); //insert here your SSID and password
 
 

}

void sendUptime()
{
  
 
  


float humin = Adafruit_Si7021.readHumidity();
float tempin = Adafruit_Si7021.readTempPrev();


  Blynk.virtualWrite(1, tempin); // virtual pin 
  Blynk.virtualWrite(2, humin);


  
}

void loop()
{
  Blynk.run();
  timer.run();
}

But now is wrong:

si7021__blynk.ino: In function 'void sendUptime()':
si7021__blynk:49: error: expected primary-expression before '.' token
si7021__blynk:50: error: expected primary-expression before '.' token
Multiple libraries were found for "BlynkSimpleEsp8266.h"
 Used: C:\Users\boss\Documents\Arduino\libraries\blynk-library-master
 Not used: C:\Users\boss\Documents\Arduino\libraries\Blynk
expected primary-expression before '.' token

And SI7021-master library :

#include "Arduino.h"
#include "SI7021.h"
#include <Wire.h>

#define I2C_ADDR 0x40

// I2C commands
#define RH_READ             0xE5 
#define TEMP_READ           0xE3 
#define POST_RH_TEMP_READ   0xE0 
#define RESET               0xFE 
#define USER1_READ          0xE7 
#define USER1_WRITE         0xE6 

// compound commands
byte SERIAL1_READ[]      ={ 0xFA, 0x0F };
byte SERIAL2_READ[]      ={ 0xFC, 0xC9 };

bool _si_exists = false;

SI7021::SI7021() {
}

#if defined(ARDUINO_ARCH_ESP8266)
bool SI7021::begin(int SDA, int SCL) {
    Wire.begin(SDA,SCL);
#else
bool SI7021::begin() {
    Wire.begin();
#endif
    Wire.beginTransmission(I2C_ADDR);
    if (Wire.endTransmission() == 0) {
        _si_exists = true;
    }
    return _si_exists;
}

bool SI7021::sensorExists() {
    return _si_exists;
}

int SI7021::getFahrenheitHundredths() {
    int c = getCelsiusHundredths();
    return (1.8 * c) + 3200;
}

int SI7021::getCelsiusHundredths() {
    byte tempbytes[2];
    _command(TEMP_READ, tempbytes);
    long tempraw = (long)tempbytes[0] << 8 | tempbytes[1];
    return ((17572 * tempraw) >> 16) - 4685;
}

int SI7021::_getCelsiusPostHumidity() {
    byte tempbytes[2];
    _command(POST_RH_TEMP_READ, tempbytes);
    long tempraw = (long)tempbytes[0] << 8 | tempbytes[1];
    return ((17572 * tempraw) >> 16) - 4685;
}


unsigned int SI7021::getHumidityPercent() {
    byte humbytes[2];
    _command(RH_READ, humbytes);
    long humraw = (long)humbytes[0] << 8 | humbytes[1];
    return ((125 * humraw) >> 16) - 6;
}

unsigned int SI7021::getHumidityBasisPoints() {
    byte humbytes[2];
    _command(RH_READ, humbytes);
    long humraw = (long)humbytes[0] << 8 | humbytes[1];
    return ((12500 * humraw) >> 16) - 600;
}

void SI7021::_command(byte cmd, byte * buf ) {
    _writeReg(&cmd, sizeof cmd);
#if defined(ARDUINO_ARCH_ESP8266)
    delay(25);
#endif
    _readReg(buf, 2);
}

void SI7021::_writeReg(byte * reg, int reglen) {
    Wire.beginTransmission(I2C_ADDR);
    for(int i = 0; i < reglen; i++) {
        reg += i;
        Wire.write(*reg); 
    }
    Wire.endTransmission();
}

int SI7021::_readReg(byte * reg, int reglen) {
    Wire.requestFrom(I2C_ADDR, reglen);
    while(Wire.available() < reglen) {
    }
    for(int i = 0; i < reglen; i++) { 
        reg[i] = Wire.read(); 
    }
    return 1;
}

int SI7021::getSerialBytes(byte * buf) {
  byte serial[8];
  _writeReg(SERIAL1_READ, sizeof SERIAL1_READ);
  _readReg(serial, 8);
  
  //Page23 - https://www.silabs.com/Support%20Documents%2FTechnicalDocs%2FSi7021-A20.pdf
  buf[0] = serial[0]; //SNA_3
  buf[1] = serial[2]; //SNA_2
  buf[2] = serial[4]; //SNA_1
  buf[3] = serial[6]; //SNA_0

  _writeReg(SERIAL2_READ, sizeof SERIAL2_READ);
  _readReg(serial, 6);
  buf[4] = serial[0]; //SNB_3 - device ID byte
  buf[5] = serial[1]; //SNB_2
  buf[6] = serial[3]; //SNB_1
  buf[7] = serial[4]; //SNB_0
  return 1;
}

int SI7021::getDeviceId() {
  //0x0D=13=Si7013
  //0x14=20=Si7020
  //0x15=21=Si7021
  byte serial[8];
  getSerialBytes(serial);
  int id = serial[4];
  return id;
}

void SI7021::setHeater(bool on) {
    byte userbyte;
    if (on) {
        userbyte = 0x3E;
    } else {
        userbyte = 0x3A;
    }
    byte userwrite[] = {USER1_WRITE, userbyte};
    _writeReg(userwrite, sizeof userwrite);
}

// get humidity, then get temperature reading from humidity measurement
struct si7021_env SI7021::getHumidityAndTemperature() {
    si7021_env ret = {0, 0, 0};
    ret.humidityBasisPoints      = getHumidityBasisPoints();
    ret.celsiusHundredths        = _getCelsiusPostHumidity();
    ret.fahrenheitHundredths     = (1.8 * ret.celsiusHundredths) + 3200;
    return ret;
}

// get temperature (C only) and RH Percent
struct si7021_thc SI7021::getTempAndRH()
{
    si7021_thc ret;
    
    ret.humidityPercent   = getHumidityPercent();
    ret.celsiusHundredths = _getCelsiusPostHumidity();
    return ret;

}

Multiple libraries were found for "BlynkSimpleEsp8266.h" Used: C:\Users\boss\Documents\Arduino\libraries\blynk-library-master Not used: C:\Users\boss\Documents\Arduino\libraries\Blynk

How about you delete ALL blynk libary folders and re-install the library so you only have 1 copy.

The structure should look like this, along with your saved sketches:

sketchbook/libraries/Blynk
sketchbook/libraries/BlynkESP8266_Lib
...
sketchbook/tools/BlynkUpdater
sketchbook/tools/BlynkUsbScript
...

So i was update blynk. But now:

float tempin = Adafruit_Si7021.readTempPrev();

tt.ino: In function 'void sendUptime()':
tt:48: error: expected primary-expression before '.' token
tt:49: error: expected primary-expression before '.' token
expected primary-expression before '.' token


Hi. I was try si7021 sensors connect to arduino if is sensor good. And is sensor is ok. But on ESP with same library no working…

#include <Wire.h>
#include <Artekit_SI7021.h>

Artekit_SI7021 si7021;

static void sensorRead()
{
  float temperature;
  float humidity;
 
  /* Read relative humidity and temperature */
  humidity = si7021.ReadHumidity();
  
  /* By setting true in the next function, we read the
   * temperature from the previous RH measurement.
   * Otherwise it will perform a new temperature measurement.
   */
  temperature = si7021.ReadTemperature(true);
  
  Serial.print("RH: ");
  Serial.print(humidity);
  Serial.print("%\tTemperature: ");
  Serial.println(temperature);
}

void setup()
{
  /* Start I2C */
  Wire.begin();
  /* Start serial */
  Serial.begin(9600);
}

void loop()
{
  static int counter = 0;
  static bool heater = false;
  
  /* Read sensor data each second */
  sensorRead();
   
  /* Turn on and off the heater every 30 seconds,
   * just to see the change in the temperature readings.
   */
  if (heater == false)
  {
    counter++;
    if (counter == 30)
    {
      Serial.println("Heater ON");
      si7021.EnableHeater();
      heater = true;  
    }
  } else {
    counter--;
    if (counter == 0)
    {
      Serial.println("Heater OFF");
      si7021.DisableHeater();
      heater = false;
    }    
  }
  
  delay(1000);
}

}

This may help: reading SI7021 (humidity/temp) with ESP-12 - Everything ESP8266

Thanks. But all examples are without Blynk on the Internet…
And i dont know what i have try…
Thanks.