🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    M5 Core Timer Interrupt doesnt work well.

    FAQS
    1
    1
    4.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ?
      A Former User
      last edited by ajb2k3

      This sketch works well as long as the PC is connected to the M5. The (Zahl) interrupt count up after every minute.
      When I leave the website, the counter stands and doesn't count anymore.

      I haven't found the cause yet.
      Somebody help me. Trying to solve the problem since days

      here is the sketch:


      #include <M5Stack.h>
      #include <WiFi.h>
      #include <WiFiUDP.h>
      #include <NTPClient.h>
      #include <Adafruit_Sensor.h>
      #include <Adafruit_BME280.h>

      #define SEALEVELPRESSURE_HPA (1013.25)

      int druck = 1013;
      bool flag = false;
      int Zahl = 0;
      int tdruck;
      const char* ssid = "xxxxxxxxx";
      const char* password = "yyyyyyyyyyy";
      unsigned long delayTime;
      float h, t, p;
      char temperatureCString[6];
      char humidityString[6];
      char pressureString[7];
      String progno;

      hw_timer_t * timer = NULL;
      portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

      WiFiUDP ntpUDP; //Zeitserver
      NTPClient timeClient(ntpUDP,"de.pool.ntp.org",7200,60000);
      //NTPClient timeClient(ntpUDP);

      Adafruit_BME280 bme;
      WiFiServer server(80);
      //-----------------------------------------Ab hier keine Netzwerkverbindungen und kein serial.print aufrufen-------------------------------------
      void ICACHE_RAM_ATTR getWeather() {
      h = bme.readHumidity();
      t = bme.readTemperature();
      p = bme.readPressure()/100.0;
      t = t - 2.3;
      p = p + 3.3;
      tdruck = int(p);
      dtostrf(t, 5, 1, temperatureCString);
      dtostrf(h, 5, 1, humidityString);
      dtostrf(p, 6, 1, pressureString);
      //stunde = timeClient.getHours();
      //delay(100); //Ursprünglich 100

      if(!flag){
      if (tdruck == druck){
         progno = "Konstant";
      }
      else if (tdruck > druck ){
        progno = "Steigend   ";
      }
      else { 
        progno = "Fallend";
       }
      

      druck = tdruck;
      flag = true;
      Zahl++;
      dis();

      }
      }
      //----------------------------------------In merker sowenig Anweisungen wie möglich--------------------------------------------------------------
      void ICACHE_RAM_ATTR merker(){
      flag = false;
      getWeather;
      }
      //-------------------------------------------------------------------------------------------------------------------------------------------------
      void dis(){
      M5.Lcd.clear();
      M5.Lcd.setTextColor(GREEN);
      M5.Lcd.setCursor(0, 0);
      M5.Lcd.setTextSize(2);
      M5.Lcd.println(progno);
      M5.Lcd.println();
      M5.Lcd.print(pressureString);
      M5.Lcd.print(" hPa ");
      M5.Lcd.println(Zahl);
      M5.Lcd.print(humidityString);
      M5.Lcd.println(" % rel. Luftfeuchte");
      M5.Lcd.print(temperatureCString);
      M5.Lcd.print(" \x60""Celsius" ); // "\x60
      }

      //-----------------------------------------Hilfsfunktionen---------------------------------------------------------------------------------------
      void Say(char x[]){
      Serial.println(x);
      }

      void Say(float x){
      Serial.println(x);
      }

      void Say(char x){
      Serial.println(x);
      }

      void Say(int x){
      Serial.println(x);
      }
      //--------------------------------------------------------------------------------------------------------------------------------------------------

      void setup() {
      M5.begin();
      timer = timerBegin(1, 80, true); // timer 0, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
      timerAttachInterrupt(timer, &merker, true); // edge (not level) triggered
      timerAlarmWrite(timer, 1000000 * 60, true); // 1000000 * 1 us = 1 s, autoreload true
      timerAlarmEnable(timer); // enable

      IPAddress ip(192, 168, 0, 5);
      IPAddress gateway(192, 168, 0, 1);
      IPAddress subnet(255, 255, 255, 0);
      IPAddress dns(192, 168, 0, 1);
      //IPAddress pdns(8,8,4,4); option

      WiFi.config(ip,gateway,subnet,dns);// WiFi.config(ip,gateway,subnet,dns,pdns);

      Serial.println(F("BME280 test"));
      bool status;
      status = bme.begin(0x76);

      // Connect to WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);

      WiFi.mode(WIFI_STA);

      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");

      // Start the server
      server.begin();
      Serial.println("Server started");
      delay(10);

      // Print the IP address
      Serial.println(WiFi.localIP());

      timeClient.begin(); // Zeitserver
      timeClient.update();
      Serial.println(timeClient.getFormattedTime());
      }

      void loop() {
      WiFiClient client = server.available();
      if (client) {
      Serial.println("New client");
      // delay(1000);
      // bolean to locate when the http request ends
      boolean blank_line = true;
      while (client.connected()) {
      if (client.available()) {
      char c = client.read();
      if (c == '\n' && blank_line) {
      timeClient.update(); //Zeitserver
      getWeather();
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("Connection: close");
      client.println();
      // your actual web page that displays temperature
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("<head><META HTTP-EQUIV="refresh" CONTENT="15"></head>");
      client.println("<body><h1>        Zimmertemperatur</h1>");
      client.println("<table border=10 bordercolor=#4844EC width= 40% cellpadding=10><tbody><tr bgcolor=#CCFFFF><td>");
      client.println("<h3>Zeit ");
      client.print(timeClient.getFormattedTime());
      client.println("<h3>");
      client.println("<h3>Temperatur  =  ");
      client.println(temperatureCString);
      client.println("°C</h3><h3>Luftfeuchte  = ");
      client.println(humidityString);
      client.println("%</h3>");
      client.println("<h3>Luftdruck     = ");
      client.println(pressureString);
      client.println("hPa<h3>");
      client.println("<h3>Tendenz  &nbsp     = ");
      client.println(progno);
      client.println(Zahl);
      client.println("</h3></td></tr></tbody></table></body></html>");
      break;
      }
      if (c == '\n') {
      // when starts reading a new line
      blank_line = true;
      }
      else if (c != '\r') {
      // when finds a character on the current line
      blank_line = false;
      }
      }
      }
      // closing the client connection
      delay(3);
      client.stop();
      // digitalWrite(14, LOW);
      Serial.println("Client disconnected.");
      dis();
      //M5.update();
      }

      }

      1 Reply Last reply Reply Quote 0
      • First post
        Last post