Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By makingesp8266
#24451 i have this code and i don't know why don't show me the right measurements. it gives me 125 and 1

hih6130={
dev_addr = 0x27,
id=0,
init = function (self, sda, scl)
self.id = 0
i2c.setup(self.id, sda, scl, i2c.SLOW)
end,
read_reg = function(self, dev_addr)
i2c.start(self.id)
aa=i2c.address(self.id, dev_addr ,i2c.TRANSMITTER)
i2c.stop(self.id)
tmr.delay(36)
i2c.start(self.id)
i2c.address(self.id, dev_addr,i2c.RECEIVER)
c=i2c.read(self.id,2)
i2c.stop(self.id)
print(aa)
return c
end,

readTemp = function(self)
h, l = string.byte(self:read_reg(0x27, 0xD), 1, 2)
h1=bit.lshift(h,8)
rawtemp = bit.band(bit.bor(h1,l),0xfffc)
tp1 = ((rawtemp/16383)* 165 ) - 40
print(tp1)
return tp1
end,
readHum = function(self)
h,l = string.byte(self:read_reg(0x27, 0xd), 1, 2)
h1=bit.lshift(h,8)
rawhum = bit.band(bit.bor(h1,1),0xfffc)
hum = (rawhum/16383)
print(hum)
return hum
end
}
sda=6 --GPIO12
scl=7 --GPIO13
hih6130:init(sda,scl)
tp1 = hih6130:readTemp()
hum = hih6130:readHum()

srv=net.createServer(net.TCP)
srv:listen(80,
function(conn)
conn:on("receive",function(conn,payload) print(payload)
conn:send("HTTP/1.1 200 OK\n\n")
conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">")
conn:send("<html><title>hih6130 - Temperature Log Server - ESP8266</title><body>")
conn:send("<h1>ESP8266 Temperature Log Server - hih6130</h1><BR>")
conn:send("Temperature : <b>" .. tp1 .. "&deg;C</b><BR><BR>")
conn:send("Humidity : <b>" .. hum .. " %</b><BR><BR>")
conn:send("Node.HEAP : <b>" .. node.heap() .. "</b><BR><BR>")
conn:send("IP ADDR : <b>".. wifi.sta.getip() .. "</b><BR>")
conn:send("Node MAC : <b>" .. wifi.sta.getmac() .. "</b><br>")
conn:send("TMR.NOW : <b>" .. tmr.now() .. "</b><BR<BR><BR>")
conn:send("</html></body>")
conn:on("sent",function(conn) conn:close() end)
end)
end)

any ideas?
User avatar
By jcmvbkbc
#24452
makingesp8266 wrote:i have this code and i don't know why don't show me the right measurements. it gives me 125 and 1
Code: Select all       
 readTemp = function(self)
          h, l = string.byte(self:read_reg(0x27, 0xD), 1, 2)
          h1=bit.lshift(h,8)
          rawtemp = bit.band(bit.bor(h1,l),0xfffc)
          tp1 = ((rawtemp/16383)* 165 ) - 40
          print(tp1)
          return tp1
    end,
    readHum = function(self)
          h,l = string.byte(self:read_reg(0x27, 0xd), 1,  2)
          h1=bit.lshift(h,8)
          rawhum = bit.band(bit.bor(h1,1),0xfffc)
          hum = (rawhum/16383)
          print(hum)
          return hum
     end

...
any ideas?

125 and 1 mean that both these functions get all ones in response from their respective devices, i.e. that the devices do not respond properly.
User avatar
By Ionel I
#40095 Hi,

I am trying to read a HIH 6130 temperature and humidity sensor using a Huzzah ESP8266 Breakout board.

I am using an adaptation of the code found here:
http://www.phanderson.com/arduino/hih6130.html
I only changed the I2c pins to SDA=2, SCL=14.

Although I can read the temperature from the sensor using an Arduino (Uno or Leonardo),
I never managed to read the values using the ESP8266.

All calls to Wire.read() return 0xFF, regardless of the current temperature or humidity.
I have tried with/without 2.2K pullup resistors on SDA and SCL, but always get 0xFF bytes.
I also successfully read a digital compass (HMC6352) with I2C, on pins 2, 14, on the same ESP8266 board.

Am I doing something wrong?
Any suggestions would be greatly appreciated.


Here is the full code:
-------------------------------------------------------------------------------------------------------------------------------------------
#include <Wire.h>

byte fetch_humidity_temperature(unsigned int *p_Humidity, unsigned int *p_Temperature);
void print_float(float f, int num_digits);

int sda = 2;
int scl = 14;

void setup(void)
{
delay(3000);
Serial.begin(9600);
Serial.println("Hello");
Wire.begin(sda, scl);

pinMode(sda, OUTPUT);
digitalWrite(sda, HIGH); // this turns on the HIH3610
delay(5000);
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>"); // just to be sure things are working
}

void loop(void)
{
byte _status;
unsigned int H_dat, T_dat;
float RH, T_C;

_status = fetch_humidity_temperature(&H_dat, &T_dat);
switch(_status)
{
case 0: Serial.println("Normal.");
break;
case 1: Serial.println("Stale Data.");
break;
case 2: Serial.println("In command mode.");
break;
default: Serial.println("Diagnostic.");
break;
}

RH = (float) H_dat * 6.10e-3;
T_C = (float) T_dat * 1.007e-2 - 40.0;

// print_float(RH, 1);
Serial.print(" ");
print_float(T_C, 2);
Serial.println();
delay(1000);
}

byte fetch_humidity_temperature(unsigned int *p_H_dat, unsigned int *p_T_dat)
{
byte address, Hum_H, Hum_L, Temp_H, Temp_L, _status;
unsigned int H_dat, T_dat;
address = 0x27;;
Wire.beginTransmission(address);
Wire.endTransmission();
delay(100);

Wire.requestFrom((int)address, (int) 4);
Hum_H = Wire.read();
Hum_L = Wire.read();
Temp_H = Wire.read();
Temp_L = Wire.read();
Wire.endTransmission();

//Serial.print(Hum_H, HEX); Serial.print(" ");
// Serial.print(Hum_L, HEX);Serial.print(" ");
Serial.print(Temp_H, HEX);Serial.print(" ");
Serial.print(Temp_L, HEX);Serial.println(" ");

_status = (Hum_H >> 6) & 0x03;
Hum_H = Hum_H & 0x3f;
H_dat = (((unsigned int)Hum_H) << 8) | Hum_L;
T_dat = (((unsigned int)Temp_H) << 8) | Temp_L;
T_dat = T_dat / 4;
*p_H_dat = H_dat;
*p_T_dat = T_dat;
return (_status);
}

void print_float(float f, int num_digits)
{
int f_int;
int pows_of_ten[4] = {1, 10, 100, 1000};
int multiplier, whole, fract, d, n;

multiplier = pows_of_ten[num_digits];
if (f < 0.0)
{
f = -f;
Serial.print("-");
}
whole = (int) f;
fract = (int) (multiplier * (f - (float)whole));

Serial.print(whole);
Serial.print(".");

for (n = num_digits - 1; n >= 0; n--) // print each digit with no leading zero suppression
{
d = fract / pows_of_ten[n];
Serial.print(d);
fract = fract % pows_of_ten[n];
}
}