这是我的第一篇文章,请告诉我哪里出错了。
这是我的设置。 我有一个传感器通过SCL和STA连接到ESP8266开发套件。 我的语言是arduino。这样可以,我可以将温度和湿度输出到串行监视器中。我还有一个覆盆子pi(3?),目前作为一个wifi路由器。我使用hostapd来做到这一点。我还注册了amazon AWS,并让AWSIoT与pi连接,我有mosquitto作为经纪人运行。我的目标是能够将pi和esp与传感器放在一个位置,pi与以太网连接,并让温度和湿度来到kibana浏览器,我能够看到数据。
这是我的问题。 mosquitto与mosquitto_sub和mosquitto_pub客户端一起工作。但是,我的arduino代码无法连接。我怀疑这是IP地址的问题,所以我咨询了互联网,并获得了许多测试站点,但没有人告诉我在哪里可以找到作为mqtt代理服务器的IP。我已经包含了我的代码,而且我住在日本,所以不要被时间设置吓到。正如你所看到的,我一直在尝试许多不同的东西:/。
#include "SparkFun_Si7021_Breakout_Library.h"
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <time.h>
#include <WiFiUdp.h>
float humidity = 0;
float tempf = 0;
const char* SSID = "ssid";
const char* PASSWORD = "password";
const int MQTTPORT = 1883;
我在这里尝试了很多不同的ips,但没有一个能够工作。 (使用下面的const char *格式)(我排除它们的原因是为了安全(对吧?))
//const char* MQTTSERVER = "mosquitto.service";
//const char* MQTTSERVER = "systemctl";
//const char* MQTTSERVER = "bridgeawsiot2";
const char* MQTTINITTOPIC = "/dev/init/msg/";
const char* MQTTSNTOPIC = "/ras-xdv2/Si7021";
const char* MQTTINITTOPICMSG = "{\"dev\"=\"Si7021\",\"msg\"=\"init\"}";
// APRIME
WiFiUDP ntpUDP;
const char* NTPSERVER = "ntp.nict.jp";
const int NTPTIMEOFFSET = 9 * 60 * 60;
WiFiClient espClient;
PubSubClient client(espClient);
//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather sensor;
//---------------------------------------------------------------
void setup_wifi() {
delay(10);
Serial.print("WiFi CONNECTION...");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print("Waiting...");
}
randomSeed(micros());
Serial.println(" ");
Serial.println("WiFi connected");
Serial.println("ESP8266 ip:");
Serial.println(WiFi.localIP());
}
//---------------------------------------------------------------
void mqttConnect() {
// client.setServer(MQTTSERVER, MQTTPORT);
while (!client.connected()) {
Serial.print("MQTT communications need more time...");
String clientId = "ESP8266-";
clientId += String(random(0xffff));
delay(3000);
if (client.connect(clientId.c_str())) {
Serial.println("MQTT communications online!");
client.publish(MQTTINITTOPIC, MQTTINITTOPICMSG);
} else {
Serial.print("MQTT communications unavailible, rc=");
Serial.print(client.state());
delay(2000);
Serial.println("Attempting connection in t-2 Seconds...");
delay(2000);
}
}
}
//---------------------------------------------------------------
void setup()
{
Serial.begin(115200);
sensor.begin();
setup_wifi();
client.setServer(MQTTSERVER, MQTTPORT);
delay(5000);
/*if (!sensor.begin()) {
Serial.print("sensor ded :(");
while (1);
}*/
configTime( NTPTIMEOFFSET, 0, NTPSERVER );
}
//---------------------------------------------------------------
void loop()
{
time_t t = time(NULL);
struct tm *tm;
tm = localtime(&t);
char dt[25];
sprintf(dt, "%04d-%02d-%02dT%02d:%02d:%02d+09:00", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
Serial.printf(dt);
if(!client.connected()){
mqttConnect();
} else {
client.loop();
}
char pub_json[100];
//Get readings from all sensors
getWeather();
printInfo();
delay(10000);
char tempfbuf[4];
char humiditybuf[4];
sprintf(pub_json,"{\"Temperature\":%s,\"Humidity\": %s, \"@timestamp\": %s}]}",dtostrf(tempf, 4, 2, tempfbuf), dtostrf(humidity, 4, 2, humiditybuf), dt);
client.publish(MQTTSNTOPIC, pub_json);
Serial.println("TEMPERATURE BEING PUBLISHED...");
Serial.println(pub_json);
delay(60000 * 10);
}
//---------------------------------------------------------------
void getWeather()
{
// Measure Relative Humidity from the HTU21D or Si7021
humidity = sensor.getRH();
// Measure Temperature from the HTU21D or Si7021
tempf = sensor.getTemp();
// Temperature is measured every time RH is requested.
// It is faster, therefore, to read it from previous RH
// measurement with getTemp() instead with readTemp()
}
//---------------------------------------------------------------
void printInfo()
{
//This function prints the weather data out to the default Serial Port
Serial.print("Temp:");
Serial.print(tempf);
Serial.print("F, ");
Serial.print("Humidity:");
Serial.print(humidity);
Serial.println("%");
}
当我第一次下载mosquitto时,mosquitto.conf文件是不存在的,所以我创建了它并使用了它:
请告诉我是否发布了敏感信息:我是新手:)
c4message_size_limit 0
clientid bridgeawsiot2
persistence true
persistence_file mosquitto.db
persistence_location /var/mosquitto/
log_type all
connection_messages true
log_timestamp true
allow_anonymous true
password_file /etc/mosquitto/conf.d/users
allow_anonymous true
listener 9001 127.0.0.1
protocol websockets
connection_messages true
log_timestamp true
listener 1883
connection <awsiot>
address yl42kju76zjjodsbm6nfl4yycq.ap-northeast-1.es.amazonaws.com:8883
topic /esp8266/Si7021/slack out 1
cleansession true
notifications false
start_type automatic
bridge_cafile /etc/mosquitto/certs/root-CA.crt
bridge_certfile /etc/mosquitto/certs/RAS-XD.cert.pem
bridge_keyfile /etc/mosquitto/certs/RAS-XD.private.key
也许问题是websockets的事情? 任何帮助表示赞赏!
字面上任何:)
答案 0 :(得分:0)
I am doing some pretty good work with the esp8266 and mqtt using arduino IDE. I have mine logging on to my own mqtt broker I set up in a centos 7 box with encryption using x509 certs.
I use mosquitto-auth-plug to manage user access with a mysql database set up on the same machine
when you think about it the operating system is free, mosquitto broker is free, and mysql server is free.
the only expense to having your own setup is a fixed IP and fast enough connection. Which you could set up pretty cheap in a virtual machine on azure.
Anyhow when I first started a friend (the guy who sold me my first nodemcu .9 board). Hooked me up with this script and I have been able to get it all done using this as my example.
the part I liked was if you monitored your mqtt from a client like mqtt box or others. When the chip first starts it sends payload "Start" to topic "device_name/status". This way you can always tell when it's online.
I copied the notes he gave me they are located below.
good luck
script(type='text/javascript', src='https://www.gstatic.com/charts/loader.js')
#chart_div
My code sets up it's own device name, subscribes to the MQTT topics "network/broadcast", "network/(device name)".
When it starts it sends "(device_name)/status" with a payload of "start".
When it gets a "network/broadcast" with a payload of "discover" it replies by publishing a "network/presence" and its name.
Basically, you sent MQTT events with client.publish(topic, payload).
You subscribe to events with client.subscribe(topic).
When an event is received that matches your subscription it will call mqtt_callback and give it the topic, the payload, and the length.
Hope that helps.