|
楼主 |
发表于 2018-3-31 17:57
|
显示全部楼层
我即使改成通过串口发送消息 ,也会导致wifi模块重启
#include <SoftwareSerial.h>
#include <dht11.h>
#include "WiFiEsp.h"
//温湿度接口和变量
#define DHT11PIN 2
dht11 DHT11;
//wifi和LED接口
#define WIFI_TX 9
#define WIFI_RX 8
SoftwareSerial wifi(WIFI_RX, WIFI_TX); //RX, TX
#define LED 13
//WIFI名称,密码,状态
char ssid[] = "ccc";
char pass[] = "12345678";
int status = WL_IDLE_STATUS;
//继电器
int relayPin = 7;
int val;
WiFiEspClient client;
//获得来自客户端的连续数据
String _comdata_wifi = "";
void setup() {
pinMode(LED,OUTPUT);
digitalWrite(LED,HIGH); //初始化LED为灭,HIGH为亮
Serial.begin(9600);
wifi.begin(115200);
//继电器
digitalWrite(relayPin,HIGH);
pinMode(relayPin, OUTPUT);
wifi.println("AT+CWMODE=1\r\n"); //设置为AP模式
delay(100);
wifi.println("AT+CIPMUX=1\r\n"); //设置为多连接模式
delay(100);
WiFi.init(&wifi);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true);
}
//连接wifi
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
}
delay(100);
client.connect("192.168.191.1", 9000);
delay(100);
}
void loop() {
val=Serial.read();
if(val=='A'){
digitalWrite(relayPin, LOW); //LOW to poweroff the LED light on relayPin
}
if(val=='B'){
digitalWrite(relayPin, HIGH);
}
//获得客户端通过WiFi发送的数据
getWifiSerialData();
}
//接受Wifi模块的数据
void getWifiSerialData(){
while (wifi.available() > 0){
_comdata_wifi += char(wifi.read());
}
delay(10);
}
//通过wifi发送数据到客户端,发送数据需要按照固定的格式
void sendData(int connectionId,String data){
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=data.length();
cipSend +="\r\n";
wifi.println(cipSend);
wifi.println(data);
int dataSize = cipSend.length();
char d[dataSize];
cipSend.toCharArray(d,dataSize);
wifi.write(d,dataSize);
} |
|