sensebox / React-Ardublockly

This repository contains the new senseBox learn- and programming environment powered by google Blockly and React
Apache License 2.0
2 stars 6 forks source link

[Blockly][Codegenerator] Add ESP Now #275

Closed mariopesch closed 3 months ago

mariopesch commented 5 months ago

ESP now is a nice feature to communicate directly between two or more devices.

PaulaScharf commented 4 months ago

some minimal sketches:

  1. finding mac address The sender needs to know the mac address of the receiver.
    
    #include "WiFi.h"

void setup(){ Serial.begin(115200); WiFi.mode(WIFI_MODE_STA); Serial.println(WiFi.macAddress()); }

2. Sender

include

include

uint8_t broadcastAddress[] = {0x84, 0xF7, 0x03, 0xD1, 0x08, 0xA8};

esp_now_peer_info_t peerInfo;

void setup() { WiFi.mode(WIFI_STA); if (esp_now_init() != ESP_OK) { return; } memcpy(peerInfo.peer_addr, broadcastAddress, 6); peerInfo.channel = 0;
peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK){ return; } }

void loop() { esp_err_t result = esp_now_send(broadcastAddress, (const uint8_t*)"test", sizeof("test")); delay(2000); }

3. Receiver

include

include

void OnDataRecv(const uint8_t mac, const uint8_t incomingData, int len) { Serial.println((const char*)incomingData); }

void setup() { Serial.begin(115200);

WiFi.mode(WIFI_STA); if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } esp_now_register_recv_cb(OnDataRecv); }

void loop() {}