I'll just get right to the point:
I'm a complete newbie to coding, so forgive me if my code is sloppy, inefficient, or otherwise just a mess.
What I'm trying to do is send sensor data (temp, humidity) from a slave to a master over I2C while retaining the original 4 decimal places. As it stands my code functions exactly as I intend it to, save for the decimal places.
On the slave sender, I can see that the sensors put out values of xx.xx printed to the serial monitor, however after sending them to the master and printing them to the serial monitor I receive a value of xx.00 every time.
Master Receiver code:
/*
CONNECTED GREENHOUSE MASTER RECEIVER
The SenseHub (SH) is designed to function as the control hub of the Connected Greenhouse system
Data from each sensor (air temperature, CO2 ppm, relative humidity, water temperature, water pH,
water dissolved O2, and water conductivity) is collected and evaluated by this device.
After data is evaluated, the SH will determine what actions need to be performed.
*/
// LIBRARIES:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Constants:
const byte ledPin = 13;
long previousLCDMillis = 0; // for LCD screen update
long lcdInterval = 2500;
int table[2];
// screen to show
int screen = 0;
int screenMax = 9;
bool screenChanged = true; // initially we have a new screen, by definition
// SCREENS:
#define AIR_TEMPERATURE 0
#define HUMIDITY 1
#define CARBON 2
#define RESERVOIR 3
#define WATER_TEMPERATURE 4
#define PH 5
#define OXYGEN 6
#define GENERATOR 7
#define LOAD 8
#define TIME 9
// SENSORS:
#define AIRT1_SLAVE 0x08
// TARGET INTEGERS
int airTtarget;
int airHtarget;
int waterP_Upper_Bound;
int waterP_Lower_Bound;
// SENSOR FLOATS:
float airT1;
float airH1;
float airC1;
float waterL1;
float waterT1;
float waterP1;
float waterDO1;
float waterC1;
float genI;
float genV;
float loadI;
float loadV;
// TX-RX FLOATS:
float dRXval;
// Global variables:
int timeLapsed;
union // Union used to convert raw data to 'float'.
{
float dRXVal;
byte bRawData[4];
} floatData;
void setup()
{
Serial.begin(9600);
Wire.begin(); // Join I2C bus.
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
lcd.setCursor(5, 0);
lcd.print("System");
lcd.setCursor(0, 1);
lcd.print("intitializing...");
delay(250);
}
void loop()
// Main code loop
{
static unsigned long prevMillis = 0;
static bool ledPinState = 0;
unsigned long currentMillis = millis(); // Get the current time.
if (currentMillis - prevMillis >= 1000) // I2C updates once per second
{
prevMillis = currentMillis; // Record the current time.
ledPinState ^= 1; // Toggle the LED
digitalWrite(ledPin, ledPinState); // " " "
// Request 4 bytes from the slave device.
}
getAirQual(); // Request the air temperature and humidity from the air sensor
if (airT1 >= airTtarget) // If the air temperature is above the target, execute temperature control
airtempControl();
delay(10);
if (airH1 >= airHtarget) // If the humidity is above the target, execute humidity control
airhumidControl();
printData(); // Print all collected data to serial monitor
displayData(); // Loop LCD through all data displays
delay(1000);
}
void getAirQual()
{
Wire.requestFrom(8, 2); // request 2 bytes from slave device #2
digitalWrite(3, HIGH);
delay(10);
for (int i = 0; i < 2; i++)
{
int c = Wire.read(); // receive a byte as character
Serial.print(c);
table[i] = c;
Serial.println('\t');
}
Serial.print('\n');
Serial.print(table[0]);
airH1 = table[0];
Serial.println('\t');
Serial.print(table[1]);
airT1 = table[1];
Serial.println('\n');
digitalWrite(3, LOW);
}
void printData()
{
// Print air temp to serial monitor
Serial.print ("Air Temp: ");
Serial.print (airT1);
Serial.print ("C");
Serial.println ();
// Print air humidity to serial monitor
Serial.print ("R-Humidity: ");
Serial.print (airH1);
Serial.print ("%");
Serial.println ();
}
Slave Sender code:
#include <Adafruit_Si7021.h>
#include <Wire.h>
Adafruit_Si7021 sensor = Adafruit_Si7021();
// SENSOR FLOATS
float humidRXval;
float tempRXval;
// DEFINES
#define SLAVE_ADDRESS 0x8
// CONSTANTS
const int txLED = 4;
int ledPin = 13;
int table[] = {0, 0, 0};
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(txLED, OUTPUT);
Wire.begin(SLAVE_ADDRESS); // Join I2C bus with address.
// wait for serial port to open
while (!Serial) {
delay(10);
}
Wire.onRequest(requestEvent); // Register request handler.
Serial.println("Si7021 test!");
if (!sensor.begin()) {
Serial.println("Did not find Si7021 sensor!");
while (true)
;
}
Serial.print("Found model ");
switch (sensor.getModel()) {
case SI_Engineering_Samples:
Serial.print("SI engineering samples"); break;
case SI_7013:
Serial.print("Si7013"); break;
case SI_7020:
Serial.print("Si7020"); break;
case SI_7021:
Serial.print("Si7021"); break;
case SI_UNKNOWN:
default:
Serial.print("Unknown");
}
Serial.print(" Rev(");
Serial.print(sensor.getRevision());
Serial.print(")");
Serial.print(" Serial #"); Serial.print(sensor.sernum_a, HEX); Serial.println(sensor.sernum_b, HEX);
}
void loop() {
static unsigned long prevMillis = 0;
static bool ledPinState = 0;
unsigned long currentMillis = millis(); // Get the current time.
if (currentMillis - prevMillis >= 1000) // Toggle the LED once per second
{
prevMillis = currentMillis; // Record the current time.
ledPinState ^= 1; // Toggle the LED.
digitalWrite(ledPin, ledPinState); // " " "
}
Serial.print("Humidity: ");
Serial.print(sensor.readHumidity(), 2);
humidRXval = sensor.readHumidity();
Serial.print("\tTemperature: ");
Serial.println(sensor.readTemperature(), 2);
tempRXval = sensor.readTemperature();
table[0] = (humidRXval);
table[1] = (tempRXval);
delay(500);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
digitalWrite(txLED, HIGH);
delay(10);
// for(int i=0;i<3;i++)
// {
uint8_t Buffer[2];
Buffer[0] = table[0];
Buffer[1] = table[1];
Wire.write(Buffer, 2);
digitalWrite(txLED, LOW);
}