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

    (SOLVED) M5Atom crash when I add M5.dis.drawpix()

    Arduino
    2
    3
    525
    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.
    • C
      cepics
      last edited by cepics

      Hi All,
      I'm working on a espnow communication between two M5Atom
      the communication works well but when I try to use the built in led, the tx atom crash...

      this is the working sender sketch (without led):

      
      #include "M5Atom.h"
      #include <Preferences.h>
      Preferences preferences;
      
      #include "Leddar.h"
      
      #include <Arduino.h>
      #include <esp_now.h>
      #include <esp_wifi.h>
      
      byte FilmPlate = 20;
      short ToRs232;
      short cm1;
      short cm2;
      short cm3;
      short cm4;
      short cm5;
      short cm6;
      short cm7;
      short cm8;
      
      float Amp1;
      float Amp2;
      float Amp3;
      float Amp4;
      float Amp5;
      float Amp6;
      float Amp7;
      float Amp8;
      
      ///////////////// OTA /////////////////////
      #include <WiFi.h>
      #include <WiFiClient.h>
      #include <WebServer.h>
      #include <ESPmDNS.h>
      #include <Update.h>
      const char *host = "xx";
      const char *ssid = "xxx";
      const char *password = "xxxxx";
      WebServer server(80);
      ///////////////// fine OTA /////////////////////
      
      bool StatoRX;
      bool StatoPAIR = 0;
      
      // //////////////////// ESPNOW /////////////////////////
      // Set your Board and Server ID
      #define BOARD_ID 1
      #define MAX_CHANNEL 13  // for North America // 13 in Europe
      uint8_t serverAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
      
      typedef struct struct_message2 {
        uint8_t msgType;
        uint8_t id;
        uint8_t macAddr[6];
        short ToRs232;
        bool unit;
      } struct_message2;
      
      typedef struct struct_message {
        uint8_t msgType;
        uint8_t id;
        // uint8_t macAddr[6];
        byte FilmPlate;
        short ToRs232;
        short cm1;
        short cm2;
        short cm3;
        short cm4;
        short cm5;
        short cm6;
        short cm7;
        short cm8;
        float Amp1;
        float Amp2;
        float Amp3;
        float Amp4;
        float Amp5;
        float Amp6;
        float Amp7;
        float Amp8;
      } struct_message;
      
      typedef struct struct_pairing {  // new structure for pairing
        uint8_t msgType;
        uint8_t id;
        uint8_t macAddr[6];
        uint8_t channel;
      } struct_pairing;
      
      //Create 2 struct_message
      struct_message myData;   // data to send
      struct_message2 inData;  // data received
      struct_pairing pairingData;
      
      enum PairingStatus { NOT_PAIRED,
                           PAIR_REQUEST,
                           PAIR_REQUESTED,
                           PAIR_PAIRED,
      };
      PairingStatus pairingStatus = NOT_PAIRED;
      
      enum MessageType { PAIRING,
                         DATA,
      };
      MessageType messageType;
      
      #ifdef SAVE_CHANNEL
      int lastChannel;
      #endif
      int channel = 1;
      
      
      unsigned long currentMillis = millis();
      unsigned long previousMillis = 0;  // Stores last time temperature was published
      const long interval = 10000;       // Interval at which to publish sensor readings
      unsigned long start;               // used to measure Pairing time
      unsigned int readingId = 0;
      
      void addPeer(const uint8_t *mac_addr, uint8_t chan) {
        esp_now_peer_info_t peer;
        ESP_ERROR_CHECK(esp_wifi_set_channel(chan, WIFI_SECOND_CHAN_NONE));
        esp_now_del_peer(mac_addr);
        memset(&peer, 0, sizeof(esp_now_peer_info_t));
        peer.channel = chan;
        peer.encrypt = false;
        memcpy(peer.peer_addr, mac_addr, sizeof(uint8_t[6]));
        if (esp_now_add_peer(&peer) != ESP_OK) {
          Serial.println("Failed to add peer");
          return;
        }
        memcpy(serverAddress, mac_addr, sizeof(uint8_t[6]));
      }
      
      void printMAC(const uint8_t *mac_addr) {
        char macStr[18];
        snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
                 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
        Serial.print(macStr);
      }
      
      void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
        // Serial.print("\r\nLast Packet Send Status:\t");
        // Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
        if (status == ESP_NOW_SEND_SUCCESS) {
          // M5.dis.drawpix(0, 0x00ff00);  // GREEN  绿色
          StatoRX = 1;
        } else {
          // M5.dis.drawpix(0, 0xff0000);  // RED  红色
          StatoRX = 0;
        }
      }
      
      void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len) {
        // Serial.print("Packet received from: ");
        // printMAC(mac_addr);
        // Serial.println();
        // Serial.print("data size = ");
        // Serial.println(sizeof(incomingData));
        uint8_t type = incomingData[0];
        switch (type) {
          case DATA:  // we received data from server
            memcpy(&inData, incomingData, sizeof(inData));
      
            ToRs232 = inData.ToRs232;
      
            break;
      
          case PAIRING:  // we received pairing data from server
            memcpy(&pairingData, incomingData, sizeof(pairingData));
            if (pairingData.id == 0) {  // the message comes from server
              printMAC(mac_addr);
              Serial.print("Pairing done for ");
              printMAC(pairingData.macAddr);
              Serial.print(" on channel ");
              Serial.print(pairingData.channel);  // channel used by the server
              Serial.print(" in ");
              Serial.print(millis() - start);
              Serial.println("ms");
              addPeer(pairingData.macAddr, pairingData.channel);  // add the server  to the peer list
      #ifdef SAVE_CHANNEL
              preferences.begin("C-Wheels", false);
              preferences.putUInt("lastChannel", lastChannel);  // Store to the Preferences
              preferences.end();                                // Close the Preferences
      
      #endif
              pairingStatus = PAIR_PAIRED;  // set the pairing status
            }
            break;
        }
      }
      
      PairingStatus autoPairing() {
        switch (pairingStatus) {
          case PAIR_REQUEST:
            Serial.print("Pairing request on channel ");
            Serial.println(channel);
            StatoPAIR = 1;  // probabilmente si puo togliere
            // set WiFi channel
            ESP_ERROR_CHECK(esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE));
            if (esp_now_init() != ESP_OK) {
              Serial.println("Error initializing ESP-NOW");
            }
      
            // set callback routines
            esp_now_register_send_cb(OnDataSent);
            esp_now_register_recv_cb(OnDataRecv);
      
            // set pairing data to send to the server
            pairingData.msgType = PAIRING;
            pairingData.id = BOARD_ID;
            pairingData.channel = channel;
      
            // add peer and send request
            addPeer(serverAddress, channel);
            esp_now_send(serverAddress, (uint8_t *)&pairingData, sizeof(pairingData));
            previousMillis = millis();
            pairingStatus = PAIR_REQUESTED;
            break;
      
          case PAIR_REQUESTED:
            // time out to allow receiving response from server
            currentMillis = millis();
            if (currentMillis - previousMillis > 250) {
              previousMillis = currentMillis;
              // time out expired,  try next channel
              channel++;
              if (channel > MAX_CHANNEL) {
                channel = 1;
              }
              pairingStatus = PAIR_REQUEST;
            }
            break;
      
          case PAIR_PAIRED:
            StatoPAIR = 0;
            // nothing to do here
            break;
        }
        return pairingStatus;
      }
      ///////////////////////////FINE ESPNOW///////////////
      
      byte CH;
      byte CHold;
      
      bool aggiornaCH;
      
      LeddarVu8 Leddar(115200, 1);  //Baudrate = 115200 Modbus slave ID = 01
      
      void setup() {
      
        M5.begin();
        // M5.begin(false, false, true);
        Serial.begin(115200);
        Serial1.begin(9600, SERIAL_8N1, 22, 19);  // atom RS232 MOD
        delay(1000);
      
        M5.update();
      
        Serial.println();
        Serial.print("Client Board MAC Address:  ");
        Serial.println(WiFi.macAddress());
        WiFi.mode(WIFI_STA);
        WiFi.disconnect();
        start = millis();
      
      #ifdef SAVE_CHANNEL
        preferences.begin("xxxx", false);
        lastChannel = preferences.getUInt("lastChannel", 0);
        preferences.end();
      
        Serial.println(lastChannel);
        if (lastChannel >= 1 && lastChannel <= MAX_CHANNEL) {
          channel = lastChannel;
        }
        Serial.println(channel);
      #endif
        pairingStatus = PAIR_REQUEST;
      
        Serial1.begin(9600, SERIAL_8N1, 23, 19);  //ATOM_RS232
      
        Leddar.init();
      
        // M5.dis.drawpix(0, 0xfff000);  // YELLOW 黄色
      
        M5.update();
      }
      
      void loop() {
      
        char result = Leddar.getDetections();
      
        if (result >= 0) {
          for (int i = 0; i < Leddar.NbDet; i++) {
            if (Leddar.Detections[i].Segment + 1 == 1) {
              myData.cm1 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp1 = Leddar.Detections[i].Amplitude;
            }
            if (Leddar.Detections[i].Segment + 1 == 2) {
              myData.cm2 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp2 = Leddar.Detections[i].Amplitude;
            }
            if (Leddar.Detections[i].Segment + 1 == 3) {
              myData.cm3 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp3 = Leddar.Detections[i].Amplitude;
            }
            if (Leddar.Detections[i].Segment + 1 == 4) {
              myData.cm4 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp4 = Leddar.Detections[i].Amplitude;
      
              if (M5.Btn.isPressed()) {  //ATOM
                FilmPlate = 100 - Leddar.Detections[i].Distance;
              }
            }
            if (Leddar.Detections[i].Segment + 1 == 5) {
              myData.cm5 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp5 = Leddar.Detections[i].Amplitude;
            }
            if (Leddar.Detections[i].Segment + 1 == 6) {
              myData.cm6 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp6 = Leddar.Detections[i].Amplitude;
            }
            if (Leddar.Detections[i].Segment + 1 == 7) {
              myData.cm7 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp7 = Leddar.Detections[i].Amplitude;
            }
            if (Leddar.Detections[i].Segment + 1 == 8) {
              myData.cm8 = Leddar.Detections[i].Distance + FilmPlate;
              myData.Amp8 = Leddar.Detections[i].Amplitude;
            }
      
            myData.FilmPlate = FilmPlate;
      
          }
        } else {
          Serial.print("Vu8 Error: ");
          Serial.print((int)result);
          Serial.print("\n");
        }
      
        sendData();
      
        // if (pairingStatus == PAIR_PAIRED) {
        //   M5.dis.drawpix(0, 0x00ff00);
        // }
      
        //   M5.dis.drawpix(0, 0x00ff00);
        M5.update();
      
        delay(20);  //crasha se la tolgo
      }
      
      void sendData() {
        if (autoPairing() == PAIR_PAIRED) {
          myData.msgType = DATA;
          myData.id = BOARD_ID;
          esp_err_t result = esp_now_send(serverAddress, (uint8_t *)&myData, sizeof(myData));
        }
      }
      

      when I add M5.dis.drawpix(0, 0x00ff00);

      the Atom crash..

      this is the serial monitor output:

      M5At
      Client Board MAC Address:  D8:A0:1D:5C:95:0C
      Pairing request on channel 1
      
      assert failed: xQueueSemaphoreTake queue.c:1545 (( pxQueue ))
      
      
      Backtrace: 0x400843b1:0x3ffb2060 0x4008cb85:0x3ffb2080 0x400921c5:0x3ffb20a0 0x4008db95:0x3ffb21d0 0x400d4430:0x3ffb2210 0x400d311f:0x3ffb2240 0x400d3459:0x3ffb2270 0x400dc9ed:0x3ffb2290
      
      
      
      
      ELF file SHA256: 75047b7540bb511b
      
      Rebooting...
      

      any idea??

      felmueF 1 Reply Last reply Reply Quote 0
      • felmueF
        felmue @cepics
        last edited by felmue

        Hello @cepics

        I think you need to enable LED use first by setting the last parameter in M5.begin() to true. See here.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        C 1 Reply Last reply Reply Quote 0
        • C
          cepics @felmue
          last edited by

          @felmue You are right!!!

          I thought that calling M5.begin() without any parameters would set all parameters to true..

          thanks a lot

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