diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/CCS811.cpp | 154 | ||||
-rw-r--r-- | src/main/CCS811.h | 224 | ||||
-rw-r--r-- | src/main/Makefile | 58 | ||||
-rw-r--r-- | src/main/main.ino | 126 | ||||
-rw-r--r-- | src/main/ota.h | 98 | ||||
-rw-r--r-- | src/main/wifiinfo.h | 6 |
6 files changed, 0 insertions, 666 deletions
diff --git a/src/main/CCS811.cpp b/src/main/CCS811.cpp deleted file mode 100644 index 05b71c0..0000000 --- a/src/main/CCS811.cpp +++ /dev/null | |||
@@ -1,154 +0,0 @@ | |||
1 | #include "CCS811.h" | ||
2 | |||
3 | int CCS811::begin(void) | ||
4 | { | ||
5 | uint8_t id=0; | ||
6 | Wire.begin(); | ||
7 | softReset(); | ||
8 | delay(100); | ||
9 | if(readReg(CCS811_REG_HW_ID,&id,1) != 1){DBG(""); | ||
10 | DBG("bus data access error");DBG(""); | ||
11 | return ERR_DATA_BUS;DBG(""); | ||
12 | } | ||
13 | |||
14 | DBG("real sensor id=");DBG(id); | ||
15 | if(id != CCS811_HW_ID){DBG(""); | ||
16 | delay(1); | ||
17 | return ERR_IC_VERSION; | ||
18 | } | ||
19 | writeReg(CCS811_BOOTLOADER_APP_START, NULL, 0); | ||
20 | setMeasurementMode(0,0,eMode4); | ||
21 | setInTempHum(25, 50); | ||
22 | return ERR_OK; | ||
23 | } | ||
24 | |||
25 | void CCS811::softReset(){ | ||
26 | uint8_t value[4] = {0x11, 0xE5, 0x72, 0x8A}; | ||
27 | writeReg(CCS811_REG_SW_RESET, value, 4); | ||
28 | } | ||
29 | |||
30 | bool CCS811::checkDataReady() | ||
31 | { | ||
32 | int8_t status[1] = {0}; | ||
33 | readReg(CCS811_REG_STATUS, status, 1); | ||
34 | DBG(status[0],HEX); | ||
35 | if(!((status[0] >> 3) & 0x01)) | ||
36 | return false; | ||
37 | else | ||
38 | return true; | ||
39 | } | ||
40 | |||
41 | uint16_t CCS811::readBaseLine(){ | ||
42 | uint8_t buffer[2]; | ||
43 | readReg(CCS811_REG_BASELINE, buffer, 2); | ||
44 | return buffer[0]<<8|buffer[1]; | ||
45 | } | ||
46 | |||
47 | void CCS811::writeBaseLine(uint16_t baseLine){ | ||
48 | uint8_t buffer[2]; | ||
49 | |||
50 | buffer[0] = baseLine>>8; | ||
51 | buffer[1] = baseLine; | ||
52 | writeReg(CCS811_REG_BASELINE, buffer, 2); | ||
53 | } | ||
54 | |||
55 | void CCS811::setMeasurementMode(uint8_t thresh, uint8_t interrupt, eDRIVE_MODE_t mode){ | ||
56 | uint8_t measurement[1] = {0}; | ||
57 | measurement[0] = (thresh << 2) | (interrupt << 3) | (mode << 4); | ||
58 | writeReg(CCS811_REG_MEAS_MODE, measurement, 1); | ||
59 | } | ||
60 | |||
61 | void CCS811::setMeasCycle(eCycle_t cycle){ | ||
62 | uint8_t measurement[1] = {0}; | ||
63 | measurement[0] = cycle << 4; | ||
64 | writeReg(CCS811_REG_MEAS_MODE, measurement, 1); | ||
65 | } | ||
66 | |||
67 | uint8_t CCS811::getMeasurementMode(){ | ||
68 | uint8_t meas[1] = {0}; | ||
69 | readReg(CCS811_REG_MEAS_MODE, meas, 1); | ||
70 | return meas[0]; | ||
71 | } | ||
72 | |||
73 | void CCS811::setThresholds(uint16_t lowToMed, uint16_t medToHigh) | ||
74 | { | ||
75 | uint8_t buffer[] = {(uint8_t)((lowToMed >> 8) & 0xF), | ||
76 | (uint8_t)(lowToMed & 0xF), | ||
77 | (uint8_t)((medToHigh >> 8) & 0xF), | ||
78 | (uint8_t)(medToHigh & 0xF)}; | ||
79 | |||
80 | writeReg(CCS811_REG_THRESHOLDS, buffer, 5); | ||
81 | uint8_t buf[1]; | ||
82 | readReg(CCS811_REG_THRESHOLDS, buf, 1); | ||
83 | Serial.println(buf[0],HEX); | ||
84 | } | ||
85 | |||
86 | uint16_t CCS811::getCO2PPM(){ | ||
87 | uint8_t buffer[8]; | ||
88 | readReg(CCS811_REG_ALG_RESULT_DATA, buffer, 8); | ||
89 | eCO2 = (((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1]); | ||
90 | return eCO2; | ||
91 | } | ||
92 | |||
93 | uint16_t CCS811::getTVOCPPB(){ | ||
94 | uint8_t buffer[8]; | ||
95 | readReg(CCS811_REG_ALG_RESULT_DATA, buffer, 8); | ||
96 | eTVOC = (((uint16_t)buffer[2] << 8) | (uint16_t)buffer[3]); | ||
97 | return eTVOC; | ||
98 | } | ||
99 | |||
100 | void CCS811::setInTempHum(float temperature, float humidity) // compensate for temperature and relative humidity | ||
101 | { | ||
102 | int _temp, _rh; | ||
103 | if(temperature>0) | ||
104 | _temp = (int)temperature + 0.5; // this will round off the floating point to the nearest integer value | ||
105 | else if(temperature<0) // account for negative temperatures | ||
106 | _temp = (int)temperature - 0.5; | ||
107 | _temp = _temp + 25; // temperature high byte is stored as T+25°C in the sensor's memory so the value of byte is positive | ||
108 | _rh = (int)humidity + 0.5; // this will round off the floating point to the nearest integer value | ||
109 | |||
110 | uint8_t envData[4]; | ||
111 | |||
112 | envData[0] = _rh << 1; // shift the binary number to left by 1. This is stored as a 7-bit value | ||
113 | envData[1] = 0; // most significant fractional bit. Using 0 here - gives us accuracy of +/-1%. Current firmware (2016) only supports fractional increments of 0.5 | ||
114 | envData[2] = _temp << 1; | ||
115 | envData[3] = 0; | ||
116 | |||
117 | writeReg(CCS811_REG_ENV_DATA, &envData, 4); | ||
118 | } | ||
119 | |||
120 | void CCS811::writeReg(uint8_t reg, const void* pBuf, size_t size) | ||
121 | { | ||
122 | if(pBuf == NULL){ | ||
123 | DBG("pBuf ERROR!! : null pointer"); | ||
124 | } | ||
125 | uint8_t * _pBuf = (uint8_t *)pBuf; | ||
126 | _pWire->beginTransmission(_deviceAddr); | ||
127 | _pWire->write(®, 1); | ||
128 | |||
129 | for(uint16_t i = 0; i < size; i++){ | ||
130 | _pWire->write(_pBuf[i]); | ||
131 | } | ||
132 | _pWire->endTransmission(); | ||
133 | } | ||
134 | |||
135 | uint8_t CCS811::readReg(uint8_t reg, const void* pBuf, size_t size) | ||
136 | { | ||
137 | if(pBuf == NULL){ | ||
138 | DBG("pBuf ERROR!! : null pointer"); | ||
139 | } | ||
140 | uint8_t * _pBuf = (uint8_t *)pBuf; | ||
141 | _pWire->beginTransmission(_deviceAddr); | ||
142 | _pWire->write(®, 1); | ||
143 | |||
144 | if( _pWire->endTransmission() != 0){ | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | _pWire->requestFrom(_deviceAddr, (uint8_t) size); | ||
149 | for(uint16_t i = 0; i < size; i++){ | ||
150 | _pBuf[i] = _pWire->read(); | ||
151 | } | ||
152 | _pWire->endTransmission(); | ||
153 | return size; | ||
154 | } | ||
diff --git a/src/main/CCS811.h b/src/main/CCS811.h deleted file mode 100644 index 2640535..0000000 --- a/src/main/CCS811.h +++ /dev/null | |||
@@ -1,224 +0,0 @@ | |||
1 | #ifndef _CCS811_H | ||
2 | #define _CCS811_H | ||
3 | |||
4 | #if ARDUINO >= 100 | ||
5 | #include "Arduino.h" | ||
6 | #else | ||
7 | #include "WProgram.h" | ||
8 | #endif | ||
9 | #include <Wire.h> | ||
10 | |||
11 | |||
12 | /*I2C ADDRESS*/ | ||
13 | #define CCS811_I2C_ADDRESS1 0x5A | ||
14 | #define CCS811_I2C_ADDRESS2 0x5B | ||
15 | |||
16 | #define CCS811_REG_STATUS 0x00 | ||
17 | #define CCS811_REG_MEAS_MODE 0x01 | ||
18 | #define CCS811_REG_ALG_RESULT_DATA 0x02 | ||
19 | #define CCS811_REG_RAW_DATA 0x03 | ||
20 | #define CCS811_REG_ENV_DATA 0x05 | ||
21 | #define CCS811_REG_NTC 0x06 | ||
22 | #define CCS811_REG_THRESHOLDS 0x10 | ||
23 | #define CCS811_REG_BASELINE 0x11 | ||
24 | #define CCS811_REG_HW_ID 0x20 | ||
25 | #define CCS811_REG_HW_VERSION 0x21 | ||
26 | #define CCS811_REG_FW_BOOT_VERSION 0x23 | ||
27 | #define CCS811_REG_FW_APP_VERSION 0x24 | ||
28 | #define CCS811_REG_INTERNAL_STATE 0xA0 | ||
29 | #define CCS811_REG_ERROR_ID 0xE0 | ||
30 | #define CCS811_REG_SW_RESET 0xFF | ||
31 | |||
32 | #define CCS811_BOOTLOADER_APP_ERASE 0xF1 | ||
33 | #define CCS811_BOOTLOADER_APP_DATA 0xF2 | ||
34 | #define CCS811_BOOTLOADER_APP_VERIFY 0xF3 | ||
35 | #define CCS811_BOOTLOADER_APP_START 0xF4 | ||
36 | |||
37 | #define CCS811_HW_ID 0x81 | ||
38 | //Open the macro to see the detailed program execution process. | ||
39 | //#define ENABLE_DBG | ||
40 | |||
41 | #ifdef ENABLE_DBG | ||
42 | #define DBG(...) {Serial.print("[");Serial.print(__FUNCTION__); Serial.print("(): "); Serial.print(__LINE__); Serial.print(" ] "); Serial.println(__VA_ARGS__);} | ||
43 | #else | ||
44 | #define DBG(...) | ||
45 | #endif | ||
46 | |||
47 | class CCS811 | ||
48 | { | ||
49 | public: | ||
50 | #define ERR_OK 0 //OK | ||
51 | #define ERR_DATA_BUS -1 //error in data bus | ||
52 | #define ERR_IC_VERSION -2 //chip version mismatch | ||
53 | |||
54 | uint8_t _deviceAddr; | ||
55 | typedef enum{ | ||
56 | eMode0, //Idle (Measurements are disabled in this mode) | ||
57 | eMode1, //Constant power mode, IAQ measurement every second | ||
58 | eMode2, //Pulse heating mode IAQ measurement every 10 seconds | ||
59 | eMode3, //Low power pulse heating mode IAQ measurement every 60 seconds | ||
60 | eMode4 //Constant power mode, sensor measurement every 250ms 1xx: Reserved modes (For future use) | ||
61 | }eDRIVE_MODE_t; | ||
62 | |||
63 | typedef enum{ | ||
64 | eClosed, //Idle (Measurements are disabled in this mode) | ||
65 | eCycle_1s, //Constant power mode, IAQ measurement every second | ||
66 | eCycle_10s, //Pulse heating mode IAQ measurement every 10 seconds | ||
67 | eCycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds | ||
68 | eCycle_250ms //Constant power mode, sensor measurement every 250ms 1xx: Reserved modes (For future use) | ||
69 | }eCycle_t; | ||
70 | /** | ||
71 | * @brief Constructor | ||
72 | * @param Input in Wire address | ||
73 | */ | ||
74 | CCS811(TwoWire *pWire = &Wire, uint8_t deviceAddr = 0x5A){_pWire = pWire; _deviceAddr = deviceAddr;}; | ||
75 | |||
76 | /** | ||
77 | * @brief Constructor | ||
78 | * @return Return 0 if initialization succeeds, otherwise return non-zero. | ||
79 | */ | ||
80 | int begin(); | ||
81 | /** | ||
82 | * @brief Judge if there is data to read | ||
83 | * @return Return 1 if there is, otherwise return 0. | ||
84 | */ | ||
85 | bool checkDataReady(); | ||
86 | /** | ||
87 | * @brief Reset sensor, clear all configured data. | ||
88 | */ | ||
89 | void softReset(), | ||
90 | /** | ||
91 | * @brief Set environment parameter | ||
92 | * @param temperature Set temperature value, unit: centigrade, range (-40~85℃) | ||
93 | * @param humidity Set humidity value, unit: RH, range (0~100) | ||
94 | */ | ||
95 | setInTempHum(float temperature, float humidity), | ||
96 | /** | ||
97 | * @brief Measurement parameter configuration | ||
98 | * @param thresh:0 for Interrupt mode operates normally; 1 for interrupt mode only asserts the nINT signal (driven low) if the new | ||
99 | * @param interrupt:0 for Interrupt generation is disabled; 1 for the nINT signal is asserted (driven low) when a new sample is ready in | ||
100 | * @param mode:in typedef enum eDRIVE_MODE_t | ||
101 | */ | ||
102 | setMeasurementMode(uint8_t thresh, uint8_t interrupt, eDRIVE_MODE_t mode), | ||
103 | /** | ||
104 | * @brief Measurement parameter configuration | ||
105 | * @param mode:in typedef enum eDRIVE_MODE_t | ||
106 | */ | ||
107 | setMeasCycle(eCycle_t cycle), | ||
108 | /** | ||
109 | * @brief Set interrupt thresholds | ||
110 | * @param lowToMed: interrupt triggered value in range low to middle | ||
111 | * @param medToHigh: interrupt triggered value in range middle to high | ||
112 | */ | ||
113 | setThresholds(uint16_t lowToMed, uint16_t medToHigh); | ||
114 | /** | ||
115 | * @brief Get current configured parameter | ||
116 | * @return configuration code, needs to be converted into binary code to analyze | ||
117 | * The 2nd: Interrupt mode (if enabled) operates normally,1: Interrupt mode (if enabled) only asserts the nINT signal (driven low) if the new | ||
118 | * The 3rd: Interrupt generation is disabled,1: The nINT signal is asserted (driven low) when a new sample is ready in | ||
119 | * The 4th: 6th: in typedef enum eDRIVE_MODE_t | ||
120 | */ | ||
121 | uint8_t getMeasurementMode(); | ||
122 | |||
123 | /** | ||
124 | * @brief Get the current carbon dioxide concentration | ||
125 | * @return current carbon dioxide concentration, unit:ppm | ||
126 | */ | ||
127 | uint16_t getCO2PPM(), | ||
128 | /** | ||
129 | * @brief Get current TVOC concentration | ||
130 | * @return Return current TVOC concentration, unit: ppb | ||
131 | */ | ||
132 | getTVOCPPB(); | ||
133 | uint16_t readBaseLine(); | ||
134 | void writeBaseLine(uint16_t baseLine); | ||
135 | |||
136 | protected: | ||
137 | |||
138 | typedef struct{ | ||
139 | /* | ||
140 | * The CCS811 received an I²C write request addressed to this station but with invalid register address ID | ||
141 | */ | ||
142 | uint8_t sWRITE_REG_INVALID: 1; | ||
143 | /* | ||
144 | * The CCS811 received an I²C read request to a mailbox ID that is invalid | ||
145 | */ | ||
146 | uint8_t sREAD_REG_INVALID: 1; | ||
147 | /* | ||
148 | * The CCS811 received an I²C request to write an unsupported mode to MEAS_MODE | ||
149 | */ | ||
150 | uint8_t sMEASMODE_INVALID: 1; | ||
151 | /* | ||
152 | * The sensor resistance measurement has reached or exceeded the maximum range | ||
153 | */ | ||
154 | uint8_t sMAX_RESISTANCE: 1; | ||
155 | /* | ||
156 | * The The Heater current in the CCS811 is not in range | ||
157 | */ | ||
158 | uint8_t sHEATER_FAULT: 1; | ||
159 | /* | ||
160 | * The Heater voltage is not being applied correctly | ||
161 | */ | ||
162 | uint8_t sHEATER_SUPPLY: 1; | ||
163 | } __attribute__ ((packed))sError_id; | ||
164 | |||
165 | typedef struct{ | ||
166 | /* | ||
167 | * ALG_RESULT_DATA crosses one of the thresholds set in the THRESHOLDS register | ||
168 | * by more than the hysteresis value (also in the THRESHOLDS register) | ||
169 | */ | ||
170 | uint8_t sINT_THRESH: 1; | ||
171 | /* | ||
172 | * At the end of each measurement cycle (250ms, 1s, 10s, 60s) a flag is set in the | ||
173 | * STATUS register regardless of the setting of this bit. | ||
174 | */ | ||
175 | uint8_t sINT_DATARDY: 1; | ||
176 | /* | ||
177 | * A new sample is placed in ALG_RESULT_DATA and RAW_DATA registers and the | ||
178 | * DATA_READY bit in the STATUS register is set at the defined measurement interval. | ||
179 | */ | ||
180 | uint8_t sDRIVE_MODE: 3; | ||
181 | } __attribute__ ((packed))sMeas_mode; | ||
182 | |||
183 | typedef struct{ | ||
184 | /* | ||
185 | * This bit is cleared by reading ERROR_ID | ||
186 | * It is not sufficient to read the ERROR field of ALG_RESULT_DATA and STATUS | ||
187 | */ | ||
188 | uint8_t sERROR: 1; | ||
189 | /* | ||
190 | * ALG_RESULT_DATA is read on the I²C interface | ||
191 | */ | ||
192 | uint8_t sDATA_READY: 1; | ||
193 | uint8_t sAPP_VALID: 1; | ||
194 | /* | ||
195 | * After issuing a VERIFY command the application software must wait 70ms before | ||
196 | * issuing any transactions to CCS811 over the I²C interface | ||
197 | */ | ||
198 | uint8_t sAPP_VERIFY: 1; | ||
199 | /* | ||
200 | * After issuing the ERASE command the application software must wait 500ms | ||
201 | * before issuing any transactions to the CCS811 over the I2C interface. | ||
202 | */ | ||
203 | uint8_t sAPP_ERASE: 1; | ||
204 | uint8_t sFW_MODE: 1; | ||
205 | } __attribute__ ((packed))sStatus; | ||
206 | |||
207 | |||
208 | void getData(void); | ||
209 | |||
210 | void writeConfig(); | ||
211 | |||
212 | virtual void writeReg(uint8_t reg, const void* pBuf, size_t size); | ||
213 | virtual uint8_t readReg(uint8_t reg, const void* pBuf, size_t size); | ||
214 | |||
215 | |||
216 | |||
217 | private: | ||
218 | TwoWire *_pWire; | ||
219 | |||
220 | uint16_t eCO2; | ||
221 | uint16_t eTVOC; | ||
222 | }; | ||
223 | |||
224 | #endif | ||
diff --git a/src/main/Makefile b/src/main/Makefile deleted file mode 100644 index 2000b24..0000000 --- a/src/main/Makefile +++ /dev/null | |||
@@ -1,58 +0,0 @@ | |||
1 | # Optionally some environment variables can be set: | ||
2 | # | ||
3 | # FQBN Fully Qualified Board Name; if not set in the environment | ||
4 | # it will be assigned a value in this makefile | ||
5 | # | ||
6 | # SERIAL_DEV Serial device to upload the sketch; if not set in the | ||
7 | # environment it will be assigned: | ||
8 | # /dev/ttyUSB0 if it exists, or | ||
9 | # /dev/ttyACM0 if it exists, or | ||
10 | # unknown | ||
11 | # | ||
12 | # IOT_NAME Name of the IOT device; if not set in the environment | ||
13 | # it will be assigned a value in this makefile. This is | ||
14 | # very useful for OTA update, the device will be searched | ||
15 | # on the local subnet using this name | ||
16 | # | ||
17 | # OTA_PORT Port used by OTA update; if not set in the environment | ||
18 | # it will be assigned the default value of 8266 in this | ||
19 | # makefile | ||
20 | # | ||
21 | # OTA_PASS Password used for OTA update; if not set in the environment | ||
22 | # it will be assigned the default value of an empty string | ||
23 | |||
24 | FQBN = esp8266:esp8266:espmxdevkit | ||
25 | IOT_NAME = mush | ||
26 | OTA_PORT = 8266 | ||
27 | OTA_PASS = | ||
28 | |||
29 | SERIAL_DEV = $(firstword $(wildcard /dev/ttyUSB* /dev/ttyACM*)) | ||
30 | |||
31 | cli = arduino-cli -b "$(FQBN)" | ||
32 | |||
33 | BUILD_DIR != $(cli) compile --show-properties | sed -ne 's/^build.path=//p' | ||
34 | |||
35 | SRC := main.ino | ||
36 | HDRS := $(wildcard *.h) | ||
37 | BIN := $(BUILD_DIR)/main.ino.bin | ||
38 | ELF := $(BUILD_DIR)/main.ino.elf | ||
39 | |||
40 | all: $(ELF) | ||
41 | .PHONY: all | ||
42 | |||
43 | compile: $(ELF) | ||
44 | .PHONY: compile | ||
45 | |||
46 | $(BIN) $(ELF): $(SRC) $(HDRS) | ||
47 | $(cli) compile | ||
48 | |||
49 | upload: | ||
50 | $(cli) upload -p "$(SERIAL_DEV)" | ||
51 | |||
52 | PLAT_PATH != $(cli) compile --show-properties | sed -ne 's/^runtime.platform.path=//p' | ||
53 | PY_PATH != $(cli) compile --show-properties | sed -ne 's/^runtime.tools.python3.path=//p' | ||
54 | IOT_IP != getent ahostsv4 mush.local | (read ip _; echo $$ip) | ||
55 | |||
56 | ota: $(BIN) | ||
57 | "$(PY_PATH)/python3" "$(PLAT_PATH)/tools/espota.py" -i "$(IOT_IP)" -p "$(OTA_PORT)" --auth="$(OTA_PASS)" -f "$(BIN)" | ||
58 | |||
diff --git a/src/main/main.ino b/src/main/main.ino deleted file mode 100644 index 6c2688f..0000000 --- a/src/main/main.ino +++ /dev/null | |||
@@ -1,126 +0,0 @@ | |||
1 | #include <DHT.h> | ||
2 | #include "ota.h" | ||
3 | #include <WiFiClient.h> | ||
4 | #include "CCS811.h" | ||
5 | |||
6 | #define TEMP_SENSOR_PIN 12 | ||
7 | #define HUMIDITY_SENSOR_PIN 12 | ||
8 | #define FAN_PIN 15 | ||
9 | #define CO2_PIN 16 | ||
10 | #define ULTRASONIC_PIN 12 | ||
11 | |||
12 | #define PHOTORESISTOR 0 // pin 0 is analog | ||
13 | |||
14 | #define CHECK_FREQUENCY 2 // check sensors every 2 seconds | ||
15 | #define HUMIDITY_DESIRED 75 | ||
16 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off | ||
17 | // at (75 + 3 = 78) | ||
18 | WiFiClient *wific = 0; | ||
19 | |||
20 | // Tempature + Humidity Sensor | ||
21 | #define DHTPIN 12 | ||
22 | #define DHTTYPE DHT11 | ||
23 | DHT dht(DHTPIN, DHTTYPE); | ||
24 | |||
25 | /* | ||
26 | * IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered. | ||
27 | */ | ||
28 | //CCS811 sensor(&Wire, /*IIC_ADDRESS=*/0x5A); | ||
29 | CCS811 sensor; | ||
30 | |||
31 | void setup(void) | ||
32 | { | ||
33 | setupWifi((char *) hostname); | ||
34 | setupOTA((char *) hostname); | ||
35 | |||
36 | dht.begin(); // temp+humidity sensor | ||
37 | pinMode(PHOTORESISTOR, INPUT); // photo resistor | ||
38 | wific = new WiFiClient(); | ||
39 | |||
40 | |||
41 | Serial.begin(115200); | ||
42 | /*Wait for the chip to be initialized completely, and then exit*/ | ||
43 | while (sensor.begin()) { | ||
44 | Serial.println("failed to init chip, please check if the chip connection is fine"); | ||
45 | delay(1000); | ||
46 | } | ||
47 | /** | ||
48 | * @brief Set measurement cycle | ||
49 | * @param cycle:in typedef enum{ | ||
50 | * eClosed, //Idle (Measurements are disabled in this mode) | ||
51 | * eCycle_1s, //Constant power mode, IAQ measurement every second | ||
52 | * Ecycle_10s, //Pulse heating mode IAQ measurement every 10 seconds | ||
53 | * eCycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds | ||
54 | * eCycle_250ms //Constant power mode, sensor measurement every 250ms | ||
55 | * }eCycle_t; | ||
56 | */ | ||
57 | sensor.setMeasCycle(sensor.eCycle_250ms); | ||
58 | } | ||
59 | |||
60 | void writeBoth(char *buf) | ||
61 | { | ||
62 | Serial.print(buf); | ||
63 | if (wific->connected()) { | ||
64 | wific->write(buf); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | void log_reading (float temp_c, float humidity, int photons, uint16_t co2, uint16_t tvoc) | ||
69 | { | ||
70 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %d TVOC: %d\r\n"; | ||
71 | char buf[500]; | ||
72 | |||
73 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); | ||
74 | |||
75 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); | ||
76 | writeBoth(buf); | ||
77 | } | ||
78 | |||
79 | struct SensorState | ||
80 | { | ||
81 | float last_reading; | ||
82 | virtual void sense() = 0; | ||
83 | }; | ||
84 | |||
85 | struct TemperatureSensor : public SensorState { virtual void sense() { last_reading = dht.readTemperature(); } }; | ||
86 | struct HumiditySensor : public SensorState { virtual void sense() { last_reading = dht.readHumidity(); } }; | ||
87 | struct PhotoSensor : public SensorState { virtual void sense() { last_reading = analogRead(PHOTORESISTOR); } }; | ||
88 | struct CO2Sensor : public SensorState { virtual void sense() { last_reading = sensor.getCO2PPM(); } }; | ||
89 | struct TVOCSensor : public SensorState { virtual void sense() { last_reading = sensor.getTVOCPPB(); } }; | ||
90 | |||
91 | TemperatureSensor temperatureSensor; | ||
92 | HumiditySensor humiditySensor; | ||
93 | PhotoSensor photoSensor; | ||
94 | CO2Sensor cO2Sensor; | ||
95 | TVOCSensor tVOCSensor; | ||
96 | |||
97 | SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor }; | ||
98 | |||
99 | void sensor_loop() | ||
100 | { | ||
101 | for (int i=0; i<sizeof(sensors)/sizeof(sensors[0]); ++i) | ||
102 | { | ||
103 | sensors[i]->sense(); | ||
104 | } | ||
105 | sensor.writeBaseLine(0x847B); | ||
106 | } | ||
107 | |||
108 | void loop() | ||
109 | { | ||
110 | ArduinoOTA.handle(); | ||
111 | |||
112 | auto ip = IPAddress(192,168,1,2); | ||
113 | auto port = 3141; | ||
114 | if (!wific->connected()) { | ||
115 | Serial.println("Attempting to connect"); | ||
116 | wific->connect(ip, port); | ||
117 | } | ||
118 | |||
119 | sensor_loop(); | ||
120 | |||
121 | log_reading(temperatureSensor.last_reading, humiditySensor.last_reading, photoSensor.last_reading, cO2Sensor.last_reading, tVOCSensor.last_reading); | ||
122 | |||
123 | delay(1000); | ||
124 | } | ||
125 | |||
126 | |||
diff --git a/src/main/ota.h b/src/main/ota.h deleted file mode 100644 index 28fb37b..0000000 --- a/src/main/ota.h +++ /dev/null | |||
@@ -1,98 +0,0 @@ | |||
1 | // Include what is needed to implement Over The Air Update | ||
2 | // The WiFi connection is not done on this include file, so must be done | ||
3 | // in the main program file | ||
4 | // | ||
5 | // This include file provides the functions | ||
6 | // setupWifi(hostname) | ||
7 | // hostname can be an empty string; this function will | ||
8 | // establish the wifi connections, WiFi credentials are supplied | ||
9 | // in the file "wifiinfo.h", see "wifiinfo.h.sample" | ||
10 | // setupOTA(hostname) | ||
11 | // hostname can be an empty string; will be used to retrieve the | ||
12 | // correct board to upgrade Over The Air. | ||
13 | // If an empty string the default name is esp8266-[ChipID] | ||
14 | // The above 2 functions must be called in the setup() function | ||
15 | // | ||
16 | // in the loop function the function | ||
17 | // ArduinoOTA.handle() | ||
18 | // must be called | ||
19 | |||
20 | #ifndef ota_h | ||
21 | #define ota_h | ||
22 | // needed include files | ||
23 | #include <ESP8266WiFi.h> | ||
24 | #include <ESP8266mDNS.h> | ||
25 | #include <WiFiUdp.h> | ||
26 | #include <ArduinoOTA.h> | ||
27 | |||
28 | // include our Network SSID and PASSWORD | ||
29 | #include "wifiinfo.h" | ||
30 | |||
31 | // ================================================================== | ||
32 | // Establish WiFi connection | ||
33 | // ================================================================== | ||
34 | void setupWifi(char hostname[]) { | ||
35 | int i = 0; | ||
36 | WiFi.mode(WIFI_STA); | ||
37 | if (sizeof(hostname) > 0) { | ||
38 | WiFi.hostname(hostname); | ||
39 | } | ||
40 | if (Serial) {Serial.println("Connecting ");} | ||
41 | WiFi.begin(ssid, password); | ||
42 | while (WiFi.status() != WL_CONNECTED) { | ||
43 | if (Serial) {Serial.print(".");} | ||
44 | delay(500); | ||
45 | } | ||
46 | if (Serial) { | ||
47 | Serial.println("."); | ||
48 | Serial.print("Connected! IP: "); | ||
49 | Serial.println(WiFi.localIP()); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | |||
54 | // ================================================================== | ||
55 | // Setup for OTA update | ||
56 | // ================================================================== | ||
57 | |||
58 | void setupOTA(char hostname[]) { | ||
59 | // Port defaults to 8266 | ||
60 | // ArduinoOTA.setPort(8266); | ||
61 | |||
62 | // Hostname defaults to esp8266-[ChipID] | ||
63 | if (sizeof(hostname) > 0) { | ||
64 | ArduinoOTA.setHostname(hostname); | ||
65 | } | ||
66 | |||
67 | // No authentication by default | ||
68 | // ArduinoOTA.setPassword((const char *)"123"); | ||
69 | |||
70 | ArduinoOTA.onStart([]() { | ||
71 | if (Serial) {Serial.println("Start");} | ||
72 | }); | ||
73 | ArduinoOTA.onEnd([]() { | ||
74 | if (Serial) {Serial.println("\nEnd");} | ||
75 | }); | ||
76 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | ||
77 | if (Serial) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));} | ||
78 | }); | ||
79 | ArduinoOTA.onError([](ota_error_t error) { | ||
80 | if (Serial) { | ||
81 | Serial.printf("Error[%u]: ", error); | ||
82 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | ||
83 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | ||
84 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | ||
85 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | ||
86 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); | ||
87 | } | ||
88 | }); | ||
89 | ArduinoOTA.begin(); | ||
90 | if (Serial) { | ||
91 | Serial.println("Ready"); | ||
92 | Serial.print("IP address: "); | ||
93 | Serial.println(WiFi.localIP()); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | |||
98 | #endif | ||
diff --git a/src/main/wifiinfo.h b/src/main/wifiinfo.h deleted file mode 100644 index fb94cdd..0000000 --- a/src/main/wifiinfo.h +++ /dev/null | |||
@@ -1,6 +0,0 @@ | |||
1 | #ifndef wifiinfo_h | ||
2 | #define wifiinfo_h | ||
3 | const char *hostname = "mush"; | ||
4 | const char* ssid = "omni"; | ||
5 | const char* password = "everywhere"; | ||
6 | #endif | ||