This is the 12th of my Blogs for the Bluetooth Unleashed Design Challenge
The other posts are here :-
Concept
The idea is to detect the bluetooth transmitted from the vehicle and signal other Home Automation functions.
If the vehicle is known then it can open the garage door, and inform the home owner that xx is home.
Hardware
The detection point needs to be at the start of the driveway, and because there is no power source, this will need to be low power with solar charging.
The PSOC range seems a very good fit, but because of the timeline and my need to upskill, the inital design will be Arduino based and some form of RF transmitter/transceiver.
Adding a vehicle detection loop or beam is necessary to ensure those vehicles without bluetooth will also trigger the system.
In my quest to sort out the glue that joins all this together, I thought I'd concentrate on the indoor notification portion.
This is an ESP8266 with a neopixel display that runs around pretending to be some sort of clock, while it's waiting on a message.
You can see it here BT_Sentry : Notifications
Broker
The default settings for Mosquitto allow you to test without specifying the host name.
Within the Raspberry Pi I was able to prove that Mosquitto was working by Subscribing an Publishing and it played nice.
Once I tried to connect with the broker using an ESP8266, it all went downhill.
No matter what I tried, the connection was refused.
RPi IP address, default hostname, and some other weird and wonderful creations were no help.
I even changed the WiFi the ESP8266 was connecting to, thinking it was some routing issue.
Still no luck.
In desperation I tried one of the examples, and it connected perfectly to some external broker, so that proved it wasn't my network.
Change the broker in the example back to the Mosquitto and same issue again ... no connection to the broker.
IPV6
During this problem solving I discovered that for whatever reason there was an IVP6 connection listening to port 1883.
I couldn't recall setting this up, or even allowing it to occur, and worse it was still there when I stopped Mosquitto.
It seems that by default IPV6 in a Raspberry Pi is on.
While this is okay, what was a bit tricky was turning it off.
There were a few suggestions and eventually I found this discussion.
https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=138899&start=25
It involves changing the /etc/modprobe.d/ipv6.conf file to read :-
alias net-pf-10 off alias ipv6 off options ipv6 disable_ipv6=1 blacklist ipv6
A reboot and check of Netstat shows the TCP6 connections are gone.
There is something listening to 1883 but it still refused to connect.
I even went back and tried all the weird and wonderful mqtt_server settings I had tried before, but despite all my efforts, the connection was still being refused.
I tried stopping mosquitto with
sudo service mosquitto stop
followed by
sudo service mosquitto status
which showed that mosquitto was stopped.
During all this experimenting, the configuration file I showed on the other post was causing errors
(I've edited this to show the proper settings)
Restarting it from the command line gave this problem.
For whatever reason something was opening a port on 1883.
Typing netstat --help gave me the idea to use -e and get extended information.
There's my problem, OpenHAB is starting a MQTT listener and therefore I'm trying to connect to it.
Stop OpenHAB and check to see it has stopped.
sudo service openhab2 stop
and then check the status
sudo service openhab2 status
Note that OpenHAB does take a few minutes to stop, and in the meantime the prompt disappears into the ether.
recheck netstat.
Hurray, now we're getting somewhere.
Now restarting mosquitto works.
sudo service mosquitto restart
and the ESP8266 is able to connect
and when I publish something
At last some success.
/* Sketch to run a ESP3266 driven by time. This displays on a 12 x neopixel ring The colors of the 'hands' are: RED = Hours GREEN = Minutes BLUE = Seconds credit to https://www.hackster.io/thearduinoguy/esp8266-neopixel-ring-clock-a9cc74 TimeClient.h can be obtained from https://github.com/squix78/esp8266-weather-station Extract the TimeClient.h and TimeClient.cpp files and add them into the sketch folder (hence the "TimeClient.h") Mark Beckett */ #include <Adafruit_NeoPixel.h> #include <ESP8266WiFi.h> //#include "TimeClient.h" #include <PubSubClient.h> #define PIN D5 int ledsInString = 12; long lastUpdate = millis(); long lastSecond = millis(); String hours, minutes, seconds; int currentSecond, currentMinute, currentHour; const char ssid[] = "Your WiFi"; // your network SSID (name) const char pass[] = "Your WiFi password"; // your network password const char* mqttServer = "Your RPi IP address"; const int mqttPort = 1883; const char* mqttUser = ""; const char* mqttPassword = ""; const char* mqtt_topic = "OpenHAB_mqtt"; // topic is OpenHAB_mqtt const float UTC_OFFSET = 12; //TimeClient timeClient(UTC_OFFSET); Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledsInString, PIN); WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); Serial.println(); Serial.println(); strip.begin(); strip.setBrightness(128); strip.show(); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //timeClient.updateTime(); // updateTime() ; //lastUpdate = millis(); //lastSecond = millis(); delay(2000); // give system time client.setServer(mqttServer, mqttPort); client.setCallback(callback); while (!client.connected()) { Serial.println("Connecting to MQTT..."); if (client.connect("ESP8266Client")) { Serial.println("connected"); } else { Serial.print("failed with state "); Serial.print(client.state()); delay(2000); } } client.subscribe(mqtt_topic); } void loop() { // if ((millis() - lastUpdate) > 1800000) updateTime(); // // if ((millis() - lastSecond) > 1000) // { // // strip.setPixelColor((currentHour * 1), 0, 0, 0); // strip.setPixelColor((currentMinute / 5), 0, 0, 0); // strip.setPixelColor((currentSecond / 5), 0, 0, 0); // // strip.show(); // lastSecond = millis(); // currentSecond++; // if (currentSecond > 59) // { currentSecond = 0; // currentMinute++; // if (currentMinute > 59) { // currentMinute = 1; // currentHour++; // if (currentHour > 12) currentHour = 0; // } // } // String currentTime = String(currentHour) + ':' + String(currentMinute) + ':' + String(currentSecond); // Serial.println(currentTime); // // strip.setPixelColor((currentHour * 1), 255, 0, 0); // strip.setPixelColor((currentMinute / 5), 0, 255, 0); // strip.setPixelColor((currentSecond / 5), 0, 0, 255); // strip.show(); // } client.loop(); } //void updateTime() //{ // hours = timeClient.getHours(); // minutes = timeClient.getMinutes(); // seconds = timeClient.getSeconds(); // currentHour = hours.toInt(); // if (currentHour >= 12) currentHour = currentHour - 12; // currentMinute = minutes.toInt(); // currentSecond = seconds.toInt(); // lastUpdate = millis(); //} void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); Serial.print("Message:"); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); Serial.println("-----------------------"); }
This is my ESP8266 sketch, and I've commented out the time bits, to test the mqtt part.
Time
This discovery process has taken the best part of two days.
While it was frustrating at times, it has shown that having good examples, rather than detailed explanations is a better solution.
e.g The setting up of a configuration file https://mosquitto.org/man/mosquitto-conf-5.html states
However is this xxx.xxx.xxx.xxx or "xxx.xxx.xxx.xxx" or some other variation.
is there an equals sign, colon or something in between required?
I even saw a few trying to connect to tcp://xxx.xxx.xxx.xxx:1883
So including the actual syntax for each command does make it easier for anyone heading down this road.
I suspect their documentation is a WIP as the links don't seem to work (however I was using Firefox)
Mark
Top Comments