From 355a0337386bac2c35dc0ef1bcec9868731db3b1 Mon Sep 17 00:00:00 2001 From: Dooho Yi Date: Sun, 21 Feb 2021 22:59:19 +0900 Subject: [PATCH] broadcast tx -> rx esp8266 tested. IT WORKS. --- broadcast/platformio.ini | 43 ++++++++++++++ broadcast/src/main.cpp | 124 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 broadcast/platformio.ini create mode 100644 broadcast/src/main.cpp diff --git a/broadcast/platformio.ini b/broadcast/platformio.ini new file mode 100644 index 0000000..bfecb36 --- /dev/null +++ b/broadcast/platformio.ini @@ -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 diff --git a/broadcast/src/main.cpp b/broadcast/src/main.cpp new file mode 100644 index 0000000..eee4593 --- /dev/null +++ b/broadcast/src/main.cpp @@ -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 + +//======================== +// +#define LED_PERIOD (11111) +#define LED_ONTIME (1) +#define LED_GAPTIME (222) +// +#define WIFI_CHANNEL 5 +// +//======================= + +//======================== +#if defined(ARDUINO_FEATHER_ESP32) // featheresp32 +#define LED_PIN 13 +#else +#define LED_PIN 2 +#endif +//======================= + +//arduino +#include + +//espnow +#include +#include + +// 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 +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(); + // +}