264 lines
7 KiB
C++
264 lines
7 KiB
C++
//
|
|
// wirelessly connected cloud (based on a LPWAN, called ESP-NOW)
|
|
//
|
|
|
|
//
|
|
// puredata gathering @ ururu.cloud, Seoul
|
|
//
|
|
|
|
//
|
|
// 2024 02 17
|
|
//
|
|
// this module will be an esp-now node in a group.
|
|
// like, a bird in a group of birds.
|
|
//
|
|
// esp-now @ esp8266 w/ broadcast address (FF:FF:FF:FF:FF:FF)
|
|
// always broadcasting. everyone is 'talkative'.
|
|
//
|
|
|
|
//============<identities>============
|
|
#define MY_GROUP_ID (7000)
|
|
#define MY_ID (MY_GROUP_ID + 101)
|
|
#define MY_SIGN ("PIANO")
|
|
//============</identities>============
|
|
|
|
//============<parameters>============
|
|
#define WIFI_CHANNEL 1
|
|
//============</parameters>===========
|
|
|
|
//arduino
|
|
#include <Arduino.h>
|
|
|
|
//message types
|
|
#include "message.h"
|
|
|
|
//espnow
|
|
#include <ESP8266WiFi.h>
|
|
#include <espnow.h>
|
|
|
|
//task
|
|
#include <TaskScheduler.h>
|
|
Scheduler runner;
|
|
|
|
//-*-*-*-*-*-*-*-*-*-*-*-*-
|
|
void port_read() {
|
|
//
|
|
byte port = 0;
|
|
static byte port_prev = 0;
|
|
port = 0x00;
|
|
port |= (digitalRead(D1) ? 0x01 : 0x00);
|
|
port |= (digitalRead(D2) ? 0x02 : 0x00);
|
|
port |= (digitalRead(D3) ? 0x04 : 0x00); // by design, PULLED-UP, being used for the FLASH button.
|
|
port |= (0 ? 0x08 : 0x00); // LED pin, skip
|
|
port |= (digitalRead(D5) ? 0x10 : 0x00);
|
|
port |= (digitalRead(D6) ? 0x20 : 0x00);
|
|
port |= (digitalRead(D7) ? 0x40 : 0x00);
|
|
port |= (digitalRead(D8) ? 0x80 : 0x00);
|
|
if (port_prev != port) {
|
|
//
|
|
byte mac[6];
|
|
WiFi.macAddress(mac);
|
|
uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5];
|
|
//
|
|
Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello'
|
|
// and you can append some floats
|
|
static int count = 0;
|
|
count++;
|
|
hello.h1 = (count % 1000);
|
|
hello.h2 = 1; // 1:digital
|
|
hello.h3 = port;
|
|
// hello.h4 = 0;
|
|
//
|
|
uint8_t frm_size = sizeof(Hello) + 2;
|
|
uint8_t frm[frm_size];
|
|
frm[0] = '{';
|
|
memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello));
|
|
frm[frm_size - 1] = '}';
|
|
//
|
|
esp_now_send(NULL, frm, frm_size); // to all peers in the list.
|
|
//
|
|
port_prev = port;
|
|
}
|
|
}
|
|
Task port_read_task(1, TASK_FOREVER, &port_read, &runner, true); // every 1 ms
|
|
//
|
|
void volume_read() {
|
|
//
|
|
int val = analogRead(A0);
|
|
//
|
|
byte mac[6];
|
|
WiFi.macAddress(mac);
|
|
uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5];
|
|
//
|
|
Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello'
|
|
// and you can append some floats
|
|
static int count = 0;
|
|
count++;
|
|
hello.h1 = (count % 1000);
|
|
hello.h2 = 2; //2: analog
|
|
hello.h3 = val;
|
|
// hello.h4 = 0;
|
|
//
|
|
uint8_t frm_size = sizeof(Hello) + 2;
|
|
uint8_t frm[frm_size];
|
|
frm[0] = '{';
|
|
memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello));
|
|
frm[frm_size - 1] = '}';
|
|
//
|
|
esp_now_send(NULL, frm, frm_size); // to all peers in the list.
|
|
//
|
|
}
|
|
Task volume_read_task(50, TASK_FOREVER, &volume_read, &runner, false); // every 50 ms
|
|
//*-*-*-*-*-*-*-*-*-*-*-*-*
|
|
|
|
//task #0 : blink led
|
|
#define LED_PERIOD (11111)
|
|
#define LED_ONTIME (1)
|
|
#define LED_GAPTIME (222)
|
|
#define LED_PIN 2
|
|
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.
|
|
|
|
// on 'Note'
|
|
void onNoteHandler(Note & n) {
|
|
//is it for me?
|
|
if (n.id == MY_GROUP_ID || n.id == MY_ID) {
|
|
//
|
|
if (n.velocity < 0) n.velocity = 0;
|
|
if (n.pitch < 0) n.pitch = 0;
|
|
//
|
|
// volume: task control
|
|
if (n.velocity == 0) {
|
|
//schedule 1 read
|
|
volume_read_task.disable();
|
|
volume_read_task.setIterations(1);
|
|
volume_read_task.restart();
|
|
} else {
|
|
// limiting max. speed.
|
|
if (n.velocity < 20) n.velocity = 20;
|
|
volume_read_task.setIterations(TASK_FOREVER);
|
|
volume_read_task.setInterval(n.velocity);
|
|
volume_read_task.restart();
|
|
}
|
|
//button: pullup on/off
|
|
if (n.onoff == 1) {
|
|
pinMode(n.pitch, INPUT_PULLUP);
|
|
} else {
|
|
pinMode(n.pitch, INPUT);
|
|
}
|
|
}
|
|
}
|
|
|
|
// on 'receive'
|
|
void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
|
|
|
|
//
|
|
Serial.write(incomingData, len);
|
|
|
|
// open => identify => use.
|
|
if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) {
|
|
Hello hello("");
|
|
memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello));
|
|
//
|
|
Serial.println(hello.to_string());
|
|
}
|
|
|
|
// open => identify => use.
|
|
if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) {
|
|
Note note;
|
|
memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note));
|
|
onNoteHandler(note);
|
|
//
|
|
Serial.println(note.to_string());
|
|
}
|
|
}
|
|
|
|
// on 'sent'
|
|
void onDataSent(uint8_t *mac, uint8_t sendStatus) {
|
|
char buff[256] = "";
|
|
sprintf(buff, "Delivery failed! -> %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
if (sendStatus != 0) Serial.println(buff);
|
|
}
|
|
|
|
//
|
|
void setup() {
|
|
|
|
//led
|
|
pinMode(LED_PIN, OUTPUT);
|
|
|
|
//input
|
|
pinMode(D1, INPUT);
|
|
pinMode(D2, INPUT);
|
|
pinMode(D3, INPUT);
|
|
//
|
|
pinMode(D5, INPUT);
|
|
pinMode(D6, INPUT);
|
|
pinMode(D7, INPUT);
|
|
pinMode(D8, INPUT);
|
|
|
|
//serial
|
|
Serial.begin(115200);
|
|
delay(100);
|
|
|
|
//info
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.println("\"hi, i m your postman.\"");
|
|
Serial.println("-");
|
|
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\"");
|
|
Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL));
|
|
Serial.println("-");
|
|
|
|
//wifi - disabled
|
|
system_phy_set_max_tpw(0);
|
|
WiFiMode_t node_type = WIFI_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);
|
|
//
|
|
Serial.println("- ! (esp_now_add_peer) ==> add a 'broadcast peer' (FF:FF:FF:FF:FF:FF).");
|
|
uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
|
|
|
|
Serial.println("-");
|
|
Serial.println("\".-.-.-. :)\"");
|
|
Serial.println();
|
|
}
|
|
|
|
void loop() {
|
|
//
|
|
runner.execute();
|
|
//
|
|
}
|