Forum: Mikrocontroller und Digitale Elektronik Umschreiben auf locken MQTT Broker


von arduinoneuling (Gast)


Lesenswert?

Hallo,
ich habe folgenden sketch versucht:
1
/*
2
    ESP8266 RFID Reader - MQTT Sender
3
    Copyright 2016 Christian Moll <christian@chrmoll.de>
4
 
5
    This program is free software: you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 3 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
    Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
19
    der GNU General Public License, wie von der Free Software Foundation,
20
    Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
21
    veröffentlichten Version, weiterverbreiten und/oder modifizieren.
22
 
23
    Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
24
    OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
25
    Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
26
    Siehe die GNU General Public License für weitere Details.
27
 
28
    Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
29
    Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
30
*/
31
 
32
#include <ESP8266WiFi.h>
33
#include <WiFiClient.h>
34
#include <ESP8266WebServer.h>
35
#include <ESP8266mDNS.h>
36
#include <ESP8266HTTPUpdateServer.h>
37
 
38
#include <MQTTClient.h>
39
#include <SPI.h>
40
#include <MFRC522.h>
41
 
42
#define SS_PIN D8 //Pin on WeMos D1 Mini
43
#define RST_PIN D3 //Pin on WeMos D1 Mini
44
 
45
const char* host = "rfid_reader"; // will also be used on shiftr.io
46
const char* ssid = "ssid";
47
const char* password = "passphrase";
48
 
49
const char* brocker = "broker.shiftr.io";
50
const char* mqttUser = "mqttuser";
51
const char* mqttPass = "mqttpassword";
52
 
53
ESP8266WebServer httpServer(80);
54
ESP8266HTTPUpdateServer httpUpdater;
55
 
56
WiFiClient wifi;
57
MQTTClient mqtt;
58
 
59
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
60
MFRC522::MIFARE_Key key;
61
 
62
unsigned int batt;
63
double battV;
64
unsigned long oldMillis;
65
 
66
void connect();
67
 
68
void setup(void){
69
  SPI.begin();
70
  rfid.PCD_Init();
71
 
72
  Serial.begin(115200);
73
  Serial.println();
74
  Serial.println("Booting Sketch...");
75
  WiFi.mode(WIFI_AP_STA);
76
  WiFi.begin(ssid, password);
77
 
78
  mqtt.begin(brocker, wifi);
79
  mqtt.publish("/rfid_reader/resetReason", ESP.getResetReason());
80
 
81
  connect();
82
 
83
  MDNS.begin(host);
84
 
85
  //Attach handles for different pages.
86
  httpUpdater.setup(&httpServer);
87
 
88
  httpServer.on("/", handleRoot);
89
 
90
  httpServer.begin();
91
 
92
  MDNS.addService("http", "tcp", 80);
93
  Serial.println("Up and running!");
94
}
95
 
96
void loop(void){
97
  if(!mqtt.connected()) {
98
    connect();
99
  }
100
 
101
  httpServer.handleClient();
102
  mqtt.loop();
103
  delay(10);
104
 
105
  handleRFID();
106
 
107
  //things that should only be transmitted all 60 sec
108
  if (millis()-oldMillis > 60000) {
109
    batt = analogRead(A0);
110
    battV = mapDouble(batt, 0, 1023, 0.0, 6.6);
111
    mqtt.publish("/rfid_reader/batt", String(battV));
112
    mqtt.publish("/rfid_reader/battRaw", String(batt));
113
 
114
    oldMillis = millis();
115
  }
116
}
117
 
118
void connect() {
119
  while(WiFi.waitForConnectResult() != WL_CONNECTED){
120
    WiFi.begin(ssid, password);
121
    Serial.println("WiFi failed, retrying.");
122
  }
123
 
124
  Serial.print("IP address: ");
125
  Serial.println(WiFi.localIP());
126
 
127
  while (!mqtt.connect(host, mqttUser, mqttPass)) {
128
    Serial.print(".");
129
  }
130
  Serial.println("\nconnected!");
131
}
132
 
133
void handleRoot() {
134
  httpServer.send(200, "text/plain", "It works!!!");
135
}
136
 
137
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
138
  Serial.print("incoming: ");
139
  Serial.print(topic);
140
  Serial.print(" - ");
141
  Serial.print(payload);
142
  Serial.println();
143
}
144
 
145
void handleRFID() {
146
  if (!rfid.PICC_IsNewCardPresent()) return;
147
  if (!rfid.PICC_ReadCardSerial()) return;
148
  mqtt.publish("/rfid_reader/uid", printHex(rfid.uid.uidByte, rfid.uid.size));
149
  rfid.PICC_HaltA();
150
  rfid.PCD_StopCrypto1();
151
}
152
 
153
double mapDouble(double x, double in_min, double in_max, double out_min, double out_max)
154
{
155
  double temp = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
156
  temp = (int) (4*temp + .5);
157
  return (double) temp/4;
158
}
159
 
160
String printHex(byte *buffer, byte bufferSize) {
161
  String id = "";
162
  for (byte i = 0; i < bufferSize; i++) {
163
    id += buffer[i] < 0x10 ? "0" : "";
164
    id += String(buffer[i], HEX);
165
  }
166
  return id;
167
}
http://lazyzero.de/elektronik/esp8266/rfid_mqtt/start

ich hab es versucht auf meinen lokalen broker umzuschreiben aber ich 
bekomme es nicht hin. Mein lokaer MQTT Broker mosquito läuft auf 
192.168.178.2:1883 kein passwort oder user.

kann mir jdm helfen??

danke

von Uwe D. (monkye)


Lesenswert?

Hast Du schon recherchiert ob es erlaubt ist bei dieser Implementierung 
ohne User/Password zu agieren?

von Michael U. (amiga)


Lesenswert?

Hallo,

wie weit kommst Du denn?

Eigentlich sollte es reichen, wenn Du
const char* brocker = "192.168.178.2";

setzt und den Aufruf

  while (!mqtt.connect(host, mqttUser, mqttPass)) {
auf
  while (!mqtt.connect(host)) {
änderst wenn Du kein Passwort benutzt.
Verschlüsselung wird nicht benutzt.

Sag doch mal, wo er hängenbleibt. WiFi connect ist ok?
Broker connect?

Gruß aus Berlin
Michael

: Bearbeitet durch User
von RP6conrad (Gast)


Lesenswert?

Ich verwende eine andere MQTT library  : PubSubClient.h  Damit 
functioniert das :
1
#include <ESP8266WiFi.h>
2
#include <PubSubClient.h> 
3
#define mqtt_server "192.168.0.60"
4
#define mqtt_user "micro"
5
#define mqtt_password "password"
6
WiFiClient espClient;
7
PubSubClient client(espClient);
8
IPAddress ip(192, 168,0,66);//DHT22 was 69
9
IPAddress gateway(192, 168, 0, 1);
10
IPAddress subnet(255, 255, 255, 0); 
11
WiFi.config(ip, gateway, subnet);
12
WiFi.begin(ssid, password);
13
void loop(){
14
/*****Every minute, do measurements and dump to MQTT on the Pi*********************/
15
if (now - lastMillis_MQTT >= MQTT_interval) {
16
        lastMillis_MQTT = now;  
17
 if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
18
            Serial.println("connected");
19
            client.publish(humidity_topic, String(h_dht22).c_str(), true);
20
            client.publish(temp_topic, String(t_dht22).c_str(), true);
21
}
Das ist hier nicht komplett, nur ein beispiel. Ich kann sie auch die 
complette code senden bei Interesse. Den Wemos lest DHT22 aus, DS18B20, 
Regenmesser, und jeden stunde wird nach eine MySQL data geschickt, jeden 
Minute nach MQTT auf eine Raspberry.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.