esp8266_贝壳物联_arduino

2023-05-16

功能:
接收串口数据,将串口数据上报到贝壳物联的数据接口
此处为接收0和1数据,上报到贝壳物联

贝壳物联平台通讯协议
ArduinoJson解析
ArduinoJson Assistant非常好用的工具

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <Ticker.h>                       // 使用Ticker库,需要包含头文件
#include <WiFiClient.h>
//Ticker timer1;                            // 创建一个定时器对象
const char* ssid     = "xxxxx";    //wifi name
const char* password = "xxxxxx";  //wifi passwd
const char* device_id = "xxxxx";      //设备ID
const char* device_key = "xxxxxxx"; //设备key
const char* interface_id = "xxxxxx";   //接口id
String heart_string;                  //心跳信息

const char* host = "www.bigiot.net";  //网站 
const int  port = 8282;               //服务器向客户端发送心跳包的端口

WiFiClient client;
#define bufferSize 8192

uint8_t buf1[bufferSize];
uint16_t i1=0;

uint8_t buf2[bufferSize];
uint16_t i2=0;

bool tcp_ok = 0;         //是否是首次连接(首次连接发送心跳包)

#define packTimeout 5 // ms (if nothing more on UART, then send packet)

void WiFi_Connect()
{
  Serial.begin(9600);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void Heart_Task()
{
  //心跳包任务
  StaticJsonDocument<96> heart_json;
  heart_json["M"] = "checkin";
  heart_json["ID"] = device_id;
  heart_json["K"] = device_key;
  serializeJson(heart_json, heart_string);
  //设置周期性定时10s
  //timer1.attach(10, Heart_Live);
}

void setup() {
  WiFi_Connect();
  //心跳包服务
  Heart_Task();
}

void loop() {
  //TCP客户端
  //如果没有连接
  if(!client.connected()) { // if client not connected
    if (!client.connect(host, port)) { //连接失败退出再次连接
      Serial.println("connection failed");
      delay(5000);
      tcp_ok = 0; //连接失败
      return;
    }
  }
  //如果是第一次上线
  if(tcp_ok == 0)
  {
    tcp_ok = 1;
    //发送登陆消息
    client.println(heart_string);
  }
  //如果收到来自客户端的消息
  if(client.available()) {
    while(client.available()) {
      buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app)
      if(i1<bufferSize-1) i1++;
    }
    //解析消息
    StaticJsonDocument<128> receive_json;
    DeserializationError error = deserializeJson(receive_json, buf1, i1);
 
    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }
    // 串口发送出来:
    serializeJson(receive_json, Serial);
    const char* M = receive_json["M"]; // "heart beat"
    if(*M == 'b') //如果是心跳信号
    {
      Serial.println(" receive heart beat");
      client.println(heart_string); //发送上线通知
    }
    //Serial.write(buf1, i1);
    i1 = 0;
  }
  
  //串口接收数据
  if(Serial.available()) {
    // read the data until pause:
    while(1) {
      if(Serial.available()) {
        buf2[i2] = (char)Serial.read(); // read char from UART
        if(i2<bufferSize-1) i2++;
      } else {
        //delayMicroseconds(packTimeoutMicros);
        delay(packTimeout);
        if(!Serial.available()) {
          break;
        }
      }
    }
    // now send to WiFi:
    //client.write((char*)buf2, i2);
    Serial.write(buf2, i2);
    bool send_flag = 0;
    StaticJsonDocument<96> send_json;
    //发送数据
    if(buf2[0] == '0') //如果是没人
    {
      send_flag = 1; 
      send_json["V"][interface_id] = "0"; 
    }
    else if(buf2[0] == '1')//有人
    {
      send_flag = 1;
      send_json["V"][interface_id] = "1"; 
    }
    if(send_flag == 1)
    {
      String send_string;
      send_json["M"] = "update";
      send_json["ID"] = device_id; //设备ID
      serializeJson(send_json, send_string);
      client.println(send_string); //发送上线通知
    }
    i2 = 0;
    send_flag = 0;
  }
}

请添加图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

esp8266_贝壳物联_arduino 的相关文章

  • android BluetoothChat 使用来自另一个类的发送接收方法

    您好 我使用 BluetoothChat 示例来与 arduino 进行通信 到目前为止 一切正常 现在我想访问 sendMessage 方法 以便能够不从主类发送数据 而是从我创建的另一个类发送数据 这是BluetoothChat jav
  • 将 Arduino RGB LED 从一种颜色渐变为另一种颜色?

    目前 我已成功让 LED 灯循环显示我选择的八种颜色 一切都工作正常 除了我想要一种更自然的感觉 并且想要从一种颜色褪色 过渡到下一种颜色 而不是让它们互相替换 到目前为止 这是我的代码 int redPin 11 int greenPin
  • Arduino 中的字符串比较

    我正在开发基于网络的家庭自动化系统 因此我的Arduino向服务器发送请求并在串行监视器中获得以下响应以及 loneOn 这是由于Serial println r 陈述 HTTP 1 1 200 OK Date Mon 13 Oct 201
  • 如何在Arduino上将int转换为字符串?

    如何转换 int n 到一个字符串 以便当我通过串行发送它时 它作为字符串发送 这是我到目前为止所拥有的 int ledPin 13 int testerPin 8 int n 1 char buf 10 void setup pinMod
  • 在 Android 上将 USB 波特率从 9600 更改为 115200

    我有一个 Arduino 它以 115200 波特率串行发送数据 有一个应用程序以 9600 波特率从 Arduino 接收数据 代码是 Arduino USB serial converter setup Set control line
  • Fedora 中的 Arduino 上传错误“stk500_recv(): 程序员没有响应”

    我正在尝试上传库存Blink在 Fedora Core 15 Linux 中使用 Arduino IDE 绘制草图 我收到此错误 avrdude stk500 recv 程序员没有响应 要重现该问题 通过 USB 线插入 Arduino U
  • printf 的包装

    我在Arduino下编码 我想开发串行打印格式化功能 所以我尝试使用sprintf未知大小的缓冲区 基本上 我们可以避免谈论 Arduino 及其串行输出 并考虑将文本写入缓冲区 然后使用printf 我试过这个 include
  • 如何使用arduino从sim900模块的RTC读取日期和时间数据?

    include SIM900 h include
  • 如何将值从 Arduino 发送到 Python,然后使用该值

    我正在构建一个使用 Python 进行远程控制的机器人 通过简单的 GUI 通过互联网发送控制消息 我的部分代码 GUI 和控制系统 运行得很好 但我陷入了困境 我正在尝试使用视差平传感器来获取与物体的距离信息Arduino Mega ht
  • sim800L GPRS 发布请求

    我一直在研究 LoNet 迷你 GSM 模块 SIM800L 并将其与 Arduino 连接 我已插入 SIM 移动卡并且可以连接互联网 通过串行监视器 我可以毫无问题地与它通信 但是当向网络服务器页面发出 GET 或 POST 请求时 它
  • C-如何使用PROGMEM存储和读取char数组

    我有三个字符数组 我不希望 Arduino 将它们存储在SRAM http en wikipedia org wiki Static random access memory 所以我想使用PROGMEM来存储和读入flash http en
  • Sntp.sync() 忽略服务器

    我一直在尝试与 ntp 服务器同步时间 但是 nodemcu 似乎忽略了服务器参数 sync lua sntp sync fr pool ntp org function tm rtctime epoch2cal rtctime get p
  • C++:将成员函数作为普通函数指针传递的闭包

    我正在尝试调用外部库的成员函数 该函数接受函数指针作为参数 Timer every unsigned long period void callback void 但不幸的是我想传递的参数是一个成员函数 void MyClass the m
  • 终止导致设备或资源繁忙的进程:“/dev/ttyUSB0”?

    我使用以下 Python 代码连接到我的 Arduino 板 device glob glob dev ttyUSB 0 time sleep 1 arduino serial Serial device 115200 timeout 5
  • ESP8266 NodeMCU 堆内存不足

    我正在尝试通过从我的笔记本电脑发送 POST 使用 node js 来使用 ESP8266 01 来切换 LED 我现在遇到内存问题 因为每当我发送 POST 请求时 ESP 中使用的内存就会增加 而堆内存会减少 并且当没有剩余内存时它会崩
  • 带/不带类的回调函数指针 C++

    我被困 我正在尝试形成一个函数 它将吃掉无类函数指针和对象中的函数指针 这是我当前的代码 希望能解释更多 它应该在 Arduino 上运行 所以我不能使用大型库 首先 我在 Arduino 上使用这个库 SimpleTimer A time
  • Arduino - 高效地迭代 C 数组

    我有以下数组 PROGMEM prog uint16 t show hide info 4216 8900 4380 580 500 600 500 580 1620 580 500 600 500 580 500 600 480 600
  • 如何使用GSM模块SIM800和Arduino Uno发送短信?

    我正在尝试通过 SIM800 GSM 模块从 Arduino 发送短信 消息到达给定号码 但格式不正确 它显示 消息格式不支持 我在这里添加了我的代码 非常感谢您的快速回复 include
  • Arduino C++ 析构函数?

    我知道在Arduino中你不能使用delete 那么什么时候调用 C 类中定义的析构函数呢 同样 如果我想创建一个指向数组的指针 我必须使用malloc and free 当对象被销毁时 析构函数被调用 对于自动 堆栈上 变量 它在离开其作
  • 当我启动程序时,Arduino IDE (Win10) 崩溃

    我的 Arduino IDE Win10 上的版本为 1 8 12 在启动时崩溃 运行arduino debug exe我收到此错误消息 C Program Files x86 Arduino gt arduino debug exe Se

随机推荐

  • CF6E Exposition题解

    前置知识 st 表 xff1a 用于求静态的区间最值问题 不会的同学可以看wsyear巨佬的这篇文章https blog csdn net wsyear article details 114334351 spm 61 1001 2014
  • 最简单的柯西不等式证明

    柯西不等式证明 柯西不等式 xff0c 是形式如下的不等式 a i 2
  • CF1656E Equal Tree Sums题解

    其实这道题不难 首先假设 1 1 1 是根节点 我看到这道题第一反应就是直接假设整棵树权值之和是某一个定值 xff0c 然后再dfs造每一个 a x
  • CF1656D K-good题解

    这场比赛我没打 xff0c 错失上分好机会 这题是真的水 直接根据题意列出式子 xff1a n 61 k k
  • P7914 [CSP-S 2021] 括号序列 题解

    其实T2想清楚就不是很难 xff0c 虽然想清楚也不简单 我这里分享一种很自然的想法 xff0c 当然是区间dp啦 区间dp分6种状态 的种类数 xff0c 这种情况相当与题目中的 S S S xff0c 2到5中都一样 的种类数 xff0
  • 在Mac上安装好Anaconda,但在终端使用conda命令显示不是有效命令的解决方法

    最近新装的Mac OSX10 15 3 xff0c 新装了anaconda xff0c 从window到Mac的过渡 xff0c 有了诸多不适应 在终端中使用conda命令 xff0c 就会出现以下提示 zsh command not fo
  • LINUX 获取公网ip并发送邮件

    LINUX 获取公网ip并发送邮件 问题由来配置环境本机环境配置源 本段为CSDN博主 Tinghua M 创作编写sh文件 本段参考博主 手动销户了 问题由来 运营商的公网IP是动态的 xff0c 因此造成一段时间后无法访问公司资源 我们
  • Linux查看所有服务的状态

    Ubuntu 16 04环境 查看Linux所有服务的运行状态可输入命令 service status all 注意 xff1a all要紧跟在 status后面 xff0c 中间不要有空格 结果 那么 xff0c 服务名称前面的加减号 4
  • Qt 文件树的实现

    Qt 文件树的实现 xff08 QTreeWidget xff0c QTreeWidgetItem xff09 使用Qt框架创建文件树主要是使用了Qt仲的QTreeWidget控件和QTreeWidgetItem控件 其最主要的功能包括文件
  • chromeOS中Linux安装Flatpak,切换Flatpak数据源,安装Remmina应用

    本文基于ChromeOS 版本106 0 5249 112 xff08 正式版本 xff09 xff0c Debain 11版本 设置 开发者 Linux开发环境 启用 chromebook开启Linux容器 以下内容涉及到的技术均为Deb
  • 性能学习笔记--k8s下mysql的连接数分析和调优

    项目背景 xff1a k8s的架构下 xff0c 登录并发100后 xff0c 发现cpu的利用率过高 xff0c 超过75 xff1b 开始不知道是哪个微服务导致的cpu利用率过高 xff0c 需要进行分析 xff08 最终分析是mysq
  • C++比较函数cmp

    本文将简单介绍C 43 43 比较函数 cmp 排序函数sort sort函数是我们常用的库函数 xff0c 它的参数如下 xff1a span class token keyword void span sort span class t
  • 弹性云服务器ECS的选择:为什么我更推荐华为云?

    前言 作为一名嵌入式开发者 xff0c 平常难免不了需要一台云服务器来搭建一个调试物联网设备的测试平台 x1f604 xff0c 因此平时也没少购买云服务器 xff0c 但是云服务器厂商那么多 xff0c 我们到底应该如何做出选择呢 xff
  • git中忽略所有文件后,白名单中添加文件夹及其所有子文件(夹)

    此点很容易就出问题了 xff0c 我用的想法是要么添加 subfiledir 要么添加 subfiledir 但是按照git的逻辑 xff0c 第一行只会让subfiledir添加进来 xff0c 但是其所有子文件以及文件夹是不会被添加进来
  • 51单片机外部中断

    span class token keyword void span span class token function IrInit span span class token punctuation span span class to
  • 51单片机定时器2用作串口

    使用定时器2用作串口 span class token macro property span class token directive hash span span class token directive keyword defin
  • 二进制的计算(原码、补码以及反码)

    带符号 5 2 0000 0101 gt 5 1000 0010 gt 2 然后两个数据都转为补码进行相加 正数的补码等于原码 负数的补码等于符号位不变 xff0c 剩下的取反加一 算补码的时候符号位不参与计算 0000 0101 43 加
  • iwr6843-ROS构建

    需求 ubuntu 18 04版本 安装ros 安装教程 首先安装必要软件 sudo apt install git curl vim y 设置您的计算机以接受来自 packages ros org 的软件 sudo sh c 39 ech
  • 51nod - 1364 最大字典序排列

    给出一个1至N的排列 xff0c 允许你做不超过K次操作 xff0c 每次操作可以将相邻的两个数交换 xff0c 问能够得到的字典序最大的排列是什么 xff1f 例如 xff1a N 61 5 xff0c 1 2 3 4 5 xff0c k
  • esp8266_贝壳物联_arduino

    功能 接收串口数据 xff0c 将串口数据上报到贝壳物联的数据接口 此处为接收0和1数据 xff0c 上报到贝壳物联 贝壳物联平台通讯协议 ArduinoJson解析 ArduinoJson Assistant非常好用的工具 span cl