我正在使用ESP32 HiGrow Plant传感器板。
我稍微触摸了此固件:https://github.com/lucafabbri/HiGrow-Mongoose-OS-Firmware/blob/master/fs/init.js
要使其通过MQTT而不是HTTP发布传感器数据。
我正在使用自己的蚊子实例。
有了这些更改,我可以每5秒成功发布一次:
if (deviceId !== "" && connected) {
GPIO.write(statusLightPin, 0);
let higrowData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m};
let higrowData = JSON.stringify({
DeviceId: deviceId,
Temperature: t,
Humidity: h,
Moisture: m
});
let topic = 'devices/' + Cfg.get('device.id') + '/events';
let ok = MQTT.pub(topic, higrowData, 1);
print('Published:', ok, topic, '->', higrowData);
GPIO.write(statusLightPin, 1);
} else {
print("DeviceId:",deviceId,"Connected:",connected);
GPIO.write(statusLightPin, 1);
}
但是,在成功发布后,我尝试进入deepSleep时,设备便进入睡眠状态,但未发布...
if (deviceId !== "" && connected) {
GPIO.write(statusLightPin, 0);
let higrowData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m};
let higrowData = JSON.stringify({
DeviceId: deviceId,
Temperature: t,
Humidity: h,
Moisture: m
});
let topic = 'devices/' + Cfg.get('device.id') + '/events';
let ok = MQTT.pub(topic, higrowData, 1);
print('Published:', ok, topic, '->', higrowData);
if (ok) {
print('Success! Going to sleep');
ESP32.deepSleep(15000000); // 15 seconds
} else {
print('MQTT error');
}
GPIO.write(statusLightPin, 1);
} else {
print("DeviceId:",deviceId,"Connected:",connected);
GPIO.write(statusLightPin, 1);
}
答案 0 :(得分:0)
let ok = 1;
MQTT.setEventHandler(function(conn, ev ,edata) {
print ('MQTT event handler: connect', ev);
if (ev === 0) {
ok = MQTT.pub(topic, higrowData, 1);
print('Published:', ok, topic, '->', higrowData);
} else {
print('error');
}
}, null);
MQTT.setEventHandler(function(conn, ev ,edata) {
print ('MQTT event handler: write', ev);
if (ev === 204) {
print('Success! Going to sleep');
ESP32.deepSleep(120000000); // 120 seconds
}
}, null);
答案 1 :(得分:0)
遇到同样的问题。 MQTT调用必须是异步的,因此深度睡眠会在完成之前将其切断。
在使设备进入深度睡眠之前,我们可以使用事件处理程序从MQTT代理获取对发布的确认。 我依靠QOS设置为1来接收确认。 这与Dirks回答的概念相同
这是我想出的。完整的项目可在此处找到:https://gitlab.com/PseudoNet/higrow-sensor
...
let isConnected = false;
let hasPublished = false;
let qos = 1;//Must be set to 1
function publishData() {
let higrowdata = { deviceId: Cfg.get('higrow.id'), temperature: dht.getTemp(), humidity: dht.getHumidity(), water: ADC.read(SOILpin) };
Log.print(Log.INFO, JSON.stringify(higrowdata));
if (MQTT.pub('higrow/' + higrowdata.deviceId + '/temperature', JSON.stringify(higrowdata.temperature), qos)
&& MQTT.pub('higrow/' + higrowdata.deviceId + '/humidity', JSON.stringify(higrowdata.humidity), qos)
&& MQTT.pub('higrow/' + higrowdata.deviceId + '/water', JSON.stringify(higrowdata.water), qos)) {
Log.print(Log.INFO, 'Payload sent');
hasPublished = true;
}
}
//Wait for mqtt server connection
Timer.set(10000, Timer.REPEAT, function () {
if (isConnected) {
Log.print(Log.DEBUG, 'Poll Connected');
publishData();
} else {
Log.print(Log.DEBUG, 'No connecion...waiting');
}
}, null
);
MQTT.setEventHandler(function (conn, ev) {
if (ev !== 0) {
let evs = '?';
if (ev === MQTT.EV_CONNACK) {
evs = 'CONNACK'; // Connection to broker has been established
} else if (ev === MQTT.EV_PUBLISH) {
evs = 'PUBLISH'; // msg published to topics we are subscribed to
} else if (ev === MQTT.EV_PUBACK) {
evs = 'PUBACK'; // ACK for publishing of a message with QoS>0
} else if (ev === MQTT.EV_SUBACK) {
evs = 'SUBACK'; // ACK for a subscribe request
} else if (ev === MQTT.EV_UNSUBACK) {
evs = 'UNSUBACK'; // ACK for an unsubscribe request
} else if (ev === MQTT.EV_CLOSE) {
evs = 'CLOSE'; // connection to broker was closed
}
Log.print(Log.DEBUG, 'MQTT event:' + evs);
if (ev === MQTT.EV_CONNACK) {
Log.print(Log.DEBUG, 'MQTT Connected');
isConnected = true;
publishData();
} else if (ev === MQTT.EV_PUBACK) {
if (hasPublished) {
Log.print(Log.INFO, 'MQTT publish confirmed... init deep sleep');
ESP32.deepSleep(Cfg.get('higrow.interval_min')*60000000);
}
}
}
}, null);