远程控制开关-Nodemcu+Relay(nodemcu+继电器)

2 篇文章 0 订阅
1 篇文章 0 订阅

远程控制开关-Nodemcu+Relay

硬件

nodemcu一个
继电器一个
杜邦线3根

继电器信号端接在nodemcu的D1
注:D1–>GPIO 5

软件

1.安装 Arduino
2.开发版管理器里面添加: http://arduino.esp8266.com/stable/package_esp8266com_index.json
3.安装esp8266 开发版 ----开发环境
4.安装ArduinoJson ----解析JSON格式

代码

客户端源代码
/**
    BasicHTTP
    Created on: 2020年8月25日09:18:20
    by Yang.Gu 13482198105@163.com
*/


#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>

#ifndef STASSID
#define STASSID "aiyouwoqu"
#define STAPSK  "wcg2232v5"
#endif
#define D1 5             //继电器开关  D1
#define configRED 4      //备用开关    D2


char* nodeuuid = "1234567890";
char* nodename= "nodemcu1";
char* nodetype= "nodemcu";
char* nodesofwareversion= "2020824001";
char* nodeip;
char* senddata = "{\"nodeuuid\": \"1234567890\",\"nodename\": \"nodemcu1\",\"nodetype\": \"nodemcu\",\"nodesofwareversion\": \"2020824001\",\"nodeip\": \"\"}";

bool link;
const char* requesturl = "http://192.168.214.100:7777/IOTPlatform/GetState";
ESP8266WiFiMulti WiFiMulti;

void setup() {
  Serial.begin(115200);
  pinMode(D1,OUTPUT);
  // Serial.setDebugOutput(true);
  Serial.println();
  Serial.println();
  Serial.println();
  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }
  connectwifi();
  link = true;
  digitalWrite(configRED, HIGH);
}
bool connectwifi()
{
  WiFi.mode(WIFI_STA); 
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}
void loop() {
     String AAA = httppost();
     DynamicJsonDocument doc(1024);
     deserializeJson(doc, AAA);
     JsonObject obj = doc.as<JsonObject>();
     String D1FromDB = obj["nodestate"];
     if(D1FromDB=="1")
     {
      if(link==false)
      {
        link = true;
        digitalWrite(D1, HIGH);
      }
     }
     else if(D1FromDB=="0")
     {
      if(link==true)
      {
        link = false;
        digitalWrite(D1, LOW);
      }
     }
     else
     {}
     Serial.printf("D1FromDB:%s\n",D1FromDB.c_str());
     delay(5000);
}
String httppost()
{
    WiFiClient client;
    HTTPClient http;
    String payload;
    Serial.print("[HTTP] begin...\n");
    if (http.begin(client,requesturl )) { 
      Serial.println("###request start###");
      Serial.print("[HTTP] POST...\n");
      http.addHeader("Content-Type", "application/json;charset=UTF-8");
      
      
      Serial.printf(senddata);
      int httpCode = http.POST(senddata);
      if (httpCode > 0) {
        Serial.printf("[HTTP] POST... code: %d\n", httpCode);
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          payload = http.getString();
          Serial.println(payload);
          Serial.println("###request end###");
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
      connectwifi();
    }
    return payload;
}

在这里插入图片描述

服务端源代码
from flask import request, Flask, jsonify
import json
import random
import pymysql
import peewee
from peewee import *
import datetime

mysqlhost = 'store.guyang.site'
mysqlport = 13306
mysqluser = 'root'
mysqlpasswd = '123456'
mysqldb = 'home'

# db = MySQLDatabase(mysqldb, user=mysqluser,password = mysqlpasswd, host=mysqlhost, port=13306)
db = pymysql.connect(host=mysqlhost, port=13306, user=mysqluser, passwd=mysqlpasswd, db=mysqldb, charset='utf8')
cursor = db.cursor()
class d_heartbeat(peewee.Model):
    no=peewee.IntegerField()
    uuid=peewee.CharField()
    hostname=peewee.CharField()
    appname=peewee.CharField()
    version=peewee.CharField()
    ip=peewee.CharField()
    mac=peewee.CharField()
    regiesttime=peewee.DateTimeField()
    lastupdate=peewee.DateTimeField()
    nodetype=peewee.CharField()

    class Meta:
        database = db

class d_nodeinfo(peewee.Model):
    nodeuuid = peewee.CharField()
    nodename = peewee.CharField()
    state = peewee.CharField()
    value = peewee.CharField()
    updatetime = peewee.DateTimeField()

    class Meta:
        database = db

def after_request(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
    response.headers['Access-Control-Allow-Headers'] = 'Content-Type,Authorization'
    return response

app = Flask(__name__)
#app.config['JSON_AS_ASCII'] = False
app.after_request(after_request)

@app.route("/IOTPlatform/GetState", methods=['GET','POST'])
def IOTPlatform_GetState():
    requestvalue = request.get_json()
    print(requestvalue)
    nodeuuid = requestvalue["nodeuuid"]
    nodename = requestvalue["nodename"]
    nodetype = requestvalue["nodetype"]
    nodesofwareversion = requestvalue["nodesofwareversion"]
    nodeip = request.remote_addr
    try:
        #data = d_heartbeat.get_or_none(d_heartbeat.uuid==nodeuuid)
        # print("nodeuuid:"+nodeuuid)
        sql = 'select * from d_heartbeat where uuid = %s;'
        cursor.execute(sql,[nodeuuid])
        data = cursor.fetchall()
        # print("6543")
        if len(data)<1:
            #_nodeinfo = d_heartbeat(uuid=nodeuuid,hostname=nodename,nodetype=nodetype,version=nodesofwareversion,ip=nodeip,lastupdate="now()")
            #_nodeinfo.save()
            print("uuid is new , do insert action")
            sql ='insert into d_heartbeat (uuid,hostname,nodetype,version,ip,lastupdate) values (%s,%s,%s,%s,%s,now());'
            cursor.execute(sql,[nodeuuid,nodename,nodetype,nodesofwareversion,nodeip])
        else:
            print("uuid is already existed, do update action")
            sql ='update d_heartbeat set ip = %s , lastupdate = now() where uuid = %s;'
            cursor.execute(sql,[nodeip,nodeuuid])
        db.commit()
    except:
        print("heart update action err occur")
        db.rollback()


    resnodestate = ""
    resnodeuuid = ""
    resnodename = ""
    
    try:
        sql = 'select * from d_nodeinfo where nodeuuid = %s;'
        cursor.execute(sql,[nodeuuid])
        data = cursor.fetchall()
        if len(data)<1:
            print("this uuid is new a one,need to be assigned ")
        elif len(data) == 1:
            for row in data:
                resnodeuuid = row[1]
                resnodename = row[2] 
                resnodestate = row[3]
        else:
            print("this uuid has mutiple datas in DB ,need to check")

        db.commit()
    except:
        print("heart update action err occur")
        db.rollback()
    return {"nodeuuid":resnodeuuid,"nodename":resnodename,"nodestate":resnodestate,"datetime":datetime.datetime.now()}

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0',port=7777)

在这里插入图片描述

数据库

在这里插入图片描述

在这里插入图片描述

流程介绍

原本是想用 aspnetcore+MQTT+MongoDB做接口和存储,但是搭建环境什么的做下来发现目前没有必要,而且复杂。目前就先用最简单的搭建一套,先用起来。
mongoDB 用 mysql替换
mqtt协议不搭了,直接通过httpapi
aspnetcore 用python flask代替
最后方案是:

  1. MySql+Flask+NodeMCU+Relay
  2. 目标最终效果:
    1. 发心跳到数据库
      1. 自动新建
      2. 保持更新
    2. 读取数据库中关于自身的状态
    3. 改变自己的针脚状态
总结

以上功能均能实现
目前只是搭建了一个框架,日后较多功能还需要在这个上面扩展。

优化

1.一定几率连数据库出现问题,导致之后数据库连接不上
2.增加页面显示心跳功能
3.增加页面控制功能
4.目前延时5秒钟,需要缩短延时
5.后台或者前台,增加定时任务

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Arduino IDE来编写一个远程控制程序,使用ESP8266(ESP-01)作为WiFi模块,与继电器进行通信。以下是一个简单的示例代码: ```cpp #include <ESP8266WiFi.h> const char* ssid = "your_SSID"; // 替换为您的WiFi网络名称 const char* password = "your_PASSWORD"; // 替换为您的WiFi网络密码 const int relayPin = D1; // 替换为您连接继电器的引脚 WiFiServer server(80); // 设置TCP服务器端口 void setup() { pinMode(relayPin, OUTPUT); digitalWrite(relayPin, LOW); // 初始状态关闭继电器 Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); server.begin(); } void loop() { WiFiClient client = server.available(); if (client) { Serial.println("New client connected"); String currentLine = ""; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); if (c == '\n') { if (currentLine.length() == 0) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); if (digitalRead(relayPin) == HIGH) { client.println("<h1>Relay is ON</h1>"); client.println("<a href=\"/off\">Turn OFF</a>"); } else { client.println("<h1>Relay is OFF</h1>"); client.println("<a href=\"/on\">Turn ON</a>"); } break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } if (currentLine.endsWith("GET /on")) { digitalWrite(relayPin, HIGH); } else if (currentLine.endsWith("GET /off")) { digitalWrite(relayPin, LOW); } } } delay(10); client.stop(); Serial.println("Client disconnected"); } } ``` 请确保将代码中的`your_SSID`和`your_PASSWORD`替换为您的WiFi网络名称和密码。此程序通过ESP8266与WiFi网络连接,并在80端口上启动一个简单的TCP服务器。当您在浏览器中访问设备的IP地址时,您将看到一个页面,显示继电器的当前状态,并提供打开和关闭继电器的链接。当您单击链接时,它将发送相应的请求到ESP8266,并控制继电器的状态。 请注意,此代码仅为示例,可能需要根据您的具体硬件设置进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值