Skip to content

Instantly share code, notes, and snippets.

@bytespider
Created December 5, 2017 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytespider/88b92f545d005ec5dc3ead64f4b6dc64 to your computer and use it in GitHub Desktop.
Save bytespider/88b92f545d005ec5dc3ead64f4b6dc64 to your computer and use it in GitHub Desktop.
ESP NOW
/**
*
* Author: Andreas Spiess, 2017
* Author: Rob Griffiths
*
This sketch receives ESP-Now message and sends it as an MQTT messge
It is heavily based on of Anthony's gateway setch sketch
https://github.com/HarringayMakerSpace/ESP-Now
Anthony Elder
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Credentials.h>
extern "C" {
#include <espnow.h>
}
// keep in sync with ESP_NOW sensor struct
struct __attribute__((packed)) SENSOR_DATA {
char testdata[240];
} sensorData;
WiFiClient wifiClient;
PubSubClient mqttClient;
int heartBeat;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println("ESP_Now Controller");
Serial.println();
Serial.print("This node AP mac: "); Serial.println(WiFi.softAPmacAddress());
Serial.print("This node STA mac: "); Serial.println(WiFi.macAddress());
setupEspNow();
setupWifi();
setupMQTT();
Serial.println("Setup done");
}
void loop() {
if (millis()-heartBeat > 30000) {
Serial.println("Waiting for ESP-NOW messages...");
heartBeat = millis();
}
if(!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.loop();
}
void setupEspNow() {
Serial.println();
Serial.print("Initializing ESP Now: ");
if (esp_now_init() != 0) {
Serial.println("failed");
Serial.println("Restarting...");
ESP.restart();
}
Serial.println("success");
esp_now_set_self_role(ESP_NOW_ROLE_MAX);
esp_now_register_recv_cb([](uint8_t *mac, uint8_t *data, uint8_t len) {
String deviceMac = "";
deviceMac += String(mac[0], HEX);
deviceMac += String(mac[1], HEX);
deviceMac += String(mac[2], HEX);
deviceMac += String(mac[3], HEX);
deviceMac += String(mac[4], HEX);
deviceMac += String(mac[5], HEX);
memcpy(&sensorData, data, sizeof(sensorData));
Serial.print("Message received from device: "); Serial.print(deviceMac);
Serial.println(sensorData.testdata);
Serial.println();
publishToMQTT("ESPNow", String(sensorData.testdata));
});
}
void setupWifi() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, passphrase);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setupMQTT() {
mqttClient.setClient(wifiClient);
mqttClient.setServer(mqttServer, 1883);
// mqttClient.setCallback(callback);
}
void reconnectMQTT() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
if (!mqttClient.connect("Sensor Gateway")) {
Serial.print("failed, rc = ");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
Serial.println("connected");
}
void publishToMQTT(String topic, String message) {
Serial.println("Publish");
if (!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.publish(topic.c_str(), message.c_str());
}
/*
* Author: Andreas Spiess, 2017
* Author: Rob Griffiths
*
This sketch measures the time to send a ESP-Now message. It is a strip down of Anthony's sensor sketch
https://github.com/HarringayMakerSpace/ESP-Now
Anthony Elder
*/
#include <ESP8266WiFi.h>
extern "C" {
#include <espnow.h>
}
//#include "SparkFunBME280.h"
// this is the MAC Address of the remote ESP server which receives these sensor readings
uint8_t remoteMac[] = {0x1A, 0xFE, 0x34, 0xE0, 0xC4, 0x7E};
#define WIFI_CHANNEL 1
#define SLEEP_SECS 5 // 15 minutes
#define SEND_TIMEOUT 245 // 245 millis seconds timeout
#define MESSAGELEN 10
int heartBeat;
unsigned long entry;
// keep in sync with slave struct
struct __attribute__((packed)) SENSOR_DATA {
char testdata[MESSAGELEN];
} sensorData;
//BME280 bme280;
volatile boolean callbackCalled;
unsigned long entry1 = millis();
void setup() {
int i = 0;
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println("ESP_Now Controller");
Serial.println();
WiFi.mode(WIFI_STA); // Station mode for esp-now sensor node
WiFi.disconnect();
Serial.printf("This mac: %s, ", WiFi.macAddress().c_str());
Serial.printf("target 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");
ESP.restart();
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
Serial.println(millis() - entry1);
unsigned long entry2 = millis();
esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
Serial.println(millis() - entry2);
unsigned long entry3 = millis();
esp_now_register_send_cb([](uint8_t* mac, uint8_t sendStatus) {
Serial.printf("send_cb, send done, status = %i\n", sendStatus);
callbackCalled = true;
});
Serial.println(millis() - entry3);
unsigned long entry4 = millis();
callbackCalled = false;
for (i = 0; i < MESSAGELEN; i++) sensorData.testdata[i] = '0';
sensorData.testdata[MESSAGELEN] = '\0';
}
void loop() {
if (millis()-heartBeat > 2000) { // send every 2 seconds
uint8_t bs[sizeof(sensorData)];
memcpy(bs, &sensorData, sizeof(sensorData));
unsigned long entry = millis();
esp_now_send(NULL, bs, sizeof(sensorData)); // NULL means send to all peers
Serial.print("Time to send: ");
Serial.println(millis() - entry);
Serial.print("Overall Time: ");
Serial.println(millis() - entry1);
heartBeat = millis();
}
}
@alexands12
Copy link

What changes should I make in the code for ESP32?

@adrianmihalko
Copy link

Hi, what is the difference between this and the original version? Isn't therea working bme280 version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment