esp8266同时运行两个while_Esp8266控制小灯源代码分享

应粉丝的要求将我视频中的源代码分享出来,视频地址

1、硬件部分代码:

#include #include #include int pin1=5;WiFiClient eclient;PubSubClient client(eclient);void setup_wifi(){  WiFi.begin("wifi账号","wifi密码");  Serial.print("正在连接wifi");  while (WiFi.status()!=WL_CONNECTED)  {    delay(500);    Serial.print(".");  }  Serial.println("");  Serial.println(WiFi.localIP());}void callback(char* topic, byte* payload, unsigned int length) {  // handle message arrived  Serial.println(topic);  String cmd="";  for (int i = 0; i < length; i++)  {     cmd+=(char)payload[i];  }  Serial.println(cmd);  String _topic(topic);  if(_topic.equals("light")){    if(cmd.equals("on")){      digitalWrite(pin1,LOW);    }else if (cmd.equals("off"))    {      digitalWrite(pin1,HIGH);    }  }}void connent(){  while (!client.connected())  {     Serial.print(".");     if(client.connect("esp826601")){       client.publish("online","hello word"); //可以不用       client.subscribe("light");     }  }}void setup() {  pinMode(pin1,OUTPUT);  digitalWrite(pin1,HIGH);  Serial.begin(115200);  setup_wifi();  client.setServer("10.20.0.155",1883);  client.setCallback(callback);  connent();}void loop() {  client.loop();}

2、服务端关键代码:

在项目中引用类包:

Install-Package BlueFox.Mqtt -Version 1.0.8Install-Package BlueFox.Base.Mvc -Version 1.0.15

在startup.cs文件中修改:

public void ConfigureServices(IServiceCollection services){  services.AddControllers();  services.AddMqtt(Configuration.GetSection("mqtt"));//添加qmtt链接}

创建EspController控制器,代码如下:

 [Route("api/[controller]")]    public class EspController : PubController    {        private readonly MqttService _mqttService;        public EspController(MqttService mqttService)        {            _mqttService = mqttService;        }        [HttpGet("Light")]        public async Task Light(int statu)        {            await _mqttService.ConnectAsync();            await _mqttService.PublishAsync("light", statu == 0 ? "off" : "on");            return Success();        }    }

在appsettings.json文件中添加如下配置:

 "mqtt": {    "host": "mqttip地址",    "port": 1883,    "user": "",    "passwd": "",    "Level": 1, //0:最多一次 1:至少一次 2:仅一次    "Retained": false  }

3、app应用端:

在app.module.ts文件中添加引入:

import { HttpClientModule} from '@angular/common/http'

在imports中引入HttpClientModule:修改如下:

imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule,HttpClientModule],

在src/environments中两个environment文件中添加host字段:

environment.ts 添加 host:'./'

export const environment = {  production: false,  host:'./'};

environment.prod.ts添加 host:'http://你的ip:端口/'

export const environment = {  production: true,  host:'http://localhost:9001/'//修改为服务端ip};

在home.page.ts文件中添加引入:

import { HttpClient} from '@angular/common/http';import { environment } from 'src/environments/environment';

在类代码中添加如下代码:

  constructor(private http: HttpClient) {}    open(){  this.http.get(environment.host+`api/esp/light?statu=1`).subscribe(res=>{})  }    close(){  this.http.get(environment.host+`api/esp/light?statu=0`).subscribe(res=>{})  }

在项目目录下创建代理文件:proxy.conf.json

{      "/api": {        "target": "http://localhost:60088"      }}

在angular.json文件中找到serve节点修改options节点内容如下:

"serve": {          "builder": "@angular-devkit/build-angular:dev-server",          "options": {            "browserTarget": "app:build","proxyConfig": "proxy.conf.json"//代理配置          },          "configurations": {            "production": {              "browserTarget": "app:build:production"            },            "ci": {              "progress": false            }          }        },

完成后在命令行中运行:

ionic serve 

如需打包app:

 ionic cordova build android --prod

源码地址:https://gitee.com/fmtx/Esp8266_Course

2c754e0875f785a6f6a4fef6df9f3aef.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值