Last active
December 27, 2016 19:02
-
-
Save tomgidden/7e36c0afadd7d048bc5e0c8c3a140724 to your computer and use it in GitHub Desktop.
Control 433MHz mains switches from an ESP8266 (with 433MHz Tx board) via MQTT, using Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct RcCode { | |
const char *name; | |
int pulses[133]; | |
}; | |
/** | |
* Banks 1 and 2 are "HOME Easy" remote switch units; Bank 3 is an "ENERGENiE" | |
* power strip. | |
* | |
* Bank 3 seems supported by `rc-switch`, but Banks 1 and 2 had to be deduced | |
* from an `irrecord` session. | |
*/ | |
struct RcCode codes[] = { | |
{ | |
"{\"bank\":1,\"channel\":1,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":1,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":2,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,1348,240,320,240, | |
10000, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":2,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,1348,240,320,240, | |
10000, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":3,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":3,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":4,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":4,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":0,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":1,\"channel\":0,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":1,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":1,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":2,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,1348,240,320,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":2,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,1348,240,320,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":3,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":3,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":0,\"value\":1}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":2,\"channel\":0,\"value\":0}", | |
{ | |
240,2750,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,1348,240,320,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,320,240,1348,240,320, | |
240,1348,240,1348,240,320, | |
240,320,240,1348,240,1348, | |
240,320,240,320,240,1348, | |
240,320,240,1348,240,320, | |
240,1348,240,320,240,1348, | |
240,320,240,1348,240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":1,\"value\":1}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,662,170, | |
662,170,662,170,662,170, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":1,\"value\":0}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,662,170, | |
662,170,662,170,250,582, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":2,\"value\":1}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,250,582, | |
662,170,662,170,662,170, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":2,\"value\":0}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,250,582, | |
662,170,662,170,250,582, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":3,\"value\":1}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,662,170, | |
250,582,662,170,662,170, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":3,\"value\":0}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,662,170, | |
250,582,662,170,250,582, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":4,\"value\":1}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,662,170, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":4,\"value\":0}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":0,\"value\":1}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,662,170, | |
662,170,250,582,662,170, | |
240, | |
0 | |
} | |
}, | |
{ | |
"{\"bank\":3,\"channel\":0,\"value\":0}", | |
{ | |
262,570,662,170,662,170, | |
250,582,250,582,250,582, | |
662,170,250,582,250,582, | |
662,170,662,170,250,582, | |
250,582,250,582,250,582, | |
250,582,662,170,250,582, | |
250,582,250,582,662,170, | |
662,170,250,582,250,582, | |
240, | |
0 | |
} | |
}, | |
{ | |
NULL, {} | |
} | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static const char* WLAN_SSID = "XXXX"; | |
static const char* WLAN_PASS = "XXXX"; | |
static const char* MQTT_SERVER = "192.168.1.83"; | |
static const uint16_t MQTT_SERVERPORT = 1883; | |
static const char* MQTT_USER = ""; | |
static const char* MQTT_PASS = ""; | |
static const char* MQTT_CONTROL_TOPIC = "/controller/rf/transmit"; | |
static const char* MQTT_STATUS_TOPIC = "/controller/rf/status"; | |
static const int TX_REPETITIONS = 5; | |
static const int TX_POST_DELAY_USEC = 30000; | |
static const int TX_PIN = 5; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
#include "config.h" | |
#include "codes.h" | |
/** | |
* Transmit a single code stream | |
*/ | |
void transmitCode(RcCode *code) | |
{ | |
// Start with the first duration | |
int *ptr = (int *)(code->pulses); | |
// We start with "off" | |
int level = 0; | |
do { | |
// Change from "off" to "on", or "on" to "off" | |
level = !level; | |
// Set the transmit level, "off" or "on" | |
digitalWrite(TX_PIN, level); | |
// And wait for the duration specified | |
delayMicroseconds(*ptr); | |
// Until we find the end marker | |
} while ((*++ptr) != 0); | |
// If we end on a high, switch "off" | |
if (level) digitalWrite(TX_PIN, 0); | |
// And pad the broadcast with a suitable downtime | |
delayMicroseconds(TX_POST_DELAY_USEC); | |
} | |
/** | |
* Transmit a named code, with repetitions | |
*/ | |
bool transmit(RcCode *codes, char *name) | |
{ | |
RcCode *code = codes; | |
// Search for the named code... | |
while (code->name) { | |
// If the named code matches in the dictionary... | |
if (0==strcmp(code->name, name)) { | |
// Repeat the transmission X times | |
int rpt; | |
for (rpt=0; rpt<TX_REPETITIONS; rpt++) { | |
// Transmit the code block once | |
transmitCode(code); | |
} | |
// It worked! | |
return true; | |
} | |
// Try the next code... | |
code++; | |
} | |
// It failed :( | |
return false; | |
} | |
WiFiClient client; | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USER, MQTT_PASS); | |
Adafruit_MQTT_Subscribe rf_sub = Adafruit_MQTT_Subscribe(&mqtt, MQTT_CONTROL_TOPIC, MQTT_QOS_1); | |
Adafruit_MQTT_Publish rf_pub = Adafruit_MQTT_Publish(&mqtt, MQTT_STATUS_TOPIC); | |
// Function to connect and reconnect as necessary to the MQTT server. | |
// Should be called in the loop function and it will take care if connecting. | |
void MQTT_connect() | |
{ | |
int8_t ret; | |
// Stop if already connected. | |
if (mqtt.connected()) { | |
return; | |
} | |
Serial.print("Connecting to MQTT... "); | |
uint8_t retries = 3; | |
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println("Retrying MQTT connection in 5 seconds..."); | |
mqtt.disconnect(); | |
delay(5000); // wait 5 seconds | |
retries--; | |
if (retries == 0) { | |
// basically die and wait for WDT to reset me | |
while (1); | |
} | |
} | |
Serial.println("MQTT Connected!"); | |
rf_pub.publish("OK"); | |
} | |
void receive_mqtt_transmit(char *data, uint16_t len) | |
{ | |
transmit(codes, data); | |
} | |
void setup() { | |
pinMode(TX_PIN, OUTPUT); | |
Serial.begin(115200); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
rf_sub.setCallback(receive_mqtt_transmit); | |
mqtt.subscribe(&rf_sub); | |
} | |
void loop() { | |
Adafruit_MQTT_Subscribe *subscription; | |
MQTT_connect(); | |
mqtt.processPackets(10000); | |
if(! mqtt.ping()) { | |
mqtt.disconnect(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment