Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Can not use read functions outside loop [ESP-8266] #4

Closed
IMAN4K opened this issue Jan 17, 2018 · 2 comments
Closed

Can not use read functions outside loop [ESP-8266] #4

IMAN4K opened this issue Jan 17, 2018 · 2 comments

Comments

@IMAN4K
Copy link

IMAN4K commented Jan 17, 2018

Hi.
I've recently test the library with the HTU21D & SI7021 to read the sensor data in some interval callback like this:

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <HTU21D.h>
#include <Ticker.h>

#define D5 14
#define D6 12
#define D3 0
#define D4 2

static Ticker timer;

static HTU21D htu21d;

static void read() {
	if (!htu21d.batteryStatus()) {
		Serial.printf("HTU21D Battery: Not OK! \n");
	}

	float htu_h = htu21d.readHumidity();
	float htu_t = htu21d.readTemperature();
	Serial.printf("HTU21D(T): %f, HTU21D(RH): %f \n", htu_t, htu_h);
}

void setup() {
	Serial.begin(115200);

	WiFi.mode(WIFI_OFF);

	while (!htu21d.begin(D5, D6)) {
		Serial.print('.');
		delay(1000);
	}

	if (htu21d.batteryStatus()) {
		Serial.printf("\n HTU21D Battery: OK\n");
	}

	timer.attach(2.0, &read);
}

void loop() {

}

But inside the timer callback, sensors always report battery status as false and the humidity and temperature as error (255 == HTU21D_ERROR).

The situation for polling is not the same and sensors working properly! :

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <HTU21D.h>

#define INTERVAL 2000
#define D5 14
#define D6 12
#define D3 0
#define D4 2

static HTU21D htu21d;

void setup() {
	Serial.begin(115200);

	WiFi.mode(WIFI_OFF);

	while (!htu21d.begin(D5, D6)) {
		Serial.print('.');
		delay(1000);
	}

	if (htu21d.batteryStatus()) {
		Serial.printf("\n HTU21D Battery: OK\n");
	}
}

void loop() {
	float htu_h = htu21d.readHumidity();
	float htu_t = htu21d.readTemperature();
	Serial.printf("HTU21D(T): %f, HTU21D(RH): %f \n", htu_t, htu_h);

	delay(INTERVAL);
}

No idea what's the problem here

Setup:

  • Latest lib version
  • Latest Arduino Git version
  • ESP-8266(nodemcu)

Regards.

@IMAN4K
Copy link
Author

IMAN4K commented Jan 17, 2018

After some debugging this is the error point in reading RH.
There should be a problem with Arduino TwoWire interface.

@IMAN4K IMAN4K changed the title Problem reading data in polling vs async [ESP-8266] Can not use read functions outside loop [ESP-8266] Jan 17, 2018
@IMAN4K
Copy link
Author

IMAN4K commented Mar 18, 2018

This seems to be related to TwoWire interface.
No matter how many instance you create, you can just use one instance at a time to communicate with one I2C device !
More specifically, there are some global pin values which is set to twi_init(...) at every construction here

@IMAN4K IMAN4K closed this as completed Mar 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant