Skip to content

Instantly share code, notes, and snippets.

@davedarko
Created January 14, 2024 10:40
Show Gist options
  • Save davedarko/6bf7554d847c04405423aa844f9eb25c to your computer and use it in GitHub Desktop.
Save davedarko/6bf7554d847c04405423aa844f9eb25c to your computer and use it in GitHub Desktop.
fixed? code example for pusher client library
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PusherClient.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//PusherClient client;
Servo leftServo;
Servo rightServo;
#define WIZ_CS 10 // feather
void setup() {
pinMode(2,OUTPUT);
leftServo.attach(2);
pinMode(3, OUTPUT);
rightServo.attach(3);
leftServo.write(95);
rightServo.write(95);
Serial.begin(115200);
while(!Serial);
Serial.println("Lets go");
Ethernet.init(WIZ_CS);
if (Ethernet.begin(mac) == 0) {
Serial.println("Init Ethernet failed");
for(;;)
;
}
if(Pusher.connect()) {
Pusher.bind("forward", moveForward);
Pusher.bind("backward", moveBackward);
Pusher.bind("turn_left", turnLeft);
Pusher.bind("turn_right", turnRight);
Pusher.bind("stop", stopMoving);
Pusher.subscribe("robot_channel");
Serial.println("subscribed");
}
else {
while(1) {}
}
}
void loop() {
Ethernet.maintain();
if (Pusher.connected()) {
Pusher.monitor();
}
else {
leftServo.write(95);
rightServo.write(95);
}
}
void moveForward(const String& eventName, const String& eventData) {
leftServo.write(0);
rightServo.write(180);
Serial.println("moveForward");
}
void moveBackward(const String& eventName, const String& eventData) {
leftServo.write(180);
rightServo.write(0);
Serial.println("moveBackward");
}
void turnLeft(const String& eventName, const String& eventData) {
leftServo.write(0);
rightServo.write(0);
Serial.println("turnLeft");
}
void turnRight(const String& eventName, const String& eventData) {
leftServo.write(180);
rightServo.write(180);
Serial.println("turnRight");
}
void stopMoving(const String& eventName, const String& eventData) {
leftServo.write(95);
rightServo.write(95);
Serial.println("stopMoving");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment