ArduinoESP32 DevKit V1Lab virtual

4G MQTT (EC200U)

Quectel EC200U on an ESP32: data up, then the module's own MQTT commands publish a reading and listen for commands.

4G MQTT (EC200U) in Argynix Lab

The code

sketch.inoC++
// MQTT over 4G with Quectel's EC200U: AT+QICSGP and AT+QIACT bring data up, then
// AT+QMTOPEN, AT+QMTCONN, AT+QMTSUB and AT+QMTPUB talk to the Lab's MQTT broker.
// Every 5 s it publishes the knob on GPIO 34 to lab/ec200u/knob, and prints what
// arrives on lab/ec200u/cmd.
#define AT Serial2

String ask(const char *cmd, uint32_t ms = 800) {
  AT.print(cmd);
  AT.print("\r");
  String got;
  uint32_t t = millis();
  while (millis() - t < ms) while (AT.available()) got += (char)AT.read();
  got.trim();
  Serial.println(got);
  return got;
}

void setup() {
  Serial.begin(115200);
  AT.begin(115200, SERIAL_8N1, 16, 17);
  delay(3000);
  ask("ATE0");
  while (ask("AT+CREG?").indexOf(",1") < 0) delay(1000);
  ask("AT+QICSGP=1,1,\"internet\",\"\",\"\",1");
  ask("AT+QIACT=1", 1500);
  ask("AT+QMTOPEN=0,\"broker.lab\",1883", 1500);
  ask("AT+QMTCONN=0,\"esp32-ec200u\"", 1500);
  ask("AT+QMTSUB=0,1,\"lab/ec200u/cmd\",0", 1000);
}

void loop() {
  int knob = analogRead(34);
  char cmd[80];
  snprintf(cmd, sizeof cmd, "AT+QMTPUB=0,0,0,0,\"lab/ec200u/knob\",\"%d\"", knob);
  ask(cmd, 600);
  uint32_t t = millis();
  while (millis() - t < 5000) while (AT.available()) Serial.write(AT.read());
}