• 使用手机(网页)控制ESP32S3的LED亮灭


    1)ESP32S3

    1. #include
    2. #include
    3. const char* ssid = "TP-LINKxxxxx";
    4. const char* password = "yyyyyy";
    5. int led = 5;
    6. int led2 = 10;
    7. const char* serverURL = "http://192.168.0.105:8888/getled";
    8. void setup() {
    9. pinMode(led, OUTPUT);
    10. pinMode(led2, OUTPUT);
    11. Serial.begin(115200);
    12. WiFi.begin(ssid, password);
    13. Serial.println("正在连接到WiFi...");
    14. while (WiFi.status() != WL_CONNECTED) {
    15. delay(500);
    16. Serial.print(".");
    17. }
    18. Serial.println("");
    19. Serial.print("已连接到 ");
    20. Serial.println(ssid);
    21. Serial.print("IP地址:");
    22. Serial.println(WiFi.localIP());
    23. }
    24. void loop() {
    25. HTTPClient http;
    26. String url = serverURL;
    27. while(true){
    28. if (WiFi.status() == WL_CONNECTED) {
    29. // 链接url
    30. http.begin(url);
    31. int httpResponseCode = http.GET();
    32. // 返回值
    33. String recstr = http.getString();
    34. if (recstr == "off") {
    35. digitalWrite(led, LOW);
    36. digitalWrite(led2, LOW);
    37. Serial.println("led is off");
    38. } else {
    39. digitalWrite(led, HIGH);
    40. digitalWrite(led2, HIGH);
    41. Serial.println("led is on");
    42. }
    43. // http.end();
    44. }
    45. }
    46. }

    服务器端 NodeJS+KOAJS

    1. const Koa = require('koa');
    2. const app = new Koa();
    3. const router = require("koa-router")();
    4. const { koaBody } = require('koa-body');
    5. const url = require('url');
    6. const fs = require('fs');
    7. app.use(koaBody());
    8. // 解决跨域问题的中间件
    9. const cors = async (ctx, next) => {
    10. // 设置响应头来允许跨域资源共享
    11. ctx.set('Access-Control-Allow-Origin', '*'); // 允许任何源
    12. ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); // 允许的 HTTP 方法
    13. ctx.set('Access-Control-Allow-Headers', 'Content-Type, Accept'); // 允许的 HTTP 头
    14. if (ctx.method === 'OPTIONS') {
    15. // 对于预检请求直接返回
    16. ctx.body = 204;
    17. } else {
    18. // 非 OPTIONS 请求则继续执行后续中间件
    19. await next();
    20. }
    21. };
    22. // 使用中间件
    23. app.use(cors);
    24. // 开灯关灯逻辑,前端访问更新这里的文件的值
    25. router.get("/setled", async ctx => {
    26. const queryObject = url.parse(ctx.url, true).query;
    27. // 这里设置了一个参数len 如果是20开灯, 否则是关灯,其实这里可以使用一个比较好的参数比较好
    28. if(queryObject.len === '20'){
    29. // 写入文件on
    30. const data = "on";
    31. const filePath = './file.txt';
    32. fs.writeFile(filePath,data,'utf8', (err) => {
    33. if(err) {
    34. return console.log(err);
    35. }
    36. console.log("写入成功")
    37. });
    38. } else {
    39. // 写入文件off
    40. const data = "off";
    41. const filePath = './file.txt';
    42. fs.writeFile(filePath,data,'utf8', (err) => {
    43. if(err) {
    44. return console.log(err);
    45. }
    46. console.log("写入成功")
    47. });
    48. }
    49. ctx.body = "led is changed";
    50. })
    51. // 硬件访问的地址,开灯或者关灯配置(读取文件中的值)
    52. router.get("/getled", async ctx => {
    53. try {
    54. // 同步读取文件
    55. const data = fs.readFileSync('file.txt', 'utf8');
    56. console.log(data);
    57. ctx.body = data;
    58. } catch (err) {
    59. console.error(err);
    60. }
    61. })
    62. app.use(router.routes());
    63. app.use(router.allowedMethods());
    64. // app.use(async (ctx) => {
    65. // console.log("ctx",ctx);
    66. // ctx.body = 'ok';
    67. // });
    68. app.listen(8888,'0.0.0.0', () => {
    69. console.log('Server is running at http://localhost:8888');
    70. });

    前端 Vue2

    1. <script>
    2. import axios from 'axios';
    3. export default {
    4. name: 'HelloWorld',
    5. data() {
    6. return {
    7. data:'',
    8. }
    9. },
    10. props: {
    11. msg: String
    12. },
    13. methods:{
    14. turnon(){
    15. alert('开灯');
    16. axios.get('http://192.168.0.105:8888/setled?len=20').then( response => {
    17. this.data = response.data;
    18. console.log(this.data);
    19. }).catch(err => {
    20. console.error("there is an err", err);
    21. })
    22. },
    23. turnoff(){
    24. alert('关灯');
    25. axios.get('http://192.168.0.105:8888/setled?len=30').then( response => {
    26. this.data = response.data;
    27. console.log(this.data);
    28. }).catch(err => {
    29. console.error("there is an err", err);
    30. })
    31. }
    32. }
    33. }
    34. script>
    35. <style scoped>
    36. h3 {
    37. margin: 40px 0 0;
    38. }
    39. ul {
    40. list-style-type: none;
    41. padding: 0;
    42. }
    43. li {
    44. display: inline-block;
    45. margin: 0 10px;
    46. }
    47. a {
    48. color: #42b983;
    49. }
    50. style>

  • 相关阅读:
    网络技术-安全评估技术
    多头风险管理和空头风险管理
    「开发者说」钉钉连接器+OA审批实现学校学生假勤场景数字化
    mac M3芯片 goland 2022.1 断点调试失败(frames are not available)问题,亲测有效
    1019 数字黑洞 (坑点说明)
    R语言根据日历周期处理时间序列数据(周、月、年等):使用xts包的apply.monthly函数和mean函数计算时间序列的月平均值(monthly)
    互联网摸鱼日报(2022-11-03)
    C字符串操作笔记
    SNERT预备队招新CTF体验赛-Misc(SWCTF)
    阿里云面试:什么是语法糖?Java中有哪些语法糖?
  • 原文地址:https://blog.csdn.net/Llingmiao/article/details/139610642