创作者:Tracis | 更新日期:2016-07-17 | 在线时长:4时
arduino+ESP8266-01 实现远程控制数码管输出
远程控制数码管输出
ESP8266-01与wifi连接不稳定,常上线后又离线
指令:play, 循环显示0-9;
stop,关闭;
1 ,显示1;
2,显示2;
...
arduino r3 +ESP8266-01
的代码。
/*
* arduino+ESP8266-01 实现远程控制数码管显示0123456789程序
*使用贝壳物联平台的接口
*/
#include<aJSON.h>
String DEVICEID="xxx"; // 设备编号
String APIKEY="zzzzzz"; // 设备密码
unsigned long lastCheckInTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
String inputString = "";
boolean stringComplete = false;
boolean CONNECT = true;
char* parseJson(char *jsonString);
const int N=8;//led总数
const unsigned char duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//段码
const int LEDpins[]={2,3,4,5,6,7,8,9};//定义led引脚
//循环输出0-9函数
void led_loop (unsigned char value){
for(int i=0;i<N;i++){
digitalWrite(LEDpins[i],bitRead(value,i));//数码管共阳用非 !bitRead(value[i],i)//共阴bitRead(value[i],i)
}
}
void setup() {
//初始化引脚为输出
for(int i=0;i<N;i++){
pinMode(LEDpins[i], OUTPUT);
}
//设置波特率
Serial.begin(115200);
delay(5000);
}
void loop() {
//登录服务器
if (millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
serialEvent();
if (stringComplete) {
inputString.trim();
//Serial.println(inputString);
if(inputString=="CLOSED"){
Serial.println("connect closed!");
CONNECT=false;
}else{
int len = inputString.length()+1;
if(inputString.startsWith("{") && inputString.endsWith("}")){
char jsonString[len];
inputString.toCharArray(jsonString,len);
aJsonObject *msg = aJson.parse(jsonString);
processMessage(msg);
aJson.deleteItem(msg);
}
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
void checkIn() {
if (!CONNECT) {
Serial.print("+++");
delay(500);
Serial.print("\r\n");
delay(1000);
Serial.print("AT+RST\r\n");
delay(6000);
CONNECT=true;
lastCheckInTime=0;
}
else{
Serial.print("{\"M\":\"checkin\",\"ID\":\"");
Serial.print(DEVICEID);
Serial.print("\",\"K\":\"");
Serial.print(APIKEY);
Serial.print("\"}\r\n");
lastCheckInTime = millis();
}
}
void processMessage(aJsonObject *msg){
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
//char* st = aJson.print(msg);
if (!method) {
return;
}
String M=method->valuestring;
String C=content->valuestring;
String F_C_ID=client_id->valuestring;
if(M=="say"){
if(C=="play"){
for(int i=0;i<10;i++){
led_loop(duanma[i]);
delay(1000);
}
delay(1000);
led_loop(0);//关闭
sayToClient(F_C_ID,"play 0~9循环");
}
if(C=="0"){
led_loop(duanma[0]);
sayToClient(F_C_ID,"输出0");
}
if(C=="1"){
led_loop(duanma[1]);
sayToClient(F_C_ID,"输出1");
}
if(C=="2"){
led_loop(duanma[2]);
sayToClient(F_C_ID,"输出2");
}
if(C=="3"){
led_loop(duanma[3]);
sayToClient(F_C_ID,"输出3");
}
if(C=="4"){
led_loop(duanma[4]);
sayToClient(F_C_ID,"输出4");
}
if(C=="5"){
led_loop(duanma[5]);
sayToClient(F_C_ID,"输出5");
}
if(C=="6"){
led_loop(duanma[6]);
sayToClient(F_C_ID,"输出6");
}
if(C=="7"){
led_loop(duanma[7]);
sayToClient(F_C_ID,"输出7");
}
if(C=="8"){
led_loop(duanma[8]);
sayToClient(F_C_ID,"输出8");
}
if(C=="9"){
led_loop(duanma[9]);
sayToClient(F_C_ID,"输出9");
}
if(C=="stop"){
led_loop(0);
sayToClient(F_C_ID,"关闭");
}
}
}
void sayToClient(String client_id, String content){
Serial.print("{\"M\":\"say\",\"ID\":\"");
Serial.print(client_id);
Serial.print("\",\"C\":\"");
Serial.print(content);
Serial.print("\"}\r\n");
lastCheckInTime = millis();
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
//delay(2);
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}