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

    esp8266 to m5stack wifi communication

    PROJECTS
    1
    1
    3.7k
    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, Im looking for a way to port my "espnow esp8266 to esp8266" project to m5stack
      I'm using arduino core
      I need to send a sensor value from esp8266 to m5stuck as faster as possible

      this is my sender code: (sorry I can't find how to insert code)

      // EspnowController.ino

      // a minimal program derived from
      // https://github.com/HarringayMakerSpace/ESP-Now

      // This is the program that sends the data. (The Controller)

      //=============

      #include <ESP8266WiFi.h>
      extern "C" {
      #include <espnow.h>
      }

      // this is the MAC Address of the slave which receives the data
      uint8_t remoteMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33}; //se cambia il MAC ADDRESS ricordarsi di cambiare anche il serial print per poter clonare il modulo

      #define WIFI_CHANNEL 4 // se cambia il canale ricordarsi di cambiare anche il serial print per poter clonare il modulo

      // must match the slave struct
      struct attribute((packed)) DataStruct {
      char text[32];
      unsigned long time;
      };

      DataStruct myData;

      unsigned long lastSentMillis;
      unsigned long sendIntervalMillis = 10;
      unsigned long sentMicros;
      unsigned long ackMicros;

      unsigned long lastBlinkMillis;
      unsigned long fastBlinkMillis = 200;
      unsigned long slowBlinkMillis = 700;
      unsigned long blinkIntervalMillis = slowBlinkMillis;

      byte ledPin = 14;
      /////////////////////////////////////////////////////////
      #include <NewPingESP8266.h>
      #define TRIGGER_PIN D5 // 14 Arduino pin tied to trigger pin on the ultrasonic sensor.
      #define ECHO_PIN D6 // 12 Arduino pin tied to echo pin on the ultrasonic sensor.
      //#define MAX_DISTANCE 350 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

      //NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPingESP8266 setup of pins and maximum distance.
      NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN); // NewPingESP8266 setup of pins and maximum distance.
      long echoTime;
      ////////////////////////////////////////////////////////

      //==============

      void setup() {
      Serial.begin(115200); Serial.println();
      Serial.println("Starting EspnowController.ino");

      WiFi.mode(WIFI_STA); // Station mode for esp-now controller
      WiFi.disconnect();

      Serial.printf("This mac: %s, ", WiFi.macAddress().c_str());
      Serial.printf("slave mac: %02x%02x%02x%02x%02x%02x", remoteMac[0], remoteMac[1], remoteMac[2], remoteMac[3], remoteMac[4], remoteMac[5]);

      Serial.printf(", channel: %i\n", WIFI_CHANNEL);

      if (esp_now_init() != 0) {
      Serial.println("*** ESP_Now init failed");
      while (true) {};
      }
      esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
      esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);

      esp_now_register_send_cb(sendCallBackFunction);

      strcpy(myData.text, "WIFI CH 4");
      //strcpy(myData.text, "CRISTO !!!");
      Serial.print("WIFI_CHANNEL "); Serial.println(myData.text);

      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);

      Serial.println("Setup finished");

      }

      //==============

      void loop() {
      echoTime = (sonar.ping_median(10));
      sendData();
      blinkLed();
      }

      //==============

      void sendData() {
      if (millis() - lastSentMillis >= sendIntervalMillis) {
      lastSentMillis += sendIntervalMillis;
      //myData.time = millis();
      myData.time = echoTime;
      uint8_t bs[sizeof(myData)];
      memcpy(bs, &myData, sizeof(myData));
      sentMicros = micros();
      esp_now_send(NULL, bs, sizeof(myData)); // NULL means send to all peers
      Serial.println("sent data");
      }
      }

      //==============

      void sendCallBackFunction(uint8_t* mac, uint8_t sendStatus) {
      ackMicros = micros();
      Serial.print("Trip micros "); Serial.println(ackMicros - sentMicros);
      Serial.printf("Send status = %i", sendStatus);
      Serial.println();
      Serial.println();

      Serial.println("MAC ADD 0x36, 0x33, 0x33, 0x33, 0x33, 0x33"); //RICORDARSI DI CAMBIARLO SE CAMBIA NEL SETUP
      Serial.println("WIFI CHANNEL 4"); //RICORDARSI DI CAMBIARLO SE CAMBIA NEL SETUP
      Serial.println();
      Serial.println();
      if (sendStatus == 0) {
      blinkIntervalMillis = fastBlinkMillis;
      }
      else {
      blinkIntervalMillis = slowBlinkMillis;
      }
      }

      //================

      void blinkLed() {
      if (millis() - lastBlinkMillis >= blinkIntervalMillis) {
      lastBlinkMillis += blinkIntervalMillis;
      digitalWrite(ledPin, ! digitalRead(ledPin));
      }
      }

      and this is my rx code:

      // a minimal program derived from
      // https://github.com/HarringayMakerSpace/ESP-Now

      // This is the program that receives the data. (The Slave)

      //=============

      #include <ESP8266WiFi.h>
      extern "C" {
      #include <espnow.h>
      #include <user_interface.h>
      }

      // it seems that the mac address needs to be set before setup() is called
      // and the inclusion of user_interface.h facilitates that
      // presumably there is a hidden call to the function initVariant()

      /* Set a private Mac Address
      http://serverfault.com/questions/40712/what-range-of-mac-addresses-can-i-safely-use-for-my-virtual-machines
      Note: by setting a specific MAC you can replace this slave ESP8266 device with a new one
      and the new slave will still pick up the data from controllers which use that MAC
      */
      uint8_t mac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};

      //==============

      void initVariant() {
      WiFi.mode(WIFI_AP);
      wifi_set_macaddr(SOFTAP_IF, &mac[0]);
      }

      //==============

      #define WIFI_CHANNEL 4

      // must match the controller struct
      struct attribute((packed)) DataStruct {
      char text[32];
      unsigned int time;
      };

      DataStruct myData;

      //============

      void setup() {
      Serial.begin(115200); Serial.println();
      Serial.println("Starting EspnowSlave.ino");

      Serial.print("This node AP mac: "); Serial.println(WiFi.softAPmacAddress());
      Serial.print("This node STA mac: "); Serial.println(WiFi.macAddress());

      if (esp_now_init() != 0) {
      Serial.println("*** ESP_Now init failed");
      while (true) {};
      }

      esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);

      esp_now_register_recv_cb(receiveCallBackFunction);

      Serial.println("End of setup - waiting for messages");
      }

      //============

      void loop() {

      }

      //============

      void receiveCallBackFunction(uint8_t *senderMac, uint8_t *incomingData, uint8_t len) {
      memcpy(&myData, incomingData, sizeof(myData));
      int cm = myData.time / 29 / 2;
      int inc = myData.time / 74 / 2;
      Serial.print("NewMsg ");
      Serial.print("MacAddr ");
      for (byte n = 0; n < 6; n++) {
      Serial.print (senderMac[n], HEX);
      }
      Serial.print(" MsgLen ");
      Serial.print(len);
      Serial.print(" Text ");
      Serial.print(myData.text);
      Serial.print(" Time ");
      Serial.print(myData.time);
      Serial.print(" CM = ");
      Serial.print(cm);
      Serial.print(" IN = ");
      Serial.print(inc);
      Serial.println();
      }
      every tips apreciated ...

      tanks a lot

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