The most affordable way to remotely monitor temperature, humidity and pressure. All Sonoff TH variants


Texas Instruments has leadership positions in many segments of the semiconductor market. Following tradition, the company itself develops examples of the use of its electronic components ov and publishes materials on its website: theory, diagrams, reference designs, training videos, etc. Electronic components and finished boards (development kits and tools) are also sold there. TI also has its own forum e2e.ti.com and is supported third party resource www.43oh.com for development engineers and hobbyists. He actively works with schools and colleges, teaching microcontroller programming even to elementary school students.

However, our fans are little familiar with the remarkable products of this company. Most likely, this is due to the price and the almost absence of materials in Russian, which limits the audience of amateurs who are familiar with semiconductor products from TI. There is also one unpleasant nuance - some things will not be allowed through the customs of the Russian Federation, and others are not exported from the USA to the Russian Federation (and this is not the consequences of recent sanctions - “that’s how it was”). However, there are ways to get what you need.

With this article I want to draw the attention of amateur developers to TI solutions, in particular, applicable for smart home. A number of articles published on GT about the smart home could borrow some interesting solutions. For example, the article published by avs24rus Wireless Lighting-Sensor powered by CR2450, caused a discussion in the comments that I remember: “How to make the sensor “set it and forget it” outdoors in conditions of extreme sub-zero temperatures? Battery, solar battery, ionistor?

I propose to get acquainted with the solution to this problem from TI using the example of the reference design TIDA-00484 TI Design: The humidity and temperature sensor on TI electronic components can be powered by a popular lithium miniature battery CR2032 more than 10 years in the range –30°C… 60°C, which is limited by the operating range of CR2032, and not of electronic components for which this range is –40°C... 85°C (for the BR2032 battery the operating range is -30... 85 °C).

TIDA-00484 TI Design:

Let's go from general to specific. And first the characteristics of TIDA-00484 TI Design:

Options Description
Power supply CR2032 (capacity 240 mAh)
Sensor type Humidity and temperature
Temperature measurement accuracy ± 0.2°C
Relative Humidity Accuracy ± 3%
Measuring interval One measurement per minute
Average consumption when on 3.376 mA
On time 0.03 seconds
Average consumption at rest 269.75 nA
Time at rest 59.97 seconds
Estimated operating time from power source 11.90 years
Operating temperature range -30°C to 60°C (limited by operating temperature range for CR2032)
Working conditions Indoor and outdoor
Size 3.81 cm × 7.62 cm

Let's determine the operating time from an autonomous power source. The system can be in two states: on and off. The duration and average current of each state are factors that determine the total duration of operation from the power supply. Time is calculated using the following formula:
  • Battery life, estimated operating time from the power source in years
  • Battery capacity, power supply capacity in mAh
And the main parameters that affect the estimated period battery life of the entire system are:
  • Average consumption when switched on, I ON, in mA
  • Time in the on state, T ON, in seconds
  • Average consumption at rest, I OFF, in nA
  • Time at rest, T OFF, in seconds

Formula for Excel

Those who wish can calculate it themselves using a spreadsheet processor. Data in cells B9..B13
Battery capacity, mAh
B9=240
I on, mA
B10=3.376
T on, s
B11= 0.03
I off, nA
B12=269.75
T off, s
B13= 59.97
Battery life, years
=B9/((B10*B11+B12*B13*0.000001)/(B11+B13))*0.85/8760
Battery life turned out to be 11.89


T OFF, fully controlled by the end user because V in this case The measuring system wakes up every minute and T OFF = 1 minute – T ON. On minimum time T ON is almost impossible for the user to influence because it is determined by the time required to turn on the system, perform a measurement, transmit a radio packet and turn off the system.

I OFF is defined as the average current drawn from the battery when it is off. This current is usually determined mainly by the leakage current through the capacitors and the operating current of the sensors and microcontroller systems that provide sleep mode. Texas Instruments microcontrollers have long been known for their ultra-low power consumption, which competitors are only approaching, however, even such record-breaking efficiency is not enough to operate the device from a CR2032 element for 10 years. This reference design develops a method for measuring ambient relative humidity and temperature, achieving extremely long battery life by using a timer in the device's duty cycle.

The following graph shows two methods of organizing the device's operating cycle - using the normal microprocessor sleep mode (red) and the system timer (blue). The black dotted line is the CR2032 manufacturer's stated service life of 10 years.


Reference design is intended for use in:
  • Industry
  • Internet of Things (IoT)
  • Building automation
  • Security systems
  • HVAC sensors
  • Smart Thermostats
  • Battery Powered Systems
Let's see what results in the record-breaking efficiency of this prototype device. The device is built using the following components:


CC1310– multi-core single-chip system, low-cost, energy-efficient wireless controller optimized for operations in the sub-gigahertz range. High performance transceiver driven by dedicated processor core Cortex-M0 executing the ones flashed into its ROM low level protocols.

Upper level protocols run on a separate 32-bit processor core Cortex-M3 With clock frequency up to 48 MHz. Sensor interrogation is carried out by an independent micro-power controller (a 16-bit RISC processor capable of operating at frequencies as low as 32 kHz while the rest of the system is in sleep or standby mode), which can work with both analog and digital sensors.

The Cortex M3 controller core has a rich set of peripherals and contains:

  • temperature sensor;
  • four timer modules general purpose(2x16- or 1x32 bits with PWM mode);
  • 8-channel 12-bit ADC (up to 200 kSa/s);
  • watchdog timer;
  • analog comparator;
  • UART, I2C;
  • three SPI (one of them is micropower);
  • - AES module;
  • - 10...31 I/O lines (depending on the current configuration and case);
  • - support for up to eight capacitive buttons
Parameter
Frequency range and
supported types
modulation
Sub 1 GHz: MSK, FSK, GFSK, OOK, ASK, 4GFSK, CPM (shaped 8 FSK)
Supported protocols Star topology networks: WMBUS, SimpliciTI
Flash, kByte 128
RAM, kByte 20
Supply voltage, V 1,65...3,8
Temperature range, °C 40...85
Sensitivity 2.4 Kbit/s, dBm -121
Sensitivity 50 Kbps, dBm -111
Maximum output power at 868 MHz, dBm 15
Maximum reception bandwidth, kHz 400
Minimum reception bandwidth, kHz 40
Data transfer rate, kMbit/s up to 4
Energy consumption
  • application controller in active mode– 61 µA/MHz ( ARM Cortex M3)
  • current consumption in sleep mode with the timer running and memory contents being saved – 0.7 µA
  • sub-GHz radio path – 5.5 mA when receiving, 12 mA when transmitting (output power 10 dBm)
Technical process 65 nm

Using the TPL5111 nanopower timer gives obvious advantage because in fact, by the end of the battery life, the entire device can be replaced, for example, during planned repairs of premises, maintenance or equipment modernization. If for a smart home you rarely need more than two such devices (external and internal), then in the case of industrial facilities, buildings and ventilation systems, there will be much more such sensors and their periodic maintenance can result in serious expenses.

Description of the working cycle much shorter description of the design and its characteristics.

When on, after a certain interval, the TPL5111 timer supplies power to the TPS61291 boost converter, which raises output voltage up to 3.3 volts and to the TPS22860 load switch, connecting the increased output voltage to the rest of the system. After the supply voltage appears, the CC1310 receives the current temperature and relative humidity from the HDC1000 sensor via I2C, then transmits a “connectionless” data packet with this information (i.e., without initializing or establishing a connection with any network node), and then signals TPL5111 indicating that the system may be shut down.

When turned off, the TPS22860 load switch completely disconnects part of the system (CC1310 and HDC1000 devices) from the lithium battery. The only consumers of current from the lithium battery are the recharging and capacitor leakage currents of the lithium battery, the operating current of the TPL5111 timer, the quiescent current of the TPS61291 in bypass mode, and the leakage current of the TPS22860 load switch.


Graph of current consumption from the battery when the system is turned on.


Graph of current consumption from the battery when the system is turned off. Logarithmic scales.
A similar operating cycle can be used in other devices, for example, some water leakage sensors, door opening and closing sensors, etc. where information is not required in real time and the device power issue takes precedence.

You can learn more about the reference design in the documentation on the TI website.

IP Wi-Fi camera with IR illumination, suitable for solving various problems. For example, security of a store, warehouse, office at night, normal video surveillance in suburban areas, where dark time no lighting for days. A Wi-Fi IP camera with night illumination will be useful where there is a need for round-the-clock monitoring. An IP camera with a motion sensor and Wi-Fi transmitter will provide good security of the territory. As soon as activity is detected, the broadcast or recording will be activated.

Most of modern cameras remote video surveillance, have a motion detector in their design. A Wi-Fi IP camera with a motion sensor will quickly detect any moving object in its coverage area. It is possible to have a function to notify the owner of an alarm when activity is detected. When selecting IP Wi-Fi cameras night vision or motion sensor cameras, the main technical features are important.

List of technical features:

  • Backlight range, number of LEDs.
  • Sensor sensitivity.
  • Motion detection method.
  • Accurate sensor range.
  • Method of installation and installation of the camera.
  • Remote control tools (for example, mobile application for smartphone).
  • User reviews.
  • Availability of instructions in Russian.

In our online store it is easy to buy an IP Wi-Fi night vision camera with a motion sensor, starting at a price of 2,000 rubles. On the website you can find out all the necessary information about each model.

Characteristics of IP Wi-Fi Night Vision Camera with Motion Sensor

Before buying a Wi-FI IP camera with IR illumination and a motion sensor, it is recommended that you familiarize yourself with its technical parameters.

List of technical characteristics:

  • Photo and video resolution.
  • Viewing angle.
  • Shooting speed.
  • Power parameters, battery power.
  • The temperature range at which the camera can operate stably.
  • Housing protection class.
  • Maximum air humidity.
  • Network protocols.
  • Dimensions.
  • Camera weight.

Choosing based on these characteristics suitable model Wi-Fi IP video cameras with IR illumination and a motion sensor, you can immediately place an order on the website. The goods will soon be delivered to you by courier.

Hello. In today's review I will talk about the new version of the Sonoff TH switch. In my previous review: “.” I described the old version of the switch and at the end of the review added that Itead will release a new version of this switch that does not differ in functionality. Today we will consider all possible (and there are four) options for Sonoff TH switches.

First, a list of components for all switch options:

Sonoff TH10: $7.50
Sonoff TH16: $8.60
Sonoff Sensor-AM2301: $4.30
Sonoff Sensor-DS18B20: $3.50

The order for the switches was made on September 8th. Hong Kong Post worked quickly, and already on September 23rd I had them:

The TH switches themselves are supplied in cardboard boxes:

It’s easy to guess from the name that TH10 is designed for 10 Amps, and TH16 is designed for 16 Amps:

Features

Supports 90~250V AC input
Support max 10A /16A input
Power: 2200W(10A) /3500W(16A)
Support fast configure SSID and password connection through APP
Support automatic connect to server, register and update status info.
Support tracking device status and timely remote control through APP
Support setting countdown, single and repeat timing tasks
Support real-time temperature and humidity displaying
Support 3 temperature and humidity sensors (AM2301, DS18B20, DHT11)
Support preset temperature and humidity to turn on/off
Support group management, scene, smart scene


Unlike old version– now it’s not just a board, the switches now have a housing:

There is a button at the top. It serves to connect the switch to the application, as well as to manually control the operation of the switch.

On one side of the switch there is a hole for its fastening. On the other side is the introductory panel:

On the side there is a socket for connecting sensors:

Remove the cover of the introductory panel:

Self-clamping terminals are used.

Let's look at the switches:

Top TH16, bottom – TH10:

They are completely identical except for the relay used.

10 Amp Relay:

16 Amp Relay:

Sensor connection button and connector:

Bottom view of the boards:

The switch uses flash memory:

Wi-Fi support is carried out .

Since I believe that it is necessary to have manual control of the switch, in addition to remote control via Wi-Fi, and the built-in button on the switch when it is built into the inside of an electrical appliance is not available, I slightly upgraded one of the switches to suit my needs. I made a remote button, now you can put it in the right and convenient place:

It would be nice if such a button were optional. I don’t think it’s difficult to add another connector for a button to the case, and those who need it could purchase it separately and connect it without any soldering.

Let's move on to the sensors. One of two types of sensors can be connected to the switch:

DS18B20 – only temperature waterproof sensor (-55°С - +125°С):

And AM2301 – temperature and humidity sensor (-40°С - +80°С; 0 - 99.9% humidity):

Let's connect the sensors to the switches:

And connect the switches themselves according to the connection diagram:

The Wi-Fi icon will light up:

It's time to connect the switch to the smartphone application:

I described in detail how to install and configure the application in my review. Since the release of that review, the application has only gotten better and acquired a Russian-language interface.

Open the application and select add device. Adding devices has become even easier and is now done in four simple steps.

Step one. Press the button on the switch and hold it pressed for five seconds:

Choose wireless network and enter the password for it. If you have already used the application before, then all the fields will be immediately filled in automatically:

Set a friendly name for the switch:

The connection is complete.

Switch control page:

The switch is now manually operated and disabled.

Switch settings:

You can share the right to control the device with another smartphone:

Set one-time and repeat timers:

Countdown timers:

Switch information:

Switch on:

If we move the slider to the “Auto” position, then we will need to set the temperature or humidity and the on/off switch:

Auto mode. Here at 30 degrees the switch will turn on, at 50 degrees it will turn off.

When the switch is in automatic mode– it does not respond to pressing a virtual button:

To turn off, you need to set the switch to manual control and press virtual button. The situation is different with a real button. Even if the switch is in automatic mode, pressing the actual button will immediately turn it off. Pressing it again will turn it on. And the switch will go to the mode that was before the shutdown. Therefore, a real button in a convenient place is simply necessary.

Here is an example of an on/off setting based on temperature changes:

With these settings, if the temperature rises above 22 degrees, the switch will turn on, if the temperature drops below 18 degrees, the switch will turn off.

Settings for controlling the switch based on changes in humidity are set in the same way:

Humidity settings will only be available when using the AM2301 sensor.

The switches can also be controlled directly from the virtual remote control:

Actions can be linked and different scenarios can be created.

When the switch is turned on, in addition to the blue LED indicating Wi-Fi work, the red LED also lights up:

The DS18B20 temperature sensor is truly waterproof and tracks water temperature correctly:

Everything was done quite neatly and efficiently. I already made my only wish for the switches during the review, about the possibility of connecting an external control button.

Thank you for your attention.

The product was provided for writing a review by the store. The review was published in accordance with clause 18 of the Site Rules.

I'm planning to buy +72 Add to favorites I liked the review +32 +71

Hello. Today I will tell you about an interesting switch from Itead - Sonoff TH. Switch supports remote control via the cloud via Wi-Fi, and also has temperature and humidity sensors, so it can control the connected device depending on changes in these parameters. There is no additional radio control. If you are interested in this topic, welcome to cat.

Sonoff TH comes in a cardboard box:

Package


On the side edge of the box are marked specifications switch:

Description from the manufacturer:

Features

Support 90~250V AC power supply voltage.
Support checking real time temperature and humidity.
Support preset temperature and humidity range to turn on/off devices.
Support fast configure SSID and password through APP.
Support automatic connect to server, register and update status info.
Support tracking device status and timely remote control through APP.
Support setting single and repeat timing schedules
WiFi Characteristics

802.11 b/g/n
Built-in Tensilica L106 ultra-low power consumption 32-bit micro-MCU, dominant frequency support 80 MHz and 160 MHz, support RTOS
Built-in TCP/IP protocol stack
Built-in TR switch, balun, LNA, power amplifier and matching network
Built-in PLL, voltage regulator and power supply management components, 802.11b mode +20 dBm output power
A-MPDU&A-MSDU aggregation and 0.4μs guard interval
WiFi @ 2.4 GHz, supports WPA / WPA2 safe mode
Support cloud OTA upgrade
Support STA/AP/STA+AP mode
UART, I2C, PWM, GPIO
Deep sleep maintain current is 10 uA, shutdown current is less than 5 uA
Wake-up, connect and transfer data packets in 2 ms
Standby power consumption is less than 1.0 mW (DTIM3)
Operating temperature range: -40 ℃ - 125 ℃
Other parameters as follows



The kit includes three connecting wires:

Temperature and humidity sensor:

And the switch itself:

The switch is created on the same platform and has the same relay as the one I reviewed earlier in the fifth part of the reviews dedicated to smart home– Sonoff RF switch. I will provide links to previous parts at the end of the review.

At the same time, the switch can monitor either temperature or humidity. Therefore, two Sonoff TH switches will be tested:

Connect the sensor:

The wires are connected according to the diagram:

We supply power to the switch:

Consumption:

Both switches are connected. It's time to link them to the eWeLink control application. You can read more about working with the application in the fourth part of the reviews.

In the application, select “Add Device”:

Long press the button on the switch until the LED starts flashing. Then check the top box in the application and click “Next”:

The application will ask you to enter your Wi-Fi password. After that, click “Next”:

Devices are being searched.

Enter any name for your new device:

The switch is “linked” to your account:

After which we find ourselves in main screen switch control. The switch is installed in manual mode. It is currently disabled:

At the top you can see the temperature and humidity readings. In order to turn on the switch, you need to press the button on your virtual remote control from anywhere in the world where there is Internet, or press the button on the switch with your hand.

Switch on:

If we move the slider to the “Auto” position, we will get into setting the parameters for turning the switch on and off in automatic mode:

You can choose temperature or humidity. In this case, humidity is selected. IN top line– 60% humidity, above which the switch will turn off.

At the bottom – 40% Below which – the switch will turn on. You can set any parameters. And any options to enable/disable.

Right now it's 43% humidity. Switch off:

Switch settings:

You can share control capabilities with another device:

Set various timers:

I changed the switch settings a little to quickly check its operation.

The switch has turned off:

The switch turned on:

We connect and look for the second switch:

We will configure it to trigger based on temperature:

All settings are identical to the settings for humidity, only select the temperature:

I don’t really like to give specific examples of use in reviews, so as not to limit the imagination of those reading it. But in this case, I think it would be appropriate for better understanding switch operation.

So, let's take a simple ultrasonic humidifier:

It turns on with a key and turns off automatically when the water runs out.

Let's finalize it. We are looking in the building free place for the switch board. There is not much space there, but for small fee– there was still a place. We mark two holes for the button and the switch LED:

And we drill holes.

We cut the power cable inside the humidifier housing:

And we connect our switch:

Now glue the switch into place with hot glue. Hot-melt adhesive is needed to make assembly of the humidifier more convenient, since after assembly the switch will be pressed by the body from the inside. It’s as if a place was left for him:

We cut out a piece of the housing from the back side and glue the sensor there:

Here's what happened in the end:

Carefully assemble the humidifier, pour water into it and plug it in:

Everything works correctly:

Checking the switch settings:

Several days passed between testing and installation. And the application notifies you when new firmware is available.

Let's go to the settings and flash:

Firmware updated:

Humidity is considered comfortable from 40 to 60%. So we leave it like this:

The switch works properly in the humidifier. You can also use it to destroy moisture, for example, by using its capabilities to turn on exhaust ventilation in the bathroom.

I built the second switch, setting it to the temperature range, into the floor fan. When you go to bed it is very hot. By morning it becomes cool. But the fan continues to work. Now it turns on and off at a given temperature.

These are only special cases of application. You can use these switches as you need. Based on your needs.

While the review was being written, the manufacturer released a new version of Sonoff TH. According to him, the operating principle of the new version is no different from the old one I reviewed. And the use of the switch’s capabilities, described in the review, remains relevant

Thank you for your attention.

My previous reviews on smart home components:

To be continued…

The product was provided for writing a review by the store. The review was published in accordance with clause 18 of the Site Rules.

I'm planning to buy +61 Add to favorites I liked the review +25 +50

Disclaimer: this article may contain errors, since I have not been working with the ESP8266 module for very long and do not yet fully understand many architectural aspects of this device.

Today, almost every home has a Wi-Fi router and it would be short-sighted not to use this device for home automation, especially since today there is all the equipment available on the market to implement any ideas. Below is an option for creating a small electronic device, which is a platform for building various sensors/actuators based on Wi-Fi module- ESP8266.


This module is well described, and on this site you will find everything that humanity knows about the ESP8266 module.

So, what should the device “be able to do”:

  • Receive data from the humidity/temperature sensor DHT22;
  • Control a solid state relay (for example SSR-25 DA);
  • Connect to Wi-Fi router with a given login and password;
  • Send and receive data through an MQTT broker;
  • Connect via USB for debugging and firmware.

Device diagram:

There are a lot of modifications of the ESP8266 module (variants), but, in principle, they differ only in size, antenna type and the number of available I/O ports. I used the ESP8266 ESP-01 module:

It has only two ports (not counting USART) - GPIO0, GPIO2, but for my purposes it is enough, one port is for the sensor and the second is for load control.

The USB interface is implemented by the USB-USART converter CH340G.

After the module is flashed, you can download our scripts. There are many ways, but personally I like the ESPlorer utility - a very convenient software not only for downloading scripts, but also for developing and debugging scripts.

Now in more detail. We need to upload three scripts:

dht22.lua - the actual module that reads data from the DHT22 sensor

- ***************************************************************************
- DHT22 module for ESP8266 with nodeMCU
- - Written by Javier Yanez
- but based on a script of Pigs Fly from ESP8266.com forum
- - MIT license,
- ***************************************************************************

Local moduleName =…
local M = ()
_G=M

Local humidity
local temperature

Function M.read(pin)
local checksum
local checksumTest
humidity = 0
temperature = 0
checksum = 0

Use Markus Gritsch trick to speed up read/write on GPIO
local gpio_read = gpio.read

Local bitStream = ()
for j = 1, 40, 1 do
bitStream[j] = 0
end
local bitlength = 0
- Step 1: send out start signal to DHT22
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
tmr.delay(100)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)
gpio.write(pin, gpio.HIGH)
gpio.mode(pin, gpio.INPUT)

Step 2: DHT22 send response signal


local c=0
< 500) do c = c + 1 end
- bus will always let up eventually, don"t bother with timeout
while (gpio_read(pin) == 0) do end
c=0
while (gpio_read(pin) == 1 and c< 500) do c = c + 1 end

Step 3: DHT22 send data
for j = 1, 40, 1 do
while (gpio_read(pin) == 1 and bitlength< 10) do
bitlength = bitlength + 1
end
bitStream[j] = bitlength
bitlength = 0
- bus will always let up eventually, don"t bother with timeout
while (gpio_read(pin) == 0) do end
end

DHT data acquired, process.
for i = 1, 16, 1 do
if (bitStream[i] > 3) then
humidity = humidity + 2 ^ (16 - i)
end
end
for i = 1, 16, 1 do
if (bitStream > 3) then
temperature = temperature + 2 ^ (16 - i)
end
end
for i = 1, 8, 1 do
if (bitStream > 3) then
checksum = checksum + 2 ^ (8 - i)
end
end

ChecksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8))
checksumTest = bit.band(checksumTest, 0xFF)

If temperature > 0x8000 then
- convert to negative format
temperature = -(temperature - 0x8000)
end

Conditions compatible with float point and integer
if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
humidity = nil
end
end

Function M.getTemperature()
return temperature
end

Function M.getHumidity()
return humidity
end


main.lua - the main script, connects to Wi-Fi networks, receives data, sends it via mqtt and manages the load

function subscribe()
m:subscribe("/myhome/"..id.."/light",0,function(conn)print("Subscribe success")end)
m:on("message",function(conn,topic,data)
print(topic… ": "..data)
if data==“ON”then gpio.write(3, gpio.LOW)end
if data==“OFF”then gpio.write(3, gpio.HIGH)end
end)
end

Function dht22_get_data()
dht22=require("dht22")
dht22.read(4)
local t=dht22.getTemperature()
local h=dht22.getHumidity()
if t~=nil then
t=((t-(t % 10))/10).."."..string.format("%.i",(t % 10))
else t=nil
end
if h~=nil then
h=((h-(h % 10))/10).."."..string.format("%.i",(h % 10))
else h=nil
end
dht22=nil
package.loaded["dht22"]=nil
collectgarbage()
return t, h
end
function post_data()
t, h = dht22_get_data()
if t ~= nil then
m:publish("/myhome/"..id.."/temperature",t,0,0, function()
print("Temperature"..t)
if h ~= nil then
m:publish("/myhome/"..id.."/humidity",h,0,0, function()print("Humidity "..h)end)
end
end)
end
end

Function init_network()
collectgarbage()
print(id)
if wifi.sta.status() ~= 5 then
print("Reconnecting WIFI")
wifi.setmode(wifi.STATION)
wifi.sta.config("Login", "password")
wifi.sta.connect()
tmr.alarm(0,5000,0,function()init_network()end)
else
print("IP: "..wifi.sta.getip())
print("Connecting to MQTT server")
tmr.alarm(0,7000,0,function()init_network()end)
if m~=nil then
m:close()
end
m = mqtt.Client(id, 120)
m:connect("192.168.0.x",1883,0,function(conn)
tmr.stop(0)
print("Connected")
subscribe()
tmr.alarm(0, 60000, 1, function() post_data() end)
m:on("offline",function(con)
print("offline.Reconnecting")
init_network()
end)
end)
end
end

Gpio.mode(3, gpio.OUTPUT)
id="esp_"..wifi.sta.getmac()
init_network()


init.lua - startup script. It is launched first by NodeMCU at the start.

print("ESP8266_home_board_v_x.x")
dofile("main.lc")


There is a nuance here. Unfortunately, external flash The module memory is not enough to load NodeMCU and my scripts, so I use the following “crutch” solution: I load one script, run the command node.compile(“dht22.lua”) - this command compiles the script into “dht22.lc”, as a result it takes up less space in both flash memory and RAM, since NodeMCU will then load it into memory while the main script is running. Then we delete the uncompiled script with the command file.remove (“dht22.lua”). We do the same manipulations with main.lua. The last thing we load is the init.lua script; we don’t compile it anymore. Let's restart the module.

At startup, NodeMCU will execute the “init.lua” script, which in turn will launch “main.lua”. “main.lua” script will connect to the network, send data to the COM port and to the network to the specified mqtt broker.

I will answer in more detail about the scripts in the comments.

Well, that seems to be it. If the topic is interesting, in the next article I’ll tell you about the mqtt broker and connecting this whole thing to Openhab.

Thank you for your attention.

Tags:

  • ESP8266
  • NodeMCU
Add tags






2024 gtavrl.ru.