MQTT to fixed ip address for emoncms.org?

Hello.
I’m trying to send to emoncms.org via MQTT. I’m programming an arduino to use an ENC28J60 based ethernet adaptor to publish MQTT from a fixed ip address. The easiest way I think would be to send MQTT directly to the emoncms.org fixed IP address, would this be possible?

Any thoughts?

Dan

#include <NanodeUNIO.h>
#include <NanodeUIP.h>
#include <NanodeMQTT.h>

NanodeMQTT mqtt(&uip);
struct timer my_timer;

void setup() {
  byte macaddr[6];
  NanodeUNIO unio(NANODE_MAC_DEVICE);

  Serial.begin(38400);
  Serial.println("MQTT Publish test");
  
  unio.read(macaddr, NANODE_MAC_ADDRESS, 6);
  uip.init(macaddr);

  // FIXME: use DHCP instead
  uip.set_ip_addr(192, 168, 2, 10);
  uip.set_netmask(255, 255, 255, 0);

  uip.wait_for_link();
  Serial.println("Link is up");

  // Setup a timer - publish every 20 seconds
  timer_set(&my_timer, CLOCK_SECOND * 20);

  // FIXME: resolve using DNS instead
  mqtt.set_server_addr(10, 100, 10, 1);
  mqtt.connect();

  Serial.println("setup() done");
}

void loop() {
  uip.poll();

  if(timer_expired(&my_timer)) {
    timer_reset(&my_timer);
    if (mqtt.connected()) {
      Serial.println("Publishing...");
      mqtt.publish("test", "Hello World!");
      Serial.println("Published.");
    }
  }
}`

Why MQTT? I’d have thought the HTTP API would be simpler (but I am no Arduino programmer). A fixed IP is always a danger as domains do change their IP address from time to time.

Good to know about changing IP addresses.

I’m connecting to a router station in the middle of a public park with potentially hundreds of WiFi connections. I’ll have the ethernet cable advantage but thought to make doubly sure I’m using a quick and efficient protocol :slight_smile:
https://translate.google.com/translate?sl=de&tl=en&js=y&prev=_t&hl=en&ie=UTF-8&u=https%3A%2F%2Fwiki.freifunk.net%2FBerlin%3AStandorte%3AWindrad_Tempelhofer_Feld&edit-text=&act=url

If MQTT isn’t working I’ll fallback on a HTTP webclient arduino example I just found, it does look simpler to do that…

Do you really mean emoncms.ORG ?

There is no MQTT implementation at emoncms.org (that I’m aware of) so I assume you mean self hosted emoncms.

The current emoncms MQTT implementation has some unresolved issues, it is not reliable yet and is most likely (or perhaps just hopefully) going to change in the not too distant future. Plus the MQTT input script is hardcoded for one user only (userid: 1). I would expect a HTTP SSL connection to be both more reliable and more secure.

I’m glad I hadn’t spend too long on this :slight_smile:

emonhub has MQTT support, I assumed emoncms.org could receive MQTT messages.

There is a ‘right tool for the job’ issue here. You can knock a nail in with a spanner, but a hammer is nearly always the better tool.

You know what they say, Brian…
If it doesn’t fit, don’t force it. Get a bigger hammer! :grin:

1 Like