找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5222|回复: 7
打印 上一主题 下一主题
收起左侧

51单片机上传温湿度至Onenet服务器 源程序

  [复制链接]
跳转到指定楼层
#
ID:293363 发表于 2020-5-18 16:24 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
在论坛上找了这方面的内容,程序也是改了又改,始终上传不了数据。
今天好不容易搞出来了,把他分享给有需要的朋友,希望能给正在学习这方面的同学一些参考。

调这玩意要细心,今天才发现是自己的上传报文不对,参考了大神的帖子,让我茅塞顿开。
https://open.iot.10086.cn/bbs/fo ... d=536&fromuid=23569

废话不多说了直接上链接: 温湿度上传Onenet.rar (6.33 MB, 下载次数: 172)


单片机源程序如下:
  1. /************************************************************************************
  2. 名称:温湿度检测
  3. 功能:上传温湿度至Onenet服务器
  4. 时间:2020年5月18日15:36:25
  5. 地点:内江职业技术学院
  6. 作者:陈润源
  7. 申明:未经作者允许,禁止在其他论坛发布,此程序目前只在51黑论坛、电路城发布。
  8. 注明:此程序仅供参考
  9.          89C52RC        ESP8266           DHT11
  10.       P3.0(RX)========>TX
  11.           P3.1(TX)========>RX
  12.           P2.2====================> OUT
  13. ************************************************************************************/
  14. #include "reg52.h"
  15. #include "uart.h"
  16. #include "stdio.h"
  17. #include "lcd.h"
  18. #include "dht11.h"  
  19. #include "string.h"

  20. #define uchar unsigned char
  21. #define uint  unsigned int
  22. uchar Timer0_count,Timer0_miao,RsPoint;
  23. uchar Key_num,Flag_rec,Rec_stop;
  24. uchar xdata RsBuf[100];




  25. void main()
  26. {
  27.         char *str2="AT+CIPSEND=14\r\n";
  28.         uchar i;

  29.         memset(&RsBuf[1],' ',18);
  30.     Uart_init();                 //串口初始化
  31.     lcd_init();                         //1602初始化
  32.         delay(3000);
  33.          SendString("+++");
  34.         delay(500);
  35.         SendString("+++\r\n");         //串口测试
  36.         delay(500);
  37.         Rec_stop=0;
  38.         
  39.         memset(&RsBuf[1],' ',18);
  40.         SendString("AT+CWMODE=1\r\n");
  41.         write_1602com(0x01);
  42.         write_string(0x80,"AT+CWMODE=1     ",0);  //发送AT指令至ESP8826
  43.         while(Rec_stop==0) ;             //未发送成功则等待
  44.         Rec_stop=0;
  45.         delay(2000);

  46.         memset(&RsBuf[1],' ',18);
  47.         SendString("AT+RST\r\n");
  48.         write_1602com(0x01);
  49.     write_string(0x80,"AT+RST          ",0);
  50.         while(Rec_stop==0&&RsBuf[0]!='W');
  51.         Rec_stop=0;
  52.            delay(3000);

  53.         memset(&RsBuf[1],' ',18);
  54.     SendString("ATE0\r\n");
  55.         write_string(0x80,"ATE0            ",0);
  56.         write_string(0xc0,"                ",0);
  57.         while(Rec_stop==0) ;
  58.         Rec_stop=0;
  59.         write_1602dat(RsBuf[i+2]);
  60.          delay(2000);

  61.         memset(&RsBuf[1],' ',18);
  62.         SendString("ATE0\r\n");
  63.         write_1602com(0x01);
  64.         write_string(0x80,"ATE0            ",0);
  65.         while(Rec_stop==0) ;
  66.         Rec_stop=0;
  67.     delay(2000);

  68.         memset(&RsBuf[1],' ',18);
  69.     SendString("AT+CIPMUX=0\r\n");
  70.         write_1602com(0x01);
  71.     write_string(0x80,"AT+CIPMUX=0      ",0);
  72.         while(Rec_stop==0);
  73.         Rec_stop=0;
  74.            delay(100);

  75.         memset(&RsBuf[1],' ',18);
  76.         SendString("AT+CIPSTART=\"TCP\",\"183.xx.40.33\",80\r\n");
  77.         write_1602com(0x01);
  78.     write_string(0x80,"AT+CISTART=TCP   ",0);
  79.         while(Rec_stop==0&&RsBuf[2]=='O');
  80.         Rec_stop=0;
  81.          delay(2000);


  82.         memset(&RsBuf[1],' ',18);
  83.         SendString("AT+CIPMODE=1\r\n");
  84.         write_1602com(0x01);
  85.     write_string(0x80,"AT+CIPMODE=1     ",0);
  86.         while(Rec_stop==0);
  87.         Rec_stop=0;
  88.          delay(2000);

  89.         RsPoint=0;
  90.         memset(&RsBuf[1],' ',18);
  91.     SendString("AT+CIPSEND\r\n");
  92.         write_1602com(0x01);
  93.     write_string(0x80,"AT+CIPSEND       ",0);
  94.         while(RsPoint==0);
  95.            delay(2000);
  96.         SendString("AT+CWJAP=\"SSID\",\"Password\"\r\n");                        
  97.         //在这里填入你的路由器名称和密码 分别替代 SSID 和 Password 双引号和斜杠不能删除
  98.         delay(5000);        
  99.          write_string(0x80,"Te:   C   Hu:  %",0);
  100.         write_string(0xc0,"QQ:101742xxxx   ",0);
  101.         write_1602com(0x85);
  102.         write_1602dat(0xdf);

  103.         while(1)
  104.         {
  105.                   if(FlagStart==1)                //2秒发送一次数据
  106.                 {
  107.                      TR0=0;
  108.              RH();                           //获取温湿度
  109.                           FlagStart = 0;
  110.                          write_1602com(0x83);
  111.                          write_1602dat(temperature%100/10+0x30);  //1602屏幕显示温湿度
  112.                          write_1602dat(temperature%100%10+0x30);
  113.                          write_1602com(0x8d);
  114.                          write_1602dat(humidity%100/10+0x30);
  115.                          write_1602dat(humidity%100%10+0x30);
  116.                          SendString( "POST /devices/设备ID/datapoints?type=3 HTTP/1.1\r\napi-key:你的秘钥\r\nHost:api.heclouds点com\r\n"
  117.                                         "Content-Length:59\r\n\r\n");
  118.                          //设备ID 改成你自己的设备ID   你的秘钥 改成你的APIkey  
  119.                          SendString("{\"\WD\":\"");   
  120.                  SendASC(temperature%100/10+0x30);          //1
  121.                  SendASC(temperature%10+0x30);                //1
  122.                          SendString("\",\"SD\":\"");
  123.                  SendASC(humidity%100/10+0x30);          //1
  124.                  SendASC(humidity%10+0x30);                //1
  125.                  SendString("\",\"RET\":\"OK\"}\r\n");         
  126.                          TR0=1;
  127.                          Timer0_count=0;
  128.                          Timer0_miao=0;
  129.                 }
  130.          
  131.         }

  132.    
  133. }

  134. //  通讯中断接收程序   中断函数无返回值
  135.   void uart_rx(void)  interrupt 4   //放在这里 和放在main()里面是一样的
  136. {
  137.    
  138.         unsigned char dat;
  139.     if(RI)                         //是否接收中断
  140.     {
  141.      RI=0;
  142.          dat = SBUF;
  143.          if(SBUF=='\r')                   //检测指令是否发送并被接收成功
  144.          {
  145.            RsPoint=0;
  146.            Rec_stop=1;
  147.           }
  148.           RsBuf[RsPoint]=SBUF;
  149.           RsPoint++;
  150.         }

  151. }

  152. void Timer0_ISR (void) interrupt 1 using 0
  153. {
  154.   TH0=0x4c;
  155.   TL0=0x00;
  156.   Timer0_count++;
  157.   if(Timer0_count>=20)                                //Timer0_count=20 表示1秒时间到
  158.   {
  159.    Timer0_count=0;
  160.    Timer0_miao++;

  161.    if(Timer0_miao>=2)
  162.    {
  163.     Timer0_miao=0;
  164.         FlagStart=1;
  165.    }
  166.   }
  167. }  
复制代码

评分

参与人数 2黑币 +105 收起 理由
gyc7253gyc + 15 赞一个!
admin + 90 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶2 踩
回复

使用道具 举报

7#
ID:293363 发表于 2022-11-28 11:17 | 只看该作者
有问题的朋友可以私信我
回复

使用道具 举报

6#
ID:293363 发表于 2021-5-3 22:33 | 只看该作者
zbx5 发表于 2021-5-2 21:53
数据上传没问题,但数据下载(指令)解析可能会有问题,51的资源太少了

数据解析得自己写
回复

使用道具 举报

5#
ID:915096 发表于 2021-5-2 21:53 | 只看该作者
数据上传没问题,但数据下载(指令)解析可能会有问题,51的资源太少了
回复

使用道具 举报

地板
ID:493069 发表于 2021-4-30 22:35 | 只看该作者
数据上传不了
回复

使用道具 举报

板凳
ID:293363 发表于 2020-5-19 06:23 | 只看该作者
zqy181818 发表于 2020-5-18 22:55
我看了一下 可是代码不全呀

完整代码在附件里,这里显示的只有main.c文件内容哦
回复

使用道具 举报

沙发
ID:130231 发表于 2020-5-18 22:55 | 只看该作者
我看了一下 可是代码不全呀
回复

使用道具 举报

楼主
ID:222650 发表于 2020-5-18 21:31 | 只看该作者
好帖,学习、收藏了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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