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.
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
FlyingDiver wrote:Yep- that was an active tab for two hours of my life todayhttps://arduinojson.org/v6/example/parser/
"{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}"
// 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();
}
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
"{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}"
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
-----------------------
char json[]="{\"top\":100,\"bottom\":101,\"left\":102,\"right\":103}";
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
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);
Users browsing this forum: No registered users and 2 guests