// // 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'. // //======================== #define MY_GROUP_ID (2000) #define MY_ID (MY_GROUP_ID + 1) #define MY_SIGN ("BUTTON") //======================== //======================== #define WIFI_CHANNEL 1 //======================= //arduino #include //message types #include "message.h" //espnow #include #include //task #include Scheduler runner; //-*-*-*-*-*-*-*-*-*-*-*-*- int button_pin = D5; bool button_pullup = false; void button_read() { // if (button_pullup) pinMode(button_pin, INPUT_PULLUP); else pinMode(button_pin, INPUT); // static int last = 0; int val = digitalRead(button_pin); if (val != last) { // 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. // last = val; } } Task button_read_task(1, TASK_FOREVER, &button_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.velocity < 0) n.velocity = 0; // if (n.pitch < 0) n.pitch = 0; button_pin = n.pitch; //button_pullup if (n.onoff == 1) { button_pullup = true; } else { button_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(); // }