ardunio mega2560+esp01实现物联网的数据上行和数据下行

背景

nodemcu 以及arduino nano开发板的资源引脚有限,使用mega2560单片机作为系统控制模块的资源优势可以解决很多问题,那么使用mega2560怎么实现物联网的功能呢?
本次教程以连接公用emqx物联网服务器为例,使用mega2560单片机+esp01s模块实现物联网,主要是完成数据上行和数据下行功能。

一、实现思想

mega2560单片机与wifi模块esp01之间的通信是通过AT指令,所以这次的通信还是通过串口完成的.而arduino提供了软串口的方式完成串口通信。
既然是通过软串口的方式,那么就需要为两个模块分别编程,模块之间的串口数据收发,mega2560单片机将采集的传感器数据通过串口发送给esp01模块,esp01模块接受到从而发送给到云服务器上。同样,下行数据是esp01模块接受到服务器下发的消息后,通过at指令发给mega2560从而执行控制功能。
二、ESP01部分


#include <ESP8266WiFi.h>

#include <PubSubClient.h>



// WiFi
const char *ssid = "XXX"; // Enter your WiFi name
const char *password = "XXXXX";  // Enter WiFi password
 
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topicSub = "/XXX/sub";
const char *topicPub = "/XXX/pub";
const char *mqtt_username = "emqxeqwe";
const char *mqtt_password = "publicdasds";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);


unsigned long lastMsMain = 0;


String content = ""; //用来存储  
void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
    Serial.println("Connected to the WiFi network");
    //connecting to a mqtt broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        String client_id = "esp8266-client-";
        client_id += String(WiFi.macAddress());
        Serial.println("Connecting to public emqx mqtt broker.....");
        if (client.connect("sdafsafaf",NULL,NULL)) {
            Serial.println("Public emqx mqtt broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }
    client.publish(topicPub, "hello emqx");
    client.subscribe(topicSub);
}

void loop() {
  // put your main code here, to run repeatedly:
 client.loop();
   
 
   //解析MEGA2560单片机发来的消息
    while (Serial.available() > 0)  
    {
        content += char(Serial.read());  //使用这个直接循环加起来
       delay(5);
    }
    if (content.length() > 0)
    {
       //Serial.println(content);
       char attributes[100];
       content.toCharArray( attributes, 100 );
       client.publish(topicPub,attributes);
       content = "";
   }
}

void callback(char *topic, byte *payload, unsigned int length) {
    String message;
    for (int i = 0; i < length; i++) {
        message = message + (char) payload[i];  // convert *byte to string
    }
    Serial.print(message);
}

三、mega2560部分

#include <SoftwareSerial.h>
//mega2560的12号引脚接外设的RX端 11号接外设的TX端
SoftwareSerial mySerial(11, 12); // RX, TX
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  mySerial.begin(9600);
  }
 void loop() { // run over and over
    
    //读取ESP8266 发来的消息
     while (mySerial.available() > 0)  
    {
        content += char(mySerial.read());  //使用这个直接循环加起来
        delay(2);
    }
    if (content.length() > 0)
    {
        Serial.println(content);
        //将接收的消息进行比对
        if(content=="LED3")
        {
          digitalWrite(LED3,!digitalRead(LED3)); 
        }
         if(content=="LED4")
        {
          digitalWrite(LED4,!digitalRead(LED4)); 
        }
        if(content=="LED5")
        {
          digitalWrite(LED5,!digitalRead(LED5)); 
        }
       if(content=="LED6")
        {
          digitalWrite(LED6,!digitalRead(LED6)); 
        }
        content = "";
    }
    //mega2560 发给wifi模块
    if (millis() - lastMsMain >= 5000) //5s
    {
      lastMsMain = millis();
      String a =publicMessage();
      mySerial.print(a);
    }
}

四、其他说明
1、有问题在评论区留言
2、请勿转载,转载请注明出处。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜大大的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值