Last active
June 3, 2019 13:13
-
-
Save key/60f853b2b89ca38b415c13e9e5d48c2f to your computer and use it in GitHub Desktop.
m5stack env sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This code is example to get environment variable and upload to google spreadsheet. | |
* | |
* Requirements: | |
* - M5STACK (I tested this code on M5STACK FIRE) | |
* - Adafruit_BMP280 library | |
* - DHT12 library | |
* | |
* Usage: | |
* - Connect env unit to M5Stack port A. | |
*/ | |
#include <Wire.h> | |
#include <WiFi.h> | |
#include <M5Stack.h> | |
#include <HTTPClient.h> | |
#include "DHT12.h" | |
#include "Adafruit_Sensor.h" | |
#include "Adafruit_BMP280.h" | |
#include "preferences.h" | |
#define BATTERY_LEVEL_SUPPORT true | |
DHT12 dht12; //Preset scale CELSIUS and ID 0x5c. | |
Adafruit_BMP280 bme; | |
unsigned long t = 0; | |
unsigned long lastUploaded = 0; | |
const int sensorPollingInterval = 1000 * 1; | |
const int uploadInterval = 1000 * 60 * 5; | |
uint8_t hh; | |
uint8_t mm; | |
uint8_t ss; | |
struct tm timeInfo; | |
void setup() { | |
M5.begin(); | |
Wire.begin(); | |
M5.Lcd.setBrightness(10); | |
Serial.println(F("ENV Unit(DHT12 and BMP280) test...")); | |
if (!bme.begin(0x76)) { | |
Serial.println("Could not find a valid BMP280 sensor, check wiring!"); | |
while (1); | |
} | |
M5.Lcd.println("ENV Unit test..."); | |
Serial.print("[WiFi] Attempting to connect to SSID: "); | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
Serial.println(wifiSSID); | |
setupWiFi(); | |
configTime(9 * 3600L, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp"); | |
} | |
void loop() { | |
getLocalTime(&timeInfo); | |
hh = timeInfo.tm_hour; | |
mm = timeInfo.tm_min; | |
ss = timeInfo.tm_sec; | |
t = millis(); | |
float tmp = dht12.readTemperature(); | |
float hum = dht12.readHumidity(); | |
float pressure = bme.readPressure(); | |
Serial.printf("Time: %d Last Uploaded: %d\r\nTemperature: %2.2fC Humidity: %0.2f%% Pressure: %0.2fhPa\r\n", t, lastUploaded, tmp, hum, pressure / 100); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.setTextColor(WHITE, BLACK); | |
M5.Lcd.setTextSize(3); | |
M5.Lcd.printf("Time: %02d:%02d:%02d\r\nTemp: %2.1fC\r\nHumidity: %2.0f%%\r\nPress: %2.1fhPa\r\n", hh, mm, ss, t, tmp, hum, pressure / 100); | |
#ifdef BATTERY_LEVEL_SUPPORT | |
M5.Lcd.printf("BAT Level %d%%", getBatteryLevel()); | |
#endif | |
if (lastUploaded == 0 || lastUploaded + uploadInterval <= millis()) { | |
postJson(tmp, hum, pressure); | |
lastUploaded = t; | |
Serial.printf("Next upload time after %d seconds\r\n", (lastUploaded + uploadInterval) / 1000); | |
} | |
delay(sensorPollingInterval); | |
} | |
// get battery level from charge IC | |
// see: https://github.com/m5stack/M5Stack/issues/74 | |
int8_t getBatteryLevel() { | |
Wire.beginTransmission(0x75); | |
Wire.write(0x78); | |
if (Wire.endTransmission(false) == 0 && Wire.requestFrom(0x75, 1)) { | |
switch (Wire.read() & 0xF0) { | |
case 0xE0: return 25; | |
case 0xC0: return 50; | |
case 0x80: return 75; | |
case 0x00: return 100; | |
default: return 0; | |
} | |
} | |
return -1; | |
} | |
void setupWiFi() { | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.print("[WiFi] waiting for connection"); | |
WiFi.begin(wifiSSID, wifiPassword); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("connected!"); | |
Serial.print("[WiFi] Connected to "); | |
Serial.println(wifiSSID); | |
} | |
} | |
void postJson(float tmp, float hum, float pressure) { | |
setupWiFi(); | |
//make JSON | |
char json[100]; | |
sprintf(json, "{\"temp\": %2.2f , \"humid\": %2.2f, \"pressure\": %2.2f}", tmp, hum, pressure / 100); | |
//HTTPClient code start | |
HTTPClient http; | |
// configure traged server and url | |
Serial.print("[HTTP] Begin...\n"); | |
http.begin(spreadsheetUrl); //HTTP | |
Serial.print("[HTTP] POST started...\n"); | |
// start connection and send HTTP header | |
int httpCode = http.POST(json); | |
// httpCode will be negative on error | |
if (httpCode > 0) { | |
// HTTP header has been send and Server response header has been handled | |
Serial.printf("[HTTP] POST finished... code: %d\n", httpCode); | |
// file found at server | |
if (httpCode == HTTP_CODE_OK) { | |
String payload = http.getString(); | |
Serial.println(payload); | |
} | |
} else { | |
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment