/* Author: Francesco Dugnani Date: 2019-05-01 Project name: Weather Station 2019 Connect SHT35-DIS to MKRWAN1300 as follows: SHT35 -- MKRWAN1300 SDA -- 11 SCL -- 12 Vcc -- 4 Gnd -- Gnd */ #include #include #include #include "ClosedCube_SHT31D.h" #define DEBUG #define RESULT // LoRa variable #define timeoutLoRa 1800; // delay between messages 15 mins // Wakeup Timer #define SLEEP_PERIOD ( (uint32_t) 900000 ) // ADC configuration #define intBatteryPin PIN_A1 #define refVoltagePin PIN_A0 #define i2cDRIVER 4 LoRaModem modem; // Uncomment if using the Murata chip as a module // LoRaModem modem(Serial1); #include "arduino_secrets.h" // Please enter your sensitive data in the Secret tab or arduino_secrets.h String appEui = SECRET_APP_EUI; String appKey = SECRET_APP_KEY; ClosedCube_SHT31D sht3xd; void blink(int numBlink = 1, unsigned int speed = 200) { int i; while (numBlink--) { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(speed); // wait digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(speed); // wait } } void reboot() { NVIC_SystemReset(); while (1) ; } void pinStr( uint32_t ulPin, unsigned strength) // works like pinMode(), but to set drive strength { // Handle the case the pin isn't usable as PIO if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) { return ; } if (strength) strength = 1; // set drive strength to either 0 or 1 copied PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.DRVSTR = strength ; } void setup() { #ifdef DEBUG Serial.begin(115200); delay(5000); #endif blink(2); pinMode(LED_BUILTIN, OUTPUT); pinMode(i2cDRIVER, OUTPUT); pinStr(i2cDRIVER, 1); digitalWrite(i2cDRIVER, LOW); pinMode(intBatteryPin, OUTPUT); pinStr(intBatteryPin, 1); // Configure the ADC analogReadResolution(12); analogWriteResolution(12); analogReference(AR_INTERNAL1V65); #ifdef DEBUG Serial.println("Welcome to Weather Station 2019 Rev.2"); Serial.println("starting Murata CMWX1ZZABZ LoRa Module..."); #endif // change this to your regional band (eg. US915, AS923, ...) if (!modem.begin(EU868)) { #ifdef DEBUG Serial.println("Failed to start module"); #endif delay(1000); reboot(); }; #ifdef DEBUG Serial.print("Your module version is: "); Serial.println(modem.version()); Serial.print("Your device EUI is: "); Serial.println(modem.deviceEUI()); Serial.print("appKey is: "); Serial.println(appKey); #endif appKey.trim(); #ifdef DEBUG Serial.print("appEui is: "); Serial.println(appEui); #endif appEui.trim(); int connected = modem.joinOTAA(appEui, appKey); if (!connected) { int joinFailed = 0; while (!connected && joinFailed < 10) { connected = modem.joinOTAA(appEui, appKey); if (!connected) { #ifdef DEBUG Serial.println("Something went wrong; are you indoor? Move near a window and retry"); #endif blink(1); delay(60000); joinFailed++; } } } else { #ifdef DEBUG Serial.println("logged LoRa Network to Loriot"); #endif } modem.minPollInterval(60); } void loop() { float temperature; float humidity; char bufferLORA[11] = {0x01, 0x67, 0x00, 0x00, 0x02, 0x68, 0x00, 0x03, 0x02, 0x00, 0x00}; while (1) { blink(3); digitalWrite(i2cDRIVER, HIGH); delay(1000); Wire.begin(); sht3xd.begin(0x44); // I2C address: 0x44 or 0x45 #ifdef DEBUG Serial.println("*************************************"); Serial.print("Serial #"); Serial.println(sht3xd.readSerialNumber()); Serial.println("Pooling Mode"); #endif SHT31D result = sht3xd.readTempAndHumidity(SHT3XD_REPEATABILITY_HIGH, SHT3XD_MODE_POLLING, 50); if (result.error == SHT3XD_NO_ERROR) { #ifdef DEBUG temperature = result.t; humidity = result.rh; Serial.println("*************************************"); Serial.print("Temperature: "); Serial.print(temperature); Serial.println("°C"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%"); #endif } else { #ifdef DEBUG Serial.println("*************************************"); Serial.print("[ERROR] Code #"); Serial.println(result.error); #endif temperature = 0.00; humidity = 0.00; } bufferLORA[2] = (int)(temperature * 10) >> 8; bufferLORA[3] = (int)(temperature * 10); bufferLORA[6] = humidity * 2; analogWrite(refVoltagePin, 1024); // apply voltage to DAC to test analog input delay(10); int batteryVALUE = analogRead(intBatteryPin); float value = (batteryVALUE / 4096.0) * 6.6; int percent = (int)((value / 3.3) * 100.00); #ifdef DEBUG Serial.print("Battery(V): "); Serial.print(value); Serial.print(" ("); Serial.print(batteryVALUE); Serial.print(") "); Serial.print(percent); Serial.println("%"); #endif bufferLORA[9] = (int)(percent * 100) >> 8; bufferLORA[10] = (int)(percent * 100); #ifdef RESULT Serial.print("LoRa String: "); Serial.print(bufferLORA[0], HEX); Serial.print(":"); Serial.print(bufferLORA[1], HEX); Serial.print(":"); Serial.print(bufferLORA[2], HEX); Serial.print(":"); Serial.print(bufferLORA[3], HEX); Serial.print(":"); Serial.print(bufferLORA[4], HEX); Serial.print(":"); Serial.print(bufferLORA[5], HEX); Serial.print(":"); Serial.print(bufferLORA[6], HEX); Serial.print(":"); Serial.print(bufferLORA[7], HEX); Serial.print(":"); Serial.print(bufferLORA[8], HEX); Serial.print(":"); Serial.print(bufferLORA[9], HEX); Serial.print(":"); Serial.println(bufferLORA[10], HEX); #endif #ifdef RESULT Serial.println("Enabling ADR and setting low spreading factor"); #endif modem.setADR(true); modem.dataRate(5); modem.beginPacket(); modem.write(bufferLORA, sizeof(bufferLORA)); int err = modem.endPacket(true); if (err > 0) { #ifdef RESULT Serial.println("Message sent correctly!"); #endif } else { #ifdef RESULT Serial.println("Error sending message!"); #endif } Serial.flush(); Wire.end(); digitalWrite(i2cDRIVER, LOW); #ifdef DEBUG Serial.end(); #endif LowPower.deepSleep(SLEEP_PERIOD); delay(1000); #ifdef DEBUG Serial.begin(115200); Serial.print("\r\n"); #endif } }