我正在创建一个带有SG90伺服和NodeMCU的喂鱼器
我用了这个草图:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>
// Update these with values suitable for your network.
const char* ssid = "your_wifi_hotspot";
const char* password = "your_wifi_password";
const char* mqtt_server = "broker.mqttdashboard.com";
//const char* mqtt_server = "iot.eclipse.org";
Servo myservo; // create servo object to control a servo
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Command from MQTT broker is : [");
Serial.print(topic);
for(int i=0;i<length;i++)
{
if((int)payload[i]>194||(int)payload[i]<0)
break;
myservo.write((int)payload[i]); // tell servo to go to position in variable '(int)payload[i]'
}
}//end callback
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe("OsoyooCommand");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
} //end reconnect()
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
myservo.attach(D1); // attaches the servo on pin D1 to the servo object
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
当我使用 MQTTBox 以"十进制数组"发送有效载荷时,伺服器正在工作,但是当我将有效载荷作为 JSON 字符串发送时
,这让我很难。如果我发送"十进制数组"1,它确实会将伺服转到位置 1,但是如果我只是将 1 作为有效载荷作为字符串发送,它会将伺服移动到位置 49。如果我将有效载荷发送为 2,它会移动到位置 50。如果我将有效载荷发送为 10,则位置为 4948看起来位置 1 和位置 0 同时。
我的最终目标是通过家庭助理发送这些有效负载,这些有效负载以字符串或 JSON 形式发送,但是我目前没有找到正确的解决方案。我将非常感谢任何帮助或解决方案。
MQTT 有效负载采用 UTF-8 编码,因此 Arduino PubSubClient 库将有效负载视为uint8_t数组。
如果要发送和接收 JSON,则可以使用 ArduinoJson 库来解析 JSON 有效负载。因此,假设 JSON 有效负载如下所示:
{
"position": 123
}
然后,您可以实现回调,例如:
#include <ArduinoJson.h>
// Assuming a fixed sized JSON buffer
StaticJsonBuffer<200> jsonBuffer;
void callback(char* topic, byte* payload, unsigned int length)
{
JsonObject& root = jsonBuffer.parseObject(payload);
if (root.success() && root.is<JsonObject>())
{
int position = root.as<JsonObject>().get<int>("position");
myservo.write(position);
}
}
当您将其作为字符串发送时,电机似乎处于 ASCII 等效数字的位置。
即
- ASCII 相当于十进制中 49 个字符"1"
- ASCII 等效于十进制 50 中的字符"2"
尝试发送字符"a",电机将变为 97。
如果要发送字符串,则必须更改以下代码:
for(int i=0;i<length;i++)
{
if((int)payload[i]>194||(int)payload[i]<0)
break;
myservo.write((int)payload[i]); // tell servo to go to position in variable '(int)payload[i]'
}
自:
int location=String((char*)payload).toInt()
if((location>194)||(location<0))
return;
myservo.write(location);