/******************************************************************************/ // Function: Sends temperature, humidity, and atmospheric pressure // to the IoT visualization service (Ambient) // Usage: Write via Arduino IDE // Hardware: M5AtomLite and ENV.2 Unit // Arduino IDE: Arduino-1.8.13 // Author: Hideto Manjo // Date: Oct 10, 2020 // Version: v0.1 // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // /******************************************************************************/ #include #include #include #include #include // sensor #define TIME_TO_SLEEP 300 #define PRESSURE_LOWB 750 //hPa // WiFi #define SSID "ssid" #define PASSWORD "password" #define RETRY 20 // Ambient #define CHANNELID 00000 #define WRITEKEY "whitekey" Adafruit_SHT31 sht3x; Adafruit_BMP280 bme; WiFiClient client; Ambient ambient; bool connect_wifi() { int i = RETRY; WiFi.begin(SSID, PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if (!i--) { Serial.println("Could not connect to WiFi"); return false; } } Serial.print("WiFi connected\r\nIP address: "); Serial.println(WiFi.localIP()); return true; } void setup() { M5.begin(); Wire.begin(26, 32); while (!bme.begin(0x76)) { Serial.println("BMP280 init fail"); } while (!sht3x.begin(0x44)) { Serial.println("SHT3x init fail"); } float tmp = sht3x.readTemperature(); float hum = sht3x.readHumidity(); // Since bme280 often returns an incorrect pressure value, // we set a lower limit. float pressure = 0.0; do { pressure = bme.readPressure(); }while(pressure < PRESSURE_LOWB * 100); Serial.printf("temp: %4.1f'C\r\n", tmp); Serial.printf("humid:%4.1f%%\r\n", hum); Serial.printf("press:%4.0fhPa\r\n", pressure / 100); if (connect_wifi()) { ambient.begin(CHANNELID, WRITEKEY, &client); ambient.set(1, tmp); ambient.set(2, hum); ambient.set(3, pressure / 100); if (ambient.send()) { Serial.println("The data was successfully sent to the ambient"); }else{ Serial.println("Failed to send data to ambient"); } } esp_deep_sleep(TIME_TO_SLEEP * 1000000); } void loop() { }