Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
Locked Re: MQTT Connection in JMRI
¿ªÔÆÌåÓýMy WIP codeSee comments for my thinking on how to iterate into features, but the idea for me is that I am going to make a second q that allows these things to ¡°self config¡± Then maybe on that q I also send into these things from node red a polling request, then I can control when and how (maybe even combine the setup and poll message to make these sensors VERY flexible) But for now I have hardcoded some variable. My q pattern is /trains/sensors/MAC.pin Payload is JSON that mimics the JMRI one with an exception, I change the number to a word, and I am fixing that in mode red. Again my idea here is to make this as plug and play as I can. I don¡¯t want to write code specific to each of my ESPs I just want to load them up and be done. Of course the ip of the server is a pain, but I have yet to see a zero conf enabled MQTT¡. If you want to make this work directly with JMRI and you assume the MQTT is running on the same IP, you could scan for a zero config that comes from JMRI, use that IP and change this code to conform directly to the JMRI format. Since node red is awesome I always use it as something in the middle to transform and I use it to blink LEDs and other things in my system. /* ? ?Nathan end of 2018 ? ?Idea is to make code that can be pushed to any esp 2886 ? ?that auto forms a sensor net for model rail ? ?the first part is not done: ? ?have a zero conf mqtt config q that the devices get their infor from to have them act normal ? ?instead I am just hard coding it all ? ?other todos ? ?some kind of debouncing - not realy debouncing, but I have not yet though about how I want the fliping of state to handled ? ?as of right now these messages are managed by node-red and then go into JMRI since JMRI doenst have a uniform interface ? ?ead the device type and make a list of input pins (the idea is to make this thing super automatic) right now I am just hard coding them ? ?Adafruit Feather HUZZAH ESP8266 pins: #0, #2, #4, #5, #12, #13, #14, #15, #16 ? ?{"type":"sensor","data":{"name":"IS2","state":4}} ? ?{"type":"sensor","data":{"name":"IS2","state":4}} ? ?this is designed to run every loopDelay ms and report all the sensors on pins in usablePins[] ? ?I have thought that I should only report changes, but it seems to be more reliable to report all and often, I need to think about this ? ?which is why debouncePins is there - starting to think through how I only report changes. ? ?and a third idea is an API to request a read using the q this thing is susbribed to they way I can control the polling from node red */ #include "EspMQTTClient.h" #include <SoftwareSerial.h> String payload = "I'm here! " + WiFi.macAddress(); int lastreading = 0; String topicPrefix = "trains/sensors/"; void onConnectionEstablished(); String sensorPrefixJSON = "{\"type\":\"sensor\",\"data\":{\"name\":\""; String sensorInFixJSON = "\",\"state\":"; String sensorSuffixJSON = "}}"; int usablePins[] = {4,5,12,14}; int debouncePins[] = {}; int loopDelay = 250; EspMQTTClient client( ? ¡°Flopper", ? ? ?// ssid ? ¡°Flipper", ? ? ? ? ? ?// password ? "192.168.20.78", ? ? ? ? // MQTT ip ? 1883, ? ? ? ? ? ? ? ? ? ?// MQTT broker port ? "", ? ? ? ? ? ? ? ? ? ? ?// MQTT username ? "", ? ? ? ? ? ? ? ? ? ? ?// MQTT password ? WiFi.macAddress(), ? ? ? // Client name ? onConnectionEstablished, // Connection established callback ? true, ? ? ? ? ? ? ? ? ? ?// Enable web updater ? true ? ? ? ? ? ? ? ? ? ? // Enable debug messages ); void setup() { ? Serial.begin(115200); } void onConnectionEstablished() { ? /* ? ? ?in here I want to call a special queue/config somehow so this thing auto configs ?? ? String subTopic = topicPrefix + "#"; ? client.subscribe(subTopic, [] (const String & payload) ? { ? ? //Serial.println(payload); ? }); */ } void loop() { ? client.loop(); ? int arraySize = sizeof(usablePins) / sizeof(usablePins[0]); ? for (int x = 0; x < arraySize; x++) { ? ? int pinToUse = usablePins[x]; ? ? int reading = digitalRead(pinToUse); ? ? String topic = topicPrefix + WiFi.macAddress() + "/" + pinToUse; ? ? if (reading == 1) { ? ? ? String publishJSON = sensorPrefixJSON + WiFi.macAddress() + "." + pinToUse + sensorInFixJSON + "OFF" + sensorSuffixJSON; ? ? ? client.publish(topic, publishJSON); ? ? } else if (reading == 0) { ? ? ? String publishJSON = sensorPrefixJSON + WiFi.macAddress() + "." + pinToUse + sensorInFixJSON + "ON" + sensorSuffixJSON; ? ? ? client.publish(topic, publishJSON); ? ? } ? } ? delay(loopDelay); }
|
to navigate to use esc to dismiss