I am puzzled, I am working with the ss_oled library by Larry Bank.
I am trying to display temperature and humidity data to one of two I2c OLEDs.
The sensor data is posted on the serial monitor but not on the OLED. I have determined that the variable is not being converted correctly to be displayed.
ahtValue1 = temperature value (prefer it to be in Fahrenheit)
ahtValue2 = humidity value
[code]
//
// Small Simple OLED library multi-display
//
// By Larry Bank
//
#include <ss_oled.h>
#include <AHTxx.h>
// Use -1 for the Wire library default pins
// or specify the pin numbers to use with the Wire library or bit banging on any GPIO pins
// These are reversed because I did straight-through wiring for my SSD1306
// and it has the 4-pin header as GND,VCC,SCL,SDA, but the GROVE connector is
// GND,VCC,SDA,SCL
//#define GROVE_SDA_PIN 32
//#define GROVE_SCL_PIN 26
// These are the pin numbers for the M5Stack Atom default I2C
#define SDA_PIN A4
#define SCL_PIN A5
// Set this to -1 to disable or the GPIO pin number connected to the reset
// line of your display if it requires an external reset
#define RESET_PIN -1
// let ss_oled figure out the display address
//#define OLED_ADDR -1
// don't rotate the display
#define FLIP180 0
// don't invert the display
#define INVERT 0
// Bit-Bang the I2C bus
#define USE_HW_I2C 0
// Change these if you're using different OLED displays
// OLED1 address 0x3C
#define MY_OLED1 OLED_128x64
//OLED2 address 0x3D
#define MY_OLED2 OLED_128x64
// AHT21 address 0x38
float ahtValue1; //to store Temp result
float ahtValue2; //to store humidity result
AHTxx aht20(AHTXX_ADDRESS_X38, AHT2x_SENSOR); //sensor address, sensor type
byte celsius = 26;
byte fahrenheit = (celsius * 9) / 5 + 32;
byte humidity = ahtValue2;
// 2 copies of the SSOLED structure. Each structure is about 56 bytes
// There is no limit to the number of simultaneous displays which can be controlled by ss_oled
SSOLED ssoled[2];
void setup() {
int rc;
// The I2C SDA/SCL pins set to -1 means to use the default Wire library
// If pins were specified, they would be bit-banged in software
// This isn't inferior to hw I2C and in fact allows you to go faster on certain CPUs
// The reset pin is optional and I've only seen it needed on larger OLEDs (2.4")
// that can be configured as either SPI or I2C
//
// oledInit(SSOLED *, type, oled_addr, rotate180, invert, bWire, SDA_PIN, SCL_PIN, RESET_PIN, speed)
rc = oledInit(&ssoled[1], MY_OLED2, 0x3D, FLIP180, INVERT, 1, SDA_PIN, SCL_PIN, RESET_PIN, 400000L); // use standard I2C bus at 400Khz
if (rc != OLED_NOT_FOUND)
{
oledFill(&ssoled[1], 0, 1);
oledSetTextWrap(&ssoled[1], 1);
oledWriteString(&ssoled[1], 0,9,1,(char *)"Display 2", FONT_NORMAL, 0, 1);
}
Serial.begin(9600);
Serial.println();
while (aht20.begin() != true)
{
Serial.println(F("AHT2x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
delay(5000);
}
//Serial.println(F("AHT20 OK"));
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
} /* setup() */
void loop() {
ahtValue1 = aht20.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
int rc;
rc = oledInit(&ssoled[0], MY_OLED1, 0x3C, FLIP180, INVERT, 1, SDA_PIN, SCL_PIN, RESET_PIN, 400000L); // use standard I2C bus at 400Khz
if (rc != OLED_NOT_FOUND)
{
char szString1[32];
char szString2[32];
sprintf(szString1, fahrenheit);
//sprintf(szString2, ahtValue2);
sprintf(szString2, humidity);
// Line one
oledFill(&ssoled[0], 0, 1);
oledWriteString(&ssoled[0], 0,0,0,(char *)"Helm: ", FONT_NORMAL, 0, 1);
//oledWriteString(&ssoled[0], 0,50,0,(char *)"80F", FONT_NORMAL, 0, 1);
oledWriteString(&ssoled[0], 0,90,0,(char *)"50%", FONT_NORMAL, 0, 1);
oledWriteString(&ssoled[0], 0,50,0,(szString1), FONT_NORMAL, 0,1);
//oledWriteString(&ssoled[0], 0,90,0, szString1, FONT_NORMAL, 0,1);
// Line two
//oledFill(&ssoled[0], 0, 1);
oledWriteString(&ssoled[0], 0,0,2,(char *)"Pack: ", FONT_NORMAL, 0, 1);
oledWriteString(&ssoled[0], 0,50,2,(char *)"25F", FONT_NORMAL, 0, 1);
oledWriteString(&ssoled[0], 0,90,2,(char *)"70%", FONT_NORMAL, 0, 1);
//oledWriteString(&ssoled[0], 0,0,4, szString, FONT_NORMAL, 0,1);
// Line three
//oledFill(&ssoled[0], 0, 1);
oledWriteString(&ssoled[0], 0,0,4,(char *)"Cool: ", FONT_NORMAL, 0, 1);
oledWriteString(&ssoled[0], 0,50,4,(char *)"ON/Off", FONT_NORMAL, 0, 1);
//oledWriteString(&ssoled[0], 0,0,4, szString, FONT_NORMAL, 0,1);
// Line four
//oledFill(&ssoled[0], 0, 1);
oledWriteString(&ssoled[0], 0,0,6,(char *)"Fan: ", FONT_NORMAL, 0, 1);
oledWriteString(&ssoled[0], 0,50,6,(char *)"100%", FONT_NORMAL, 0, 1);
//oledWriteString(&ssoled[0], 0,0,4, szString, FONT_NORMAL, 0,1);
}
Serial.print(F("Temperature: "));
if (ahtValue1 != AHTXX_ERROR) //AHTXX_ERROR = 255, library returns 255 if error occurs
{
Serial.print(fahrenheit);
Serial.println(F("F"));
Serial.print(ahtValue1);
Serial.println(F("C"));
}
else
{
printStatus(); //print temperature command status
}
//ahtValue2 = aht20.readHumidity(AHTXX_USE_READ_DATA); //use 6-bytes from temperature reading, takes zero milliseconds!!!
humidity = aht20.readHumidity(AHTXX_USE_READ_DATA); //use 6-bytes from temperature reading, takes zero milliseconds!!! = aht20.readHumidity(AHTXX_USE_READ_DATA); //use 6-bytes from temperature reading, takes zero milliseconds!!!
Serial.print(F("Humidity...: "));
if (ahtValue2 != AHTXX_ERROR) //AHTXX_ERROR = 255, library returns 255 if error occurs
{
Serial.print(humidity);
Serial.println(F("%"));
Serial.println();
}
else
{
printStatus(); //print temperature command status not humidity!!! RH measurement use same 6-bytes from T measurement
}
delay(15000); //recomended polling frequency 8sec..30sec
} /* loop() */
void printStatus()
{
switch (aht20.getStatus())
{
case AHTXX_NO_ERROR:
Serial.println(F("no error"));
break;
case AHTXX_BUSY_ERROR:
Serial.println(F("sensor busy, increase polling time"));
break;
case AHTXX_ACK_ERROR:
Serial.println(F("sensor didn't return ACK, not connected, broken, long wires (reduce speed), bus locked by slave (increase stretch limit)"));
break;
case AHTXX_DATA_ERROR:
Serial.println(F("received data smaller than expected, not connected, broken, long wires (reduce speed), bus locked by slave (increase stretch limit)"));
break;
case AHTXX_CRC8_ERROR:
Serial.println(F("computed CRC8 not match received CRC8, this feature supported only by AHT2x sensors"));
break;
default:
Serial.println(F("unknown status"));
break;
}
}
[/code]
thank you