├── LICENSE ├── README.md ├── firmware ├── examples │ ├── .DS_Store │ ├── ch_03_Blink_White.cpp │ ├── ch_03_PWM_Output.cpp │ ├── ch_03_SOS_Array.cpp │ ├── ch_03_SOS_Flash_Photon.cpp │ ├── ch_03_SOS_Function.cpp │ ├── ch_03_SOS_Functions2.cpp │ ├── ch_03_SOS_Naive.cpp │ ├── ch_03_SOS_Vars.cpp │ ├── ch_05_analog_ins.cpp │ ├── ch_08_Temp_Monitor_Pub.cpp │ ├── ch_08_Temp_Monitor_Sub.cpp │ ├── ch_09_Trace_Example.cpp │ ├── p_02_RGB_LED.cpp │ ├── p_03_Morse_Flasher.cpp │ ├── p_04_Morse_External_LED.cpp │ ├── p_05_Morse_Switch.cpp │ ├── p_06_LED_Function.cpp │ ├── p_06_LED_Function_Blink.cpp │ ├── p_07_LED_Relays.cpp │ ├── p_08_Morse_function.cpp │ ├── p_09_Lightmeter.cpp │ ├── p_10_Thermometer_Dallas.cpp │ ├── p_12_Tweet_Bell.cpp │ └── p_15_Magic_Rope.cpp ├── photon_book.cpp └── photon_book.h ├── html ├── .DS_Store ├── ch_05_led_control.html ├── ch_06_relays.html ├── ch_06_relays_fb.html ├── justgage.1.0.1.min.js ├── p_08_led_control.html ├── p_08_morese_function.html ├── p_08_morse_function.html ├── p_09_light_meter.html ├── p_10_thermometer copy.html ├── p_10_thermometer.html ├── p_14_rover.html ├── raphael.2.1.0.min.js └── switch_test.html └── spark.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Simon Monk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # photon_book 2 | The code to accompany the book Getting Started with Photon 3 | -------------------------------------------------------------------------------- /firmware/examples/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonmonk/photon_book/51758a088422283daa1d6efc100bb856ba5d6206/firmware/examples/.DS_Store -------------------------------------------------------------------------------- /firmware/examples/ch_03_Blink_White.cpp: -------------------------------------------------------------------------------- 1 | void setup() { 2 | RGB.control(true); 3 | } 4 | 5 | void loop() { 6 | RGB.color(255, 255, 255); 7 | delay(500); 8 | RGB.color(0, 0, 0); 9 | delay(500); 10 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_PWM_Output.cpp: -------------------------------------------------------------------------------- 1 | int ledPin = A5; 2 | int switchPin = D3; 3 | 4 | int brightness = 0; 5 | 6 | void setup() { 7 | pinMode(A5, OUTPUT); 8 | pinMode(switchPin, INPUT_PULLUP); 9 | } 10 | 11 | void loop() { 12 | if (digitalRead(switchPin) == LOW) { 13 | brightness += 25; 14 | if (brightness > 255) { 15 | brightness = 0; 16 | } 17 | analogWrite(ledPin, brightness); 18 | delay(200); 19 | } 20 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_SOS_Array.cpp: -------------------------------------------------------------------------------- 1 | int durations[] = {200, 200, 200, 600, 600, 600, 200, 200, 200}; 2 | int gaps[] = {200, 200, 600, 200, 200, 600, 200, 200, 600}; 3 | 4 | void setup() { 5 | RGB.control(true); 6 | } 7 | 8 | void loop() { 9 | for (int i = 0; i < 9; i++) { 10 | flash(durations[i], gaps[i]); 11 | } 12 | 13 | delay(2000); // delay before repeat 14 | } 15 | 16 | void flash(int duration, int gap) { 17 | RGB.color(255, 255, 255); 18 | delay(duration); 19 | RGB.color(0, 0, 0); 20 | delay(gap); 21 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_SOS_Flash_Photon.cpp: -------------------------------------------------------------------------------- 1 | // .-- .... --- - --- -. 2 | int durations[] = {200, 600, 600, 200, 200, 200, 200, 600, 600, 600, 600, 600, 600, 600, 600, 200}; 3 | int gaps[] = {200, 200, 200, 600, 200, 200, 200, 600, 200, 200, 600, 200, 200, 600, 200, 600}; 4 | 5 | void setup() { 6 | RGB.control(true); 7 | } 8 | 9 | void loop() { 10 | for (int i = 0; i < 16; i++) { 11 | flash(durations[i], gaps[i]); 12 | } 13 | 14 | delay(2000); // delay before repeat 15 | } 16 | 17 | void flash(int duration, int gap) { 18 | RGB.color(255, 255, 255); 19 | delay(duration); 20 | RGB.color(0, 0, 0); 21 | delay(gap); 22 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_SOS_Function.cpp: -------------------------------------------------------------------------------- 1 | void setup() { 2 | RGB.control(true); 3 | } 4 | 5 | void loop() { 6 | // flash S 7 | flash(200, 200); 8 | flash(200, 200); 9 | flash(200, 600); 10 | // flash O 11 | flash(600, 200); 12 | flash(600, 200); 13 | flash(600, 600); 14 | // flash S 15 | flash(200, 200); 16 | flash(200, 200); 17 | flash(200, 600); 18 | 19 | delay(2000); // delay before repeat 20 | } 21 | 22 | void flash(int duration, int gap) { 23 | RGB.color(255, 255, 255); 24 | delay(duration); 25 | RGB.color(0, 0, 0); 26 | delay(gap); 27 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_SOS_Functions2.cpp: -------------------------------------------------------------------------------- 1 | void setup() { 2 | RGB.control(true); 3 | } 4 | 5 | void loop() { 6 | flashS(); 7 | flashO(); 8 | flashS(); 9 | 10 | delay(2000); // delay before repeat 11 | } 12 | 13 | void flash(int duration, int gap) { 14 | RGB.color(255, 255, 255); 15 | delay(duration); 16 | RGB.color(0, 0, 0); 17 | delay(gap); 18 | } 19 | 20 | void flashS() { 21 | flash(200, 200); 22 | flash(200, 200); 23 | flash(200, 600); 24 | } 25 | 26 | void flashO() { 27 | flash(600, 200); 28 | flash(600, 200); 29 | flash(600, 600); 30 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_SOS_Naive.cpp: -------------------------------------------------------------------------------- 1 | void setup() { 2 | RGB.control(true); 3 | } 4 | 5 | void loop() { 6 | // flash S 7 | // dot 8 | RGB.color(255, 255, 255); 9 | delay(200); 10 | RGB.color(0, 0, 0); 11 | delay(200); 12 | // dot 13 | RGB.color(255, 255, 255); 14 | delay(200); 15 | RGB.color(0, 0, 0); 16 | delay(200); 17 | // dot 18 | RGB.color(255, 255, 255); 19 | delay(200); 20 | RGB.color(0, 0, 0); 21 | delay(600); 22 | // end of S 23 | // flash O 24 | // dash 25 | RGB.color(255, 255, 255); 26 | delay(600); 27 | RGB.color(0, 0, 0); 28 | delay(200); 29 | // dash 30 | RGB.color(255, 255, 255); 31 | delay(600); 32 | RGB.color(0, 0, 0); 33 | delay(200); 34 | // dash 35 | RGB.color(255, 255, 255); 36 | delay(600); 37 | RGB.color(0, 0, 0); 38 | delay(600); 39 | // end of O 40 | // flash S 41 | // dot 42 | RGB.color(255, 255, 255); 43 | delay(200); 44 | RGB.color(0, 0, 0); 45 | delay(200); 46 | // dot 47 | RGB.color(255, 255, 255); 48 | delay(200); 49 | RGB.color(0, 0, 0); 50 | delay(200); 51 | // dot 52 | RGB.color(255, 255, 255); 53 | delay(200); 54 | RGB.color(0, 0, 0); 55 | delay(600); 56 | // end of S 57 | 58 | delay(2000); // delay before repeat 59 | } -------------------------------------------------------------------------------- /firmware/examples/ch_03_SOS_Vars.cpp: -------------------------------------------------------------------------------- 1 | int dot = 100; 2 | int dash = dot * 3; 3 | 4 | void setup() { 5 | RGB.control(true); 6 | } 7 | 8 | void loop() { 9 | flashS(); 10 | flashO(); 11 | flashS(); 12 | 13 | delay(2000); // delay before repeat 14 | } 15 | 16 | void flash(int duration, int gap) { 17 | RGB.color(255, 255, 255); 18 | delay(duration); 19 | RGB.color(0, 0, 0); 20 | delay(gap); 21 | } 22 | 23 | void flashS() { 24 | flash(dot, dot); 25 | flash(dot, dot); 26 | flash(dot, dash); 27 | } 28 | 29 | void flashO() { 30 | flash(dash, dot); 31 | flash(dash, dot); 32 | flash(dash, dash); 33 | } -------------------------------------------------------------------------------- /firmware/examples/ch_05_analog_ins.cpp: -------------------------------------------------------------------------------- 1 | // https://api.spark.io/v1/devices/54ff72066672524860351167/analog?access_token=cb8b348000e9d0ea9e354990bbd39ccbfb57b30e 2 | 3 | int reading = 0; 4 | int analogPin = A0; 5 | 6 | void setup() { 7 | Spark.variable("analog", &reading, INT); 8 | } 9 | 10 | void loop() { 11 | reading = analogRead(analogPin); 12 | } 13 | -------------------------------------------------------------------------------- /firmware/examples/ch_08_Temp_Monitor_Pub.cpp: -------------------------------------------------------------------------------- 1 | #include "spark-dallas-temperature/spark-dallas-temperature.h" 2 | #include "OneWire/OneWire.h" 3 | 4 | int tempSensorPin = D2; 5 | OneWire oneWire(tempSensorPin); 6 | DallasTemperature sensors(&oneWire); 7 | 8 | boolean toohot = false; 9 | 10 | void setup() { 11 | sensors.begin(); 12 | } 13 | 14 | void loop() { 15 | sensors.requestTemperatures(); 16 | float tempC = sensors.getTempCByIndex(0); 17 | float tempF = tempC * 9.0 / 5.0 + 32.0; 18 | if (tempF > 80.0 && toohot == false) { 19 | Spark.publish("toohot"); 20 | toohot = true; 21 | } 22 | if (tempF < 78.0 && toohot == true) { 23 | Spark.publish("tempnormal"); 24 | toohot = false; 25 | } 26 | } -------------------------------------------------------------------------------- /firmware/examples/ch_08_Temp_Monitor_Sub.cpp: -------------------------------------------------------------------------------- 1 | int relayPin = D0; 2 | 3 | void setup() { 4 | pinMode(relayPin, OUTPUT); 5 | Spark.subscribe("toohot", soundAlarm); 6 | Spark.subscribe("tempnormal", cancelAlarm); 7 | } 8 | 9 | void loop() { 10 | } 11 | 12 | void soundAlarm(const char *event, const char *data) { 13 | digitalWrite(relayPin, HIGH); 14 | } 15 | 16 | void cancelAlarm(const char *event, const char *data) { 17 | digitalWrite(relayPin, LOW); 18 | } -------------------------------------------------------------------------------- /firmware/examples/ch_09_Trace_Example.cpp: -------------------------------------------------------------------------------- 1 | void setup() { 2 | Serial.begin(9600); 3 | } 4 | 5 | void loop() { 6 | if (Serial.available()) { 7 | char ch = Serial.read(); 8 | if (ch == '?') { 9 | Serial.print("Hello millis()="); 10 | Serial.println(millis()); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /firmware/examples/p_02_RGB_LED.cpp: -------------------------------------------------------------------------------- 1 | void setup() { 2 | RGB.control(true); 3 | } 4 | 5 | void loop() { 6 | RGB.color(255, 0, 0); 7 | delay(2000); 8 | RGB.color(0, 255, 0); 9 | delay(2000); 10 | RGB.color(0, 0, 255); 11 | delay(2000); 12 | } -------------------------------------------------------------------------------- /firmware/examples/p_03_Morse_Flasher.cpp: -------------------------------------------------------------------------------- 1 | String message = "My Photon speaks Morse"; 2 | 3 | int dot = 200; 4 | int dash = dot * 3; 5 | 6 | String letters[] = { 7 | ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I 8 | ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 9 | "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z 10 | }; 11 | 12 | void setup() { 13 | RGB.control(true); 14 | } 15 | 16 | void loop() { 17 | flashMessage(message); 18 | delay(5000); // delay before repeat 19 | } 20 | 21 | void flashMessage(String message) { 22 | for (int i = 0; i < message.length(); i++) { 23 | char letter = message.charAt(i); 24 | flashLetter(letter); 25 | } 26 | } 27 | 28 | void flashLetter(char letter) 29 | { 30 | if (letter >= 'a' && letter <= 'z') { 31 | flashDotsAndDashes(letters[letter - 'a']); 32 | } 33 | else if (letter >= 'A' && letter <= 'Z') { 34 | flashDotsAndDashes(letters[letter - 'A']); 35 | } 36 | else if (letter == ' ') { 37 | delay(dot * 7); // gap between words 38 | } 39 | } 40 | 41 | void flashDotsAndDashes(String dotsAndDashes) { 42 | for (int i = 0; i < dotsAndDashes.length(); i++) { 43 | char dotOrDash = dotsAndDashes.charAt(i); 44 | if (dotOrDash == '.') { 45 | flash(dot); 46 | } 47 | else { 48 | flash(dash); 49 | } 50 | delay(dot); // gap between dots and dashes of a letter 51 | } 52 | delay(dash - dot); // gap between letters of a word 53 | } 54 | 55 | void flash(int duration) { 56 | RGB.color(255, 255, 255); 57 | delay(duration); 58 | RGB.color(0, 0, 0); 59 | } 60 | -------------------------------------------------------------------------------- /firmware/examples/p_04_Morse_External_LED.cpp: -------------------------------------------------------------------------------- 1 | String message = "My Photon speaks Morse"; 2 | 3 | int ledPin = D7; 4 | int dot = 200; 5 | int dash = dot * 3; 6 | 7 | String letters[] = { 8 | ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I 9 | ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 10 | "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z 11 | }; 12 | 13 | void setup() { 14 | pinMode(ledPin, OUTPUT); 15 | } 16 | 17 | void loop() { 18 | flashMessage(message); 19 | delay(5000); // delay before repeat 20 | } 21 | 22 | void flashMessage(String message) { 23 | for (int i = 0; i < message.length(); i++) { 24 | char letter = message.charAt(i); 25 | flashLetter(letter); 26 | } 27 | } 28 | 29 | void flashLetter(char letter) 30 | { 31 | if (letter >= 'a' && letter <= 'z') { 32 | flashDotsAndDashes(letters[letter - 'a']); 33 | } 34 | else if (letter >= 'A' && letter <= 'Z') { 35 | flashDotsAndDashes(letters[letter - 'A']); 36 | } 37 | else if (letter == ' ') { 38 | delay(dot * 7); // gap between words 39 | } 40 | } 41 | 42 | void flashDotsAndDashes(String dotsAndDashes) { 43 | for (int i = 0; i < dotsAndDashes.length(); i++) { 44 | char dotOrDash = dotsAndDashes.charAt(i); 45 | if (dotOrDash == '.') { 46 | flash(dot); 47 | } 48 | else { 49 | flash(dash); 50 | } 51 | delay(dot); // gap between dots and dashes of a letter 52 | } 53 | delay(dash - dot); // gap between letters of a word 54 | } 55 | 56 | void flash(int duration) { 57 | digitalWrite(ledPin, HIGH); 58 | delay(duration); 59 | digitalWrite(ledPin, LOW); 60 | } 61 | -------------------------------------------------------------------------------- /firmware/examples/p_05_Morse_Switch.cpp: -------------------------------------------------------------------------------- 1 | String message = "sos sos sos sos sos"; 2 | 3 | int ledPin = D7; 4 | int switchPin = D3; 5 | 6 | int dot = 100; 7 | int dash = dot * 3; 8 | 9 | String letters[] = { 10 | ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I 11 | ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 12 | "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z 13 | }; 14 | 15 | void setup() { 16 | pinMode(ledPin, OUTPUT); 17 | pinMode(switchPin, INPUT_PULLUP); 18 | } 19 | 20 | void loop() { 21 | if (digitalRead(switchPin) == LOW) { 22 | flashMessage(message); 23 | } 24 | } 25 | 26 | void flashMessage(String message) { 27 | for (int i = 0; i < message.length(); i++) { 28 | char letter = message.charAt(i); 29 | flashLetter(letter); 30 | } 31 | } 32 | 33 | void flashLetter(char letter) 34 | { 35 | if (letter >= 'a' && letter <= 'z') { 36 | flashDotsAndDashes(letters[letter - 'a']); 37 | } 38 | else if (letter >= 'A' && letter <= 'Z') { 39 | flashDotsAndDashes(letters[letter - 'A']); 40 | } 41 | else if (letter == ' ') { 42 | delay(dot * 7); // gap between words 43 | } 44 | } 45 | 46 | void flashDotsAndDashes(String dotsAndDashes) { 47 | for (int i = 0; i < dotsAndDashes.length(); i++) { 48 | char dotOrDash = dotsAndDashes.charAt(i); 49 | if (dotOrDash == '.') { 50 | flash(dot); 51 | } 52 | else { 53 | flash(dash); 54 | } 55 | delay(dot); // gap between dots and dashes of a letter 56 | } 57 | delay(dash - dot); // gap between letters of a word 58 | } 59 | 60 | void flash(int duration) { 61 | digitalWrite(ledPin, HIGH); 62 | delay(duration); 63 | digitalWrite(ledPin, LOW); 64 | } 65 | -------------------------------------------------------------------------------- /firmware/examples/p_06_LED_Function.cpp: -------------------------------------------------------------------------------- 1 | int ledPin = D7; 2 | 3 | void setup() { 4 | pinMode(ledPin, OUTPUT); 5 | Spark.function("led", ledSwitcher); 6 | } 7 | 8 | void loop() { 9 | } 10 | 11 | int ledSwitcher(String command) { 12 | if (command.equalsIgnoreCase("on")) { 13 | digitalWrite(ledPin, HIGH); 14 | return 1; 15 | } 16 | else if (command.equalsIgnoreCase("off")) { 17 | digitalWrite(ledPin, LOW); 18 | return 1; 19 | } 20 | return -1; 21 | } 22 | 23 | // To test with curl 24 | // curl https://api.spark.io/v1/devices//led -d access_token= -d params=on -------------------------------------------------------------------------------- /firmware/examples/p_06_LED_Function_Blink.cpp: -------------------------------------------------------------------------------- 1 | int ledPin = D7; 2 | 3 | boolean blinking = false; 4 | 5 | void setup() { 6 | pinMode(ledPin, OUTPUT); 7 | Spark.function("led", ledSwitcher); 8 | } 9 | 10 | void loop() { 11 | if (blinking) { 12 | digitalWrite(ledPin, HIGH); 13 | delay(200); 14 | digitalWrite(ledPin, LOW); 15 | delay(200); 16 | } 17 | } 18 | 19 | int ledSwitcher(String command) { 20 | if (command.equalsIgnoreCase("on")) { 21 | blinking = true; 22 | return 1; 23 | } 24 | else if (command.equalsIgnoreCase("off")) { 25 | blinking = false; 26 | return 1; 27 | } 28 | return -1; 29 | } 30 | 31 | // To test with curl 32 | // curl https://api.spark.io/v1/devices//led -d access_token= -d params=on -------------------------------------------------------------------------------- /firmware/examples/p_07_LED_Relays.cpp: -------------------------------------------------------------------------------- 1 | int relayPins[] = {D0, D1, D2, D3}; 2 | 3 | void setup() { 4 | for (int i = 0; i < 4; i++) { 5 | pinMode(relayPins[i], OUTPUT); 6 | } 7 | Spark.function("relay", relaySwitcher); 8 | } 9 | 10 | void loop() { 11 | } 12 | 13 | int relaySwitcher(String command) { 14 | if (command.length() != 2) return -1; 15 | int relayNumber = command.charAt(0) - '0'; 16 | int state = command.charAt(1) - '0'; 17 | digitalWrite(relayPins[relayNumber-1], state); 18 | return 1; 19 | } 20 | 21 | // To test with curl 22 | // curl https://api.spark.io/v1/devices//relay -d access_token= -d params=11 -------------------------------------------------------------------------------- /firmware/examples/p_08_Morse_function.cpp: -------------------------------------------------------------------------------- 1 | String message = ""; 2 | 3 | int ledPin = D7; 4 | int buzzerPin = D1; 5 | int dot = 100; 6 | int dash = dot * 3; 7 | boolean flashing = false; 8 | 9 | String letters[] = { 10 | ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I 11 | ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R 12 | "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z 13 | }; 14 | 15 | void setup() { 16 | pinMode(ledPin, OUTPUT); 17 | pinMode(buzzerPin, OUTPUT); 18 | Spark.function("morse", startFlashing); 19 | } 20 | 21 | void loop() { 22 | if (flashing) { 23 | flashMessage(message); 24 | flashing = false; 25 | } 26 | } 27 | 28 | int startFlashing(String param) { 29 | if (param.length() <= 63) { 30 | message = param; 31 | flashing = true; 32 | return 1; 33 | } 34 | else { 35 | return -1; // message too long 36 | } 37 | } 38 | 39 | void flashMessage(String message) { 40 | for (int i = 0; i < message.length(); i++) { 41 | char letter = message.charAt(i); 42 | flashLetter(letter); 43 | } 44 | } 45 | 46 | void flashLetter(char letter) 47 | { 48 | if (letter >= 'a' && letter <= 'z') { 49 | flashDotsAndDashes(letters[letter - 'a']); 50 | } 51 | else if (letter >= 'A' && letter <= 'Z') { 52 | flashDotsAndDashes(letters[letter - 'A']); 53 | } 54 | else if (letter == ' ') { 55 | delay(dot * 7); // gap between words 56 | } 57 | } 58 | 59 | void flashDotsAndDashes(String dotsAndDashes) { 60 | for (int i = 0; i < dotsAndDashes.length(); i++) { 61 | char dotOrDash = dotsAndDashes.charAt(i); 62 | if (dotOrDash == '.') { 63 | flash(dot); 64 | } 65 | else { 66 | flash(dash); 67 | } 68 | delay(dot); // gap between dots and dashes of a letter 69 | } 70 | delay(dash - dot); // gap between letters of a word 71 | } 72 | 73 | void flash(int duration) { 74 | digitalWrite(ledPin, HIGH); 75 | tone(buzzerPin, 1000); 76 | delay(duration); 77 | noTone(buzzerPin); 78 | digitalWrite(ledPin, LOW); 79 | } 80 | 81 | // curl https://api.spark.io/v1/devices/55ff6b065075555322151487/morse -d access_token=cb8b348000e9d0ea9e354990bbd39ccbfb57b30e -d params=ABCD 82 | -------------------------------------------------------------------------------- /firmware/examples/p_09_Lightmeter.cpp: -------------------------------------------------------------------------------- 1 | int reading = 0; 2 | double volts = 0.0; 3 | int analogPin = A0; 4 | 5 | void setup() { 6 | Spark.variable("analog", &reading, INT); 7 | Spark.variable("volts", &volts, DOUBLE); 8 | } 9 | 10 | void loop() { 11 | reading = analogRead(analogPin); 12 | volts = reading * 3.3 / 4096.0; 13 | } 14 | 15 | 16 | // https://api.spark.io/v1/devices/54ff72066672524860351167/analog?access_token=cb8b348000e9d0ea9e354990bbd39ccbfb57b30e 17 | // https://api.spark.io/v1/devices/54ff72066672524860351167/volts?access_token=cb8b348000e9d0ea9e354990bbd39ccbfb57b30e -------------------------------------------------------------------------------- /firmware/examples/p_10_Thermometer_Dallas.cpp: -------------------------------------------------------------------------------- 1 | #include "OneWire/OneWire.h" 2 | #include "spark-dallas-temperature/spark-dallas-temperature.h" 3 | 4 | double tempC = 0.0; 5 | double tempF = 0.0; 6 | 7 | int tempSensorPin = D2; 8 | 9 | OneWire oneWire(tempSensorPin); 10 | DallasTemperature sensors(&oneWire); 11 | 12 | void setup() { 13 | sensors.begin(); 14 | Spark.variable("tempc", &tempC, DOUBLE); 15 | Spark.variable("tempf", &tempF, DOUBLE); 16 | } 17 | 18 | void loop() { 19 | sensors.requestTemperatures(); 20 | tempC = sensors.getTempCByIndex(0); 21 | tempF = tempC * 9.0 / 5.0 + 32.0; 22 | } 23 | 24 | 25 | // https://api.spark.io/v1/devices/54ff72066672524860351167/tempc?access_token=cb8b348000e9d0ea9e354990bbd39ccbfb57b30e 26 | -------------------------------------------------------------------------------- /firmware/examples/p_12_Tweet_Bell.cpp: -------------------------------------------------------------------------------- 1 | int relayPins[] = {D0, D1, D2, D3}; 2 | 3 | void setup() { 4 | for (int i = 0; i < 4; i++) { 5 | pinMode(relayPins[i], OUTPUT); 6 | } 7 | Spark.function("relaycontrol", relaySwitcher); 8 | } 9 | 10 | void loop() { 11 | } 12 | 13 | int relaySwitcher(String command) { 14 | // "11", "10", "1P 1000" - P for pulse duration in millis 15 | int relayNumber = command.charAt(0) - '0'; 16 | char action = command.charAt(1); 17 | if (action == '1') { 18 | digitalWrite(relayPins[relayNumber-1], HIGH); 19 | } 20 | else if (action == '0') { 21 | digitalWrite(relayPins[relayNumber-1], LOW); 22 | } 23 | else if (action == 'P' || action == 'p') { 24 | int duration = command.substring(3).toInt(); 25 | digitalWrite(relayPins[relayNumber-1], HIGH); 26 | delay(duration); 27 | digitalWrite(relayPins[relayNumber-1], LOW); 28 | } 29 | return 1; 30 | } 31 | 32 | // To test with curl 33 | // curl https://api.spark.io/v1/devices//relaycontrol -d access_token= -d params=1P-1000 34 | // curl https://api.spark.io/v1/devices/54ff6d066678574934420567/relaycontrol -d access_token=cb8b348000e9d0ea9e354990bbd39ccbfb57b30e -d params=1P-1000 -------------------------------------------------------------------------------- /firmware/examples/p_15_Magic_Rope.cpp: -------------------------------------------------------------------------------- 1 | int motorPin = D4; 2 | // int ledPin = D7; 3 | int potPin = A0; 4 | 5 | String thisID = Spark.deviceID(); 6 | boolean myTurn = true; 7 | int maxPosn = 4000; 8 | int minPosn = 3000; 9 | 10 | // To debug using LED flashing on pin D7, uncomment 11 | // the lines above and below. 12 | // two blinks for a publish 13 | // four blinks for a subscription event recieved 14 | 15 | void setup() { 16 | Spark.subscribe("pulled", remoteRopePulled); 17 | pinMode(motorPin, OUTPUT); 18 | // pinMode(ledPin, OUTPUT); 19 | moveSliderTo(maxPosn); 20 | } 21 | 22 | void loop() { 23 | int newLocalPosition = analogRead(potPin); 24 | if (newLocalPosition < minPosn && myTurn) { 25 | Spark.publish("pulled", thisID); 26 | // flash(2); 27 | myTurn = false; 28 | //delay(2000); 29 | } 30 | } 31 | 32 | void remoteRopePulled(const char *event, const char *data) 33 | { 34 | // flash(4); 35 | String dataS = String(data); 36 | if (dataS.indexOf(thisID) == -1) // ignore messages from yourself 37 | { 38 | moveSliderTo(maxPosn); 39 | myTurn = true; 40 | } 41 | } 42 | 43 | void moveSliderTo(int newPosition) { 44 | while (analogRead(potPin) < newPosition) { 45 | digitalWrite(motorPin, HIGH); 46 | }; 47 | digitalWrite(motorPin, LOW); 48 | } 49 | 50 | 51 | // void flash(int n) { 52 | // for (int i = 0; i < n; i++) { 53 | // digitalWrite(ledPin, HIGH); 54 | // delay(300); 55 | // digitalWrite(ledPin, LOW); 56 | // delay(300); 57 | // } 58 | // } 59 | -------------------------------------------------------------------------------- /firmware/photon_book.cpp: -------------------------------------------------------------------------------- 1 | // This is'nt actually a library. Its a collection of all the programs from the book 2 | // Getting Started with Photon, by Simon Monk 3 | -------------------------------------------------------------------------------- /firmware/photon_book.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonmonk/photon_book/51758a088422283daa1d6efc100bb856ba5d6206/firmware/photon_book.h -------------------------------------------------------------------------------- /html/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonmonk/photon_book/51758a088422283daa1d6efc100bb856ba5d6206/html/.DS_Store -------------------------------------------------------------------------------- /html/ch_05_led_control.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | 23 | 24 | 25 |

On/Off Control

26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /html/ch_06_relays.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 17 | 18 | 19 | 20 | 21 |

Relay Control

22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 | -------------------------------------------------------------------------------- /html/ch_06_relays_fb.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 14 | 15 | 48 | 49 | 50 | 51 | 52 | 53 | 54 |

Relay Control

55 | 56 |
57 |
58 |
59 |
60 | 61 | 62 | -------------------------------------------------------------------------------- /html/justgage.1.0.1.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * JustGage - a handy JavaScript plugin for generating and animating nice & clean dashboard gauges. 3 | * Copyright (c) 2012 Bojan Djuricic - pindjur(at)gmail(dot)com | http://www.madcog.com 4 | * Licensed under MIT. 5 | * Date: 31/07/2012 6 | * @author Bojan Djuricic (@Toorshia) 7 | * @version 1.0 8 | * 9 | * http://www.justgage.com 10 | */ 11 | 12 | JustGage=function(x){if(!x.id){alert("Missing id parameter for gauge!");return false}if(!document.getElementById(x.id)){alert('No element with id: "'+x.id+'" found!');return false}this.config={id:x.id,title:(x.title)?x.title:"Title",titleFontColor:(x.titleFontColor)?x.titleFontColor:"#999999",value:(x.value)?x.value:0,valueFontColor:(x.valueFontColor)?x.valueFontColor:"#010101",min:(x.min)?x.min:0,max:(x.max)?x.max:100,showMinMax:(x.showMinMax!=null)?x.showMinMax:true,gaugeWidthScale:(x.gaugeWidthScale)?x.gaugeWidthScale:1,gaugeColor:(x.gaugeColor)?x.gaugeColor:"#edebeb",label:(x.label)?x.label:"",showInnerShadow:(x.showInnerShadow!=null)?x.showInnerShadow:true,shadowOpacity:(x.shadowOpacity)?x.shadowOpacity:0.2,shadowSize:(x.shadowSize)?x.shadowSize:5,shadowVerticalOffset:(x.shadowVerticalOffset)?x.shadowVerticalOffset:3,levelColors:(x.levelColors)?x.levelColors:percentColors,levelColorsGradient:(x.levelColorsGradient!=null)?x.levelColorsGradient:true,labelFontColor:(x.labelFontColor)?x.labelFontColor:"#b3b3b3",startAnimationTime:(x.startAnimationTime)?x.startAnimationTime:700,startAnimationType:(x.startAnimationType)?x.startAnimationType:">",refreshAnimationTime:(x.refreshAnimationTime)?x.refreshAnimationTime:700,refreshAnimationType:(x.refreshAnimationType)?x.refreshAnimationType:">"};if(x.value>this.config.max){this.config.value=this.config.max}if(x.value1.25){c=1.25*q;l=q}else{c=d;l=d/1.25}var i=(d-c)/2;var g=(q-l)/2;var s=((l/8)>10)?(l/10):10;var n=i+c/2;var m=g+l/6.5;var k=((l/6.4)>16)?(l/6.4):16;var r=i+c/2;var p=g+l/1.4;var b=((l/16)>10)?(l/16):10;var f=i+c/2;var e=p+k/2+6;var j=((l/16)>10)?(l/16):10;var w=i+(c/10)+(c/6.666666666666667*this.config.gaugeWidthScale)/2;var v=g+l/1.126760563380282;var h=((l/16)>10)?(l/16):10;var u=i+c-(c/10)-(c/6.666666666666667*this.config.gaugeWidthScale)/2;var t=g+l/1.126760563380282;this.params={canvasW:d,canvasH:q,widgetW:c,widgetH:l,dx:i,dy:g,titleFontSize:s,titleX:n,titleY:m,valueFontSize:k,valueX:r,valueY:p,labelFontSize:b,labelX:f,labelY:e,minFontSize:j,minX:w,minY:v,maxFontSize:h,maxX:u,maxY:t};this.canvas.customAttributes.pki=function(K,L,N,E,O,F,D,y){var B=(1-(K-L)/(N-L))*Math.PI,G=E/2-E/10,J=G-E/6.666666666666667*y,C=E/2+F,A=O/1.25+D,H=E/2+F+G*Math.cos(B),P=O-(O-A)+D-G*Math.sin(B),M=E/2+F+J*Math.cos(B),z=O-(O-A)+D-J*Math.sin(B),I;I+="M"+(C-J)+","+A+" ";I+="L"+(C-G)+","+A+" ";I+="A"+G+","+G+" 0 0,1 "+H+","+P+" ";I+="L"+M+","+z+" ";I+="A"+J+","+J+" 0 0,0 "+(C-J)+","+A+" ";I+="z ";return{path:I}};this.gauge=this.canvas.path().attr({stroke:"none",fill:this.config.gaugeColor,pki:[this.config.max,this.config.min,this.config.max,this.params.widgetW,this.params.widgetH,this.params.dx,this.params.dy,this.config.gaugeWidthScale]});this.gauge.id=this.config.id+"-gauge";this.level=this.canvas.path().attr({stroke:"none",fill:getColorForPercentage((this.config.value-this.config.min)/(this.config.max-this.config.min),this.config.levelColors,this.config.levelColorsGradient),pki:[this.config.min,this.config.min,this.config.max,this.params.widgetW,this.params.widgetH,this.params.dx,this.params.dy,this.config.gaugeWidthScale]});this.level.id=this.config.id+"-level";this.txtTitle=this.canvas.text(this.params.titleX,this.params.titleY,this.config.title);this.txtTitle.attr({"font-size":this.params.titleFontSize,"font-weight":"bold","font-family":"Arial",fill:this.config.titleFontColor,"fill-opacity":"1"});this.txtTitle.id=this.config.id+"-txttitle";this.txtValue=this.canvas.text(this.params.valueX,this.params.valueY,this.originalValue);this.txtValue.attr({"font-size":this.params.valueFontSize,"font-weight":"bold","font-family":"Arial",fill:this.config.valueFontColor,"fill-opacity":"0"});this.txtValue.id=this.config.id+"-txtvalue";this.txtLabel=this.canvas.text(this.params.labelX,this.params.labelY,this.config.label);this.txtLabel.attr({"font-size":this.params.labelFontSize,"font-weight":"normal","font-family":"Arial",fill:this.config.labelFontColor,"fill-opacity":"0"});this.txtLabel.id=this.config.id+"-txtlabel";this.txtMin=this.canvas.text(this.params.minX,this.params.minY,this.config.min);this.txtMin.attr({"font-size":this.params.minFontSize,"font-weight":"normal","font-family":"Arial",fill:this.config.labelFontColor,"fill-opacity":(this.config.showMinMax==true)?"1":"0"});this.txtMin.id=this.config.id+"-txtmin";this.txtMax=this.canvas.text(this.params.maxX,this.params.maxY,this.config.max);this.txtMax.attr({"font-size":this.params.maxFontSize,"font-weight":"normal","font-family":"Arial",fill:this.config.labelFontColor,"fill-opacity":(this.config.showMinMax==true)?"1":"0"});this.txtMax.id=this.config.id+"-txtmax";var a=this.canvas.canvas.childNodes[1];var o="http://www.w3.org/2000/svg";if(ie<9){onCreateElementNsReady(function(){this.generateShadow()})}else{this.generateShadow(o,a)}this.level.animate({pki:[this.config.value,this.config.min,this.config.max,this.params.widgetW,this.params.widgetH,this.params.dx,this.params.dy,this.config.gaugeWidthScale]},this.config.startAnimationTime,this.config.startAnimationType);this.txtValue.animate({"fill-opacity":"1"},this.config.startAnimationTime,this.config.startAnimationType);this.txtLabel.animate({"fill-opacity":"1"},this.config.startAnimationTime,this.config.startAnimationType)};JustGage.prototype.refresh=function(b){originalVal=b;if(b>this.config.max){b=this.config.max}if(b",b[0]){}return a>4?a:c}()); -------------------------------------------------------------------------------- /html/p_08_led_control.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | 23 | 24 | 25 |

On/Off Control

26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /html/p_08_morese_function.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |

Morse Code Sender

33 | 34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /html/p_08_morse_function.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |

Morse Code Sender

33 | 34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /html/p_09_light_meter.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 28 | 29 | 30 | 31 |
32 | 33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /html/p_10_thermometer copy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 28 | 29 | 30 | 31 |
32 | 33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /html/p_10_thermometer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 28 | 29 | 30 | 31 |
32 | 33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /html/p_14_rover.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 22 | 23 | 24 | 25 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 |
94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 |
W
ASD
Z
103 | 104 | 105 | 106 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /html/raphael.2.1.0.min.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonmonk/photon_book/51758a088422283daa1d6efc100bb856ba5d6206/html/raphael.2.1.0.min.js -------------------------------------------------------------------------------- /html/switch_test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 15 | 16 | 17 | 18 | 19 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /spark.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "photon_book", 3 | "author": "Simon Monk ", 4 | "license": "MIT", 5 | "version": "0.0.10", 6 | "description": "The code for the book Getting Started with Photon" 7 | } 8 | --------------------------------------------------------------------------------