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 的相关文章

  • 读取串行端口 - 忽略在一定时间内写入串行端口的部分数据

    我想定期读取串行端口上的数据和Arduino 所以本质上是这样的 读一读 Wait 读一读 Wait Take etc 我面临的问题是端口将缓冲其信息 因此一旦我调用等待函数 串行端口上的数据就会开始缓冲 等待函数完成后 我尝试再次读取数据
  • 如何使用arduino从sim900模块的RTC读取日期和时间数据?

    include SIM900 h include
  • Arduino I2S 正弦波

    我正在开发一个项目 我想通过组合不同的正弦波来生成 简单 声音 我使用的是 arduino mkrZero 因为它内置了 I2S 接口 并且似乎有足够的处理能力来满足我的需求 I have wired my system exactly l
  • ARDUINO 常数有什么用?以前是22,现在是100

    我正在使用热电偶并下载了 MAX6675 库 我想知道以下几行中 ARDUINO 常量的值是做什么用的 if ARDUINO gt 100 lcd write byte 0 else lcd print 0 BYTE endif lcd p
  • 如何通过蓝牙在Raspberry Pi 4和Arduino Nano BLE之间进行读写?

    我能够通过 Rpi4 的 bluepy 和 Arduino Nano BLE 的 ArduinoBLE h 连接 Raspberry Pi 4 和 Arduino Nano BLE 不幸的是 当我尝试从 Rpi4 写入 Arduino Na
  • 经典蓝牙 (2.1) 设备可实现的最小数据包延迟是多少?

    我正在使用 RN42 http www microchip com wwwproducts en RN42 http www microchip com wwwproducts en RN42 蓝牙模块以 115200 波特率 UART S
  • Python自动选择串口(适用于Arduino)

    目前 Python 程序必须知道设备 Arduino 位于哪个端口 然后 Python 才能与设备进行通信 Problem 每当设备拔出并重新插入时 其 COM 端口都会发生变化 因此必须再次向 Python 提供正确的串行端口 以便它找到
  • sim800L GPRS 发布请求

    我一直在研究 LoNet 迷你 GSM 模块 SIM800L 并将其与 Arduino 连接 我已插入 SIM 移动卡并且可以连接互联网 通过串行监视器 我可以毫无问题地与它通信 但是当向网络服务器页面发出 GET 或 POST 请求时 它
  • 在Python中快速绘制数据

    我正在尝试使用 arduino 绘制来自 mpu6050 imu 的数据 MPU6050 发送数据的速度比绘图快 Arduino 代码从串口提供 6 个数据 即偏航 俯仰 滚动 轴 ay 和 az 我需要快速情节的建议 Python代码 i
  • Arduino:连接字符串时崩溃和错误

    我尝试将 AES 256 加密的输出连接到一个字符串 将此字符串与从 Android 手机发送的加密字符串进行比较 基本上 连接似乎有效 但在几次运行后会出现错误 不可读的字符 字符串变得更短而不是更长 或崩溃 它是可重现的 重启后在同一点
  • 使用 Java 控制 Arduino

    我正在寻找一个LED http en wikipedia org wiki Light emitting diode用Java程序打开和关闭 我用 C 完成了这个项目大约 5 分钟 但用 Java 似乎更具挑战性 我让 Arduino 等待
  • 如何使用GSM模块SIM800和Arduino Uno发送短信?

    我正在尝试通过 SIM800 GSM 模块从 Arduino 发送短信 消息到达给定号码 但格式不正确 它显示 消息格式不支持 我在这里添加了我的代码 非常感谢您的快速回复 include
  • 如何在 Windows 上获取 Arduino 草图的汇编语言列表?

    我希望能够看到我的 Arduino 草图的汇编语言列表 我怎样才能实现这个目标 Update 我正在 Windows 机器上运行 Arduino 软件 一种方法是使用avr objdump on the elf构建创建的文件 例如 在 OS
  • Arduino C++ 析构函数?

    我知道在Arduino中你不能使用delete 那么什么时候调用 C 类中定义的析构函数呢 同样 如果我想创建一个指向数组的指针 我必须使用malloc and free 当对象被销毁时 析构函数被调用 对于自动 堆栈上 变量 它在离开其作
  • KAA 0.10.0 中使用 ESP8266 SDK 编译错误

    我根据以下内容构建了ESP8266 SDK这个文件 https kaaproject github io kaa docs v0 10 0 Programming guide Using Kaa endpoint SDKs C SDK ES
  • Arduino 高低

    我有一个 Arduino 我想知道到底是什么HIGH and LOW意思是 就实际值而言 它们是否有符号ints unsigned ints unsigned char是 他们的价值观是什么 我猜HIGH and LOW可能是unsigne
  • Arduino 错误:未命名类型?

    我已经编写了一个库 但遇到错误未命名类型的问题 我已经尝试了一切 搜索了几个小时 但没有运气 库放置在arduino sketch文件夹的 libraries 文件夹中 请帮忙 我使用的是 OSX 但 Windows 上也出现同样的问题 这
  • 如何将 Android 应用程序正确连接到支持蓝牙的 Arduino 微控制器上的 RFCOMM 套接字?

    我正在与我大学的一些学生合作开发一个简单的蓝牙 Android 应用程序 该应用程序将用于与连接有蓝牙模块的 Arduino 微控制器进行串行通信 RFCOMM 据我所知 我正在使用正确的蓝牙地址和 UUIDRFCOMM SPP 00001
  • 无法访问超过 255 个的 Modbus 寄存器 SimpleModbus

    我是法国学生 我已经使用 SimpleModbus 1 周了 它工作得很好 直到我尝试实现一个具有大量寄存器 1000needed 使用从站 1 无法访问超过 255 个的寄存器 无论我做什么 一旦寄存器读数超过 255 我就会收到超时错误
  • esp8266互联网交换机问题

    我正在尝试制作一个门继电器开关系统 我可以通过端口转发从任何地方进行操作 我找到了一个非常有用的指南和代码 我的程序基于 https openhomeautomation net control a lamp remotely using

随机推荐

  • 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