BlynkClient

//调试开关
#define DEBUG true
//软串口驱动库
#include <SoftwareSerial.h>
//实例化一个软串口对象,PIN2和PIN3作为参数传入. PIN2为接收脚RX,PIN3为发送脚TX
SoftwareSerial EspSerial(2, 3); // RX, TX
//W5100 Arduino网络扩展卡驱动库
#include <SPI.h>
#include <Ethernet.h>
//Blynk Arduino平台网络支持库
#include <BlynkSimpleEthernet.h>
//你可以在Blynk发送的邮件中找到正确的访问令牌
char auth[] = "FZLjxU5D4gdsfMZwA8-wlfZ8aR0DQNbk";
#define W5100_CS  10
#define SDCARD_CS 4
//本客户端的多连接ID=0
int mux_id=0;
//温度缓冲器,默认100度方便调试时判断是否正常
int tempValue=100;

uint8_t host[] = "192.168.3.8";
const char* ssid     = "HUAWEI"; //修改成你可访问的wifi名称
const char* password = "99101201";  // 修改成wifi密码
const char* ip     = "192.168.3.8"; // 你的网点域名或IP 

#define SSID "HUAWEI"
#define PASS "99101201"
#define DST_IP "192.168.3.8"
#define CONNECT_ATTEMPTS 5

//Blynk端的客厅灯按钮事件
BLYNK_WRITE(V1)
{
  //从参数解析出虚拟引脚V1的值
  int pinValue = param.asInt();

  //连接TCP服务器
  connectMaster(ip);
   
  // 发送给TCP服务器
  if(pinValue)  
    command2MasterNode(mux_id,",lamp=,on,");
  else
    command2MasterNode(mux_id,",lamp=,off,");
}

//Blynk端的空调按钮按事件
BLYNK_WRITE(V3)
{
  //从参数解析出虚拟引脚V3的值
  int pinValue = param.asInt();

  //连接TCP服务器
  connectMaster(ip);
   
  // 发送给TCP服务器
  if(pinValue==2)  
    command2MasterNode(mux_id,",air=,on,");
  else if(pinValue==1)
    command2MasterNode(mux_id,",air=,off,");
}

//上传温湿度数据
BLYNK_READ(V2) { 
  Blynk.virtualWrite(V2,tempValue);
}
void setup()
{
  //设置硬串口的波特率为9600
  Serial.begin(115200);
  //ESP-12F串口默认波特率为115200
  EspSerial.begin(115200); 
  //有线网卡的特别设置,关闭SD卡功能 
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card 
  //设置ESP-12F为TCP客户端
  setSAP();
  //连接Blynk云
  Blynk.begin(auth);
}

void loop()
{    
  //连接TCP服务器
  connectMaster(ip);
  if(EspSerial.available()) 
  {
      String response = "";
      while(EspSerial.available())
      {
        // The esp has data so display its output to the serial window 
        char c = EspSerial.read(); // read the next character.
        //if(c=='+')
          response+=c;
      }
      //去掉字符串前后的空格,便于比较
      response.trim();
      Serial.println("*************************");
      Serial.println(response);
      Serial.println("*************************");
      int dataSize = response.length();
      char Espdata[dataSize];
      response.toCharArray(Espdata,dataSize); 
       
      //parseCommand(Espdata,dataSize);
  }
//  //更新温度值
//  if (EspSerial.available()) {    
//    if(EspSerial.find(",temp==,"))
//    {
//      //等待缓冲区填满
//      //delay(1000); 
//      tempValue =  EspSerial.parseInt();// EspSerial.read()-48;
//    }
//  }
  
  //启动Blynk云服务
  Blynk.run();
}
void setSAP()
{
  sendCommand("AT\r\n",1000,DEBUG);  
  sendCommand("AT+RST\r\n",1000,DEBUG);   
  delay(1000);    
  sendCommand("AT+CWMODE=3\r\n",1000,DEBUG); // 开启 AP+STA 模式
  //使用11通道,不加密(0:open)
  //sendCommand("AT+CWSAP=\"TTT\",\"sw#1234567\",11,0\r\n",3000,DEBUG);
  sendCommand("AT+CWJAP=\"HUAWEI\",\"99101201\"\r\n",3000,DEBUG);
  delay(5000);
  //设置非透传模式
  sendCommand("AT+CIPMODE=0\r\n",1000,DEBUG);  
  //设置多连接
  sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG);
  //启动服务器
  sendCommand("AT+CIPSERVER=1\r\n",1000,DEBUG);
  delay(3000);    
  sendCommand("AT+CIFSR\r\n",1000,DEBUG);  
  delay(3000);
  //连接TCP服务器
  connectMaster(ip);
}
void setSTA()

  /* 
  EspSerial.println("AT+RST");
  delay(1000);
  if (EspSerial.find("ready")) Serial.println("Module is ready");
  else {
    Serial.println("ESP8266 Module did not respond.");
    Serial.println("Enter Commands Manually.");
    while (1) chk_serial_io();
  }
  delay(1000);
  */
  sendCommand("AT+RST\r\n",1000,DEBUG);   
  delay(1000);
  sendCommand("AT+CWMODE=1\r\n",1000,DEBUG); // 开启STA 模式
  //连入家居控制系统局域网smartHome
  sendCommand("AT+CWJAP=\"HUAWEI\",\"99101201\"\r\n",3000,DEBUG);
  //等待网络稳定 
  delay(5000);
  //while (EspSerial.available()) Serial.write(EspSerial.read());
  //EspSerial.println("AT+CIPMUX=1");  
  //设置非透传模式
  sendCommand("AT+CIPMODE=0\r\n",1000,DEBUG);
  //设置多连接        
  sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG);
  //连接TCP服务器,建立连接通道0(mux_id=0)
  sendCommand("AT+CIPSTART=0,\"TCP\",\"192.168.3.75\",333\r\n",3000,DEBUG); 
  delay(3000);
  //发送请求连接标识符“BLYNKREQ”
  command2MasterNode(mux_id,"BLYNKREQ");
}
void chk_serial_io() {
  if (EspSerial.available()) {
//    if (EspSerial.find("busy")) {
//      Serial.println("Resetting...");
//      reset();//GPIO8
//    }
    int inByte = EspSerial.read();
    Serial.write(inByte);
  }
  if (Serial.available()) {
    int inByte = Serial.read();
    EspSerial.write(inByte);
  }
}

boolean connectWiFi() {
  EspSerial.println("AT+CWMODE=1");
  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  Serial.println(cmd);
  EspSerial.println(cmd);
  delay(2000);
  if (EspSerial.find("OK")) {
    Serial.println("OK, WiFi Connected.");
    return true;
  } else {
    Serial.println("Can not connect to WiFi.");
    return false;
  }
}
void connectMaster(char* ip)
{
  //发送连接请求
  String cmd = "AT+CIPSTART=0,\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",333";
  EspSerial.println(cmd);
  //if (EspSerial.find("Error")) return;
  if(EspSerial.find("CONNECT"))
  {
    Serial.println("connect master node ok!");
    sendData(0,",air=,OFFFFO,");
   } 
}
void coloseConnection(int mux_id)
{
  //关闭任何连接
  String closeCommand = "AT+CIPCLOSE="; 
  closeCommand+=mux_id; // append connection id
  closeCommand+="\r\n";
  sendCommand(closeCommand,1000,DEBUG); // close connection
}
//发送命令给主控制节点的函数
void sendData(int mux_id,String cmd)
{  
  String cipSend="AT+CIPSEND=";
  cipSend+=mux_id;
  cipSend+=",";
  cipSend+=cmd.length();
  cipSend+="\r\n";
  cmd+="\r\n";
  EspSerial.print(cipSend);
  delay(100);
  if(EspSerial.find(">"))
  {
    EspSerial.print(cmd);
  }else {
    EspSerial.println("AT+CIPCLOSE");
    Serial.println("connect timeout");
    delay(1000);
    return;
  }  
}
//发送命令给主控制节点的函数
void command2MasterNode(int mux_id,String cmd)
{  
  //Serial.println("command2MasterNode");
  mux_id=mux_id;
  String cipSend="AT+CIPSEND=";
  cipSend+=mux_id;
  cipSend+=",";
  cipSend+=cmd.length();
  cipSend+="\r\n";
  cmd+="\r\n";
  EspSerial.print(cipSend);
  delay(100);
  if(EspSerial.find(">"))
  {
    EspSerial.print(cmd);
  } 
}
//串口发送AT命令给ESP-12F的函数
String sendCommand(String command, const int timeout, boolean debug)
{
  String response = "";
  EspSerial.print(command); // send the read character to the esp8266
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(EspSerial.available())
    {
      // The esp has data so display its output to the serial window 
      char c = EspSerial.read(); // read the next character.
      response+=c;
    }  
  }
  if(debug)
  {
    Serial.println(response);
  }
  return response;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值