Loom 4 v4.5
Arduino library for Internet of Things Rapid Prototyping in environmental sensing
Loading...
Searching...
No Matches
EZOSensor.h
1#pragma once
2
3#include <Wire.h>
4
5#include "../I2CDevice.h"
6#include "Logger.h"
7
8class EZOSensor : public I2CDevice{
9 public:
10
11 /* Construct a new EZO device */
12 EZOSensor(const char* modName) : I2CDevice(modName) {};
13
14
15 /* General command to transmit data over I2C to the given device*/
16 bool sendTransmission(const char* command){
17 Wire.beginTransmission(module_address);
18 Wire.write(command);
19
20 // Use a ternary operator to ensure if it is 0 its true if not we are false
21 return Wire.endTransmission() == 0;
22 };
23
24 /* Calibrate The Device */
25 bool calibrate(){
26 // Send the calibrate command
27 if(!sendTransmission("Cal")){
28 ERROR(F("Failed to transmit calibration command"));
29 return false;
30 }
31
32 LOG(F("Calibrating Device..."));
33
34 // Wait calibration time
35 delay(1300);
36
37 LOG(F("Device successfully calibrated!"));
38
39 return true;
40 };
41
48 bool readSensor(int waitTime){
49 char output[OUTPUT_SIZE];
50 int i;
51 if(moduleInitialized){
52 // Clear the sensorData received previously
53 memset(sensorData, '\0', 32);
54
55 // Attempt to send a read command to the device
56 if(!sendTransmission("r")){
57 ERROR(F("Failed to send 'read' command to device"));
58 return false;
59 }
60
61 // Wait the desired warm-up period
62 delay(waitTime);
63
64 // Request 32 bytes of data from the device
65 Wire.requestFrom(module_address, 32, 1);
66
67 // Check if the I2C code was not valid
68 code = Wire.read();
69
70 if(code != 1){
71 snprintf(output, OUTPUT_SIZE, "Unsuccessful Response Code Received: %s", responseCodes[code-1]);
72 ERROR(output);
73 return false;
74 }
75
76 // Read out only the next 32 bytes
77 for(i = 0; i < 32; i++){
78 currentChar = (char)Wire.read();
79 // If a null char was received break out of the loop
80 if(currentChar == '\0') break;
81
82 // If not append the current char to the string
83 strncat(sensorData, &currentChar, 32);
84 }
85 strncat(sensorData, "\0", 32);
86 }
87
88 return true;
89 };
90
91 /* Get the most recently collected sensor data */
92 const char* getSensorData() { return sensorData; };
93
94 private:
95 int8_t code = 0; // I2C Response Code
96 char currentChar; // Current character we have read in
97 const char* responseCodes[4] = {"Success", "Failed", "Pending", "No Data"}; // Stringified I2C Response codes
98 char sensorData[33]; // Convert the char array to a string to improve parse-ability
99
100};
Definition: EZOSensor.h:8
bool readSensor(int waitTime)
Definition: EZOSensor.h:48
Definition: I2CDevice.h:6