board.on('ready', function () { board.pinMode(13,board.MODES.OUTPUT); board.loop(1000, function () { board.digitalWrite(13,board.pins[13].value ? 0 : 1); }); });
socket.on("sample", (c) => { //First, check if board is currently ready if(boardStatus().ready) { //Sets pinModes and activate them board.pinMode(redPin, five.Pin.PWM); board.pinMode(greenPin, five.Pin.PWM); board.pinMode(bluePin, five.Pin.PWM); board.analogWrite(redPin, c.red); board.analogWrite(greenPin, c.green); board.analogWrite(bluePin, c.blue); var data = c.red + "," + c.green + "," + c.blue + ","; var buffer = Buffer.from(data); var byteArray = Array.from(buffer); board.io.serialWrite(portId, byteArray); } });
connection.on('message', function (message) { var pin; // the pin number contained within a message var index; // an index for the devices array var deviceID; // the device identifier contained within a message if (debugLevel >= 1) { console.log('message received: ' + message.utf8Data); } // Messages components are delimited by '/'. // A message is split into its components and placed in msg array. // The message ID is always the first element // TODO: explore using JSON instead of current delimitation scheme var msg = message.utf8Data.split('/'); // Process each message type received switch (msg[0]) { // handshake with client at start up of socket case 'Xi4sOnline': if (debugLevel >= 1) { console.log('Xi Server has a websocket to Xi4S'); } break; /********************************************************************************************** ********************* PIN MODE COMMAND HANDLERS ***************************************** **********************************************************************************************/ // set pin mode to analog in // create an an instance of an analog sensor and add it to the array case 'setAnalogIN': // get the pin number and add the 'analog' designator pin = msg[3]; if (debugLevel >= 1) { console.log("setAnalogIn", msg); } if (validateAnalogSetMode(msg[1], pin) === false) { break; } // specific to XiDuino - needs to see the 'A' designator if (serverType === 'ard') { pin = 'A' + pin; } index = devices.length; deviceID = msg[1]; addSensor(index, pin, deviceID); break; // set pin mode to digital in // create a switch object to handle a digital input pin and add it to the array case 'setDigitalIN': pin = msg[2]; if (validateDigitalSetMode(msg[1], pin, five.Pin.INPUT) === false) { break; } index = devices.length; deviceID = msg[3]; if (debugLevel >= 2) { console.log("adding switch", pin, deviceID); } addSwitch(index, pin, deviceID); break; // set up pin as PWM - we do not create device objects for output pins, but access them directly case 'setAnalogOUT': pin = msg[2]; // PWM is considered here to be a digital mode if (validateDigitalSetMode(msg[1], pin, five.Pin.PWM) !== false) { board.pinMode(pin, five.Pin.PWM); } break; // set up pin as digital output // we do not create device objects for output pins, but access them directly case 'setDigitalOUT': pin = msg[2]; // just set the pin (msg[2], to the output mode. if (validateDigitalSetMode(msg[1], pin, five.Pin.OUTPUT) !== false) { board.pinMode(pin, five.Pin.OUTPUT); } break; // This will set a pin to servo mode and add a standard servo device case 'setStandardServoMode': pin = msg[2]; if (debugLevel >= 3) { console.log('setServoMode current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (validateDigitalSetMode(msg[1], pin, five.Pin.SERVO) !== false) { board.pinMode(pin, five.Pin.SERVO); if (debugLevel >= 2) { console.log("adding servo", pin); } addServo(pin, 'standard', false); } break; // This will set a pin to Servo and add a continuous servo device case 'setContinuousServoMode': inverted = false; pin = msg[2]; if (debugLevel >= 3) { console.log('setServoMode current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (validateDigitalSetMode(msg[1], pin, five.Pin.SERVO) !== false) { board.pinMode(pin, five.Pin.SERVO); if (debugLevel >= 2) { console.log("adding servo", pin); } addServo(pin, 'continuous'); } break; // Set a pin for HC-SR04 type support and a ping device // Requires a replacement sketch for StandardFirmata // https://github.com/rwaldron/johnny-five/wiki/Ping#setup case 'setSonarMode': deviceID = msg[3]; pin = msg[2]; if (debugLevel >= 1) { console.log("setSonarMode", msg); } if (validateDigitalSetMode(msg[1], pin, five.Pin.INPUT) === false) { break; } index = devices.length; addPing(index, pin, deviceID); break; // set a pin for infrared distance sensing and add the device case 'setInfraRedDistanceMode': deviceID = msg[3]; pin = msg[2]; if (debugLevel >= 1) { console.log("setInfraRedDistanceMode", msg); } if (validateAnalogSetMode(msg[1], pin, five.Pin.ANALOG) === false) { break; } index = devices.length; addInfraRed(index, pin, deviceID); break; // set a pin to piezo tone mode and add the device case 'setToneMode': pin = msg[2]; if (validateDigitalSetMode(msg[1], pin, five.Pin.OUTPUT) !== false) { addPiezo(pin); } break; // The stepper motor support requires a replacement sketch for StandardFirmata // https://github.com/soundanalogous/AdvancedFirmata // set the pins for a 4 wire stepper and a 4 wire stepper device case 'fourWireStepperPins': var pinA = parseInt(msg[2]); var pinB = parseInt(msg[3]); var pinC = parseInt(msg[4]); var pinD = parseInt(msg[5]); var revSteps = parseInt(msg[6]); if (debugLevel >= 1) { console.log("fourWireStepperPin", msg); } if (validateDigitalSetMode(msg[1], pinA, five.Pin.OUTPUT) === false) { break; } if (validateDigitalSetMode(msg[1], pinB, five.Pin.OUTPUT) === false) { break; } if (validateDigitalSetMode(msg[1], pinC, five.Pin.OUTPUT) === false) { break; } if (validateDigitalSetMode(msg[1], pinD, five.Pin.OUTPUT) === false) { break; } addFourWireStepper(pinA, pinB, pinC, pinD, revSteps); break; // The stepper motor support requires a replacement sketch for StandardFirmata // https://github.com/soundanalogous/AdvancedFirmata // set the pins for a stepper driver board and a driver stepper device case 'stepperDriverPins': if (debugLevel >= 1) { console.log("stepperDriverPins", msg); } pinA = parseInt(msg[2]); pinB = parseInt(msg[3]); revSteps = parseInt(msg[4]); if (validateDigitalSetMode(msg[1], pinA, five.Pin.OUTPUT) === false) { break; } if (validateDigitalSetMode(msg[1], pinB, five.Pin.OUTPUT) === false) { break; } addDriverStepper(pinA, pinB, revSteps); break; /********************************************************************************************** ********************* COMMAND BLOCK HANDLERS ********************************** **********************************************************************************************/ // write out the digital value to the pin case 'digitalWrite': if (debugLevel >= 3) { console.log('digitalWrite: board ' + msg[1] + 'pin' + msg[2] + ' value ' + msg[3]); } // validate that this pin was initially set to correct mode if (board.io.pins[msg[2]].mode !== five.Pin.OUTPUT) { connection.send('invalidPinCommand/' + 2 + '/' + msg[1] + '/' + msg[2]); } else { if (msg[3] === 'Off') { if (debugLevel >= 3) { console.log(' pin off'); } board.digitalWrite(msg[2], 0); } else { if (debugLevel >= 3) { console.log(' pin on'); } board.digitalWrite(msg[2], 1); } } break; // write out the analog value to the PWM pin case 'analogWrite': pin = msg[2]; if (debugLevel >= 3) { console.log('analogWrite current mode === ' + board.io.pins[msg[2]].mode); console.log('pin = ' + pin); console.log(msg); } if (board.io.pins[msg[2]].mode !== five.Pin.PWM) { // send alert string connection.send('invalidPinCommand/' + 3 + '/' + msg[1] + '/' + pin); } else { board.analogWrite(msg[2], msg[3]); } break; // play a tone case 'playTone': pin = msg[2]; var frequency = msg[3]; var duration = msg[4]; if (debugLevel >= 3) { console.log('tone pin current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (board.io.pins[pin].mode !== five.Pin.OUTPUT) { // send alert string connection.send('invalidPinCommand/' + 4 + '/' + msg[1] + '/' + pin); break; } else { // retrieve piezo from array for (i = 0; i < piezoArray.length; i++) { if (piezoArray[i].pin === pin) { piezoArray[i].frequency(frequency, duration); return; } } if (debugLevel >= 3) { console.log('playTone - piezo not found for pin ' + pin); } } break; case 'noTone': pin = msg[2]; if (debugLevel >= 3) { console.log('notone for pin' + pin); } for (i = 0; i < piezoArray.length; i++) { if (piezoArray[i].pin === pin) { piezoArray[i].noTone(); return; } } if (debugLevel >= 3) { console.log('noTone - piezo not found for pin ' + pin); } break; // move the servo to position in degrees // message indices: 1= board, 2 = pin, 3 = degrees case 'moveStandardServo': pin = parseInt(msg[2]); var degrees = parseInt(msg[3]); var inverted = msg[4]; if (debugLevel >= 3) { console.log('servo current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (board.io.pins[pin].mode !== five.Pin.SERVO) { // send alert string connection.send('invalidPinCommand/' + 5 + '/' + msg[1] + '/' + pin); break; } else { // retrieve servo from array for (var i = 0; i < servoArray.length; i++) { if (servoArray[i].pin === pin) { if (servoArray[i].type !== "standard") { connection.send('invalidPinCommand/' + 6 + '/' + msg[1] + '/' + pin); return; } servoArray[i].isInverted = inverted !== 'False'; servoArray[i].to(degrees); return; } } if (debugLevel >= 3) { console.log('moveStandardServo - servo not found for pin ' + pin); } } break; // move a continuous servo in the given direction and speed case 'moveContinuousServo': pin = parseInt(msg[2]); var direction = msg[3]; inverted = msg[4]; var speed = parseFloat(msg[5]); speed = parseFloat(speed.toFixed(2)); if (debugLevel >= 3) { console.log('servo current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (board.io.pins[pin].mode !== five.Pin.SERVO) { // send alert string connection.send('invalidPinCommand/' + 5 + '/' + msg[1] + '/' + pin); break; } else { // retrieve servo from array for (i = 0; i < servoArray.length; i++) { if (servoArray[i].pin === pin) { if (servoArray[i].type !== "continuous") { connection.send('invalidPinCommand/' + 7 + '/' + msg[1] + '/' + pin); return; } if (speed >= 0.0 && speed <= 1.0) { servoArray[i].isInverted = inverted !== 'False'; if (direction === 'CW') { servoArray[i].cw(speed); } else { servoArray[i].ccw(speed); } return; } else { connection.send('invalidSetMode/Board ' + msg[1] + ' Pin ' + pin + ' Speed must be in the range of 0.0 to 1.0'); } return; } } if (debugLevel >= 3) { console.log('moveContinuousServo - servo not found for pin ' + pin); } } break; // stop servo motion - used for both standard and continuous case 'stopServo': pin = parseInt(msg[2]); if (debugLevel >= 3) { console.log('stopServo servo current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (board.io.pins[pin].mode !== five.Pin.SERVO) { // send alert string connection.send('invalidPinCommand/' + 5 + '/' + msg[1] + '/' + pin); break; } else { // retrieve servo from array for (i = 0; i < servoArray.length; i++) { if (servoArray[i].pin === pin) { servoArray[i].stop(); return; } } if (debugLevel >= 3) { console.log('stopServo - servo not found for pin ' + pin); } } break; // move a stepper motor - this works for both 4 wire and driver. case 'moveStepper': pin = parseInt(msg[2]); var rpms = parseInt(msg[3]); var dir = msg[4]; var acc = parseInt(msg[5]); var dec = parseInt(msg[6]); var stp = parseInt(msg[7]); if (debugLevel >= 3) { console.log('moveStepper mode '); console.log('pin = ' + pin); console.log(msg); } if (dir === 'CW') { dir = five.Stepper.DIRECTION.CW; } else { dir = five.Stepper.DIRECTION.CCW; } if (board.io.pins[pin].mode !== board.io.MODES.STEPPER) { // send alert string connection.send('invalidPinCommand/' + 8 + '/' + msg[1] + '/' + pin); break; } else { // retrieve servo from array for (i = 0; i < stepperArray.length; i++) { if (stepperArray[i].pins.step === pin) { stepperArray[i].step({ steps: stp, rpm: rpms, direction: dir, accel: acc, decel: dec }, function () { console.log("Done stepping!"); }); return; } } if (debugLevel >= 3) { console.log('moveStepper - stepper not found for pin ' + pin); } } break; // sets number of steps to 0 to stop stepper movement case 'stopStepper': pin = parseInt(msg[2]); if (debugLevel >= 3) { console.log('stopSStepper servo current mode === ' + board.io.pins[pin].mode); console.log('pin = ' + pin); console.log(msg); } if (board.io.pins[pin].mode !== board.io.MODES.STEPPER) { // send alert string connection.send('invalidPinCommand/' + 8 + '/' + msg[1] + '/' + pin); break; } else { // retrieve servo from array for (i = 0; i < stepperArray.length; i++) { if (stepperArray[i].pins.step === pin) { stepperArray[i].step({ steps: 0, direction: five.Stepper.DIRECTION.CW }, function () { console.log("stopped stepper pin " + pin); }); return; } } if (debugLevel >= 3) { console.log('stopStepper - stepper not found for pin ' + pin); } } break; case 'resetBoard': // reset the board if (debugLevel >= 2) { console.log('Client is shutting down'); } boardReset(); break; default: console.log('Xi Server unknown message received: ' + msg[0]); } });
board.on("ready", function() { // When the board is ready // Create Led object on pin 13 var led = new five.Led(13); //board.pinMode( 8, 1 ); board.pinMode( 8, five.Pin.PWM ); /** * Morse * * @description :: get an action (0, 1 or 2) and control the LED * */ function morse(action) { switch(action) { case 0: process.stdout.write(" "); break; case 1: process.stdout.write("."); led.on(); board.analogWrite(8, BUZZ_FREQ); setTimeout(function(){ led.off(); board.analogWrite(8, 0); }, SPEED); break; case 2: process.stdout.write("-"); led.on(); board.analogWrite(8, BUZZ_FREQ); setTimeout(function(){ led.off(); board.analogWrite(8, 0);}, SPEED*5); break; } } /** * tweetToMorse * * @description :: Querry the server, returns an array of actions or false, process the array * * EXAMPLE JSON RECEIVED * { * "hashtag":"benjamin", * "tweet":"Wednesdays are my Fridays #Benjamin \ud83d\ude0d", * "morse":".-- . -.. -. . ... -.. .- -.-- ... .- .-. . -- -.-- ..-. .-. .. -.. .- -.-- ... -... . -. .--- .- -- .. -. .-.-.", * "chars":[".--",".","-..","-.",".","...","-..",".-","-.--","..."," ",".-",".-.","."," ","--","-.--"," ","..-.",".-.","..","-..",".-","-.--","..."," ","-...",".","-.",".---",".-","--","..","-."," ",".-.-."], * "digits":[1,2,2,1,2,1,1,2,1,1,1,1,1,2,1,1,1,2,2,1,2,2,1,1,1,0,0,0,1,2,1,2,1,1,0,0,0,2,2,2,1,2,2,0,0,0,1,1,2,1,1,2,1,1,1,2,1,1,1,2,2,1,2,2,1,1,1,0,0,0,2,1,1,1,1,2,1,1,2,2,2,1,2,2,2,1,1,2,1,0,0,0,1,2,1,2,1] * } * */ function tweetToMorse() { console.log("Query the server"); request({ uri: server_url+"/api/"+HASHTAG, method: "GET", json: {'':''}, }, function(error, response, body) { console.log("STATUS Code: "+response.statusCode); if(!error && response.statusCode == 200) { // Response from the server console.log("RESPONSE Tweet: "+body.tweet); console.log("MORSE: "); var actions = body.digits; i = 0; // Process the array of actions function actions_loop() { // For each action, control the LED morse(actions[i]); setTimeout(function() { i++; if (i<actions.length) { actions_loop(); } }, SPEED*7); }; actions_loop(); } else { // General error console.log("Error while trying to reach the server: "+response.statusCode+" "+error); } }); } // BBBBBBBBBBBBBBBB // B MAIN calls B // BBBBBBBBBBBBBBBB console.log("Run Morse"); tweetToMorse(); // then run automatically setInterval(function(){ console.log("Run Morse"); tweetToMorse(); },FREQUENCY); });