Komunikasi ESPNOW dari ESP32 ke ESP8266 dan sebaliknya, ternyata memungkinkan lho, berikut kode dan video pembahasannya.
Cek Mac Address ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WiFi.h" | |
void setup(){ | |
Serial.begin(115200); | |
WiFi.mode(WIFI_MODE_STA); | |
Serial.println(WiFi.macAddress()); | |
} | |
void loop(){ | |
} |
ESP8266 Sebagai Pengirim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <espnow.h> | |
// mac address tujuan | |
uint8_t mac_addr_tujuan[] = {0xA4, 0xCF, 0x12, 0x05, 0xD5, 0x98}; | |
// struktur data yang diterima | |
typedef struct struct_message { | |
char a[32]; | |
int b; | |
float c; | |
bool d; | |
} struct_message; | |
struct_message data_ku; | |
unsigned long waktu_sebelum, interval=2000; | |
// callback Jika data terkirim | |
void OnDataSent(uint8_t *mac_addr, uint8_t _status) { | |
Serial.print("Status Pengiriman: "); | |
if (_status == 0){ | |
Serial.println("Sukses"); | |
} | |
else{ | |
Serial.println("Gagal"); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
// mengatur esp ke mode station | |
WiFi.mode(WIFI_STA); | |
// inisialisasi espnow | |
if (esp_now_init() != 0) { | |
Serial.println("Gagal Inisialisasi espnow"); | |
return; | |
} | |
// mendaftarkan fungsi callback | |
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); | |
esp_now_register_send_cb(OnDataSent); | |
// Menambahkan peer | |
esp_now_add_peer(mac_addr_tujuan, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); | |
} | |
void loop() { | |
if ((millis() - waktu_sebelum) > interval) { | |
// memberi nilai string | |
strcpy(data_ku.a, "Dari ESP8266"); | |
data_ku.b = random(1, 10); | |
data_ku.c = 3.14; | |
data_ku.d = false; | |
// mengirim data | |
esp_now_send(mac_addr_tujuan, (uint8_t *) &data_ku, sizeof(data_ku)); | |
// update waktu_sebelum | |
waktu_sebelum = millis(); | |
} | |
} |
ESP32 Sebagai Penerima
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <esp_now.h> | |
// struktur data yang diterima | |
typedef struct struct_message { | |
char a[32]; | |
int b; | |
float c; | |
bool d; | |
} struct_message; | |
struct_message data_ku; | |
// callback jika menerima data | |
void cb_terima(const uint8_t *mac_addr, const uint8_t *dataDiterima, int panjang) { | |
memcpy(&data_ku, dataDiterima, sizeof(data_ku)); | |
Serial.print("Bytes diterima: "); | |
Serial.println(panjang); | |
Serial.print("Char: "); | |
Serial.println(data_ku.a); | |
Serial.print("Int: "); | |
Serial.println(data_ku.b); | |
Serial.print("Float: "); | |
Serial.println(data_ku.c); | |
Serial.print("Bool: "); | |
Serial.println(data_ku.d); | |
Serial.println(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
// mengatur esp ke mode station | |
WiFi.mode(WIFI_STA); | |
// inisialisasi espnow | |
if (esp_now_init() != ESP_OK) { | |
Serial.println("Gagal Inisialisasi espnow"); | |
return; | |
} | |
// mendaftarkan fungsi callback | |
esp_now_register_recv_cb(cb_terima); | |
} | |
void loop() { | |
} |
Cek Mac Address ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
void setup(){ | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.print("ESP8266 Board MAC Address: "); | |
Serial.println(WiFi.macAddress()); | |
} | |
void loop(){ | |
} |
ESP32 Sebagai Pengirim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <esp_now.h> | |
// mac address tujuan | |
uint8_t mac_addr_tujuan[] = {0x84, 0x0D, 0x8E, 0xAA, 0x8C, 0x63}; | |
// struktur data yang diterima | |
typedef struct struct_message { | |
char a[32]; | |
int b; | |
float c; | |
bool d; | |
} struct_message; | |
struct_message data_ku; | |
// variabel untuk jeda kirim | |
unsigned long waktu_sebelum, interval=2000; | |
// variabel peerInfo | |
esp_now_peer_info_t peerInfo; | |
// callback Jika data terkirim | |
void cb_terkirim(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
Serial.print("\r\nStatus Pengiriman: "); | |
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Sukses" : "Gagal"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
// mengatur esp ke mode station | |
WiFi.mode(WIFI_STA); | |
// inisialisasi espnow | |
if (esp_now_init() != ESP_OK) { | |
Serial.println("Gagal Inisialisasi espnow"); | |
return; | |
} | |
// mendaftarkan fungsi callback | |
esp_now_register_send_cb(cb_terkirim); | |
// Mendata Peer / Kawan | |
memcpy(peerInfo.peer_addr, mac_addr_tujuan, 6); | |
peerInfo.channel = 0; | |
peerInfo.encrypt = false; | |
// Menambahkan Peer / Kawan | |
if (esp_now_add_peer(&peerInfo) != ESP_OK){ | |
Serial.println("Gagal Menambah Peer"); | |
return; | |
} | |
} | |
void loop() { | |
if ((millis() - waktu_sebelum) > interval) { | |
// mengisi nilai ke variabel | |
strcpy(data_ku.a, "Dari ESP32"); | |
data_ku.b = random(1, 10); | |
data_ku.c = 3.14; | |
data_ku.d = true; | |
// Mengirim Data | |
esp_now_send(mac_addr_tujuan, (uint8_t *) &data_ku, sizeof(data_ku)); | |
// update waktu_sebelum | |
waktu_sebelum = millis(); | |
} | |
} |
ESP8266 Sebagai Penerima
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <espnow.h> | |
// struktur data yang diterima | |
typedef struct struct_message { | |
char a[32]; | |
int b; | |
float c; | |
bool d; | |
} struct_message; | |
struct_message data_ku; | |
// callback jika menerima data | |
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) { | |
memcpy(&data_ku, incomingData, sizeof(data_ku)); | |
Serial.print("Bytes Diterima: "); | |
Serial.println(len); | |
Serial.print("Char: "); | |
Serial.println(data_ku.a); | |
Serial.print("Int: "); | |
Serial.println(data_ku.b); | |
Serial.print("Float: "); | |
Serial.println(data_ku.c); | |
Serial.print("Bool: "); | |
Serial.println(data_ku.d); | |
Serial.println(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
// mengatur esp ke mode station | |
WiFi.mode(WIFI_STA); | |
// inisialisasi espnow | |
if (esp_now_init() != 0) { | |
Serial.println("Gagal Inisialisasi espnow"); | |
return; | |
} | |
// mendaftarkan fungsi callback | |
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); | |
esp_now_register_recv_cb(OnDataRecv); | |
} | |
void loop() { | |
} |
Video
Kerjasama Robotik ID
https://linktr.ee/robotikidYoutube: https://www.youtube.com/robotikid
Instagram: https://www.instagram.com/robotikid/
Facebook: https://www.facebook.com/RobotikID/
Website: https://www.robotikindonesia.com/
Tokopedia: http://tokopedia.com/instrumentrobot
0 Comments