broadcast tx -> rx esp8266 tested. IT WORKS.

This commit is contained in:
Dooho Yi 2021-02-21 22:59:19 +09:00
parent 86432d1cc4
commit 355a033738
2 changed files with 167 additions and 0 deletions

43
broadcast/platformio.ini Normal file
View file

@ -0,0 +1,43 @@
; < NOTE >
; to enable verbose output add option -->
; $ platformio run --verbose
; to make this permanent for the proj. -->
; $ platformio settings set force_verbose Yes
; then confirm the change -->
; $ platformio settings get
; // pio v 4.0 'Build options'
; - build_type
; - build_flags
; - src_build_flags
; - build_unflags
; - src_filter
; - targets
[platformio]
default_envs = d1_mini_pro
[env]
framework = arduino
upload_port = /dev/ttyUSB0
lib_deps =
721 ; TaskScheduler
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
lib_deps =
${env.lib_deps}
upload_speed = 921600 ; 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
[env:d1_mini_pro]
platform = espressif8266
board = d1_mini_pro
lib_deps =
${env.lib_deps}
upload_speed = 460800 ; 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600

124
broadcast/src/main.cpp Normal file
View file

@ -0,0 +1,124 @@
//
// wirelessly connected cloud (based on ESP-NOW, a kind of LPWAN?)
//
//
// Conversation about the ROOT @ SEMA warehouses, Seoul
//
//
// 2021 02 21
//
// broadcast tx/rx test with esp8266
//============<parameters>============
//
#define LED_PERIOD (11111)
#define LED_ONTIME (1)
#define LED_GAPTIME (222)
//
#define WIFI_CHANNEL 5
//
//============</parameters>===========
//============<board-specifics>============
#if defined(ARDUINO_FEATHER_ESP32) // featheresp32
#define LED_PIN 13
#else
#define LED_PIN 2
#endif
//============</board-specifics>===========
//arduino
#include <Arduino.h>
//espnow
#include <ESP8266WiFi.h>
#include <espnow.h>
// on 'sent'
void onDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
if (sendStatus != 0) Serial.println("Delivery failed!");
}
// on 'receive'
void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
Serial.write(incomingData, len);
Serial.println();
}
//task
#include <TaskScheduler.h>
Scheduler runner;
//task #0 : blink led
extern Task blink_task;
void blink() {
//
static int count = 0;
count++;
//
switch (count % 4) {
case 0:
digitalWrite(LED_PIN, LOW); // first ON
blink_task.delay(LED_ONTIME);
break;
case 1:
digitalWrite(LED_PIN, HIGH); // first OFF
blink_task.delay(LED_GAPTIME);
break;
case 2:
digitalWrite(LED_PIN, LOW); // second ON
blink_task.delay(LED_ONTIME);
break;
case 3:
digitalWrite(LED_PIN, HIGH); // second OFF
blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME);
break;
}
}
Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up.
//task #1 : regular tx
void espnow_transmit() {
//
String hello = "hello. from ... " + String(WiFi.macAddress());
esp_now_send(NULL, (uint8_t *) hello.c_str(), hello.length());
//
}
Task espnow_transmit_task(1000, TASK_FOREVER, &espnow_transmit, &runner, true); // by default, ENABLED
//
void setup() {
//led
pinMode(LED_PIN, OUTPUT);
//serial
Serial.begin(115200);
delay(100);
//wifi
WiFiMode_t node_type = WIFI_AP_STA;
WiFi.mode(node_type);
//esp-now
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(onDataSent);
esp_now_register_recv_cb(onDataReceive);
//
uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
}
void loop() {
//
runner.execute();
//
}