// // 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 (4000) #define MY_ID (MY_GROUP_ID + 1) #define MY_SIGN ("ROLLER2") //======================== //======================== #define WIFI_CHANNEL 1 //======================= //arduino #include //network credentials char ssid[] = "KT_GiGA_D565"; // ssid char pass[] = "bhc1dd4971"; // password ////udp #include #include WiFiUDP Udp; // destination IP & Port // const IPAddress dest_ip(192,168,43,255); const IPAddress dest_ip(255,255,255,255); const unsigned int dest_port = 5555; // - Local broadcast address (255.255.255.255) // - Directed broadcast address (192.168.25.255 or 192.168.255.255 depends on your subnet) // difference between 'Directed broadcast' vs 'Local broadcast' // ==> https://serverfault.com/a/219767 // ==> https://www.sysnet.pe.kr/2/0/11368 // listening Port const unsigned int listening_port = 5555; // listening port //message types #include "message.h" //osc #include #include OSCErrorCode error; //task #include Scheduler runner; //-*-*-*-*-*-*-*-*-*-*-*-*- // servo #define MOTOR_1A (D6) #define MOTOR_1B (D5) // my tasks int speed = 0; bool isactive = false; void set_speed() { int r = speed; // if (r >= 0) { digitalWrite(MOTOR_1A, LOW); analogWrite(MOTOR_1B, r); } else { digitalWrite(MOTOR_1B, LOW); analogWrite(MOTOR_1A, r*(-1)); } Serial.print("set_speed:"); Serial.println(r); isactive = true; } Task set_speed_task(0, TASK_ONCE, &set_speed, &runner, false); // void rest() { analogWrite(MOTOR_1A, LOW); analogWrite(MOTOR_1B, LOW); isactive = false; } Task rest_task(0, TASK_ONCE, &rest, &runner, false); // uint8_t watch_counter = 0; void watcher() { if (isactive) { if (watch_counter > 3) { rest_task.restartDelayed(10); watch_counter = 0; } else { watch_counter++; } } } Task watcher_task(1000, TASK_FOREVER, &watcher, &runner, true); // #define MOTOR_2A (D3) #define MOTOR_2B (D2) // my tasks int speed2 = 0; bool isactive2 = false; void set_speed2() { int r = speed2; // if (r >= 0) { digitalWrite(MOTOR_2A, LOW); analogWrite(MOTOR_2B, r); } else { digitalWrite(MOTOR_2B, LOW); analogWrite(MOTOR_2A, r*(-1)); } Serial.print("set_speed2:"); Serial.println(r); isactive2 = true; } Task set_speed2_task(0, TASK_ONCE, &set_speed2, &runner, false); // void rest2() { analogWrite(MOTOR_2A, LOW); analogWrite(MOTOR_2B, LOW); isactive2 = false; } Task rest2_task(0, TASK_ONCE, &rest2, &runner, false); // uint8_t watch2_counter = 0; void watcher2() { if (isactive2) { if (watch2_counter > 3) { rest2_task.restartDelayed(10); watch2_counter = 0; } else { watch2_counter++; } } } Task watcher2_task(1000, TASK_FOREVER, &watcher2, &runner, true); //*-*-*-*-*-*-*-*-*-*-*-*-* // on 'Note' void onNoteHandler(Note & n) { Serial.println(n.to_string()); //is it for me? if (n.id == MY_GROUP_ID || n.id == MY_ID) { // if (n.pitch == 0) { speed = n.velocity; // if (n.onoff == 1) { set_speed_task.restartDelayed(10); watch_counter = 0; } else if (n.onoff == 0) { rest_task.restartDelayed(10); } else if (n.onoff == 2) { set_speed_task.restartDelayed(10); rest_task.restartDelayed(10 + n.x1); } } // else if (n.pitch == 1) { speed2 = n.velocity; // if (n.onoff == 1) { set_speed2_task.restartDelayed(10); watch2_counter = 0; } else if (n.onoff == 0) { rest2_task.restartDelayed(10); } else if (n.onoff == 2) { set_speed2_task.restartDelayed(10); rest2_task.restartDelayed(10 + n.x1); } } // } } //*-*-*-*-*-*-*-*-*-*-*-*-* //common 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. //common task #1 : listen on osc messages // - osc processing void route_note(OSCMessage& msg, int offset) { // Serial.println("got route_note!"); // matches will happen in the order. that the bundle is packed. static Note note; // (1) --> /onoff if (msg.fullMatch("/onoff", offset)) { // note.clear(); // note.onoff = msg.getFloat(0); // if (note.onoff != 0) note.onoff = 1; } // (2) --> /velocity if (msg.fullMatch("/velocity", offset)) { note.velocity = msg.getFloat(0); } // (3) --> /pitch if (msg.fullMatch("/pitch", offset)) { note.pitch = msg.getFloat(0); } // (4) --> /id if (msg.fullMatch("/id", offset)) { note.id = msg.getInt(0); } // (5) --> /x if (msg.fullMatch("/x", offset)) { note.x1 = msg.getFloat(0); note.x2 = msg.getFloat(1); note.x3 = msg.getFloat(2); note.x4 = msg.getFloat(3); note.ps = msg.getFloat(4); // onNoteHandler(note); // } } // - osc task extern Task osc_task; void osc() { //osc OSCBundle bundleIN; int size = Udp.parsePacket(); if (size > 0) { Serial.println(size); while (size--) { bundleIN.fill(Udp.read()); } if (!bundleIN.hasError()) { // on '/note' bundleIN.route("/note", route_note); } else { error = bundleIN.getError(); Serial.print("error: "); Serial.println(error); } } } Task osc_task(1, TASK_FOREVER, &osc, &runner, false); // -> ENABLED, at start-up. //*-*-*-*-*-*-*-*-*-*-*-*-* // void setup() { //led pinMode(LED_PIN, OUTPUT); //pwm freq. analogWriteFreq(40000); //serial Serial.begin(115200); delay(100); //wifi Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); Udp.begin(listening_port); Serial.print("Local port: "); Serial.println(Udp.localPort()); Udp.flush(); //start osc processing osc_task.restartDelayed(10); //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("-"); Serial.println("\".-.-.-. :)\""); Serial.println(); //random seed randomSeed(analogRead(0)); } void loop() { // runner.execute(); // }