Home › Forums › Mayfly Data Logger › MaximDS18 on I2C Hub
- This topic has 3 replies, 3 voices, and was last updated 2020-04-06 at 3:19 PM by Anthony Aufdenkampe.
-
AuthorPosts
-
-
2020-04-06 at 8:48 AM #14027
How do I get my DS18 to work on an I2C Hub plugged into my I2C Grove port? It’s currently working on Pin 5 and I have the DeviceAddress but when I plug the DS18 into the I2C hub I can’t find a pin that will work.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373/*****************************************************************************Based on logging_to_MMW.inoAdapted by Anthony and BrianFrom source by: Sara Damiano (sdamiano@stroudcenter.org)Development Environment: PlatformIOHardware Platform: EnviroDIY Mayfly Arduino DataloggerSoftware License: BSD-3.Copyright (c) 2017, Stroud Water Research Center (SWRC)and the EnviroDIY Development TeamThis example sketch is written for ModularSensors library version 0.23.4currently using 0.23.11This shows most of the standard functions of the library at once.DISCLAIMER:THIS CODE IS PROVIDED "AS IS" - NO WARRANTY IS GIVEN.*****************************************************************************/// ==========================================================================// Include the base required libraries// ==========================================================================#include <Arduino.h> // The base Arduino library#include <EnableInterrupt.h> // for external and pin change interrupts#include <LoggerBase.h> // The modular sensors library#include <Wire.h>#include <OneWire.h>#include <SDL_Arduino_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility#include <AMAdafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)#include <SPI.h> // For SPI comm (needed for not getting compile error)#include <SoftwareWire.h> // for the stream communication// ==========================================================================// Data Logger Settings// ==========================================================================// The library version this example was written forconst char *libraryVersion = "0.23.11";// The name of this fileconst char *sketchName = "WXSTN_Mini_Mobile.ino";// Logger ID, also becomes the prefix for the name of the data file on SD cardconst char *LoggerID = "WX001";// How frequently (in minutes) to log dataconst uint8_t loggingInterval = 15;// Your logger's timezone.const int8_t timeZone = -6; // Central Standard Time// NOTE: Daylight savings time will not be applied! Please use standard time!// ==========================================================================// Primary Arduino-Based Board and Processor// ==========================================================================#include <sensors/ProcessorStats.h>const long serialBaud = 115200; // Baud rate for the primary serial port for debuggingconst int8_t greenLED = 8; // MCU pin for the green LED (-1 if not applicable)const int8_t redLED = 9; // MCU pin for the red LED (-1 if not applicable)const int8_t buttonPin = 21; // MCU pin for a button to use to enter debugging mode (-1 if not applicable)const int8_t wakePin = A7; // MCU interrupt/alarm pin to wake from sleep// Set the wake pin to -1 if you do not want the main processor to sleep.// In a SAMD system where you are using the built-in rtc, set wakePin to 1const int8_t sdCardPwrPin = -1; // MCU SD card power pin (-1 if not applicable)const int8_t sdCardSSPin = 12; // MCU SD card chip select/slave select pin (must be given!)const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power (-1 if not applicable)// Create the main processor chip "sensor" - for general metadataconst char *mcuBoardVersion = "v0.5b";ProcessorStats mcuBoard(mcuBoardVersion);// ==========================================================================// Wifi/Cellular Modem Settings// ==========================================================================// Create a reference to the serial port for the modem// Extra hardware and software serial ports are created in the "Settings for Additional Serial Ports" sectionHardwareSerial &modemSerial = Serial1; // Use hardware serial if possible// AltSoftSerial &modemSerial = altSoftSerial; // For software serial if needed// NeoSWSerial &modemSerial = neoSSerial1; // For software serial if needed// Modem Pins - Describe the physical pin connection of your modem to your boardconst int8_t modemVccPin = -2; // MCU pin controlling modem power (-1 if not applicable)const int8_t modemStatusPin = 19; // MCU pin used to read modem status (-1 if not applicable)const int8_t modemResetPin = 20; // MCU pin connected to modem reset pin (-1 if unconnected)const int8_t modemSleepRqPin = 23; // MCU pin used for modem sleep/wake request (-1 if not applicable)const int8_t modemLEDPin = redLED; // MCU pin connected an LED to show modem status (-1 if unconnected)// Network connection informationconst char *apn = "hologram"; // The APN for the gprs connection#if not defined MS_BUILD_TESTING || defined MS_BUILD_TEST_XBEE_CELLULAR// For any Digi Cellular XBee's// NOTE: The u-blox based Digi XBee's (3G global and LTE-M global)// are more stable used in bypass mode (below)// The Telit based Digi XBees (LTE Cat1) can only use this mode.#include <modems/DigiXBeeCellularTransparent.h>const long modemBaud = 9600; // All XBee's use 9600 by defaultconst bool useCTSforStatus = false; // Flag to use the modem CTS pin for status// NOTE: If possible, use the STATUS/SLEEP_not (XBee pin 13) for status, but// the CTS pin can also be used if necessaryDigiXBeeCellularTransparent modemXBCT(&modemSerial,modemVccPin, modemStatusPin, useCTSforStatus,modemResetPin, modemSleepRqPin,apn);// Create an extra reference to the modem by a generic name (not necessary)DigiXBeeCellularTransparent modem = modemXBCT;#endifconst int8_t I2CPower = 22;// Create an instance of the OLED displaySDL_Arduino_SSD1306 display(4); // FOR I2C// ==========================================================================// Maxim DS3231 RTC (Real Time Clock)// ==========================================================================#include <sensors/MaximDS3231.h>// Create a DS3231 sensor objectMaximDS3231 ds3231(1);// ==========================================================================// Maxim DS18 One Wire Temperature Sensor// ==========================================================================#include <sensors/MaximDS18.h>// OneWire Address [array of 8 hex characters]// If only using a single sensor on the OneWire bus, you may omit the addressDeviceAddress OneWireAddress1 = {0x28, 0xE7, 0x7E, 0x91, 0x0B, 0x00, 0x00, 0xD2};const int8_t OneWirePower = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected)const int8_t OneWireBus = 5; // Pin attached to the OneWire Bus (-1 if unconnected) (D24 = A0)// Create a Maxim DS18 sensor objects (use this form for a known address)MaximDS18 ds18(OneWireAddress1, OneWirePower, OneWireBus);// Create a Maxim DS18 sensor object (use this form for a single sensor on bus with an unknown address)// MaximDS18 ds18(OneWirePower, OneWireBus);// ==========================================================================// Creating the Variable Array[s] and Filling with Variable Objects// ==========================================================================#include <VariableArray.h>Variable *variableList[] = {// new ProcessorStats_SampleNumber(&mcuBoard, "12345678-abcd-1234-ef00-1234567890ab"),// new RainCounterI2C_Tips(&tbi2c, "12345678-abcd-1234-ef00-1234567890ab"),new ProcessorStats_Battery(&mcuBoard, "b17cb0f2-5538-4790-8641-39f416d185a3"),new MaximDS3231_Temp(&ds3231, "3a304193-2e51-49e8-96a5-41015b445484"),new MaximDS18_Temp(&ds18, "e1788d85-f8ca-451f-af49-5d4068650a04"),new Modem_RSSI(&modem, "e1788d85-f8ca-451f-af49-5d4068650a04"),new Modem_SignalPercent(&modem, "94c67feb-ade1-42eb-aef2-dcb021b85ef4"),};// Count up the number of pointers in the arrayint variableCount = sizeof(variableList) / sizeof(variableList[0]);// Create the VariableArray objectVariableArray varArray(variableCount, variableList);// ==========================================================================// The Logger Object[s]// ==========================================================================// Create a new logger instanceLogger dataLogger(LoggerID, loggingInterval, &varArray);// ==========================================================================// A Publisher to Monitor My Watershed / EnviroDIY Data Sharing Portal// ==========================================================================// Device registration and sampling feature information can be obtained after// registration at https://monitormywatershed.org or https://data.envirodiy.orgconst char *registrationToken = "ffb78d86-af9e-426d-ad44-b807f9e0xxxx"; // Device registration tokenconst char *samplingFeature = "f5290c01-05a9-4047-b096-ca21f21bxxxx"; // Sampling feature UUID// Create a data publisher for the EnviroDIY/WikiWatershed POST endpoint#include <publishers/EnviroDIYPublisher.h>EnviroDIYPublisher EnviroDIYPOST(dataLogger, &modem.gsmClient, registrationToken, samplingFeature);// ==========================================================================// Working Functions// ==========================================================================// Flashes the LED's on the primary boardvoid greenredflash(uint8_t numFlash = 4, uint8_t rate = 75){for (uint8_t i = 0; i < numFlash; i++) {digitalWrite(greenLED, HIGH);digitalWrite(redLED, LOW);delay(rate);digitalWrite(greenLED, LOW);digitalWrite(redLED, HIGH);delay(rate);}digitalWrite(redLED, LOW);}// Read's the battery voltage// NOTE: This will actually return the battery level from the previous update!float getBatteryVoltage(){if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update();return mcuBoard.sensorValues[0];}// ==========================================================================// Main setup function// ==========================================================================unsigned long delayTime;void setup(){// Wait for USB connection to be established by PC// NOTE: Only use this when debugging - if not connected to a PC, this// could prevent the script from starting#if defined SERIAL_PORT_USBVIRTUALwhile (!SERIAL_PORT_USBVIRTUAL && (millis() < 10000)){}#endif// Start the primary serial connectionSerial.begin(serialBaud);Serial.println(F("SuccesS"));//Added from Example_06_Mayfly_BME280_OLED.inopinMode(5, INPUT);display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // initialize with the I2C addr 0x3C (for the 128x64)display.clearDisplay();display.setTextSize(1);display.setTextColor(WHITE);display.setCursor(0,0);display.println("Mobile Mini Weather");display.println("What's the Temp?");display.println("");display.setTextSize(1);display.setTextColor(WHITE);display.setCursor(0,0);display.println("MMWX 001");display.setTextSize(2);display.println("72 degrees and sunny");display.display();// Print a start-up note to the first serial portSerial.print(F("Now running "));Serial.print(sketchName);Serial.print(F(" on Logger "));Serial.println(LoggerID);Serial.println();Serial.print(F("Using ModularSensors Library version "));Serial.println(MODULAR_SENSORS_VERSION);if (String(MODULAR_SENSORS_VERSION) != String(libraryVersion))Serial.println(F("WARNING: THIS EXAMPLE WAS WRITTEN FOR A DIFFERENT VERSION OF MODULAR SENSORS!!"));// Allow interrupts for software serial#if defined SoftwareSerial_ExtInts_henableInterrupt(softSerialRx, SoftwareSerial_ExtInts::handle_interrupt, CHANGE);#endif#if defined NeoSWSerial_henableInterrupt(neoSSerial1Rx, neoSSerial1ISR, CHANGE);#endif// Start the serial connection with the modemmodemSerial.begin(modemBaud);// Set up pins for the LED'spinMode(greenLED, OUTPUT);digitalWrite(greenLED, LOW);pinMode(redLED, OUTPUT);digitalWrite(redLED, LOW);// Blink the LEDs to show the board is on and starting upgreenredflash();// Set up some of the power pins so the board boots up with them off// NOTE: This isn't necessary at all. The logger begin() function// should leave all power pins off when it finishes.if (modemVccPin >= 0){pinMode(modemVccPin, OUTPUT);digitalWrite(modemVccPin, LOW);}if (sensorPowerPin >= 0){pinMode(sensorPowerPin, OUTPUT);digitalWrite(sensorPowerPin, LOW);}// Set the timezones for the logger/data and the RTC// Logging in the given time zoneLogger::setLoggerTimeZone(timeZone);// It is STRONGLY RECOMMENDED that you set the RTC to be in UTC (UTC+0)Logger::setRTCTimeZone(0);// Attach the modem and information pins to the loggerdataLogger.attachModem(modem);modem.setModemLED(modemLEDPin);dataLogger.setLoggerPins(wakePin, sdCardSSPin, sdCardPwrPin, buttonPin, greenLED);// Begin the loggerdataLogger.begin();// Note: Please change these battery voltages to match your battery// Check that the battery is OK before powering the modemif (getBatteryVoltage() > 3.7){modem.modemPowerUp();modem.wake();modem.setup();// At very good battery voltage, or with suspicious time stamp, sync the clock// Note: Please change these battery voltages to match your batteryif (getBatteryVoltage() > 3.8 ||dataLogger.getNowEpoch() < 1546300800 || /*Before 01/01/2019*/dataLogger.getNowEpoch() > 1735689600) /*After 1/1/2025*/{// Synchronize the RTC with NISTSerial.println(F("Attempting to connect to the internet and synchronize RTC with NIST"));if (modem.connectInternet(120000L)){dataLogger.setRTClock(modem.getNISTTime());}else{Serial.println(F("Could not connect to internet for clock sync."));}}}// Set up the sensors, except at lowest battery levelif (getBatteryVoltage() > 3.4){Serial.println(F("Setting up sensors..."));varArray.setupSensors();}// Power down the modemmodem.disconnectInternet();modem.modemSleepPowerDown();// Create the log file, adding the default header to it// Do this last so we have the best chance of getting the time correct and// all sensor names correct// Writing to the SD card can be power intensive, so if we're skipping// the sensor setup we'll skip this too.if (getBatteryVoltage() > 3.4){dataLogger.turnOnSDcard(true); // true = wait for card to settle after power updataLogger.createLogFile(true); // true = write a new headerdataLogger.turnOffSDcard(true); // true = wait for internal housekeeping after write}// Call the processor sleepSerial.println(F("Putting processor to sleep"));dataLogger.systemSleep();}// ==========================================================================// Main loop function// ==========================================================================// Use this short loop for simple data logging and sendingvoid loop(){// Note: Please change these battery voltages to match your battery// At very low battery, just go back to sleepif (getBatteryVoltage() < 3.4){dataLogger.systemSleep();}// At moderate voltage, log data but don't send it over the modemelse if (getBatteryVoltage() < 3.6){dataLogger.logData();}// If the battery is good, send the data to the worldelse{dataLogger.logDataAndPublish();}}Attachments:
-
2020-04-06 at 10:43 AM #14029
If your goal is to have everything on that one 4-port splitter – it won’t work.
The DS18 isn’t I2C – it’s OneWire. If you really want to use it on that Grove port, you should be able to use it as either D16(SCL) or D17(SDA), but I strongly suspect that attempting to dual-use one of the I2C pins for OneWire would screw up communication with actual I2C devices (like the build in clock or ADC and your little display). I’d be surprised if you could even get a sketch to run. The I2C library (from Arduino core) has a bug (a while loop with no timeout) such that if either pin on the I2C bus is held low during any I2C communication the board will freeze in endless wait. If you really have your heart set on that connection pattern, you can experiment with it, but I’d just just a different port if I were you.
-
2020-04-06 at 11:10 AM #14030
Sara, Thank you (sigh of relief of knowing why it wouldn’t work). That was my goal and now I know it shouldn’t be. That is very helpful. No wonder I couldn’t find an example anywhere.
-
2020-04-06 at 3:19 PM #14032
Brian, you can use that DS18 temperature sensor on any of the digital pins (labeled on the Mayfly as: D4, D5, D6, D7, D10, D11), and because it’s OneWire, you can connect many DS18 sensors to a single pin using the I2C Hub (which generically connects multiple Grove connections in parallel). It just won’t work using I2C protocols.
-
-
AuthorPosts
- You must be logged in to reply to this topic.