esp8266接线
右前1、左后4分别是VCC、GND
功能 | 描述 |
---|---|
GPIO 16 | 外部的Reset信号,低电平复位,高电平工作(默认情况下为高) |
CH_PD | 高电平工作,低电平模块关掉 |
UTXD | 发送 |
URXD | 接收 |
GPIO 0 | 工作模式,悬空;下载模式下拉 |
GPIO 2 | 开机上电时为高电平,禁止硬件下拉,内部默认拉高 |
esp8266 code
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "oppo";
const char* password = "741852963";
const char* mqtt_server = "iot.celitea.cn";
char *water = "water";
char *apple = "apple";
char *banana = "banana";
char *medicine = "medicine";
char *advance = "advance";
char *retreat = "retreat";
char *left = "left";
char *right = "right";
char *shutoff = "shutoff";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
char str[length];
for (int i = 0; i < length; i++) {
str[i] = (char)payload[i];
}
str[length] = '\0';
if (strcmp(water, str) == 0)
Serial.println(1);
else if (strcmp(apple, str) == 0)
Serial.println(2);
else if (strcmp(banana, str) == 0)
Serial.println(3);
else if (strcmp(medicine, str) == 0)
Serial.println(4);
/*前后左右停*/
else if (strcmp(advance, str) == 0)
Serial.println(11);
else if (strcmp(retreat, str) == 0)
Serial.println(12);
else if (strcmp(left, str) == 0)
Serial.println(13);
else if (strcmp(right, str) == 0)
Serial.println(14);
else if (strcmp(shutoff, str) == 0)
Serial.println(15);
else
Serial.println(0);
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
if (client.connect("ESP8266Client")) {
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
client.publish("outTopic", msg);
}
}
舵机代码
int servopin = 7; //定义舵机接口数字接口7
void servopulse(int angle)//定义一个脉冲函数
{
int pulsewidth=(angle*11)+500; //将角度转化为500-2480的脉宽值
digitalWrite(servopin,HIGH); //将舵机接口电平至高
delayMicroseconds(pulsewidth); //延时脉宽值的微秒数
digitalWrite(servopin,LOW); //将舵机接口电平至低
delayMicroseconds(20000-pulsewidth);
}
void setup()
{
pinMode(servopin,OUTPUT);//设定舵机接口为输出接口
}
void loop()
{
//把值的范围映射到0到165左右
//发送50个脉冲
for(int i=0;i<100;i++)
{
//引用脉冲函数
servopulse(1);
}
for(int i=0;i<100;i++)
{
servopulse(164);
}
}