We have connected our DLMS Indian Standarad using an ESP32 and a 4G modem. The ESP32 acts as a serial passthrough i.e. it will take the data received over modem serial and forward the same to the meter. The ESP32 does not do anything other than beig a simple postman.
This code works perfectly if we have meter over serial1 and data send out by esp32 wifi
The same code only by removing wifi for sending out data and using 4g modem on serial2 is not working. we get data retry messages
Whats the issue between wifi and modem. Since esp32 is only a passthrough this should not matter.
adding code that we have created in esp32.
===========
esp32 arduino wifi working code:
#include <WiFi.h>
const char* ssid = "XXX";
const char* password = "XXXX*";
// rs port config
const int baudrate = 19200;
const int rs_config = SERIAL_8N1;
// reading buffor config
#define BUFFER_SIZE 1024
// global objects
WiFiServer wifiServer(4059);
byte buff[BUFFER_SIZE];
int size = 0;
char s[50];
void setup()
{
Serial.begin(115200);
Serial1.begin(baudrate, rs_config, 22, 23); //this is the meter on serial1
delay(10);
// We start by connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
wifiServer.begin();
// Use WiFiClient class to create TCP connections
delay(2000);
Serial.print("Writing <<CONFIGOK>>");
Serial1.write("<<CONFIGOK>>");
// Serial1.flush();
// read data from serial and send to wifi client
while ((size = Serial1.available())) {
Serial.print("reading in setup\n");
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial1.readBytes(buff, size);
snprintf(s, 50, "%d", buff);
Serial.print(s);
Serial1.flush();
}
delay(1000);
}
int value = 0;
void loop()
{
// delay(5000);
++value;
Serial.print("waiting for connection\r\n");
//// Serial.println(host);
//
WiFiClient client = wifiServer.available();
if (client) {
while (client.connected()) {
// read data from wifi client and send to serial
while (size = client.available() > 0) {
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
client.read(buff, size);
snprintf(s, 50, "%d", buff);
Serial1.write(buff, size);
Serial1.flush();
}
// read data from serial and send to wifi client
while ((size = Serial1.available())) {
Serial.print("Writing to Client\n");
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial1.readBytes(buff, size);
client.write(buff, size);
client.flush();
}
}
client.stop();
Serial.println("Client disconnected");
delay(100);
}
}
===========
esp32 using serial 4g modem code:
#include <WiFi.h>
// rs port config
const int baudrate = 19200;
const int rs_config = SERIAL_8N1;
// reading buffor config
#define BUFFER_SIZE 1024
// global objects
//WiFiServer server;
//WiFiClient client;
byte buff[BUFFER_SIZE];
const unsigned int MAX_MESSAGE_LENGTH = 12;
int size = 0;
char s[50];
void setup()
{
Serial1.begin(baudrate, rs_config, 22, 23); //this is the meter on serial1
delay(10);
// Serial.print("Writing <<CONFIGOK>>");
Serial1.write("<<CONFIGOK>>\r\n");
Serial1.flush();
read data from serial and send to wifi client
while ((size = Serial1.available())) {
// Serial.print("reading in setup\n");
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial1.readBytes(buff, size);
snprintf(s, 50, "%d", buff);
// Serial.print(s);
// client.write(s);
Serial1.flush();
}
delay(2000);
Serial2.begin(115200, SERIAL_8N1, 3, 1); //this is the modem on serial2
delay(100);
Serial2.write("at+creg?\r\n");
delay(1000);
// Serial.print(Serial2.readStringUntil('\n'));
Serial2.write("at+cmee=0\r\n");
Serial2.write("at+cipmux=0\r\n");
Serial2.write("AT+CGPADDR\r\n");
delay(3000);
// Serial.print(Serial2.readStringUntil('\n'));
Serial2.write("AT+CIPSERVER=1,10002,1\r\n");
delay(2000);
}
void loop()
{
// Serial.print("waiting for connection\r\n");
Serial2.write("at+cipstatus\r\n");
char response[200];
// for(int i = 0 ; Serial2.available() > 0 && i<200 ; i++) {
// response[i++] = Serial2.read();
// }
// Serial.print(response);
while ((size = Serial2.available())) {
// Serial.print("reading in setup\n");
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial2.readBytes(buff, size);
snprintf(response, 200, "%s", buff);
Serial.print(response);
Serial2.flush();
}
if(strstr(response, "STATE:CONNECTED")) {
delay(1000);
int flag = 1;
while (flag == 1) {
// read data from wifi client and send to serial
while (size = Serial2.available() > 0) {
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial2.read(buff, size);
// snprintf(s, 50, "%d", buff);
// if(s=="201275")
// ESP.restart();
// // Serial.println(s);
Serial1.write(buff, size);
Serial1.flush();
}
//
// // read data from serial and send to wifi client
Serial2.write("at+cipsend\r\n");
delay(200);
// Serial2.write("we got in");
while ((size = Serial1.available())) {
// Serial.print("Writing to Client\n");
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial1.readBytes(buff, size);
Serial2.write(buff, size);
Serial2.flush();
}
Serial2.write(char(26));
delay(200);
Serial2.write("at+cipstatus\r\n");
char response1[200];
// for(int i = 0 ; Serial2.available() > 0 && i<200 ; i++) {
// response1[i++] = Serial2.read();
// }
while ((size = Serial2.available())) {
// Serial.print("reading in setup\n");
size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
Serial2.readBytes(buff, size);
snprintf(response1, 200, "%s", buff);
Serial.print(response1);
Serial2.flush();
}
if(strstr(response1, "STATE:INITIAL")) {
flag = 0;
}
//
}
//
// client.stop();
Serial.println("Client disconnected");
delay(100);
}
delay(2000);
}
===========
Error with 4g modem using Python Client:
TX: 10:55:47 00 01 00 30 00 01 00 70 60 6E A1 09 06 07 60 85 74 05 08 01 03 A6 0A 04 08 44 4D 53 30 30 30 30 34 8A 02 07 80 8B 07 60 85 74 05 08 02 02 AC 12 80 10 01 CF 44 47 C3 D9 69 59 F2 DC 1A 5E 2F B5 A2 2F BE 34 04 32 21 30 30 00 00 00 00 4E 1F 33 40 E5 80 33 EA 4D 74 4F B7 FB 02 ED A0 F5 A0 17 2A 38 EC EE 60 AC 70 F2 E6 F7 24 1E D5 8A 96 6E 53 C4 B4 A4 26 15 83 34
Data send failed. Try to resend 1/3
Data send failed. Try to resend 2/3
RX: 10:56:02
Traceback (most recent call last):
Error with 4g modem using Java Client:
Invalid data type: 76
or Invalid data type: 40
or any other number.
===========
Thank you in advance.
Hi, I believe that the…
Hi,
I believe that the problem is that you are using WRAPPER framing and you should use HDLC framing with the serial port.
Change Interface from WRAPPER to HDLC and try again.
BR,
Mikko
Thanks MIko for the reply,…
Thanks MIko for the reply, but the same code is working when I am connecting through WIFi.
In both the cases, wifi as well as 4G the meter is always connected on serial to the esp32, its the cloud side that has either wifi or 4G
however I tried with HDLC too and the issue is still there.
Hi, Are you receiving data…
Hi,
Are you receiving data to ESP32 using 4G modem? I believe that the issue is there and data is not received to the ESP32 from the GPRS network.
BR,
Mikko
yes I have confirmed that…
yes I have confirmed that complete data is received by the 4G modem.
Hi, Your code looks correct…
Hi,
Your code looks correct. Are you receiving any data to the client?
You can try to make an echo server for the ESP32 and test that you can receive data with 4G.
BR,
Mikko