鳕鱼天空

This is Mr Wang's Tech Blog.

[转]基于esp8266的开发导航帖arduino ide for esp8266

自从乐鑫esp8266出现后,其价格低廉的解决方案,引起了业界的广泛关注,堪称业界里程碑。但是很多人买来之后不知道怎么使用,今天在这里和大家讨论分享一下:   

   ESP8266简介,乐鑫的单芯片wifi 控制器

目前市面上有3种开发方式:
1:使用乐鑫AT 固件,使用时,单片机通过串口用AT指令来实现wifi连接、网络通迅,例如arduino uno+esp8266他们间使用AT指令来交互。

2:使用乐鑫SDK开发包,直接用C编程。例如果云esp8266 sdk开发之类。直接开发BIN文件
3:使用NODEMCU固件,这是国内大能 在SDK基础上,加入了LUA语言

现在,出现了第4种开发方式,arduino直接编程,原理还是在SDK基础上开发,不过已经封装为我们熟的Arduino语言了。(想想都有点小激动{:soso_e102:})

  注意已经不需要ARDUINO硬件了,而是利用arduino软件平台来编译8266的源码。即利用arduino 语句开发8266的sdk,

下面的几篇帖子将分别介绍开发的一些教程。

第一篇,基于esp8266的智能家居控制系统-基础篇1介绍arduino ide for esp8266,介绍背景,及软件下载

第二篇,基于esp8266的智能家居控制系统-基础篇2硬件平台搭建 ,搭建硬件开发环境
第三篇,基于esp8266的智能家居控制系统-通用篇,常用的示例代码,非无线
第四篇,基于esp8266的智能家居控制系统-局域网篇1 TCP/UDP,tcp通讯的准备作为客户端与电脑通讯
第五篇,基于esp8266的智能家居控制系统-局域网篇1手机控制和透传作为客户端与手机通讯及透传
第六篇,基于esp8266的智能家居控制系统-局域网篇2,tcpserver,作为服务端与其他作为客户端的设备通讯
楼楼将持续更新,么么哒

转载自:http://www.arduino.cn/forum.php?mod=viewthread&tid=18361&highlight=esp8266

[转]js复制内容到剪贴板代码,js复制代码

例子

<script type="text/JavaScript">
    function jsCopy(){
        var e=document.getElementById("contents");//对象是contents
        e.select(); //选择对象
        document.execCommand("Copy"); //执行浏览器复制命令
    }
</script>
<textarea id="contents" cols="40" rows="5"></textarea>
<br />
<input type="button" onClick="jsCopy();" value="复制" />

js复制无非是以下三步:
1、获取复制的对象:document.getElementById("contents")这句话就是获取ID=“contents”的textarea。
2、全选对象的内容:e.select()
3、用execCommand("")函数将内容复制。
下面来详细介绍execCommand("")函数的参数:
1、格式:document.execCommand(sCommand[,交互方式, 动态参数])
2、参数详解:

 

001 1、〖全选〗命令的实现
002 [格式]:document.execCommand(”selectAll”)
003 [说明]将选种网页中的全部内容!
004  
005 2、〖打开〗命令的实现
006 [格式]:document.execCommand(”open”)
007 [说明]这跟VB等编程设计中的webbrowser控件中的命令有些相似,大家也可依此琢磨琢磨。
008  
009 3、〖另存为〗命令的实现
010 [格式]:document.execCommand(”saveAs”)
011 [说明]将该网页保存到本地盘的其它目录!
012  
013 4、〖打印〗命令的实现
014 [格式]:document.execCommand(”print”)
015 [说明]当然,你必须装了打印机!
016  
017 Js代码下面列出的是指令参数及意义
018  
019 //相当于单击文件中的打开按钮
020 document.execCommand(”Open”);
021  
022 //将当前页面另存为
023 document.execCommand(”SaveAs”);
024  
025 //剪贴选中的文字到剪贴板;
026 document.execCommand(”Cut”,”false”,null);
027  
028 //删除选中的文字;
029 document.execCommand(”Delete”,”false”,null);
030  
031 //改变选中区域的字体;
032 document.execCommand(”FontName”,”false”,sFontName);
033  
034 //改变选中区域的字体大小;
035 document.execCommand(”FontSize”,”false”,sSize|iSize);
036  
037 //设置前景颜色;
038 document.execCommand(”ForeColor”,”false”,sColor);
039  
040 //使绝对定位的对象可直接拖动;
041 document.execCommand(”2D-Position”,”false”,”true”);
042  
043 //使对象定位变成绝对定位;
044 document.execCommand(”AbsolutePosition”,”false”,”true”);
045  
046 //设置背景颜色;
047 document.execCommand(”BackColor”,”false”,sColor);
048  
049 //使选中区域的文字加粗;
050 document.execCommand(”Bold”,”false”,null);
051  
052 //复制选中的文字到剪贴板;
053 document.execCommand(”Copy”,”false”,null);
054  
055 //设置指定锚点为书签;
056 document.execCommand(”CreateBookmark”,”false”,sAnchorName);
057  
058 //将选中文本变成超连接,若第二个参数为true,会出现参数设置对话框;
059 document.execCommand(”CreateLink”,”false”,sLinkURL);
060  
061 //设置当前块的标签名;
062 document.execCommand(”FormatBlock”,”false”,sTagName);
063  
064 //相当于单击文件中的打开按钮
065 document.execCommand(”Open”);
066  
067 //将当前页面另存为
068 document.execCommand(”SaveAs”);
069  
070 //剪贴选中的文字到剪贴板;
071 document.execCommand(”Cut”,”false”,null);
072  
073 //删除选中的文字;
074 document.execCommand(”Delete”,”false”,null);
075  
076 //改变选中区域的字体;
077 document.execCommand(”FontName”,”false”,sFontName);
078  
079 //改变选中区域的字体大小;
080 document.execCommand(”FontSize”,”false”,sSize|iSize);
081  
082 //设置前景颜色;
083 document.execCommand(”ForeColor”,”false”,sColor);
084  
085 //使绝对定位的对象可直接拖动;
086 document.execCommand(”2D-Position”,”false”,”true”);
087  
088 //使对象定位变成绝对定位;
089 document.execCommand(”AbsolutePosition”,”false”,”true”);
090  
091 //设置背景颜色;
092 document.execCommand(”BackColor”,”false”,sColor);
093  
094 //使选中区域的文字加粗;
095 document.execCommand(”Bold”,”false”,null);
096  
097 //复制选中的文字到剪贴板;
098 document.execCommand(”Copy”,”false”,null);
099  
100 //设置指定锚点为书签;
101 document.execCommand(”CreateBookmark”,”false”,sAnchorName);
102  
103 //将选中文本变成超连接,若第二个参数为true,会出现参数设置对话框;
104 document.execCommand(”CreateLink”,”false”,sLinkURL);
105  
106 //设置当前块的标签名;
107 document.execCommand(”FormatBlock”,”false”,sTagName);

 

 

注:火狐不支持此方法!

 

找了好久的一个方法,很管用,网上很多文章一大抄,拿下来根本没法用,他们自己也没有试验过,就写,真是服了

Arduino驱动DS3231高精度时钟模块

很想要个时钟模块,自己焊又太麻烦,干脆在TB上买下来了,省时。

  模块参数:
  1.尺寸:38mm(长)*22mm(宽)*14mm(高)
  2.重量:8g
  3.工作电压:3.3--5.5V
  4.时钟芯片:高精度时钟芯片DS3231
  5.时钟精度:0-40℃范围内,精度2ppm,年误差约1分钟
  6.带2个日历闹钟
  7.可编程方波输出
  8.实时时钟产生秒、分、时、星期、日期、月和年计时,并提供有效期到2100年的闰年补偿
  9.芯片内部自带温度传感器,精度为±3℃
  10.存储芯片:AT24C32(存储容量32K)
  11.IIC总线接口,最高传输速度400KHz(工作电压为5V时)
  12.可级联其它IIC设备,24C32地址可通过短路A0/A1/A2修改,默认地址为0x57
  13.带可充电电池LIR2032,保证系统断电后,时钟任然正常走动

接线说明,以Arduino uno r3为例:
  SCL→A5
  SDA→A4
  VCC→5V
  GND→GND

 
 

代码部分:

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
 // 启动I2C(IIC)接口
 Wire.begin();
        //以下部分是初始化时间,每次板子通电之后都会初始化成这个时间,只是测试用,以后可以删除。
        Clock.setSecond(50);//Set the second
        Clock.setMinute(59);//Set the minute 设置分钟
        Clock.setHour(11);  //Set the hour 设置小时
        Clock.setDoW(5);    //Set the day of the week 设置星期几
        Clock.setDate(31);  //Set the date of the month 设置月份
        Clock.setMonth(5);  //Set the month of the year 设置一年中的月份
        Clock.setYear(13);  //Set the year (Last two digits of the year) 设置年份(在今年的最后两位数——比如2013年最后的13)
 // Start the serial interface
 Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
 
  temperature=Clock.getTemperature();
 
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");  //这里是显示温度
  Serial.print(temperature);
  Serial.print('\n');
}
void loop()
{
  ReadDS3231();
  delay(1000);  //间隔1000ms(1000ms=1秒)循环一次。
}

用Arduino制作简易电压测试仪

一直想用锂电给arduino的板子供电,买了2种带充放的模块,其中一种有电量指示灯,另外一种没有,想在锂电为板子供电同时测量锂电电压,类似手机剩余电量显示这种。

电路比较简单,找2个10K的电阻串联在电池两端,电池负极接GND,电阻连接处接A3,代码如下:

#define voltsInPin A3

void setup()
{
  pinMode(voltsInPin, INPUT);

  Serial.begin(9600);
  Serial.println("volts:");
}

void loop()
{
  int rawReading = analogRead(voltsInPin);
  float volts = rawReading / 220.62 * 2;
  Serial.println(volts);
  delay(500);
}

每隔半秒测试一次,220.62是我根据万用表测出的数值经过换算得来的,理论上应该是(1023/5=204.8),乘以2是因为两个10K的电阻分压了,所以测量到的电压是实际的一半大小,3.7V锂电充满一般4.2V左右。然后我找了个4位计时器(TM1637),加了几句代码,可以用数码管显示电压值了,两个脚分别接D2和D3。

#define voltsInPin A3

// 下面是4位计时器定义
#include "TM1637.h"
#define CLK 3//pins definitions for TM1637 and can be changed to other ports       
#define DIO 2
TM1637 tm1637(CLK, DIO);

void setup()
{
  pinMode(voltsInPin, INPUT);
  //pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("volts:");

  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);

}

void loop()
{
  int rawReading = analogRead(voltsInPin);
  float volts = rawReading / 220.62 * 2;
  Serial.println(volts);
  int num = volts*100;
  // 4位数显示
  tm1637.display(0, num / 1000);
  tm1637.display(1, (num % 1000) / 100);
  tm1637.display(2, (num % 100) / 10);
  tm1637.display(3, num % 10);
  delay(500);
}

最后,请大家保持冷静,上面的代码在用USB供电时一点问题都没有,但是直接用升压模块接锂电供电后,测量的电压低了很多,而且跳跃的很厉害,我暂时还没有搞清楚到底是不能这样测试还是数码管的问题(数码管噪音很大),改天再测试了,或者还是直接用带电压显示的升压模块算了。。。。。。

人肉分析空调红外遥控器 with Arduino & C#(01)——温度篇

最近迷上了Arduino,简直痴迷,感叹小时候咋就没有那么好玩的东西。

那么,今天的主题是用Arduino控制红外发射器控制带红外的电器(没有红外的以后可以考虑另加,比如通过Nano板子:),今天的小白鼠是家里一个老的AUX壁挂空调,据说海尔的加密云云,改天再研究。

拿出UNO的板子,红外发送和接收的模块,装好库(https://github.com/z3t0/Arduino-IRremote ),翻翻示例,先是找到了IRrecvDumpV2,数据针脚插11,按下遥控器按钮后提示(IR code too long. Edit IRremoteInt.h and increase RAWBUF),问了万能的度娘,说是很多空调编码过长,100个缓存不够用,然调到255并无效果,此法弃疗。

接着问度娘,找到了一片神作,《“高帅富”空调的红外解码和遥控方法”》 http://www.arduino.cn/thread-5775-1-1.html,光看这个名字就厉害的不得了,我就转个代码,具体文章请去原地址拜读

/*
Author: AnalysIR
Revision: 1.0
 
This code is provided to overcome an issue with Arduino IR libraries
It allows you to capture raw timings for signals longer than 255 marks & spaces.
Typical use case is for long Air conditioner signals.
 
You can use the output to plug back into IRremote, to resend the signal.
 
This Software was written by AnalysIR.
 
Usage: Free to use, subject to conditions posted on blog below.
Please credit AnalysIR and provide a link to our website/blog, where possible.
 
Copyright AnalysIR 2014
 
Please refer to the blog posting for conditions associated with use.
[url]http://www.analysir.com/blog/2014/03/19/air-conditioners-problems-recording-long-infrared-remote-control-signals-arduino/[/url]
 
Connections:
IR Receiver      Arduino
V+          ->  +5v
GND          ->  GND
Signal Out   ->  Digital Pin 2
(If using a 3V Arduino, you may connect V+ to +3V)
*/
 
#define LEDPIN 13
//you may increase this value on Arduinos with greater than 2k SRAM
#define maxLen 600
 
volatile  unsigned int irBuffer[maxLen]; //stores timings - volatile because changed by ISR
volatile unsigned int x = 0; //Pointer thru irBuffer - volatile because changed by ISR
 
void setup() {
  Serial.begin(9600); //change BAUD rate as required
  attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//set up ISR for receiving IR signal
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
  //Serial.println(F("Press the button on the remote now - once only"));
  delay(5000); // pause 5 secs
  if (x) { //if a signal is captured
    digitalWrite(LEDPIN, HIGH);//visual indicator that signal received
    Serial.println();
    Serial.print(F("Raw: (")); //dump raw header format - for library
    Serial.print((x - 1));
    Serial.print(F(") "));
    detachInterrupt(0);//stop interrupts & capture until finshed here
    for (int i = 1; i < x; i++) { //now dump the times
      //if (!(i & 0x1)) Serial.print(F("-"));
      Serial.print(irBuffer[i] - irBuffer[i - 1]);
      Serial.print(F(","));
    }
    x = 0;
    Serial.println();
    Serial.println();
    digitalWrite(LEDPIN, LOW);//end of visual indicator, for this time
    attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//re-enable ISR for receiving IR signal
  }
 
}
 
void rxIR_Interrupt_Handler() {
  if (x > maxLen) return; //ignore if irBuffer is already full
  irBuffer[x++] = micros(); //just continually record the time-stamp of signal transitions
 
}

仔细通篇看完,用此君的代码,读到了211和423,这里遇到了一个坑,其实此君代码会把连按的2次操作合到一起,最后经过多次测试,这只AUX遥控的按键RAW统统是211长度。经过对IRremote库的源代码的涩读(ir_NEC.cpp),发现了这个基本符合NEC代码的开头,先来段读出来的RAW:

9016,4520,540,1684,544,1684,544,560,544,556,544,556,544,560,544,1684,544,1684,544,1684,548,1680,548,1684,544,556,544,556,544,560,544,1684,544,1684,544,560,544,556,544,556,544,560,544,556,544,556,544,560,544,556,544,556,544,560,544,556,544,556,544,560,544,556,544,556,544,556,548,556,544,556,544,560,544,556,544,556,544,1688,544,556,544,1684,544,556,544,560,544,556,544,556,544,560,544,556,544,556,548,556,544,556,544,556,548,556,564,536,544,556,548,556,544,1684,568,532,568,532,568,536,564,540,540,560,560,540,564,540,564,536,564,536,544,556,568,532,548,556,568,532,568,532,568,536,568,532,564,540,564,536,568,532,568,532,568,536,568,532,568,532,568,536,568,532,564,540,568,532,568,532,568,532,572,532,568,532,568,536,568,532,568,1660,568,532,568,1664,564,536,568,532,568,536,564,536,568,532,568,1664,568,1660,568,1660,568,1660,568,536,568,1660,568,1660,568,532,596

看花了吧,然后用IRsendRawDemo竟然可以成功实现功能,但arduino那么小的内存如何装得下所有的代码,就算装下了,这不分析下具体原理和代码生成方式晚上如何睡得着。

于是,摘下眼镜揉一揉接着戴上,要上了!

第一个9000左右和第二个4500左右的值代表了NEC的特征,后面209个的最后一个600左右的是结尾,那么中间剩下208个就好分析了(什么,分析无力,那么多数字看花了,问我什么意思我也不知道,于是乎又去找度娘姐姐研究了下红外的原理:【扫盲贴】浅谈38K红外发射接受编码(非常好) http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21227800&id=5747884 一遍看不懂多看几遍,还是看不懂就往下看,那个数字代表高电平低电平的时长,但是用38KHz波来实现的,有这个波低电平,没这个波高电平,一低一高的组合可以变成0或1),于是乎又从库的源代码中找到了转换的方法(ir_NEC.cpp)就2个方法,其中一个就是解码:

  // Build the data
  for (int i = 0;  i < NEC_BITS;  i++) {
    // Check data "mark"
    if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK))  return false ;
    offset++;
    // Suppend this bit
    if  (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE ))  data = (data << 1) | 1 ;
    else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE))  data = (data << 1) | 0 ;
    else                return false ;
    offset++;
  }

先来560左右的NEC_BIT_MARK,然后判断是NEC_ONE_SPACE就是1,NEC_ZERO_SPACE就是0,转换如下:

11000011111000110000000000000000000001010000000000000010000000000000000000000000000000001010000011110110

共104bit,换算成16进制就是C3E3000005000200000000A0F6(此时按的32度,除湿,微风,关机)

此时你心中也许会有千万匹神兽飞奔而过,那么多数字眼睛也看花了,老兄,你看看标题好不好,C#写的转换代码啦,你还真以为全人肉解码啊!

同理下面是开机代码:

11000011111000110000000000000000000001010000000000000010000000000000000000000100000000001010000011110001

共104bit C3E3000005000200000400A0F1(32度,除湿,微风,开机)

28度 除湿 风速无,开

11000011111001010000000000000000000001010000000000000010000000000000000000000100000000001010000011110110 
104bit C3E5000005000200000400A0F6

28度 除湿 风速无,关

11000011111001010000000000000000000001010000000000000010000000000000000000000000000000001010000011110010 
104bit C3E5000005000200000000A0F2

27度 除湿 风速无,开

11000011111110010000000000000000000001010000000000000010000000000000000000000100000000001010000011100110 
104bit C3F9000005000200000400A0E6

27度 除湿 风速无,关

11000011111110010000000000000000000001010000000000000010000000000000000000000000000000001010000011100010 
104bit C3F9000005000200000000A0E2

好,下面就找到了变化的部分,C3之后和结尾,结尾估计是校验码,以后再说,C3之后1个字节估计就和温度有关了,经过人肉演算,发现了如下规律:

E3 11100011 32

……

E5 11100101 28

F9 11111001 27

E9 11101001 26

F1 11110001 25

E1 11100001 24

FE 11111110 23

……

E2 11100010 16

什么,你没有找到规律!从第8位倒着往前看5位,还是不明白?我又抄了一遍。。。。。。

11000 32(度)

……

10100 28

10011 27

10010 26

10001 25

10000 24

01111 23

……

01000 16度(8)

基本上就是这样了,温度应该就是这几位了,顺便补充一下,大多数空调遥控器会把所有的状态一起发过去,所以每次一样长,而且带有所有信息,今天就分析到这,下次继续。

下面放一些也许值得参考的文章(次序不分先后):

万用遥控之红外解码分析仪(上位机源码、下位机源码、详细的制作讲解)

【扫盲贴】浅谈38K红外发射接受编码(非常好)

C#+Arduino使用红外遥控器

“高帅富”空调的红外解码和遥控方法【精华】 

Arduino教程(提高篇)——红外遥控(接收篇)

C#实现读取红外线遥控器

Arduino-IRremote库

海尔彩电遥控器型号对照表