找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1197|回复: 0
收起左侧

Arduino+ESP01上传不了,也链接不上WIFI

[复制链接]
ID:357573 发表于 2021-5-26 14:35 | 显示全部楼层 |阅读模式
  1. #include <SCoop.h>//引入头文件
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <Wire.h>
  5. #include <DS1302.h>
  6. #include <Wire.h>
  7. #include <SPI.h>
  8. #include <Adafruit_Sensor.h>  
  9. #include <Adafruit_BMP280.h>
  10. #include <dht11.h>
  11. #include <SoftwareSerial.h>


  12. dht11 DHT11;
  13. SoftwareSerial esp8266(5, 6);        //(rx,tx),声明WIFI
  14. #define DHT11PIN 4
  15. #define BMP_SCK 13  
  16. #define BMP_MISO 12
  17. #define BMP_MOSI 11
  18. #define BMP_CS 10
  19. Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
  20. DS1302 rtc(9, 8, 7); // RST, DAT, CLK
  21. #define ONE_WIRE_BUS A0 //数据总线
  22. OneWire oneWire(ONE_WIRE_BUS); //声明
  23. DallasTemperature sensors(&oneWire); //声明
  24. int x5 = A5;
  25. String  outa="";    //读取温度值存入变量
  26. String  outb="";    //读取时间值存入变量
  27. String  outc="";    //读取气压值存入变量
  28. String  outd="";    //读取海拔值存入变量
  29. String  oute="";    //占用
  30. String  outf="空";  //空
  31. String  outh="";    //湿度

  32. //WIFI变量
  33. String AP = "padavan-2x";   
  34. String PASS = "12345678";
  35. String Data;
  36. int countTrueCommand;
  37. int countTimeCommand;
  38. boolean found = false;
  39. //WIFI变量

  40. void setup() {
  41.    mySCoop.start();
  42.    pinMode(x5, OUTPUT);
  43.    Serial.begin(9600);


  44.   sensors.begin(); //初始化总线
  45.   sensors.setWaitForConversion(false); //设置为非阻塞模式


  46. }


  47. void loop()
  48. {
  49.    yield();
  50. }


  51. defineTaskLoop(Task1)//水泵
  52. {

  53.   digitalWrite(x5, HIGH);   // 点亮
  54.     sleep(3600000);             // 等待
  55.   digitalWrite(x5, LOW);   // 通过将引脚电平拉低,关闭
  56.     sleep(10000);            // 等待

  57.    }

  58. defineTaskLoop(Task2)//温度
  59. {

  60.     float tempC = sensors.getTempCByIndex(0); //获取索引号0的传感器摄氏温度数据
  61.     if (tempC != DEVICE_DISCONNECTED_C)       //如果获取到的温度正常
  62.     {
  63.       Serial.print("\n当前温度是: ");
  64.       Serial.print(tempC);
  65.       Serial.println(" ℃");
  66.     }

  67.     //Serial.println("发起温度转换");
  68.     sensors.requestTemperatures(); //发起新的温度转换
  69.     //Serial.print(tempC);  //打印当前温度值
  70.     //outa=String(tempC)+"℃";
  71.      outa=String(tempC)+"℃";
  72.      sleep(1000);


  73.    }

  74. /*

  75. defineTaskLoop(Task3)//时间
  76. {


  77.   outb=String(rtc.getDateStr(FORMAT_LONG, FORMAT_LITTLEENDIAN, '-'))+"-"+String(rtc.getTimeStr());
  78.   delay(1000);

  79.    }

  80. */


  81. defineTaskLoop(Task4)//大气压
  82. {

  83.    if (!bmp.begin())
  84.    {  
  85.     Serial.println(F("找不到对应的传感器"));
  86.     while (1);
  87.   }
  88.   /*
  89.     Serial.print(F("温度 = "));

  90.     Serial.print(bmp.readTemperature());

  91.     Serial.println(" *C");



  92.     Serial.print(F("气压 = "));

  93.     Serial.print(bmp.readPressure());

  94.     Serial.println(" Pa");



  95.     Serial.print(F("海拔 = "));

  96.     Serial.print(bmp.readAltitude(1013.25));

  97.     Serial.println(" m");

  98.     */
  99.    oute=String(Serial.print(F("")));
  100.    outc=String(bmp.readPressure())+"Pa";
  101.    outd=String(bmp.readAltitude(1013.25))+"m";

  102.     delay(1000);

  103.    }

  104.   defineTaskLoop(Task5)//湿度
  105. {

  106.   int chk = DHT11.read(DHT11PIN);
  107.   switch (chk)
  108.   {
  109.     case DHTLIB_OK:
  110.     Serial.println("OK");
  111.     break;
  112.     case DHTLIB_ERROR_CHECKSUM:
  113.    // Serial.println("Checksum error");
  114.     break;
  115.     case DHTLIB_ERROR_TIMEOUT:
  116.    // Serial.println("Time out error");
  117.     break;
  118.     default:
  119.    // Serial.println("Unknown error");
  120.     break;
  121.   }

  122.   // 获取测量数据
  123.    outh=String(((float)DHT11.humidity, 2));
  124.    delay(1000);
  125. }

  126.   defineTaskLoop(Task6)//上传
  127. {

  128.   esp8266.begin(115200);
  129.   sendCommand("AT", 5, "OK");
  130.   sendCommand("AT+CWMODE=1", 5, "OK");
  131.   sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
  132.   Data = "GET /crzz.php?aa="+outh+"&ab="+outd+"&ac="+outc+"&ad="+outb+"&ae="+outa;
  133.   sendCommand("AT+CIPMUX=1",5,"OK");
  134.   sendCommand("AT+CIPSTART=0,\"TCP\",\"192.168.10.204\",80",4,"OK");
  135.   sendCommand("AT+CIPSEND=0," +String(Data.length()+4),2,">");
  136.   esp8266.println(Data);
  137.   delay(1000);
  138.   countTrueCommand++;
  139.   sendCommand("AT+CIPCLOSE=0",2,"OK");
  140.   }


  141. void sendCommand(String command, int maxTime, char readReplay[]) {
  142.   Serial.print(countTrueCommand);
  143.   Serial.print(". at command => ");
  144.   Serial.print(command);
  145.   Serial.print(" ");
  146.   while (countTimeCommand < (maxTime * 1))
  147.   {
  148.     esp8266.println(command);//at+cipsend
  149.     if (esp8266.find(readReplay)) //ok
  150.     {
  151.       found = true;
  152.       break;
  153.     }

  154.     countTimeCommand++;
  155.   }

  156.   if (found == true)
  157.   {
  158.     Serial.println("Yes");
  159.     countTrueCommand++;
  160.     countTimeCommand = 0;
  161.   }

  162.   if (found == false)
  163.   {
  164.     Serial.println("Fail");
  165.     countTrueCommand = 0;
  166.     countTimeCommand = 0;
  167.   }

  168.   found = false;
  169.   }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表