Receive data from an ESP8266 slave and send it through FTP protocol

My project is to get data from different sensors from a slave ESP8266 and to send it to a master ESP8266. Then, the master will send these data to a server via a FTP protocol. I can do both separately but I have problems with the master when I have to switch from a position of receiver to a sender.

In details, for the slave, I use the classical protocol:

#include <ESP8266WiFi.h>    // set device as a Wifi station
#include <espnow.h>         // For communication with the other device

// MAC Address of your receiver (Master)
uint8_t broadcastAddress[] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX};

#include <DHT.h>
#define dht_dpin D5
// Set DHT type, uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 22  (AM2302)
// Initialize DHT sensor for normal 16mhz Arduino:
DHT dht = DHT(dht_dpin, DHTTYPE);

// Weather station
float hum = 0.0;
float temp = 0.0;

// Send data every...
long interval = 30000;         // 30 sec

// Structure example to send data
// Must match the receiver structure
typedef struct struct_data {
  float hum, temp;
} struct_data;

struct_data myData;

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail: Status = " + String(sendStatus));
  }
}

void setup()
{
  //Use Serial as debugging serial port 
  Serial.begin(9600);

  //Use software serial port mySerial as communication serial port 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);

  // Setup sensor:
  dht.begin();

}


void loop()
{
    if ((millis() - lastTime) > interval) {

      hum = dht.readHumidity();
      temp = dht.readTemperature();

    // Send message via ESP-NOW
    esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
    
    lastTime = millis();
  }
}

Normally, to transfer data through the FTP protocol, I use the modified code from SurferTim's FTP code (http://playground.arduino.cc/Code/FTP). But if I do so, it doesn't work.

To receive data from my slave, I have this in the setup of my master:

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);

And:

// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Temp: ");
  Serial.println(myData.temp);
  Serial.print("Humid: ");
  Serial.println(myData.hum);
}

It does display the values but only if I reset the Wifi with:

  WiFi.persistent(false);
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);

Then, to transfer data by FTP, I need to do this:

  WiFi.begin (ssid, pwd );
  while (WiFi.status() != WL_CONNECTED) {
    ...

By doing this, it doesn't connect and the Wifi.status returns 6.

What should I do to switch from a receiver position which will wait for data from my slave to a sender where I can send data through the FTP protocol? And then come back to the receiver position.

Thank you in advance

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum