logo elektroda
logo elektroda
X
logo elektroda

Bascom - Atmega1284P + ESP8266 + Domoticz - random JSON data sending

maximus22_kr 2646 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16726784
    maximus22_kr
    Level 18  
    I have a circuit with the configuration as in the title ( to which DS sensors, BME280, SD recording, RFM12 )

    The last step is to send via JSON to Domoticz. Unfortunately the sending fails and does it randomly, sometimes after sending data from several sensors, sometimes it will go through the whole "cycle"
    The interval between sending is between 20 and 30 seconds, while the interval between sending data for a specific sensor is 11 minutes.
    The whole code is attached, I paste only the part responsible for Domoticz + the Select Case fragment. For the reception of the UART, I used Serial0charmatch according to Niveasoft's idea.
    Code: VB.net
    Log in, to see the code
    .

    And now connecting to the UART from the Tx side of the ESP8266 I have:
    Code: Bash
    Log in, to see the code
    .

    It looks like the Atmega stops responding to the "OK" response - to the AT+PING="192.168.0.92" command. The ESP8266 is unlikely to be at fault - it sends back "OK" and the Atmega should start sending at that point. I reset it while it was running, this didn't change anything, only a reset of the whole chip helps - for a while.

    In addition, as a "bonus" ( but this requires a look at the code ) up to the moment when sending to Domoticz still works, communication with Master 2 does not work, at the moment when sensor data is no longer sent to Domoticz, receiving data from Master 2 starts to work as it should. And, of course, apart from this problem, the whole thing works - whether data is sent or not - reading sensors, sending to clients and writing to SD - it all works.

    What could be the reason for this behaviour of the Atmega UART ?
    Attachments:
    • Atmega1284P_Domoticz_v.6d.zip (28.82 KB) You must be logged in to download this attachment.
  • ADVERTISEMENT
  • #2 16734452
    maximus22_kr
    Level 18  
    I converted the program to Atmega328. Now it just has to receive data from the master on the Atega1284p and send to Domoticz.
    Code: VB.net
    Log in, to see the code
    .

    I also changed the IP address where it sends the PING to 8.8.8.8

    But nothing has changed. Sometimes a "+timeout" pops up
    Code: Bash
    Log in, to see the code
    .


    Maybe Domoticz itself ( installed on Orange PI Zero ) is doing some problems ? Maybe I am updating too often ?
    I guess it will come to wait for someone smarter to brag about their achievements.
  • ADVERTISEMENT
  • #3 16740484
    maximus22_kr
    Level 18  
    Eventually I send to Domoticz like this:
    Code: VB.net
    Log in, to see the code
    .

    It works, but only once after a power on/reset.
    Maybe someone can suggest what is wrong ? The only idea is that the UART is not handled properly. Maybe you need to throw away that extra flag Domoticz_en , just in the first Case of the Domoticz_send subroutine to parse it ?
  • #4 16740518
    Anonymous
    Anonymous  
  • #5 16742173
    maximus22_kr
    Level 18  
    I know and use the other UART, even just to know where the program is.
    But it seemed to me that plugging in the Tx of the ESP8266 would make the most sense.
    RealTerm showed the CrLF characters
    Code: Bash
    Log in, to see the code
    .

    Note that the first time is fine - it passes all 28 entries. Maybe the ESP8266 is sending something and this is "clogging up" the UART. So far RealTerm is only showing CrLf characters after CLOSED

    Code: Bash
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #6 16742299
    Anonymous
    Anonymous  
  • #7 16745740
    maximus22_kr
    Level 18  
    If you have the ability to do so, that would be great. As you can see the Atmega is behaving strangely. As if it doesn't work at all I would look for the problem in the code.
    It's not likely a hardware problem, the operating conditions don't change.
  • ADVERTISEMENT
  • #8 16746030
    Anonymous
    Anonymous  
  • #9 16746056
    maximus22_kr
    Level 18  
    Well it's Orange Pi Zero easier, all you need is a terminal set to 115200 and Putty, to start with I was connecting via terminal, now I'm connecting via SSH.
    I've done a little bit of work with it, i.e. activated the ports, tried to control them from Python, installed a server with PHP 5, MySql, Apache 2.

    The Orange Pi Zero doesn't have HDMI, only composite I think.
    Even a DUC program to communicate with the Noip - except that something doesn't work Noip - I can see the address from the Noip on the local computers, i.e. I either type in the IP or the address from the Noip and it works, but externally it doesn't work - I even ventured a topic on the forum.
  • #10 16753938
    maximus22_kr
    Level 18  
    Unfortunately today I had to change my router for a TP-Link TD-W8970 ( I just had one )
    The result is that even after a reset it does not send - it is as follows
    Code: Bash
    Log in, to see the code
    .

    Maybe some router setting is causing the problem.
  • #11 16757769
    maximus22_kr
    Level 18  
    I have converted it so that it only sends a test value ( random ) every 8 seconds

    Code: VB.net
    Log in, to see the code


    there is a problem with receiving from ESP

    Communication with tx ESP:
    Code: Bash
    Log in, to see the code
    .

    After AT ( or after Ping, depends how I initiate ) the module does not send OK. Then after the connection it does not answer CONNECT, so it is probably about the answer "Link is not valid".

    Added after 18 [hours] 29 [minutes]: .

    I added
    Clear Serialin0
    Com1_str = ""

    i.e.
    Code: VB.net
    Log in, to see the code


    and it works. I'm going to try to adjust this for bulk shipping.
  • #12 16765366
    maximus22_kr
    Level 18  
    The final result looks like this:
    Code: VB.net
    Log in, to see the code
    .

    Admittedly, after several hours of operation, sometimes only the first sent record arrives ( but only the first one ) intermittently ( interestingly there are entries in the database but with the last received value ). I need to hook it up to the UART and see what happens.
  • #13 17906531
    marcosx
    Level 12  
    Hello
    This is how I read it today, and I don't know if you have solved the problem.
    But I don't see anywhere in the code the command AT+CIPCLOSE=5
    and it is needed for the ESP8266 to work properly
    maybe I'm wrong.
    Greetings Marian

Topic summary

✨ The discussion centers on integrating an Atmega1284P microcontroller with an ESP8266 Wi-Fi module to send sensor data via JSON to the Domoticz home automation system. The original setup included multiple sensors (DS sensors, BME280), SD card recording, and RFM12 communication. The main issue was intermittent failure in sending JSON data to Domoticz, with irregular success in completing full data cycles. The user experimented with converting the code to run on an Atmega328P for simpler testing and focused on UART communication configurations, using Serial0charmatch for UART reception. Challenges included handling ESP8266 prompts and responses, managing UART buffers, and ensuring proper TCP connection handling. Router changes (to a TP-Link TD-W8970) introduced additional connectivity problems, such as "link is not valid" errors during AT command sequences. Suggestions included using a microcontroller with multiple USARTs for better debugging, monitoring ESP8266 responses carefully, and ensuring proper AT command sequences including connection closure commands like AT+CIPCLOSE. The user also explored alternative hardware like Raspberry Pi and Orange Pi Zero for network interfacing and testing Domoticz. The final code iterations focused on sending simplified test JSON data at regular intervals to isolate the problem. The discussion highlights the complexity of reliable UART communication with ESP8266 modules, the importance of handling asynchronous prompts, and the impact of network hardware on TCP communication stability.
Generated by the language model.

FAQ

TL;DR: Random JSON/HTTP updates from an ATmega1284P→ESP8266→Domoticz chain were traced to serial parsing and buffer-handling issues, not Domoticz. The author sent every 20–30 seconds and “stops responding to the ‘OK’ response.” Fixes centered on clearing UART buffers and tightening the AT state machine. [Elektroda, maximus22_kr, post #16726784] Why it matters: If your AVR misses one AT prompt, the whole Domoticz update cycle can stall.

Quick Facts

How do I fix ESP8266 ‘random’ JSON sends to Domoticz from an ATmega?

Treat it as a serial state‑machine problem. Before each send, clear the buffered UART (Clear SerialIn0) and reset the working string (Com1_str = ""). Then drive a strict AT sequence and parse only one expected response per step. This removed stalls and made single and bulk updates reliable. [Elektroda, maximus22_kr, post #16757769]

Why does it hang after AT+PING returns OK?

Residual characters in the UART buffer can misalign your next parser step. The AVR then waits for a response it already consumed or missed. Clearing SerialIn0 and Com1_str before starting the Domoticz send sequence prevents the parser from processing stale CR/LF or stray text. [Elektroda, maximus22_kr, post #16757769]

My ESP8266 shows “busy p...” and “link is not valid.” What does it mean?

These appear when the module is still processing a previous socket or when the router/connection policy changed. After a router swap, users saw CIPSTART followed by “busy p...” and then “link is not valid.” Reset the sequence, ensure a clean close, and retry the connection. [Elektroda, maximus22_kr, post #16753938]

What’s a safe, repeatable AT flow to update Domoticz?

Three-step How-To:
  1. Clear SerialIn and strings, then issue AT+PING or AT to sync.
  2. AT+CIPSTART="TCP","",, wait for OK.
  3. AT+CIPSEND of the GET line and headers, send Connection: close, wait for CLOSED. This sequence is shown working end-to-end. [Elektroda, maximus22_kr, post #16765366]

Do I need to close the TCP connection explicitly?

Yes. The working flow sends headers including “Connection: close” and waits for CLOSED. Another reader suggested adding AT+CIPCLOSE=5 to enforce closure if needed. Unclosed sockets can cause later CIPSTART/CIPSEND failures. [Elektroda, marcosx, post #17906531]

Is Bytematch reliable with ESP8266 AT responses?

Not by itself. ESP8266 emits prompts like ">" and messages without CRLF. Relying on Bytematch alone misses these, causing stalls. One contributor recommends explicit prompt and error recognition instead of Bytematch for modules like ESP8266 or GSM. “Handle prompt recognition, but also errors.” [Elektroda, 373522, post #16740518]

Could Domoticz be the bottleneck?

The thread shows Domoticz replying 200 OK with JSON status, while failures traced to UART parsing and socket handling. After buffer clearing and a strict sequence, updates proceeded, indicating the MCU/ESP side was the limiting factor rather than Domoticz itself. [Elektroda, maximus22_kr, post #16726784]

What intervals and throughput worked in practice?

The author used 20–30 seconds between sends and about 11 minutes between per‑sensor updates. In testing, a single test value sent every ~8 seconds verified stability. Use 150‑byte buffers at 19,200–38,400 baud to match this pattern. [Elektroda, maximus22_kr, post #16757769]

Why did it work once after reset, then fail later?

After power‑on, buffers are clean and the first transaction often succeeds. Subsequent transactions fail if stale bytes remain or the state machine accepts unexpected lines. Clearing the buffer before every transaction kept later cycles reliable. [Elektroda, maximus22_kr, post #16740484]

What changed when switching routers?

A new router produced “busy p...” and later “link is not valid,” even though PING showed +70 ms to +181 ms. That edge case shows network equipment can alter timing and socket handling, requiring stricter closes and retries. [Elektroda, maximus22_kr, post #16753938]

Should I switch to MQTT instead of HTTP/JSON?

MQTT reduces overhead and provides clearer acknowledgment handling. A contributor runs ESP8266 over MQTT to a VPS, sending several messages per second reliably. If HTTP state tracking is fragile, MQTT can simplify the transport logic. [Elektroda, 373522, post #16740518]

What is Domoticz and what is param=udevice?

Domoticz is a home automation system with an HTTP API. Using type=command and param=udevice lets you update a device by idx, passing nvalue and svalue as payload. The thread’s working GET line demonstrates this exact pattern. [Elektroda, maximus22_kr, post #16765366]

What is BASCOM-AVR Bytematch?

Bytematch lets BASCOM stop reading serial input when it detects a specific byte. It works with framed data, but ESP8266 outputs mixed prompts and lines, so Bytematch alone can mis-parse responses. Use explicit parsers for prompts and errors. [Elektroda, 373522, post #16740518]

What is the RFM12 mentioned in the code?

RFM12 is an SPI radio transceiver used here to collect many sensor values on the master, then forward them to the ESP8266/HTTP client. The code shows ISR-driven receive and FIFO control before packaging readings for Domoticz. [Elektroda, maximus22_kr, post #16765366]

Any final stability tips from the working build?

Use a per-request AT state machine, clear buffers before each cycle, send Connection: close, and wait for CLOSED. Log errors like SEND OK, ERROR, and CLOSED, then retry cleanly. This stabilized multi-sensor updates across 28 items. [Elektroda, maximus22_kr, post #16765366]
Generated by the language model.
ADVERTISEMENT