基于Linkit 7697的红绿灯控制系统

1、硬件准备

LinkIt 7697 *1,继电器模块*1,面包板*1,RGB LED灯*1(共阳极,工作电流20mA,红灯压降2~2.2V,绿灯蓝灯压降3~3.2V),限流电阻2个(一个150Ω,一个100Ω),两头针杜邦线若干,micro USB数据线一根,小号螺丝刀一套,万用表一台。

2、软件准备

1)安装arduino1.8.xxx版。

2)文件-->首选项-->附加开发板管理器网址,设置为:http://download.labs.mediatek.com/package_mtk_linkit_7697_index.json 后保存。

3)工具-->开发板-->开发板管理器,搜索7697,安装0.10.21版(由于网络原因可能会失败,多试几次吧)

4)通过micro USB线将Linkit 7697开发板连接到电脑,在设备管理器中查看,确认可以看到开发板

3、硬件连接

注1:继电器选用单路继电器即可

注2;限流电阻按照LED规格计算选用合适的

4、编程控制

功能描述:供电后,自动连接设定wifi,默认自动切换红绿灯,间隔时间10秒,上位机监听UDP 8080端口,向主控板8080端口发送指令可切换控制方式:

指令1:"change",切换为手动控制,每发送一次切换一次状态,回复"changed"

指令2:"auto",切换回自动控制,回复"auto"

附源码:

#include <LWiFi.h>
#include <WiFiUdp.h>
#include "LTimer.h"

int status = WL_IDLE_STATUS;
char ssid[] = "myfreewifi"; //  your network SSID (name)
char pass[] = "jab198438";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
int pin = 2;
int state = 0;
int autoCtl = 1;
unsigned int localPort = 8080;      // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
WiFiUDP Udp;
LTimer timer0(LTIMER_0);

void _callback0(void *usr_data)
{
    if (autoCtl==1){
        if (state==0) {
            digitalWrite(pin, 1);
            state = 1;
        }else{
            digitalWrite(pin, 0);
            state = 0;
        }
    }
}
void setup() {
    //Initialize serial and wait for port to open:
    pinMode(pin, OUTPUT);
    Serial.begin(9600);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
    }
    
    // attempt to connect to Wifi network:
    while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
        status = WiFi.begin(ssid, pass);
    }
    Serial.println("Connected to wifi");
    printWifiStatus();
    
    Serial.println("\nStarting connection to server...");
    // if you get a connection, report back via serial:
    Udp.begin(localPort);
    timer0.begin();
    timer0.start(10000, LTIMER_REPEAT_MODE, _callback0, NULL);
}
void loop() {
    // if there's data available, read a packet
    int packetSize = Udp.parsePacket();
    if (packetSize) {
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remoteIp = Udp.remoteIP();
        Serial.print(remoteIp);
        Serial.print(", port ");
        Serial.println(Udp.remotePort());
        
        // read the packet into packetBufffer
        int len = Udp.read(packetBuffer, 255);
        String msg = "";
        if (len > 0) {
            packetBuffer[len] = 0;
            for(int i=0;i<len;i++){
                msg += packetBuffer[i];
            }
        }
        Serial.println("Contents:");
        Serial.println(msg);
        // send a reply, to the IP address and port that sent us the packet we received
        Udp.beginPacket(Udp.remoteIP(),8080);
        if(msg.startsWith("auto")){
            autoCtl = 1;
            char  ReplyBuffer[] = "auto";
            Udp.write(ReplyBuffer);
        }else if(msg.startsWith("change")){
            autoCtl = 0;
            if (state==0) {
                digitalWrite(pin, state);
                state = 1;
            }else{
                digitalWrite(pin, state);
                state = 0;
            }
            char  ReplyBuffer[] = "changed";
            Udp.write(ReplyBuffer);
        }else{
            Udp.write(packetBuffer);
        }
        Udp.endPacket();
    }
}
void printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    
    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);
    
    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

溪风沐雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值