TWIL 0007 | 24w44 | Arduino - ESP

Posted on Nov 5, 2024

Arduino Uno with ESP8266

I bought a board that is two boards in one place. Of course I didn’t know that beforehand - the price for learning everything on the fly. I found some firmware for ESP, that should do everything out of box so I need to care only about Uno. But never got that working.

What I need is to connect to MQTT (Mosquitto) and pub/sub messages from that. Turned out the chips communicate between each other through serial and that can be used for passing messages. Life-saving tutorial on how to make it work.

Code is hacky, but working.

My ESP code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
// TODO to config file
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "192.168.x.y";
const int mqtt_port = 1883;
const char* log_queue = "log";


WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;

#define MSG_BUFFER_SIZE	(15)
char msg[MSG_BUFFER_SIZE];

String msg_s = "";
int value = 0;

void setup_wifi() {
  delay(10);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      // Once connected, publish an announcement...
      client.publish(log_queue, "connected");
      // ... and resubscribe
      client.subscribe("queue");
    } else {
      // Wait 5 seconds before retrying
      delay(500);
    }
  }
}


void callback(char* topic, byte* payload, unsigned int length) {
  /**
   * callback function takes a msg from MQTT, prepends "ESP|" and pass it into Serial
   */
  msg_s = "";
  for (int i = 0; i < length; i++) {
    msg_s.concat((char)payload[i]);
  }
  Serial.println("ESP|" + msg_s);
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  /**
   * Here we transfer msgs from Uno to Log queue
   */
  if (Serial.available()) {
    String serialMsg = Serial.readString();
    if (serialMsg.startsWith("UNO|")) {
      // here you can also trim "UNO|" if you need to
      serialMsg.toCharArray(msg, MSG_BUFFER_SIZE);
      client.publish(log_queue, msg);
    }

  }
  delay(5); // to keep it sane
}

So the messages from the MQTT are prepended with “ESP|” and printed to the Serial. Messages from the Uno have “UNO|” prepended and those are routed to the log_queue.

Uno code:

#include <Arduino.h>
String msg;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.flush();

}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()) {
    msg = Serial.readString();
    if (msg.startsWith("ESP|")) {
      // DO MAGIC HERE

      // SEND MESSAGE TO QUEUE
      Serial.println("UNO|MESSAGE");
    }
  }
  delay(20); // to keep it sane

}

FFMPEG magic

This is how to speed up a video with ffmpeg, the most magical tool of all.

ffmpeg -i SOURCE.mp4 -filter:v "setpts=0.25*PTS" -an TARGET.mp4

Everything is a nail

AI hammer

Source on X