[READ-ONLY] Mirror of https://github.com/jphastings/jan-poka. Jan Poka is a suite of tools for controlling custom internet-of-things devices that help make your distant friends feel closer.
geolocation hardware mqtt
0

Configure Feed

Select the types of activity you want to include in your feed.

Migrate mapper placeholder project to platformio

+149
+5
hardware/mapper/.gitignore
··· 1 + .pio 2 + .vscode/.browse.c_cpp.db* 3 + .vscode/c_cpp_properties.json 4 + .vscode/launch.json 5 + .vscode/ipch
+21
hardware/mapper/platformio.ini
··· 1 + ; PlatformIO Project Configuration File 2 + ; 3 + ; Build options: build flags, source filter 4 + ; Upload options: custom upload port, speed and extra flags 5 + ; Library options: dependencies, extra library storages 6 + ; Advanced options: extra scripting 7 + ; 8 + ; Please visit documentation for the other options and examples 9 + ; https://docs.platformio.org/page/projectconf.html 10 + 11 + [env:firebeetle32] 12 + platform = espressif32 13 + board = firebeetle32 14 + framework = arduino 15 + lib_deps = 16 + waspinator/AccelStepper@^1.61 17 + knolleary/PubSubClient@^2.8 18 + bblanchon/ArduinoJson@^6.18.5 19 + upload_port = /dev/cu.usbserial-0001 20 + monitor_port = /dev/cu.usbserial-0001 21 + monitor_speed = 115200
+116
hardware/mapper/src/main.cpp
··· 1 + #include <Arduino.h> 2 + #include <AccelStepper.h> 3 + #include <WiFi.h> 4 + #include <PubSubClient.h> 5 + #include <ArduinoJson.h> 6 + #include <math.h> 7 + #include "vars.h"; 8 + 9 + #define APP_NAME "jan-poka:mapper" 10 + #define ONE_ROTATION_STEPS 8192 11 + 12 + int INNER_DIR = 14; // D5 13 + int INNER_STP = 12; // D6 14 + int OUTER_DIR = 5; // D1 15 + int OUTER_STP = 4; // D2 16 + int DISABLE = 16; // D0 17 + int BOOTING = 2; // D4 18 + 19 + int MAX_SPEED = 9000; 20 + 21 + WiFiClient wifiClient; 22 + PubSubClient mqttClient(wifiClient); 23 + 24 + AccelStepper inner = AccelStepper(AccelStepper::DRIVER, INNER_STP, INNER_DIR); 25 + AccelStepper outer = AccelStepper(AccelStepper::DRIVER, OUTER_STP, OUTER_DIR); 26 + 27 + void setup() { 28 + Serial.begin(115200); 29 + pinMode(BOOTING, OUTPUT); 30 + digitalWrite(BOOTING, HIGH); 31 + pinMode(DISABLE, OUTPUT); 32 + digitalWrite(DISABLE, HIGH); 33 + 34 + mqttClient.setServer(MQTT_HOST, MQTT_PORT); 35 + 36 + inner.setMaxSpeed(MAX_SPEED); 37 + outer.setMaxSpeed(MAX_SPEED); 38 + 39 + mqttConnectionLoop(); 40 + Serial.println("\nBooted"); 41 + digitalWrite(BOOTING, LOW); 42 + } 43 + 44 + void mqttConnectionLoop() { 45 + if (!mqttClient.connected()) { 46 + if (!mqttClient.connect(APP_NAME, MQTT_USER, MQTT_PASS)) { 47 + Serial.println("Failed to connect to MQTT broker"); 48 + // TODO: Backoff 49 + return; 50 + } 51 + Serial.println("MQTT connected"); 52 + mqttClient.setCallback(handleGeoTarget); 53 + mqttClient.subscribe(MQTT_TOPIC); 54 + } 55 + mqttClient.loop(); 56 + } 57 + 58 + void handleGeoTarget(char* topic, byte* payload, unsigned int length) { 59 + /* Copied from EspMQTTClient: https://github.com/plapointe6/EspMQTTClient/blob/master/src/EspMQTTClient.cpp#L649 */ 60 + // Convert the payload into a String 61 + // First, We ensure that we dont bypass the maximum size of the PubSubClient library buffer that originated the payload 62 + // This buffer has a maximum length of _mqttClient.getBufferSize() and the payload begin at "headerSize + topicLength + 1" 63 + unsigned int strTerminationPos; 64 + if (strlen(topic) + length + 9 >= mqttClient.getBufferSize()) { 65 + strTerminationPos = length - 1; 66 + } else { 67 + strTerminationPos = length; 68 + } 69 + 70 + // Second, we add the string termination code at the end of the payload and we convert it to a String object 71 + payload[strTerminationPos] = '\0'; 72 + String payloadStr((char*)payload); 73 + /* end */ 74 + 75 + StaticJsonDocument<512> jsonDoc; 76 + DeserializationError err = deserializeJson(jsonDoc, payload); 77 + if (err != DeserializationError::Ok) { 78 + Serial.print("Unsuccessful at parsing MQTT JSON: "); 79 + Serial.println(err.f_str()); 80 + return; 81 + } 82 + goTo(jsonDoc["lat"], jsonDoc["ele"]); 83 + } 84 + 85 + void goTo(double azimuth, double elevation) { 86 + double zRotate = azimuth / 180.0; 87 + double xyRotate = elevation / 270.0; 88 + long innerRotation = floor(zRotate * ONE_ROTATION_STEPS); 89 + long outerRotation = floor((xyRotate - 2 * zRotate) * ONE_ROTATION_STEPS); 90 + 91 + long stepsForInner = innerRotation - inner.currentPosition(); 92 + long stepsForOuter = outerRotation - outer.currentPosition(); 93 + 94 + inner.moveTo(innerRotation); 95 + outer.moveTo(outerRotation); 96 + 97 + double innerSpeed = MAX_SPEED; 98 + double outerSpeed = MAX_SPEED; 99 + if (stepsForInner > stepsForOuter) { 100 + outerSpeed = innerSpeed * stepsForInner / stepsForOuter; 101 + } else { 102 + innerSpeed = outerSpeed * stepsForOuter / stepsForInner; 103 + } 104 + 105 + inner.setSpeed(innerSpeed); 106 + outer.setSpeed(outerSpeed); 107 + } 108 + 109 + void loop() { 110 + bool noMove = inner.distanceToGo() == 0 && outer.distanceToGo() == 0; 111 + digitalWrite(DISABLE, noMove ? HIGH : LOW); 112 + 113 + inner.runSpeedToPosition(); 114 + outer.runSpeedToPosition(); 115 + mqttConnectionLoop(); 116 + }
+7
hardware/mapper/src/vars.h
··· 1 + #define WIFI_SSID "The LAN before time" 2 + #define WIFI_PASS "" 3 + #define MQTT_HOST "mqtt.local" 4 + #define MQTT_PORT 1883 5 + #define MQTT_USER "" 6 + #define MQTT_PASS "" 7 + #define MQTT_TOPIC "home/geo/target"