IoT MQTT DIY Hardware

Posted on
Mon Aug 12, 2019 2:12 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

IoT MQTT DIY Hardware

A thread for discussion of the hardware devices you're using with MQTT.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Aug 12, 2019 3:13 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: IoT MQTT DIY Hardware

So, the first board I'm playing with is the Sonos SV running Tasmota firmware (https://github.com/arendst/Sonoff-Tasmota).

Once I got it flashed with the new firmware, it was easy to configure. MQTT setup was easy. I was trying to get it to send MQTT messages when a connected switch was pressed, but apparently the Tasmota firmware won't do that. You have to assign the switch to control a relay device and then the relay changes get broadcast. However, the relay doesn't have to actually be installed. It just needs to think it's controlling a relay.

The board does come with a fairly high amperage relay installed. Since I don't need that for my initial project idea, I'll try a smaller (thinner) board next. I have a Wemo D1 Mini Pro on order.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Aug 13, 2019 4:33 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: IoT MQTT DIY Hardware

This is a very interesting entry on Github: hobbyquaker/awesome-mqtt :)

Seems there are a large number of possibilities to explore. :D

Posted on
Sat Aug 17, 2019 7:34 am
cuhouse offline
Posts: 144
Joined: Feb 21, 2007
Location: Virginia, USA

Re: IoT MQTT DIY Hardware

FlyingDiver wrote:
So, the first board I'm playing with is the Sonos SV running Tasmota firmware (https://github.com/arendst/Sonoff-Tasmota).

Once I got it flashed with the new firmware, it was easy to configure. MQTT setup was easy. I was trying to get it to send MQTT messages when a connected switch was pressed, but apparently the Tasmota firmware won't do that. You have to assign the switch to control a relay device and then the relay changes get broadcast. However, the relay doesn't have to actually be installed. It just needs to think it's controlling a relay.

The board does come with a fairly high amperage relay installed. Since I don't need that for my initial project idea, I'll try a smaller (thinner) board next. I have a Wemo D1 Mini Pro on order.


Joe,

Sorry for the slow reply as you may have already moved on to the Wemo D1. The Sonoff will do what you want with the switch. Use switchmode. I use this with a switch contact and it sends a MQTT command.
indigo/sonoff/contact/cmnd/POWER1
If you need any more details just let me know.

Jody

Indigo 2022.1.2, Big Sur v11.7.1, Dedicated late 2014 Mac Mini, PowerLinc 2413U.

Posted on
Sat Aug 17, 2019 7:40 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: IoT MQTT DIY Hardware

cuhouse wrote:
Sorry for the slow reply as you may have already moved on to the Wemo D1. The Sonoff will do what you want with the switch. Use switchmode. I use this with a switch contact and it sends a MQTT command.
indigo/sonoff/contact/cmnd/POWER1


I think I did eventually get that working on the Sonoff. On the other hand, the Wemos is definitely smaller, so is probably a better choice if you don't need the relay.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Aug 22, 2019 3:36 pm
BonfireVA offline
Posts: 62
Joined: Sep 17, 2017

Re: IoT MQTT DIY Hardware

Currently I am using a Wemos D1 (not the mini) with several Dallas ds180b20 temp sensors run thought the house (and in the pool). I have several Wemos D1mini's, some just running motion sensing with an AM32 PIR Sensor, and some with temp/hum/pressure using BME280 sensors and ambient light sensing with a temt6000 sensor. I have a couple nodemcu's one set up as a multi sensor and one driving relays with reed switches for my three garage doors. Also running several Sonnoff basics and pow's and I just started playing with a zemismart KS-811-3 wifi switch and a Uno D1 R32 (anduino uno form factor with esp32 chip) . I use the zemismart as a scene controller to set house modes and I am tinkering with BLE presents detection with the R32. Everything is running esphome firmware.

I noticed others mentioned tasmota, also a great option, I used tasmota for quite a while but finally settled on esphome because of its support for esp32 based boards as well as really slick native API integration into home assistant. But you can't go wrong with either firmware!

-Eric

Posted on
Mon Aug 26, 2019 1:36 am
agame offline
Posts: 514
Joined: Jul 13, 2017
Location: Melbourne, Australia

Re: IoT MQTT DIY Hardware

anyone else using LoRa Devices with MQTT?

long battery life/low power consumption, extremely long range, and fairly cheap hardware makes these very interesting. I'm creating a private network in a remote area, not pursuing the LoRaWAN public network option.

I'm setting up a system for managing a remote (500m from house) solar water pump (monitoring voltage, switching via relay, pulse counter on water meter). Thus far I appear to have end-to-end connectivity but will be a couple of weeks before I can properly deploy in the field and look at real data.

my first devices are these

https://www.iot-store.com.au/collections/lorawan/products/lorawan-i-o-controller-lt-33222-l
https://www.dragino.com/products/lora-lorawan-gateway/item/143-lg01n.html

Posted on
Thu Sep 17, 2020 4:53 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: IoT MQTT DIY Hardware

I’m sorry that I have no idea where to start with this. I can post a lot more detail, but has a question been asked and answered about how to read a json payload (sent from MQTT) on an Arduino?

I think the MQTT Connector will be great for controlling four stepper motors running off of an Arduino for my theater screen motorized masking system.

I have a stepper motor on an Arduino with an ethernet shield running an MQTT client that gets commands from MQTT Connector in Indigo. I need three more, all of which will have individual data sent to them, hence my desire to figure out how to convert the payload into json so I can use that to run logic on the Arduino.

(FlyingDiver, i am MinHeadroom on avsforums, the build is “Adult Cave but Not in a Creepy Way.”)

Posted on
Thu Sep 17, 2020 6:26 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: IoT MQTT DIY Hardware


joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Sep 17, 2020 10:53 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

IoT MQTT DIY Hardware

FlyingDiver wrote:
https://arduinojson.org/v6/example/parser/
Yep- that was an active tab for two hours of my life today :)
Apparently I am missing something. I will try to formulate my conundrum better tomorrow.

Edit: and thank you :)

Posted on
Fri Sep 18, 2020 4:14 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: IoT MQTT DIY Hardware

Post what you have for a sketch and maybe we can help.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Sep 18, 2020 10:50 am
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: IoT MQTT DIY Hardware

I will when i get home- I realize the problem results from my forgetting everything about C programming and getting spoiled with python.

I am using a pubsub.h callback function that reads the mqtt payload as byte*, thereafter converting it to a String. I am trying to use ArduinoJSON to convert a char[] into into JSON, and I think I ran into problems because of my inconsistent NULL usage. I THOUGHT the problem was my use of escape backslashes in the payload sent from Indigo.

Details to follow and thank you

Posted on
Fri Sep 18, 2020 3:29 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

Re: IoT MQTT DIY Hardware

Okey Doke, here it is, it ain't prety.

The goal is to receive an MQTT command from a broker. I want to learn how to send and receive commands using json, as that sems to be pretty important as I endeavour to learn MQTT.

The payload MQTT sends looks like this:
Code: Select all
"{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}"


Pretty simple, but it's killing me. I cannot get the byte* payload to turn into a char[] that i need it to be in for the ArduinoJSON parser.
Code: Select all
// This dope sketch allows the arduino with ethernet shield to connect to network, subscribe to mqtt, receive payload, then control a big ass stepper motor using am a4988 driver
// In this sketch, it  takes a payload with numbers of revolutions for each screen masking roller

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <AccelStepper.h>
#include <ArduinoJson.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define leftDirPin 2
#define leftStepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, leftStepPin, leftDirPin);

// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);

// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
 
// Set your MAC address and IP address here
byte mac[] = { 0xDE, 0xAA, 0xBB, 0xCC, 0xDD, 0x00 };
IPAddress ip(192, 168, 1, 159);
 
//Enter your mqtt server configurations
const char* mqttServer = "REDACTED//Enter Your mqttServer address
const int mqttPort = 1883;                  //Port number
const char* mqttUser = "REDACTED";        //User
const char* mqttPassword = "REDACTED";  //Password

void setup()
{
  // Useful for debugging purposes
  Serial.begin(9600);
 
  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);
 
  // initialize the Ethernet shield using DHCP:
  Serial.println("Obtaining an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to obtaining an IP address");

    // check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware)
      Serial.println("Ethernet shield was not found");

    // check for Ethernet cable
    if (Ethernet.linkStatus() == LinkOFF)
      Serial.println("Ethernet cable is not connected.");
  }
 
  // BELOW IS ERROR CHECKING FOR IP CONNECTIVITY
  Serial.print("- Arduino's IP address   : ");
  Serial.println(Ethernet.localIP());
  Serial.print("- Gateway's IP address   : ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("- Network's subnet mask  : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("- DNS server's IP address: ");
  Serial.println(Ethernet.dnsServerIP());
 
  // Ethernet takes some time to boot
  delay(3000);                         
 
  // Set the MQTT server to the server stated above ^
  mqttClient.setServer(mqttServer, mqttPort);
  mqttClient.setCallback(MQTTcallback);
  while (!mqttClient.connected()) {
    Serial.println("Connecting to MQTT...");
    if (mqttClient.connect("NewEthernetShield", mqttUser, mqttPassword )) {
      Serial.println("Connected"); 
    } else {
      Serial.print("failed with state ");
      Serial.println(mqttClient.state());  //If you get state 5: mismatch in configuration
      delay(2000);
    }
  }
 
  mqttClient.publish("ethernetshield/test", "Hello from EthernetShield v2");
  mqttClient.subscribe("ethernetshield/test");
}

void MQTTcallback(char* topic, byte* payload, unsigned int length) {
  int speed = 400;
  int targetPosition;
  String message;
  StaticJsonDocument<54> doc;
  char json[100];
 
  Serial.print("Message arrived in topic: "); //WORKS
  Serial.println(topic);
 
  //Output payload and build a char[] in json formatting //WORKS
  Serial.print("Message by character:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    json[i] = (char)payload[i];  //NOT SURE IF THIS WORKS
  }
  Serial.print("\n");

  //CONVERT STRING TO A CHAR ARRAY USABLE BY JSONDESERIALIZER ??DEFINITELY DOES NOT WORK
  //char json[message.length()];
  /*message.replace(" ", "");
  message.replace("\n", "");
  message.trim();
  //message.remove(0,1);
  message.toCharArray(json, message.length()+1);
  */

  //char json[]="{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}"; //this is for testing and works fine
  //CHECK TO SEE WHAT THIS ARRAY LOOKS LIKE
  Serial.print("Contents of json:");
  for (int i=0; i<strlen(json); i++){
      Serial.print(json[i]);
  }
  Serial.print("\n");
 
  //DESERIALIZE INTO JSON //WORKS SOME TIMES< INSOFAR AS IT DOES NOT THROW AN ERROR
  DeserializationError error = deserializeJson(doc, json);
    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.c_str());
      return;
    }
    else Serial.println("Deserealization successful");

  //CHECK THE CONTENTS ??DOES NOT WORK, ALL VALUES ARE zeros
  Serial.println("Instructions:");
  int top = doc["top"];
  Serial.print("Top:");
  Serial.println(top);
  int bottom = doc["bottom"];
  Serial.print("Bottom:");
  Serial.println(bottom);
  int left = doc["left"];
  Serial.print("Left:");
  Serial.println(left);
  int right = doc["right"];
  Serial.print("Right:");
  Serial.println(right);

  //TELL IT WHAT ROLLER's INFO TO TURN
  targetPosition = left;

  // SET CURRENT POSITION
  stepper.setCurrentPosition(0);
  Serial.println("Stepper current position set to 0");
  Serial.print("Stepper target position is:");
  Serial.println(targetPosition);

  //MAKE THE SPEED NEGATIVE IF TARGET IS NEGATIVE
  if(targetPosition<0) speed*=-1;
 
  while(stepper.currentPosition() != targetPosition)
  {
    stepper.setSpeed(speed);
    stepper.runSpeed();
    Serial.println(stepper.currentPosition());
  }
  // END IMPORT
  Serial.println("Done");
  Serial.println("-----------------------"); 
}

void loop()
{
  mqttClient.loop();
}


And here is my output:
Code: Select all
Obtaining an IP address using DHCP
- Arduino's IP address   : 192.168.1.159
- Gateway's IP address   : 192.168.1.1
- Network's subnet mask  : 255.255.255.0
- DNS server's IP address: 192.168.1.1
Connecting to MQTT...
Connected
Message arrived in topic: ethernetshield/test
Message by character:-800
Contents of json:-800p":200,"bottom":300,"left":100,"right":600}
deserializeJson() failed: InvalidInput

Ignore this error, some day I will figure out what is sending out this -800 message that every client on my network receives upon booting

Now, if I use Indigo or MQTTool to send
Code: Select all
"{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}"
, i get this output:

Code: Select all
Message arrived in topic: ethernetshield/test
Message by character:"{\"top\":200,\"bottom\":300,\"left\":100,\"right\":600}"
Contents of json:"{\"top\":200,\"bottom\":300,\"left\":100,\"right\":600}"103  <-- Edit, not sure what's going on here
Deserealization successful
Instructions:
Top:0
Bottom:0
Left:0
Right:0
Stepper current position set to 0
Stepper target position is:0
Done
-----------------------


But, if I don't use the byte stream from the MQTT payload, and instead use my own variable:
Code: Select all
char json[]="{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}";
,
then everything works and I can control my motors...
Code: Select all
Contents of json:{"top":100,"bottom":101,"left":102,"right":103}
Deserealization successful
Instructions:
Top:100
Bottom:101
Left:102
Right:103
Stepper current position set to 0
Stepper target position is:102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Done


I am pretty sure this is based on my lowsy understanding of memory management and data types, which I hope to improve upon.

Also, I am really, really jazzed at the idea of using Indigo to control more complex motor-powered devices with cheap logic boards on them. Any help is really appreciated

Posted on
Fri Sep 18, 2020 3:58 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: IoT MQTT DIY Hardware

Take a look at this code as an alternative starting point:

https://gist.github.com/virgilvox/ffe1cc08a240db9792d3

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Fri Sep 18, 2020 8:10 pm
mundmc offline
User avatar
Posts: 1060
Joined: Sep 14, 2012

IoT MQTT DIY Hardware

I have never seen a 4 loop that begins with i=26 !

But i need to learn about this JasonObject& part:

Code: Select all
 char inData[80];

 
 Serial.print("payload: ");
 for(int i =26; i<length; i++){
  // Serial.print((char)payload[i]);
   inData[(i - 26)] = (char)payload[i];
 }
 Serial.println();
 
 JsonObject& root = jsonBuffer.parseObject(inData); 


Thank you I hope I can figure it out from here

Who is online

Users browsing this forum: No registered users and 3 guests

cron