Got an odd one.
WeMos D1 R1.
The first sketch is a test to determine the best LED flash patterns to allow me to test two different events from across a room (or farther). I used the same includes as the EspNowSlave.ino, and structured the LED code in the same way.
This test works fine. The LED flashes as it should.
#include <ESP8266WiFi.h>
extern "C" {
#include <espnow.h>
}
byte ledPin = 14;
void setup() {
pinMode(ledPin, OUTPUT);
ledOff();
}
void loop() {
loopLED();
}
void loopLED() {
ledOn();
delay (300);
ledOff();
delay(2000);
ledOn();
delay (100);
ledOff();
delay (75);
ledOn();
delay (100);
ledOff();
delay(2000);
}
void ledOn() {
digitalWrite(ledPin,HIGH);
}
void ledOff() {
digitalWrite(ledPin,LOW);
}
This sketch is using, as a basis, Robin2's excellent code sample (EspNowSlave.ino) in this thread for an ESP-Now implementation for the ESP8266.
In this one, the LED does not flash when it is called from receiveCallBackFunction(). Other than that, the prgram does what it should, receiving the strings "SIP" and "PUFF". I have two Serial.println() lines to tell me when I am in the 'if' code.
// EspnowSlave.ino
// a minimal program derived from
// https://github.com/HarringayMakerSpace/ESP-Now
// This is the program that receives the data. (The Slave)
// COM5
//=============
#include <ESP8266WiFi.h>
#include <Arduino.h>
extern "C" {
#include <espnow.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]);
}
//==============
// For ESP8266
#define WIFI_CHANNEL 4
// must match the controller struct
struct __attribute__((packed)) DataStruct {
char text[4];
unsigned int time;
};
DataStruct myData;
char sip[4] = {'S','I','P','\0x0'};
char puf[4] = {'P','U','F','\0x0'};
// For LED and Relax Actuator
byte ledPin = 14;
//============
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");
Serial.print("LED pin: ");
Serial.print(ledPin);
pinMode (ledPin, OUTPUT);
ledOff();
}
//============
void loop() {
}
//============
void receiveCallBackFunction(uint8_t *senderMac, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.println();
Serial.println(myData.text);
if ( memcmp (myData.text, sip,sizeof(myData.text) == 0)) {
Serial.println("In if SIP");
ledOn();
delay (300);
ledOff();
}
if ( memcmp (myData.text, puf, sizeof(myData.text))== 0) {
Serial.println("In if PUF");
ledOn();
delay (100);
ledOff();
delay (75);
ledOn();
delay (100);
ledOff();
}
}
void ledOn() {
digitalWrite(ledPin,HIGH);
}
void ledOff() {
digitalWrite(ledPin,LOW);
}
Is it because it is in that function? Do I need to write code to run in loop() for it to work?