/* * 433 MHz SC2262 Remote Emulator (Spark Core port) * Alistair MacDonald 2014 * * Once uploaded to a Spark Core it can be executed remotly using the following command... * * curl https://api.spark.io/v1/devices/{device_id}/switch -d access_token={token} -d "args={remote_device},{on_off}" * * Wherer... * {device_id} = The Device ID of the Spark Core (Browse to https://www.spark.io/build/ and select the "Cores" icon to find) * {token} = Your Access Token (Browse to https://www.spark.io/build/ and select the "Settings" icon to find) * {remote_device} = The ID set on the mains switch. Add the group (A=0,B=4,C=8,D=12) and the device (1-4) * {on_off} = 0=Off, 1=On * */ // General settings #define TRANSMITTERPIN D0 // Pin connected to the 433MH transmitter // Timing values that might need tweeking for some devices #define DURATIONSHORT 400 // Short pulse length #define DURATIONLONG 1200 // Long pulse length #define DURATIONSYNCLONG 12400 // Lond sync pusle length // Send a virtual low void sendLow () { // send short long digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONSHORT); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONLONG); // send short long digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONSHORT); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONLONG); } // Send a virtual high void sendHigh () { // send long short digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONLONG); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONSHORT); // send long short digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONLONG); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONSHORT); } // Send a virtual float void sendFloat () { // send short long digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONSHORT); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONLONG); // send long short digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONLONG); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONSHORT); } // Send a sync pulse void sendSync () { // send short synclong digitalWrite (TRANSMITTERPIN, HIGH); delayMicroseconds (DURATIONSHORT); digitalWrite (TRANSMITTERPIN, LOW); delayMicroseconds (DURATIONSYNCLONG); } // Send the command void sendSC2262Packet ( int inDevice, int inOnOff ) { // Send the device set code int theABCD = ( inDevice / 4 ) % 4; ( theABCD == 0 ) ? sendLow() : sendFloat(); ( theABCD == 1 ) ? sendLow() : sendFloat(); ( theABCD == 2 ) ? sendLow() : sendFloat(); ( theABCD == 3 ) ? sendLow() : sendFloat(); // Send the device number code int the123 = inDevice % 4; ( the123 == 1 ) ? sendLow() : sendFloat(); ( the123 == 2 ) ? sendLow() : sendFloat(); ( the123 == 3 ) ? sendLow() : sendFloat(); ( the123 == 4 ) ? sendLow() : sendFloat(); // Padding sendFloat(); sendFloat(); sendFloat(); // Send the state ( inOnOff ) ? sendHigh() : sendLow(); // Send the end of command sync sendSync(); } // Send the sommand multiple times void sendSC2262Packets( int inDevice, int inOnOff, int inCount ) { int i; for ( i=0; i < inCount; i++) { sendSC2262Packet( inDevice, inOnOff ); } } // Callback commend from the Spark Core Web API int switchCommand(String args) { // First param is the device address int loc1 = args.indexOf(","); int deviceNum = args.substring(0,loc1).toInt(); // Second param is the address is on (1) or off (0) int loc2 = args.indexOf(",",loc1+1); int onOff = (args.substring(loc1+1,loc2).toInt()>0); // Send the packet if we have a valid device address if (deviceNum>0) { sendSC2262Packets(deviceNum, onOff, 18); return onOff; } else { return -1; } } // Setup the hardware void setup () { // Configure the pin for output pinMode (TRANSMITTERPIN, OUTPUT); digitalWrite (TRANSMITTERPIN, LOW); // Register the "switch" command with the Web API Spark.function("switch", switchCommand); } // The main loop void loop() { // Busy doing nothing working the whole day through // Code can be added here but the loop must end regularly }