Mega-Mics6814-Client: Added low-level client sketch for MiCS-6814 I2C board

This commit is contained in:
Nis Wechselberg 2018-05-20 14:35:31 +02:00
parent 8592c7bed2
commit 111d1c1d6e

View file

@ -0,0 +1,79 @@
#include <Wire.h>
#define I2C_ADDR 0x04
// I2C Communication (Taken from Groove sensor, hoping to use the same library on client side)
#define CMD_ADC_NH3 1 // NH3 channel
#define CMD_ADC_CO 2 // CO channel
#define CMD_ADC_NO2 3 // NO2 channel
#define CMD_ADC_ALL 4 // All channels
#define CMD_CHANGE_I2C 5 // I2C address change
#define CMD_READ_EEPROM 6 // Read stored data from EEPROM as unsigned int
#define CMD_SET_R0_ADC 7 // Set R0 ADC value
#define CMD_GET_R0_ADC 8 // Get R0 ADC value
#define CMD_GET_R0_ADC_DEFAULT 9 // Get factory R0 ADC value
#define CMD_CONTROL_LED 10 // Control LED (no LED on my board, but Groove supports is, so ... meh!)
#define CMD_CONTROL_PWR 11 // Heater control
// EEPROM Addresses (Match Groove addresses)
#define EEPROM_INIT_DONE 0
#define EEPROM_DEFAULT_ADC_NH3 2
#define EEPROM_DEFAULT_ADC_CO 4
#define EEPROM_DEFAULT_ADC_NO2 6
#define EEPROM_ADC_NH3 8
#define EEPROM_ADC_CO 10
#define EEPROM_ADC_NO2 12
#define EEPROM_I2C_ADDR 20
void setup() {
Serial.begin(115200);
// Connect to sensor through wire lib directly
Wire.begin(D3, D4);
// Check version
Wire.beginTransmission(I2C_ADDR);
Wire.write(CMD_READ_EEPROM);
Wire.write(EEPROM_INIT_DONE);
Wire.endTransmission();
uint8_t returned = Wire.requestFrom(I2C_ADDR, 2);
if (returned == 2 && Wire.available() == 2) {
uint8_t msb = Wire.read();
uint8_t lsb = Wire.read();
Serial.print("Init data: ");
Serial.println((msb << 8) | lsb);
} else {
Serial.println("ERROR 1234: Unexpected data!");
}
// Activate Heater
Wire.beginTransmission(I2C_ADDR);
Wire.write(CMD_CONTROL_PWR);
Wire.write(1);
Wire.endTransmission();
}
void loop() {
// Grab all data
Wire.beginTransmission(I2C_ADDR);
Wire.write(CMD_ADC_ALL);
Wire.endTransmission();
uint8_t ret = Wire.requestFrom(I2C_ADDR, 6);
if (ret == 6 && Wire.available() == 6) {
uint16_t data[3];
for (uint8_t i = 0; i < 3; ++i) {
uint8_t msb = Wire.read();
uint8_t lsb = Wire.read();
data[i] = (msb << 8) | lsb;
}
Serial.print(data[0]);
Serial.print("\t");
Serial.print(data[1]);
Serial.print("\t");
Serial.println(data[2]);
} else {
Serial.println("Request error!");
}
delay(50);
}