|

楼主 |
发表于 2021-4-2 15:13:00
|
显示全部楼层
给你一个我的所有节点的模板文件吧。这是一个带OTA功能的模板。
我每次新搞一种类型的节点,就会把这个文件里面的xxxxx改成这个节点的种类的名字,然后改改数据读取的地方。收到数据,执行命令的地方,就可以加入物联网了。
- //D0 = GPIO16;
- //D1 = GPIO5;
- //D2 = GPIO4; LED on esp8266
- //D3 = GPIO0;can not download when connected to low
- //D4 = GPIO2;
- //D5 = GPIO14;
- //D6 = GPIO12;
- //D7 = GPIO13;
- //D8 = GPIO15; can not start when high input
- //D9 = GPIO3; UART RX
- //D10 = GPIO1; UART TX
- //LED_BUILTIN = GPIO16 (auxiliary constant for the board LED, not a board pin);
- #include<ESP8266WiFi.h>
- #include<WiFiUdp.h>
- #include<ESP8266mDNS.h>
- #include<ArduinoOTA.h>
- #include<ESP8266WiFiMulti.h>
- #include<time.h>
- #define timezone 8
- const char* ssid = ""; //Wifi名称
- const char* password = ""; //Wifi密码
- WiFiUDP m_WiFiUDP;
- char *time_str;
- char H1,H2,M1,M2,S1,S2;
- //#include "datastruct.h"
- //下面这两个数据结构的例子,一般放在一个H文件里面,方便和服务器的代码共用
- struct tXXXXXXXXData
- {
- unsigned long DataType; //data type = 5
- unsigned long Mac[6];
- unsigned long Power; //xx.xxxx W
- unsigned long TotalEnergy; //xxxxxx.xx kWh
- unsigned long Volt; //xxx.x v
- unsigned long Current; //xxx.xxx A
- };
- struct tXXXXXXXXCommand
- {
- unsigned long Triger;
- unsigned long On;
- };
- tXXXXXXXXData XXXXXXXXData;
- unsigned long LastServerUpdate;
- unsigned long TenthSecondsSinceStart = 0;
- void TenthSecondsSinceStartTask();
- void OnTenthSecond();
- void OnSecond();
- void setup()
- {
- delay(50);
- Serial.begin(115200);
- XXXXXXXXData.DataType = 0;
- WiFi.disconnect();
- WiFi.mode(WIFI_STA);//设置模式为STA
- byte mac[6];
- WiFi.softAPmacAddress(mac);
- printf("macAddress 0x%02X:0x%02X:0x%02X:0x%02X:0x%02X:0x%02X\r\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
- for (byte i=0;i<6;i++)
- {
- XXXXXXXXData.Mac[i] = mac[i];
- }
- Serial.print("Is connection routing, please wait");
- WiFi.begin(ssid, password); //Wifi接入到网络
- Serial.println("\nConnecting to WiFi");
- //如果Wifi状态不是WL_CONNECTED,则表示连接失败
- unsigned char WiFiTimeOut = 0;
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(1000); //延时等待接入网络
- WiFiTimeOut++;
- if (WiFiTimeOut>10)
- {
- break;
- Serial.println("\nConnecting to WiFi Failed");
- }
- }
- //设置时间格式以及时间服务器的网址
- configTime(timezone * 3600, 0, "pool.ntp.org", "time.nist.gov");
- Serial.println("\nWaiting for time");
- while (!time(nullptr)) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("");
- ArduinoOTA.onStart([]() {
- String type;
- if (ArduinoOTA.getCommand() == U_FLASH) {
- type = "sketch";
- } else { // U_SPIFFS
- type = "filesystem";
- }
- // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) {
- Serial.println("Auth Failed");
- } else if (error == OTA_BEGIN_ERROR) {
- Serial.println("Begin Failed");
- } else if (error == OTA_CONNECT_ERROR) {
- Serial.println("Connect Failed");
- } else if (error == OTA_RECEIVE_ERROR) {
- Serial.println("Receive Failed");
- } else if (error == OTA_END_ERROR) {
- Serial.println("End Failed");
- }
- });
- ArduinoOTA.begin();
- Serial.println("Ready");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- m_WiFiUDP.begin(5050);
- }
- void loop()
- {
- ArduinoOTA.handle();
- TenthSecondsSinceStartTask();
- //接受服务器发来的命令
- m_WiFiUDP.parsePacket();
- unsigned int UdpAvailable = m_WiFiUDP.available();
- if (UdpAvailable == sizeof(tXXXXXXXXCommand))
- {
- printf("UDP got %d bytes\r\n",UdpAvailable);
- tXXXXXXXXCommand tempXXXXXXXXCommand;
- m_WiFiUDP.read((char *)&tempXXXXXXXXCommand,sizeof(tXXXXXXXXCommand));
- LastServerUpdate = 0;
- }
- }
- unsigned long LastMillis = 0;
- void TenthSecondsSinceStartTask()
- {
- unsigned long CurrentMillis = millis();
- if (abs(CurrentMillis - LastMillis) > 100)
- {
- LastMillis = CurrentMillis;
- TenthSecondsSinceStart++;
- OnTenthSecond();
- }
- }
- void OnSecond()
- {
- time_t now = time(nullptr); //获取当前时间
- time_str = ctime(&now);
- H1 = time_str[11];
- H2 = time_str[12];
- M1 = time_str[14];
- M2 = time_str[15];
- S1 = time_str[17];
- S2 = time_str[18];
- printf("%c%c:%c%c:%c%c\n",H1,H2,M1,M2,S1,S2);
- //Serial.printf(time_str);
- struct tm *timenow;
- timenow = localtime(&now);
- unsigned char Hour = timenow->tm_hour;
- unsigned char Minute = timenow->tm_min;
- //在此,每秒去读取各类数据,功率,温度之类的,并保存在下面的数据结构中
- XXXXXXXXData.Volt = 100;
- //每秒向服务器发送一次数据
- m_WiFiUDP.beginPacket("192.168.0.17", 5050);
- m_WiFiUDP.write((const char*)&XXXXXXXXData, sizeof(tXXXXXXXXData));
- m_WiFiUDP.endPacket();
- }
- void OnTenthSecond()
- {
- if (TenthSecondsSinceStart%10 == 0)
- {
- OnSecond();
- }
- }
复制代码
|
|