some helps when login with credentials

This commit is contained in:
1technophile authored and GitHub committed 2016-09-18 08:51:13 +02:00
1 parent 0f8c8b44fd
commit fc9e8815dd
1 file changed
+237 -237
+237 -237
View File
@@ -1,238 +1,238 @@
/* /*
433toMQTTto433 - ESP8266 program for home automation 433toMQTTto433 - ESP8266 program for home automation
Act as a wifi gateway between your nodes and a MQTT broker Act as a wifi gateway between your nodes and a MQTT broker
Send and receiving command by MQTT Send and receiving command by MQTT
This program enables to: This program enables to:
- receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data - receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data
- publish MQTT data to a different topic related to received 433Mhz signal - publish MQTT data to a different topic related to received 433Mhz signal
Contributor: Contributor:
- 1technophile - 1technophile
Based on: Based on:
- MQTT library (https://github.com/knolleary) - MQTT library (https://github.com/knolleary)
- RCSwitch (https://github.com/sui77/rc-switch) - RCSwitch (https://github.com/sui77/rc-switch)
- ESP8266Wifi - ESP8266Wifi
Project home: https://github.com/1technophile/433toMQTTto433_ESP8266 Project home: https://github.com/1technophile/433toMQTTto433_ESP8266
Blog: https://1technophile.blogspot.com/ Blog: https://1technophile.blogspot.com/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction, and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Some usefull commands to test gateway with mosquitto Some usefull commands to test gateway with mosquitto
Subscribe to the subject for data receiption from RF signal Subscribe to the subject for data receiption from RF signal
mosquitto_sub -t home/433toMQTT mosquitto_sub -t home/433toMQTT
Send data by MQTT to convert it on RF signal Send data by MQTT to convert it on RF signal
mosquitto_pub -t home/MQTTto433/ -m 1315153 mosquitto_pub -t home/MQTTto433/ -m 1315153
*/ */
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <RCSwitch.h> // library for controling Radio frequency switch #include <RCSwitch.h> // library for controling Radio frequency switch
RCSwitch mySwitch = RCSwitch(); RCSwitch mySwitch = RCSwitch();
//Do we want to see trace for debugging purposes //Do we want to see trace for debugging purposes
#define TRACE 1 // 0= trace off 1 = trace on #define TRACE 1 // 0= trace off 1 = trace on
// Update these with values suitable for your network. // Update these with values suitable for your network.
#define wifi_ssid "mywifiid" #define wifi_ssid "mywifiid"
#define wifi_password "mypwd" #define wifi_password "mypwd"
#define mqtt_server "192.168.0.22" #define mqtt_server "192.168.0.22"
#define mqtt_user "your_username" // not compulsory #define mqtt_user "your_username" // not compulsory if you set it uncomment line 127 and comment line 129
#define mqtt_password "your_password" // not compulsory #define mqtt_password "your_password" // not compulsory if you set it uncomment line 127 and comment line 129
//adding this to bypass to problem of the arduino builder issue 50 //adding this to bypass to problem of the arduino builder issue 50
void callback(char*topic, byte* payload,unsigned int length); void callback(char*topic, byte* payload,unsigned int length);
WiFiClient espClient; WiFiClient espClient;
// client parameters // client parameters
PubSubClient client(mqtt_server, 1883, callback, espClient); PubSubClient client(mqtt_server, 1883, callback, espClient);
//MQTT last attemps reconnection number //MQTT last attemps reconnection number
long lastReconnectAttempt = 0; long lastReconnectAttempt = 0;
// Callback function, when the gateway receive an MQTT value on the topics subscribed this function is called // Callback function, when the gateway receive an MQTT value on the topics subscribed this function is called
void callback(char* topic, byte* payload, unsigned int length) { void callback(char* topic, byte* payload, unsigned int length) {
// In order to republish this payload, a copy must be made // In order to republish this payload, a copy must be made
// as the orignal payload buffer will be overwritten whilst // as the orignal payload buffer will be overwritten whilst
// constructing the PUBLISH packet. // constructing the PUBLISH packet.
trc("Hey I got a callback "); trc("Hey I got a callback ");
// Allocate the correct amount of memory for the payload copy // Allocate the correct amount of memory for the payload copy
byte* p = (byte*)malloc(length); byte* p = (byte*)malloc(length);
// Copy the payload to the new buffer // Copy the payload to the new buffer
memcpy(p,payload,length); memcpy(p,payload,length);
// Conversion to a printable string // Conversion to a printable string
p[length] = '\0'; p[length] = '\0';
String callbackstring = String((char *) p); String callbackstring = String((char *) p);
String topicNameRec = String((char*) topic); String topicNameRec = String((char*) topic);
//launch the function to treat received data //launch the function to treat received data
receivingMQTT(topicNameRec,callbackstring); receivingMQTT(topicNameRec,callbackstring);
// Free the memory // Free the memory
free(p); free(p);
} }
void setup() void setup()
{ {
//Launch serial for debugging purposes //Launch serial for debugging purposes
Serial.begin(9600); Serial.begin(9600);
//Begining ethernet connection //Begining ethernet connection
setup_wifi(); setup_wifi();
//client.setServer(mqtt_server, 1883); //client.setServer(mqtt_server, 1883);
delay(1500); delay(1500);
lastReconnectAttempt = 0; lastReconnectAttempt = 0;
mySwitch.enableTransmit(4); // RF Transmitter is connected to Arduino Pin #10 mySwitch.enableTransmit(4); // RF Transmitter is connected to Arduino Pin #10
mySwitch.setRepeatTransmit(20); //increase transmit repeat to avoid lost of rf sendings mySwitch.setRepeatTransmit(20); //increase transmit repeat to avoid lost of rf sendings
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2 mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
} }
void setup_wifi() { void setup_wifi() {
delay(10); delay(10);
// We start by connecting to a WiFi network // We start by connecting to a WiFi network
trc("Connecting to "); trc("Connecting to ");
trc(wifi_ssid); trc(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password); WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
trc("."); trc(".");
} }
trc("WiFi connected"); trc("WiFi connected");
} }
boolean reconnect() { boolean reconnect() {
// Loop until we're reconnected // Loop until we're reconnected
while (!client.connected()) { while (!client.connected()) {
trc("Attempting MQTT connection..."); trc("Attempting MQTT connection...");
// Attempt to connect // Attempt to connect
// If you want to use a username and password, change next line to // If you want to use a username and password, change next line to
//if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) { //if (client.connect("433toMQTTto433", mqtt_user, mqtt_password)) {
// and set username and password at the program beginning // and set username and password at the program beginning
if (client.connect("433toMQTTto433")) { if (client.connect("433toMQTTto433")) {
// Once connected, publish an announcement... // Once connected, publish an announcement...
client.publish("outTopic","hello world"); client.publish("outTopic","hello world");
trc("connected"); trc("connected");
//Topic subscribed so as to get data //Topic subscribed so as to get data
String topicNameRec = String("home/MQTTto433/"); String topicNameRec = String("home/MQTTto433/");
//Subscribing to topic(s) //Subscribing to topic(s)
subscribing(topicNameRec); subscribing(topicNameRec);
} else { } else {
trc("failed, rc="); trc("failed, rc=");
trc(String(client.state())); trc(String(client.state()));
trc(" try again in 5 seconds"); trc(" try again in 5 seconds");
// Wait 5 seconds before retrying // Wait 5 seconds before retrying
delay(5000); delay(5000);
} }
} }
return client.connected(); return client.connected();
} }
void loop() void loop()
{ {
//MQTT client connexion management //MQTT client connexion management
if (!client.connected()) { if (!client.connected()) {
long now = millis(); long now = millis();
if (now - lastReconnectAttempt > 5000) { if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now; lastReconnectAttempt = now;
trc("client mqtt not connected, trying to connect"); trc("client mqtt not connected, trying to connect");
// Attempt to reconnect // Attempt to reconnect
if (reconnect()) { if (reconnect()) {
lastReconnectAttempt = 0; lastReconnectAttempt = 0;
} }
} }
} else { } else {
// MQTT loop // MQTT loop
client.loop(); client.loop();
} }
// Receive loop, if data received by RF433 send it by MQTT to MQTTsubject // Receive loop, if data received by RF433 send it by MQTT to MQTTsubject
if (mySwitch.available()) { if (mySwitch.available()) {
// Topic on which we will send data // Topic on which we will send data
trc("Receiving 433Mhz signal"); trc("Receiving 433Mhz signal");
String MQTTsubject = "home/433toMQTT"; String MQTTsubject = "home/433toMQTT";
long MQTTvalue; long MQTTvalue;
MQTTvalue=mySwitch.getReceivedValue(); MQTTvalue=mySwitch.getReceivedValue();
mySwitch.resetAvailable(); mySwitch.resetAvailable();
if (client.connected()) { if (client.connected()) {
trc("Sending 433Mhz signal to MQTT"); trc("Sending 433Mhz signal to MQTT");
trc(String(MQTTvalue)); trc(String(MQTTvalue));
sendMQTT(MQTTsubject,String(MQTTvalue)); sendMQTT(MQTTsubject,String(MQTTvalue));
} else { } else {
if (reconnect()) { if (reconnect()) {
sendMQTT(MQTTsubject,String(MQTTvalue)); sendMQTT(MQTTsubject,String(MQTTvalue));
lastReconnectAttempt = 0; lastReconnectAttempt = 0;
} }
} }
} }
} }
void subscribing(String topicNameRec){ // MQTT subscribing to topic void subscribing(String topicNameRec){ // MQTT subscribing to topic
char topicStrRec[26]; char topicStrRec[26];
topicNameRec.toCharArray(topicStrRec,26); topicNameRec.toCharArray(topicStrRec,26);
// subscription to topic for receiving data // subscription to topic for receiving data
boolean pubresult = client.subscribe(topicStrRec); boolean pubresult = client.subscribe(topicStrRec);
if (pubresult) { if (pubresult) {
trc("subscription OK to"); trc("subscription OK to");
trc(topicNameRec); trc(topicNameRec);
} }
} }
void receivingMQTT(String topicNameRec, String callbackstring) { void receivingMQTT(String topicNameRec, String callbackstring) {
trc("Receiving data by MQTT"); trc("Receiving data by MQTT");
trc(topicNameRec); trc(topicNameRec);
char topicOri[26] = ""; char topicOri[26] = "";
char topicStrAck[26] = ""; char topicStrAck[26] = "";
char datacallback[26] = ""; char datacallback[26] = "";
// Below you send RF signal following data value received by MQTT // Below you send RF signal following data value received by MQTT
// converting string to char for condition statement // converting string to char for condition statement
callbackstring.toCharArray(datacallback,26); callbackstring.toCharArray(datacallback,26);
trc(datacallback); trc(datacallback);
long int data = atol(datacallback); long int data = atol(datacallback);
trc("Send received data by RF 433"); trc("Send received data by RF 433");
trc(String(data)); trc(String(data));
//send received MQTT value by RF signal (example of signal sent data = 5264660), we activate the transmitter on pin 9 the time to send data by RF //send received MQTT value by RF signal (example of signal sent data = 5264660), we activate the transmitter on pin 9 the time to send data by RF
mySwitch.send(data, 24); mySwitch.send(data, 24);
} }
//send MQTT data dataStr to topic topicNameSend //send MQTT data dataStr to topic topicNameSend
void sendMQTT(String topicNameSend, String dataStr){ void sendMQTT(String topicNameSend, String dataStr){
char topicStrSend[26]; char topicStrSend[26];
topicNameSend.toCharArray(topicStrSend,26); topicNameSend.toCharArray(topicStrSend,26);
char dataStrSend[200]; char dataStrSend[200];
dataStr.toCharArray(dataStrSend,200); dataStr.toCharArray(dataStrSend,200);
boolean pubresult = client.publish(topicStrSend,dataStrSend); boolean pubresult = client.publish(topicStrSend,dataStrSend);
trc("sending "); trc("sending ");
trc(dataStr); trc(dataStr);
trc("to "); trc("to ");
trc(topicNameSend); trc(topicNameSend);
} }
//trace //trace
void trc(String msg){ void trc(String msg){
if (TRACE) { if (TRACE) {
Serial.println(msg); Serial.println(msg);
} }
} }