Stange interaction between clock and temprature sensor setup



  • Hi and thanks for you're time

    I am a relative beginner to M5Stack and have made 2 test programs. One that displays weekday, date and time and one that use a Dallas 18B2D temperature sensor to display the room temperature. Both works interdependent.

    However a strange interaction between the two setups occur when I try to implement them both on the same time.

    If the sensor is connected to M5Stack during start up, then the internet setup won't connect, resulting in the weekday, date and time not working but the sensor value is displayed correctly.

    If the sensor is disconnected from M5Stack during start up, then the internet setup will connect and weekday, date and time displays... But the sensor will not setup correctly and the sensor value is not displayed correctly.

    I can't find the reason why the internet setup wont work when an analog input is connected to my M5Stack. Do you, masters of the M5Stack have any idea what could be the cause?

    ---------------------------------------------------- Copy of my code ---------------------------------------------------------------------------------
    #include <M5Stack.h>
    #include <WiFi.h>
    #include <GParser.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #include "time.h"

    // Set the name and password of the wifi to be connected.
    const char* ssid = "####WIFI#####";
    const char* password = "##Stuff##";

    // Setup for NTP server and GMT offset as well as if daylight savings
    const char* ntpServer = "se.pool.ntp.org"; // Set the connect NTP server.
    const long gmtOffset_sec = 3600;
    const int daylightOffset_sec = 3600;

    // Setup and declaration of therometer sensor
    #define ONE_WIRE_BUS 2 // Data wire is plugged into port 2 on the Arduino
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
    DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
    DeviceAddress insideThermometer; // arrays to hold device address

    void printLocalTime() {
    struct tm timeinfo;
    if (!getLocalTime(&timeinfo)) { // Return 1 when the time is successfully obtained.
    M5.Lcd.println("Failed to obtain time. \nPlease restart");
    return;
    }
    // Wright weekday
    M5.Lcd.setTextSize(5); // Set the font size.
    M5.Lcd.setCursor(40, 10); // Set cursor
    M5.Lcd.println(&timeinfo, "%A");

    // Wright date, month and year
    M5.Lcd.setTextSize(3);     // Set the font size.
    M5.Lcd.setCursor(10, 64);  // Set cursor
    M5.Lcd.println(&timeinfo, "%d %B %Y");
    
    // Wright time of day
    M5.Lcd.setTextSize(5);     // Set the font size.
    M5.Lcd.setCursor(40, 100);  // Set cursor
    M5.Lcd.println(&timeinfo, "%H:%M:%S");
    

    }

    // function to print a device address
    void printAddress(DeviceAddress deviceAddress)
    {
    for (uint8_t i = 0; i < 8; i++)
    {
    if (deviceAddress[i] < 16) M5.Lcd.print("0");
    M5.Lcd.print(deviceAddress[i], HEX);
    }
    }

    // function to print the temperature for a device
    void printTemperature(DeviceAddress deviceAddress)
    {
    M5.Lcd.setCursor(20, 170);
    M5.Lcd.setTextSize(3); // Set the font size.
    M5.Lcd.print("Temp C: ");
    M5.Lcd.print(sensors.getTempC(deviceAddress));
    }

    void setup() {
    M5.begin(); // Init M5Core.
    M5.Power.begin(); // Init power
    M5.Lcd.setTextSize(2); // Set the font size to 2

    // Therometer setup information    
    Serial.begin(9600);                   // start serial port
    M5.Lcd.print("Parasite power is: ");  // report parasite power requirements
    if (sensors.isParasitePowerMode()) M5.Lcd.print("ON");
    else M5.Lcd.print("OFF");;
    M5.Lcd.println();
    if (!sensors.getAddress(insideThermometer, 0)) M5.Lcd.println("Unable to find address for Device 0"); 
    M5.Lcd.print("Device 0 Address: ");// show the addresses we found on the bus
    printAddress(insideThermometer);
    M5.Lcd.println();
    sensors.setResolution(insideThermometer, 9);    // set the resolution to 9 bit
    M5.Lcd.print("Device 0 Resolution: ");
    M5.Lcd.print(sensors.getResolution(insideThermometer), DEC); 
    M5.Lcd.println();
    
    // Internet comunication information
    M5.Lcd.printf("\nConnecting to %s", ssid);
    WiFi.begin(ssid, password);  // Connect wifi and return connection status.
    while (WiFi.status() !=
           WL_CONNECTED) {  // If the wifi connection fails.
        delay(500);         // delay 0.5s.
        M5.Lcd.print(".");
    }
    M5.Lcd.println("\nCONNECTED!");
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);  // init and get the time.
    printLocalTime();
    WiFi.disconnect(true);  // Disconnect wifi.
    WiFi.mode(WIFI_OFF);    // Set the wifi mode to off.
    delay(20);
    
    // Clear all setup text
    M5.Lcd.clear();
    

    }

    void loop() {
    printLocalTime();
    sensors.requestTemperatures(); // Send the command to get temperatures
    printTemperature(insideThermometer);
    }



  • Hello @EricR

    your code runs fine (both time and temperature sensor together) for me.

    The only thing I've modified is increasing the timeout to 10 seconds for getting the time. (Default is 5 seconds.) E.g. getLocalTime(&timeinfo, 10000).

    How are you powering the temperature sensor? I don't use parasite power and feed it from the 3.3 V line.

    Have you tried to use a different GPIO for the temperature sensor?

    Thanks
    Felix



  • Hi @felmue and thanks for the help. I changed GPIO and it works now. Strange as input port 2 worked when the temperature sensor were implemented separably.

    However I have spend enough time trying to make it work. So I will not conplain now that it is working.

    Thanks again