Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display electronic power consumption meter value via ESPNow
// Display electronic power consumption meter value recieved by
// M5StickC Wi-SUN Hat ( https://kitto-yakudatsu.com/archives/7206 )
// via ESPNow
#include <TM1630LED.h>
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 6 // Use Same Wi-Fi channel as M5StickC+Wi-SUN Hat uses.
uint8_t DIOPin = 26; // For ESP32 on Arduino baord. Might differ for each Arduino boards.
uint8_t CLKPin = 25; // For ESP32 on Arduino baord. Might differ for each Arduino boards.
uint8_t STBPin = 17; // For ESP32 on Arduino baord. Might differ for each Arduino boards.
TM1630LED TM1630(DIOPin, CLKPin, STBPin);
void InitESPNow() {
WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
void configDeviceAP() {
char* SSID = "ESPDisp";
bool result = WiFi.softAP(SSID, "ESPDisp_pass", CHANNEL, 0);
if (!result) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
}
}
void setup() {
Serial.begin(115200);
Serial.println("ESPNow Display for Power Consumption Meter");
// TM1630 initialize
TM1630.Begin();
TM1630.Init();
// ESPNow initialize
WiFi.mode(WIFI_AP); //Set device in AP mode to begin with
configDeviceAP(); // configure device AP mode
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress()); // This is the mac address of the Slave in AP Mode
InitESPNow(); // Init ESPNow with a fallback logic
esp_now_register_recv_cb(OnDataRecv); // Once ESPNow is successfully Init, we will register for recv CB to get recv packer info.
}
String power;
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
// Log recived data to serial console
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("Last Packet Recv from: "); Serial.println(macStr);
Serial.print("Last Packet Recv Data: ");
int i;
char datastr[3];
for(i=0; i<data_len; i++) {
snprintf(datastr, sizeof(datastr), "%02x", data[i]);
Serial.print(datastr);
}
char *datStr;
datStr = (char *)malloc(data_len +1 );
memcpy(datStr, data, data_len);
datStr[data_len] = 0;
Serial.println("");
Serial.print("Last Packet Recv String: ");
Serial.print(datStr+10); // The actual string begin on 11th byte.
Serial.println("");
// Display data
power = String(datStr+14); // Power consumption value begin on 15th byte
TM1630.Disp(power.toInt());
free(datStr);
}
void loop() {
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.