一、简介
制作一个实时显示时钟,并且可以通过esp-01 wifi模块将RTC的温度数据上传到thingspeak上监控,舵机转动的角度用于指示温度的大小,0.96inch OLED屏用于显示时间与温度。
二、硬件
1、一个DS3231实时时钟模块
2、一个SW6812 RGB圆形灯环用于显示时间(24颗灯珠)
3、一个9G舵机用于显示温度
4、一个esp-01 WiFi模块,用于为Thingspeak更新数据
5、0.96inch显示屏显示时间与温度
6、面包板电源模块
7、面包板
8、杜邦线
三、注册thingspeak以及创建频道
1、访问ThingSpeak.com,然后点击“Get Started for Free”。
2、然后会出现一个注册表格。输入所需信息并注册ThingSpeak。
3、完成注册后,在主界面单击“New Channel”创建一个频道,按下图填写信息,然后保存。
4、转到API keys部分并复制API密钥,需要填到后面的代码中。
四、接线图
五、代码
1、下载全部代码,包含所需的库文件,请注意,必须将相关库文件复制到arduino IDE安装目录中的libraries文件夹中。
2、代码解析
IP为thingspeak固定的,不可以改变,thinkspeak API key为上面你创建频道的API key,如这填写W7Q4FCJXJSX0BN5T,你的就填写你自己的API key。
//define for esp
SoftwareSerial esp(6,7); //esp(rx,tx)
#define DEBUG true
#define IP "184.106.153.149"// thingspeak.com ip
//change it with your api key like "GET /update?key=Your Api Key"
String Api_key = "GET /update?key=W7Q4FCJXJSX0BN5T";
esp链接固定wifi
Serial.begin(9600);
esp.begin(112500);
send_command("AT+RST\r\n", 2000, DEBUG); //reset module
send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
send_command("AT+CWJAP=\"HUAWEI-3L9ML\",\"7578530\"\r\n", 2000, DEBUG); //connect wifi network
while(!esp.find("OK")) { //wait for connection
Serial.println("Connecting ...");}
Serial.println("connect success");
这里链接的wifi名称为“HUAWEI-3L9ML”,密码为“7578530”,如果要链接你自己的wifi,请改修改这两个参数即可,然后将代码上传到UNO 主板上。
可以通过打开arduino IDE的串口监视器查看wifi里的链接状态,当正在链接时串口监视器打印“Connecting …”,当成功链接时打印“connect success”,然后将温度数据上传到thinkspeak,如下图。
打开thingspeak,在temperatute频道即可查看上传的数据,如下图:
void updatedata(){
String command = "AT+CIPSTART=\"TCP\",\"";
command += IP;
command += "\",80";
Serial.println(command);
esp.println(command);
delay(2000);
if(esp.find("Error")){
return;
}
command = Api_key ;
command += "&field1=";
command += temp;
command += "\r\n";
Serial.print("AT+CIPSEND=");
esp.print("AT+CIPSEND=");
Serial.println(command.length());
esp.println(command.length());
if(esp.find(">")){
Serial.print(command);
esp.print(command);
}
else{
Serial.println("AT+CIPCLOSE");
esp.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}
String send_command(String command, const int timeout, boolean debug)
{
String response = "";
esp.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp.available())
{
char c = esp.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}
六、ws6812灯环功能
环形灯条显示时间的方式非常巧妙。灯环总共包含24个像素。使用我们的代码,灯环上的每个像素都可以关闭与显示蓝色、红色和蓝色与红色(即紫色)。
灯环上每个红色像素点表示0.5小时,每经过0.5小时就会有1个红色像素点亮。下图就是灯环在三点钟(3.00或15.00)和四点钟(4.00或16.00)时的样子。
灯环上每个蓝色像素点表示2.5分钟,每经过2.5分钟就会有1个蓝色像素点亮。随着分钟的滴答声,像素的蓝色部分点亮。已经为红色的像素变为紫色,而已褪色的像素变为蓝色。
//RGB led
if(oldminute != minute){ //Refresh every minute
oldminute = minute;
int m = map(minute,0,60,0,24);
int h = hour;
if(h>m){
for(int i=0;i<m;i++){
pixels.setPixelColor(i, pixels.Color(10,0,10)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
for(int i=0;i<(h-m);i++){
pixels.setPixelColor(i+m, pixels.Color(10,0,0)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
for(int i=0;i<(24-h);i++){
pixels.setPixelColor(i+h, pixels.Color(0,0,0)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
if(h<=m){
for(int i=0;i<h;i++){
pixels.setPixelColor(i, pixels.Color(10,0,10)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
for(int i=0;i<(m-h);i++){
pixels.setPixelColor(i+h, pixels.Color(0,0,10)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
for(int i=0;i<(24-m);i++){
pixels.setPixelColor(i+m, pixels.Color(0,0,0)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
if(h==m && h==0){
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,0,0)); // Moderately bright color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
}
七、0.96OLED显示效果
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 20);
u8g.print("Temperature:");
u8g.print(temperature);
u8g.setPrintPos(0, 40);
u8g.print("Clock:");
u8g.print(hour);
u8g.print(":");
u8g.print(minute);
u8g.print(":");
u8g.print(second);
}
八、舵机作用
舵机是用来指示温度的大小,DS3231时钟芯片自带有温度传感器,当UNO R3读取到的温度越大,舵机摆动的角度越大,反则越小,如果需要可以自己动手画一个温度指示表,然后调整代码就可以做一个温度计了,欢迎大家DIY!!!
结束!