Window-Door-sensor-Version-5 icon indicating copy to clipboard operation
Window-Door-sensor-Version-5 copied to clipboard

MQTT CODE SUPPORT

Open VarunKumaran opened this issue 3 years ago • 14 comments

Hiiii there!!! I really love your low device and would like to do the same using only MQTT to send a message to my MQTT broker(raspberry Pi) when ever the state of the door sensor changes... Can you please help me with the code for MQTT??

And secondly can u please explain the calculation part for the battery voltage value

myData.vBatt = (analogRead(A0) * 4.2 * 10 / 1023);`

VarunKumaran avatar Dec 07 '22 19:12 VarunKumaran

Hi, You can take any MQTT sketch example and use it so that it sends the message and then just remember to set GPIO16 to switch off the supply when you've finished.

I assume you're aware that it will use more battery power sending MQTT via your router and take longer, around 7 seconds, to send the message.

The ADC read takes the value from ADC(0) and converts it to an integer value by multiplying it by a factor of ten then divde it by 10 when you deal with the received message, i.e. a value of 600 from the ADC would be aout 2.46V but using the calculation above the message sent would be 24.

gadjet avatar Dec 07 '22 20:12 gadjet

I'll confirm what Gadjet is saying - you want espnow first. The delay to connect upon startup and then publish is too long.. when the window sensor is off it's disconnected from Wi-Fi so when it wakes up it needs to reconnect. I have a work around though using two d1's acting as a gateway - one as a esp-now receiver and the other that's always connected to mqtt which will publish the topic. There's a total of 3 sketches; the window sensor, the espnow slave, and mqtt gateway. I'm also using node-red to notify/alert to a Sonos device using tts. It's been working great. I'll see if I can put this all together soon to share.

steelsparky avatar Dec 07 '22 23:12 steelsparky

For anyone reading this and asking themselves why this weird calculation for the input voltage happens:

  • Why divide by 1023? The ADC pin of the ESP12 only operates at a range of 0 to 1V and in software it gives you a 10 bit unsigned value, so 0 to 1023. Thus, if you read for example 345, the input voltage is: 345 / 1023 ~ 0,337V.
  • Why multiply by 10? As gadjet explained above, that is apparently his way of putting a decimal number into a non-decimal number. By multiplying by 10 and then converting to integer, you'll keep one decimal. Obvioulsy, you need to transform it back after receiving the value.
  • Why multiply by 4.2V? Well, to measure any voltage higher than 1V, you need a voltage divider, i.e. you need to bring your maximum voltage down to 1V. That is exactly what gadjet did in this design. ADC connects to a voltage divier formed by a R1 = 3.2MOhm and a R2 = 1MOhm resistor. The output voltage at VDC (in the middle of the voltage divider) is V_in * R2 / (R1+ R2). Notice, that because of the fraction all factors of 10 for the resistor values cancel each other out. So, the same result could have been achieved with 320Ohm and 100Ohm or 3.2kOhm and 1kOhm resistors. I guess the high resistances were chosen to limit the current going into ADC. Anyway, 100/(100+320) = 1/4.2, thus this voltage divider maps the range of 0 to 4.2V to 0 to 1V. So, to reverse this mapping in the software, you multiply by 4.2. Note that this assumes you use a battery whose maximum voltage at 100% is 4.2V.

thematrixincendies avatar Aug 27 '23 09:08 thematrixincendies

thematrixincendies Thanks for the detailed explanation, the main reason for the high resistance of the potential divider network is to reduce the quiescent current drain on the battery as it is permanently connected.

gadjet avatar Aug 27 '23 11:08 gadjet