Arduino ESP32+Labview上位机
为更好理解通过labview tcp协议实现ESP32的上位机功能,本次小实验做如下整体方案。
方案:
labview上位机作为tcp服务端,esp32作为tcp客户端。(tcp/ip协议添加链接描述),客户端(esp32)向服务端(labview)发送内部芯片温度信息并实施监控,同时客户端可接受来自服务端的控制led指令。服务端(WIFI)ip地址获取添加链接描述
硬件连接
esp32开发板+0.96寸 四针 IIC oled显示屏
Arduino
#include <WiFi.h>
#include <WiFi.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <SimpleTimer.h>
#define TCP_SERVER_ADDR "***.**.**.**"//IP地址
#define TCP_SERVER_PORT "8888"//设置端口号
#endif
#define MAX_PACKETSIZE 512
#define KEEPALIVEATIME 30*1000
WiFiClient TCPclient;
String TcpClient_Buff = "";
unsigned int TcpClient_BuffIndex = 0;
unsigned long TcpClient_preTick = 0;
unsigned long preHeartTick = 0;
unsigned long preTCPStartTick = 0;
bool preTCPConnected = false;
bool subFlag = false;
void doWiFiTick();
void doTCPClientTick();
void sendtoTCPServer(String p);
void subTopic(String topic){
String tcpTemp="";
tcpTemp = "cmd=1&uid="+UID+"&topic="+topic+"\r\n";//发送消息类型,如cmd=2&uid=&topic=&msg=你好"\r\n",
sendtoTCPServer(tcpTemp);
tcpTemp="";
Serial.print("sub topic:");Serial.println(topic);//打印收到的消息
}
void doWiFiTick(){
static bool taskStarted = false;
static uint32_t lastWiFiCheckTick = 0;
if ( WiFi.status() != WL_CONNECTED ) {
if (millis() - lastWiFiCheckTick > 1000) {
lastWiFiCheckTick = millis();
WiFi.begin();
}
}
else {
if (taskStarted == false) {
taskStarted = true;
Serial.print("Get IP Address: ");//打印获取IP
Serial.println(WiFi.localIP());//打印登录IP
startTCPClient();
}
}
}
void sendtoTCPServer(String p){
if (!TCPclient.connected())
{
Serial.println("Client is not readly");//如果tcp连接打印Client is not readly
return;
}
TCPclient.print(p);
}
void startTCPClient(){
if(TCPclient.connect(TCP_SERVER_ADDR, atoi(TCP_SERVER_PORT))){
Serial.print("Connected to server:");//如果端口连接成功,打印Connected to server:
Serial.println(TCP_SERVER_ADDR);
subFlag = true;
preHeartTick = millis();
TCPclient.setNoDelay(true);
}
else{
Serial.print("Failed connected to server:");//如果端口连接失败Failed connected to server:
Serial.println(TCP_SERVER_ADDR);
TCPclient.stop();
preTCPConnected = false;
}
preTCPStartTick = millis();
}
String item;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
SimpleTimer timer;
extern "C"
{
uint8_t temprature_sens_read();
}
uint8_t temprature_sens_read();
//数据类型转换为字符串
void subTopicCenter(){
if(subFlag == true){
subFlag = false;
}
}
void doTCPClientTick(){
if(WiFi.status() != WL_CONNECTED) return;
if (!TCPclient.connected()) {
if(preTCPConnected == true){
preTCPConnected = false;
preTCPStartTick = millis();
Serial.println();
Serial.println("TCP Client disconnected.");//客户端连接成功TCP Client disconnected.
TCPclient.stop();
}
else if(millis() - preTCPStartTick > 1*1000)
startTCPClient();
}
else
{
if (TCPclient.available()) {
char c =TCPclient.read();
TcpClient_Buff +=c;
TcpClient_BuffIndex++;
TcpClient_preTick = millis();
if(TcpClient_BuffIndex>=MAX_PACKETSIZE - 1){
TcpClient_BuffIndex = MAX_PACKETSIZE-2;
TcpClient_preTick = TcpClient_preTick - 200;
}
preHeartTick = millis();
}
if(millis() - preHeartTick >= KEEPALIVEATIME){
preHeartTick = millis();
Serial.println("--Keep alive:");//没有消息发送或者接受打印--Keep alive:
sendtoTCPServer("ping\r\n");
}
}
if((TcpClient_Buff.length() >= 1) && (millis() - TcpClient_preTick>=200))
{
TCPclient.flush();
TcpClient_Buff.trim();
String topic = "";
String msg = "";
if(TcpClient_Buff.length() > 15){
int topicIndex = TcpClient_Buff.indexOf("&topic=")+7;
int msgIndex = TcpClient_Buff.indexOf("&msg=");
topic = TcpClient_Buff.substring(topicIndex,msgIndex);
msg = TcpClient_Buff.substring(msgIndex+5);
}
Serial.println(msg);//如果收到消息为on则管脚2为高电平;如果收到消息为off则管脚2为低电平
if (msg == "on") {
digitalWrite(2,HIGH);
} else if (msg == "off") {
digitalWrite(2,LOW);
}
u8g2.firstPage();
do
{
u8g2.setFont(u8g2_font_timB14_tf);
u8g2.setFontPosTop();
u8g2.setFont(u8g2_font_wqy14_t_gb2312a);
u8g2.setFontPosTop();
u8g2.setCursor(0,1);
u8g2.print(msg);//消息显示
u8g2.setCursor(0,45);
u8g2.print(String("温度:") + String(item));//温度显示
u8g2.setCursor(0,30);
u8g2.print("WIFI is connectioned!");//WiFi连接成功
}while(u8g2.nextPage());
msg = "";
topic = "";
TcpClient_Buff="";
TcpClient_BuffIndex = 0;
}
}
void publishMsg(String topic,String msg){
String tcpTemp="";
tcpTemp = "cmd=2&uid="+UID+"&topic="+topic+"&msg="+msg+"\r\n";
sendtoTCPServer(tcpTemp);
tcpTemp="";
Serial.println("A message has been published");//发送一则消息打印A message has been published
}
void Simple_timer_1() {
publishMsg("", item);
}
void setup(){
item = "";
WiFi.begin("###", "##WIFI");//"WIFI名称", "WIFI密码"
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Local IP:");
Serial.print(WiFi.localIP());
Serial.begin(9600);
pinMode(2, OUTPUT);
u8g2.setI2CAddress(0x3C*2);
u8g2.begin();
u8g2.enableUTF8Print();
timer.setInterval(1500L, Simple_timer_1);//每隔1500毫秒发送一次消息
}
void loop(){
item = int((temprature_sens_read() - 32) / 1.8);
doWiFiTick();
doTCPClientTick();
subTopicCenter();
timer.run();
}
labview
1.前面板
2.后面板
结果
服务端(labview)发送指令开灯,屏幕显示no,服务端(labview)发送指令关灯,屏幕显示off;同时客户端向服务端发送信息并显示出来