Skip to content

Instantly share code, notes, and snippets.

@gingemonster
Created March 30, 2022 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingemonster/c26cc07c30614d414e84294cc6d4dd89 to your computer and use it in GitHub Desktop.
Save gingemonster/c26cc07c30614d414e84294cc6d4dd89 to your computer and use it in GitHub Desktop.
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
MQTT client("XXX.XXX.XXX.XXX", 1883, callback);
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int count = 0;
char previousmoves[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};;
void setup()
{
Serial.begin(9600);
Serial.println("setting up");
Particle.function("moveclock", moveclock);
Particle.function("playlast", playbackPreviousTenMoves);
Serial.println("connecting mqqt client");
connectmqqtclient();
}
void connectmqqtclient(){
if(!client.isConnected() ) {
client.connect("weaslyclock", "username", "password");
if( client.isConnected() ) {
Serial.println( "Connected." );
client.subscribe("topicname");
}
}
}
void loop() {
if( client.isConnected() ) {
client.loop();
}
else{
// reconnect
connectmqqtclient();
}
}
int moveclock(String command){
myservo.attach(D0); // attaches the servo on the D0 pin to the servo object
addMoveToPreviousMoves(command.charAt(0));
moveServo(command);
return 0;
}
void moveServo(String command){
moveServo(command, true);
}
void moveServo(String command, bool pauses){
Serial.println("moving servo with command - " + command);
if(command == "1") // home
{
myservo.write(45);
}
else if(command == "2") // work
{
myservo.write(15);
}
else if(command == "3") // london
{
myservo.write(9);
}
else if(command == "4") // travelling
{
myservo.write(29);
}
else if(command == "5") // meeting
{
myservo.write(74);
}
else if(command == "6") // church
{
myservo.write(88);
}
else if(command == "7") // tweeting
{
myservo.write(56);
if(pauses){
delay(5000);
myservo.detach(); // to stop servo straining
delay(60000); // wait 1 minute
}
myservo.attach(D0);
moveServo(findPreviousMoveNotMeetingOrTweeting()); // go back to previous location
}
if(pauses){
delay(5000);
myservo.detach(); // to stop servo straining
}
}
String findPreviousMoveNotMeetingOrTweeting(){
for(int i=1;i<10;i++){ // start a 1 as zero contains current move
if(String(previousmoves[i]) != "5" && String(previousmoves[i]) != "7" ){
return String(previousmoves[i]);
}
}
return "1"; // its possible all 10 previous moves are meetings or tweeting so got to return something!
}
void addMoveToPreviousMoves(char move){
// move items in previous moves along one and insert latest move at position zero
Serial.println("adding move to previous moves - " + String(move));
char tmpArray[10];
tmpArray[0] = move;
for(int i=0;i<9;i++){
tmpArray[i+1] = previousmoves[i];
}
memcpy(previousmoves, tmpArray, 10);
}
int playbackPreviousTenMoves(String command){
Particle.publish("playback last ten start");
for(int i=1;i<10;i++){
Particle.publish(String(i) + " move is " + String(previousmoves[i]));
if(previousmoves[i] != '0'){
myservo.attach(D0);
moveServo(String(previousmoves[i]));
delay(500);
myservo.detach(); // to stop servo straining
}
else{
break;
}
}
return 0;
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("mqqtcallback");
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Particle.publish("mqqtcallback: " + String(p));
if(String(p) == "playback"){
playbackPreviousTenMoves(p);
}
else{
moveclock(p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment