267 lines
7.2 KiB
C++
267 lines
7.2 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 (6000)
|
|
#define MY_ID (MY_GROUP_ID + 3)
|
|
#define MY_SIGN ("VOLUME")
|
|
//============</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;
|
|
|
|
//-*-*-*-*-*-*-*-*-*-*-*-*-
|
|
#include <Wire.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <Adafruit_HMC5883_U.h>
|
|
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
|
|
extern Task compass_read_task;
|
|
void compass_read() {
|
|
//
|
|
if (compass_read_task.isFirstIteration()) {
|
|
//wire
|
|
Wire.begin();
|
|
|
|
/* Initialise the sensor */
|
|
if(!mag.begin()) while(1);
|
|
|
|
/* Display some basic information on this sensor */
|
|
sensor_t sensor;
|
|
mag.getSensor(&sensor);
|
|
Serial.println("------------------------------------");
|
|
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
|
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
|
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
|
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
|
|
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
|
|
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
|
|
Serial.println("------------------------------------");
|
|
Serial.println("");
|
|
}
|
|
|
|
/* Get a new sensor event */
|
|
sensors_event_t event;
|
|
mag.getEvent(&event);
|
|
|
|
float heading = atan2(event.magnetic.y, event.magnetic.x);
|
|
|
|
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
|
|
// Find yours here: http://www.magnetic-declination.com/
|
|
// Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
|
|
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
|
|
float declinationAngle = 0.22;
|
|
heading += declinationAngle;
|
|
|
|
// Correct for when signs are reversed.
|
|
if(heading < 0)
|
|
heading += 2*PI;
|
|
|
|
// Check for wrap due to addition of declination.
|
|
if(heading > 2*PI)
|
|
heading -= 2*PI;
|
|
|
|
// Convert radians to degrees for readability.
|
|
float headingDegrees = heading * 180/M_PI;
|
|
|
|
//
|
|
float val = headingDegrees;
|
|
//
|
|
Serial.println(val);
|
|
//
|
|
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 = val;
|
|
// hello.h3 = 0;
|
|
// 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 compass_read_task(25, TASK_FOREVER, &compass_read, &runner, true); // every 1 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.pitch < 0) n.pitch = 0;
|
|
// volume_pin = n.pitch; //useless: for esp8266, A0 is only one adc.
|
|
//
|
|
if (n.velocity < 0) n.velocity = 0;
|
|
//
|
|
if (n.velocity == 0) {
|
|
//schedule 1 read
|
|
compass_read_task.disable();
|
|
compass_read_task.setIterations(1);
|
|
compass_read_task.restart();
|
|
} else {
|
|
// limiting max. speed.
|
|
if (n.velocity < 20) n.velocity = 20;
|
|
compass_read_task.setIterations(TASK_FOREVER);
|
|
compass_read_task.setInterval(n.velocity);
|
|
compass_read_task.restart();
|
|
}
|
|
//pull-up on/off
|
|
if (n.onoff == 1) {
|
|
// volume_pullup = true; //useless: for esp8266, A0 no support for internal pull-up ?
|
|
} else {
|
|
// volume_pullup = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
|
|
//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();
|
|
//
|
|
}
|