Chat freely about anything...

User avatar
By ozric13
#83541 Hi All

Hope I`m in the right place to post here.

OK my dilemma, so I have a WEMOS D1 Mini used a remote Temperature and Humidity sensor, sending data to my Lolin Node MCU using ESPNOW receiver, this works great.

On the receiver I can view all the received data, all great, what I want to do is then send the data to Blynk so I can view the information anywhere in the world.

I been pulling my hair out for the last 2 days trying to figure out how to do this.

I tried setting up 2 voids , void init esp and void init blynk, so the sketch would do its normal thing and collect the received data, at this point it would check to see if data has been received, if yes would then disconnect ESPNOW set the wifi back to normal, connect to Blynk, send the data then go back to ESPNOW mode.

The script seems to work as I print everything that is going on, however it never receives the ESP data, so sends nothing.

The second scenerio, is I just have the Blynk code following the received messages, I receive the data but its never sent to Blynk.

If anyone can take a quick look at my code it would be really apprciated.

Cheers Alan

Code: Select all#include <ESP8266WiFi.h>
#include <Wire.h>
#include <BlynkSimpleEsp8266.h>

extern "C" {
#include <espnow.h>
#include "user_interface.h"
}
#define WIFI_CHANNEL 0

uint8_t mac[] = {0xBC, 0xDD, 0xC2, 0x24, 0x15, 0xEF};

struct __attribute__((packed)) SENSOR_DATA {
  float temp;
  float humidity;

} sensorData;

int blynk = 0;

void setup() {

  Serial.begin(115200);

  WiFi.mode(WIFI_AP);
  wifi_set_macaddr(SOFTAP_IF, &mac[0]);
  Serial.print("This node AP mac: "); Serial.println(WiFi.softAPmacAddress());
  Serial.print("This node STA mac: "); Serial.println(WiFi.macAddress());
  initEspNow();
  Serial.println("Setup done");
}

int heartBeat;

void loop() {
  if (millis() - heartBeat > 30000) {
    Serial.println("Waiting for ESP-NOW messages...");
    heartBeat = millis();
  }

  if (blynk = 1); {
    blynkConnect();

    delay(200);
    ESP.restart();

  }
}

void initEspNow() {

  Serial.print("Starting ESP");
  delay(5000);

  if (esp_now_init() != 0) {
    Serial.println("*** ESP_Now init failed");
    ESP.restart();
  }
  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  esp_now_register_recv_cb([](uint8_t *mac, uint8_t *data, uint8_t len) {

    char macStr[18];
    snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    Serial.print("Sensor Station MAC address is: ");  Serial.println(macStr);
    Serial.print("Data set length is: ");  Serial.println(len);
    Serial.println();
    memcpy(&sensorData, data, sizeof(sensorData));
    Serial.print("Temperature is: ");  Serial.println(sensorData.temp);
    Serial.print("Humidity is: ");  Serial.println(sensorData.humidity);

    Serial.println();
    delay(1000);

    blynk = 1;

  });
}
void blynkConnect() {
  Serial.print("Starting Blynk");
  delay(5000);

  WiFi.mode(WIFI_STA);
  Serial.println();
  Serial.print("Connecting to Blynk Server");
  Blynk.begin("mfgrtdteteSyfxgP4MhH8LspiSTyGY", "GG616", "spyroiD!!");

  Blynk.virtualWrite(0, 17);
  Blynk.virtualWrite(1, 35);
  delay(5000);
  Blynk.disconnect();

  blynk = 0;
}


Code: Select all#include <ESP8266WiFi.h>
#include <Wire.h>
#include <BlynkSimpleEsp8266.h>

extern "C" {
#include <espnow.h>
#include "user_interface.h"
}
#define WIFI_CHANNEL 0

uint8_t mac[] = {0xBC, 0xDD, 0xC2, 0x24, 0x15, 0xEF};

struct __attribute__((packed)) SENSOR_DATA {
  float temp;
  float humidity;
 
} sensorData;

int blynk = 0;

void setup() {

  Serial.begin(115200);
  WiFi.mode(WIFI_AP);
  wifi_set_macaddr(SOFTAP_IF, &mac[0]);
 
  Serial.print("This node AP mac: "); Serial.println(WiFi.softAPmacAddress());
  Serial.print("This node STA mac: "); Serial.println(WiFi.macAddress());

  initEspNow();
}

int heartBeat;

void loop() {
  if (millis() - heartBeat > 30000) {
    Serial.println("Waiting for ESP-NOW messages...");
    heartBeat = millis();
  }
  ESP.wdtFeed();
}

void initEspNow() {
  if (esp_now_init() != 0) {
    Serial.println("*** ESP_Now init failed");
    ESP.restart();
  }
  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  esp_now_register_recv_cb([](uint8_t *mac, uint8_t *data, uint8_t len) {

    char macStr[18];
    snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    Serial.print("Sensor Station MAC address is: ");  Serial.println(macStr);
    Serial.print("Data set length is: ");  Serial.println(len);
    Serial.println();
    memcpy(&sensorData, data, sizeof(sensorData));
    Serial.print("Temperature is: ");  Serial.println(sensorData.temp);
    Serial.print("Humidity is: ");  Serial.println(sensorData.humidity);
    Serial.print("Pressure is: ");  Serial.println(sensorData.pressure);
    Serial.println();
    delay(1000);

    blynk = 1;
    if (blynk = 1) {
      Serial.print("Starting Blynk");
  delay(5000);

  WiFi.mode(WIFI_STA);
  Serial.println();
  Serial.print("Connecting to Blynk Server");
  Blynk.begin("gtrtsret40LZ_eSyfxgP4MhH8LspiSTyGY", "GG616", "spyroiD!!");
  Blynk.virtualWrite(0, sensorData.temp);
  Blynk.virtualWrite(1, sensorData.humidity);
  delay(5000);
  Blynk.disconnect();

  blynk = 0;
    }
  });

}