Assistance on how to: Door Sensor -> Arduino GPIO -> ESP8266 ->MQTT or RESTful

Unsure if this should go in this forum or Program Guidance.. so i hope i chose the correct forum as i'm looking for assistance on a sketch.

The low down: I own an Mega +WiFi R3 ATmega2560+ESP8266 from aliexpress (https://www.aliexpress.com/item/Mega-WiFi-R3-ATmega2560-ESP8266-32Mb-memory-USB-TTL-CH340G-Compatible-for-Arduino-Mega-NodeMCU-WeMos/32801910180.html?spm=a2g0s.9042311.0.0.8R3yo1)

I can program it correctly and wondering how to execute the following scenario.

I run openHAB for home automation which can be interfaced over TCP/IP either though MQTT or RESTful API.

I want to use an Arduino Mega as my hub to monitor all my hardwired sensors via the GPIOs. The items connected to the GPIOs would be door sensors and motion sensors. When the arduino detects motion for the PIRs or a door opened/closed i want to send either a MQTT message or REST command to my openHAB server.

Does anyone have an example on to to do this, all i'm really looking for is an example sketch that monitors the GPIO on the Arduino and then sends either a REST command or MQTT message over the ESP8266 depending on the state changed.

Thanks

you could try WiFi Link firmware with WiFi Link library. it's new.

or you can try Firmata in Atmega and a sketch for esp using the FirmataMaster esp library.

or you already decided to use the AT firmware?

Thank you, i'll take a look at those suggestions.

I think that at that board it is possible to connect esp with atmega's Serial3. the esp has only one Serial.

with Firmata option you would always switch the dip for esp between atmega and usb. or you would use ota upload of the esp sketch.

with WiFi Link you upload the esp firmware and then you connect esp to Serial3 and atmega to usb. and you write your sketch with a good WiFi library, upload it and debug. business as usual. no dip switch flipping.

Juraj:
I think that at that board it is possible to connect esp with atmega's Serial3. the esp has only one Serial.

Yeah it appears so, i been following some sample sketches i found and when i have dip switch 1,2,3,4 set to ON and the rest OFF it allows communication: USB <-> ATmega2560 <-> ESP8266

The sample sketch i'm using is one that connects the ESP to wifi and makes a simple webserver, then i upload a sketch to the ATMega2560 and within the webserver i can click a On/OFF button which turns the internal LED ON/OFF.

The documents i'm following to grasp an understand is here, Контроллер Arduino Mega с ESP8266 | System Engineering

Using google translate works wonders lol.

The last point you have in regards to WiFi Link, would all the code reside on the ESP and you read serial3 for the GPIO's? I'm just trying to comprehend how the ESP interacts with the Mega. As i assume they are two independent units and i need to set each up individually and then make them talk together. correct me if im wrong.

Thanks

this one?

I assume your asking if thats the board i have.. yes

there are the english docs

Pythong:
The last point you have in regards to WiFi Link, would all the code reside on the ESP and you read serial3 for the GPIO's?

it would be the case with Firmata handling the Atmega, not WiFi Link

Pythong:
I'm just trying to comprehend how the ESP interacts with the Mega. As i assume they are two independent units and i need to set each up individually and then make them talk together. correct me if im wrong.

with WiFi Link the firmware handles the esp

When i try to verify/compile WiFi Link Firmware i get the following error any idea why?

Build options changed, rebuilding all
In file included from C:\Users\Chris\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.2.0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:39:0,

                 from sketch\CommLgc.h:25,

                 from sketch\CommLgc.cpp:25:

C:\Users\Chris\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.2.0\libraries\ESP8266WiFi\src/WiFiClient.h: In instantiation of 'size_t WiFiClient::write(T&, size_t) [with T = unsigned char [128]; size_t = unsigned int]':

sketch\CommLgc.cpp:750:121:   required from here

C:\Users\Chris\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.2.0\libraries\ESP8266WiFi\src/WiFiClient.h:123:36: error: request for member 'available' in 'source', which is of non-class type 'unsigned char [128]'

     size_t left = source.available();

                                    ^

C:\Users\Chris\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.2.0\libraries\ESP8266WiFi\src/WiFiClient.h:127:5: error: request for member 'read' in 'source', which is of non-class type 'unsigned char [128]'

     source.read(buffer.get(), will_send);

     ^

exit status 1
Error compiling for board Generic ESP8266 Module.

I installed ESP8266 Core 2.4-RC1 and it compiled... onto next stage :slight_smile:

Pythong:
I installed ESP8266 Core 2.4-RC1 and it compiled... onto next stage :slight_smile:

RC2 is better. with RC1 I had connection stability isues

Juraj:
RC2 is better. with RC1 I had connection stability isues

Thank you for your help. I have been running this setup for a while and been trying to diagnose some problems where my MQTT messages stop sending. Wifi isn't dropped.

I'm thinking of sending my messages over restFUL but can't find a compatible library that works with WiFiLink. Do you have any rest libraries you recommend me try?

Thanks