使用NodeMCU的TCP IP客户端显示

时间:2018-08-29 06:04:35

标签: arduino tcpclient arduino-uno modbus-tcp

我有一个NodeMCU和一个Modbus RTU设备。我已在此处附加了代码以供参考。使用下面的库,我能够连接Modbus RTU设备并与之通信。设备会正确发送请求和响应,并显示在串行监视器上。现在,必须在TCP IP客户端上显示相同的数据。如图所示,当我在网络上ping IP地址时,它总是打印0.00。有人可以建议吗?

  1. 如何使数据可见?
  2. 如何自动更新或刷新页面?
  3. 如何分配静态IP地址?
  4. 如何连接到任何wifi路由器?

TCP IP output window display

#include <ModbusMaster232.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "Tasker.h"

Tasker tasker;
unsigned int  data[100];
ModbusMaster232 node(1); // change device address here.

// Define one address for reading
#define address 1
// Define the number of bits to read
#define bitQty 30

const char* ssid = "HOT SPOT";
const char* password = "airtel123";

int ledPin = 13; // GPIO13
WiFiServer server(80);

void setup() {
  Serial.begin(9600);
  node.begin(9600);
  delay(10);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  // timer.setInterval(1000, ModBusRead);  // Update Time Check every 10 seconds
  //tasker.setInterval(client_update, 2000);
}

void loop() {
  int result =  node.readHoldingRegisters(address, bitQty);
  data[0] = node.getResponseBuffer(0); // holds value of Frequency
  data[1] = node.getResponseBuffer(1);
  data[2] = node.getResponseBuffer(2); // holds value of VRph
  data[3] = node.getResponseBuffer(3);
  data[4] = node.getResponseBuffer(4); // holds value of VYph
  data[5] = node.getResponseBuffer(5);

  data[6] = node.getResponseBuffer(6); // holds value of VBph
  data[7] = node.getResponseBuffer(7);
  data[8] = node.getResponseBuffer(8); // holds value of VRph-VYph
  data[9] = node.getResponseBuffer(9);
  data[10] = node.getResponseBuffer(10); // holds value of Vyph-Vbph
  data[11] = node.getResponseBuffer(11);

  data[12] = node.getResponseBuffer(12); // holds value of VBph-VYph
  data[13] = node.getResponseBuffer(13);
  data[14] = node.getResponseBuffer(14); // hold value of IRCurrent
  data[15] = node.getResponseBuffer(15);
  data[16] = node.getResponseBuffer(16); // hold value of IYCurrent
  data[17] = node.getResponseBuffer(17);

  data[18] = node.getResponseBuffer(18); // hold value of IBCurrent
  data[19] = node.getResponseBuffer(19);
  data[20] = node.getResponseBuffer(20); // hold value of PFR
  data[21] = node.getResponseBuffer(21);
  data[22] = node.getResponseBuffer(22); // hold value of PFY
  data[23] = node.getResponseBuffer(23);

  data[24] = node.getResponseBuffer(24); // hold value of PFB
  data[25] = node.getResponseBuffer(25);
  data[26] = node.getResponseBuffer(26); // hold value of TP
  data[27] = node.getResponseBuffer(27);
  data[28] = node.getResponseBuffer(28); // hold value of ACTPOWR
  data[29] = node.getResponseBuffer(29);

  data[30] = node.getResponseBuffer(30); // hold value of ACTPOWY
  data[31] = node.getResponseBuffer(31);
  data[32] = node.getResponseBuffer(32); // hold value of ACTPOWB
  data[33] = node.getResponseBuffer(33);
  data[34] = node.getResponseBuffer(34); // hold value of TotalACTPOWR
  data[35] = node.getResponseBuffer(35);
  data[36] = node.getResponseBuffer(36); // hold value of ACTENERR
  data[37] = node.getResponseBuffer(37);
  data[38] = node.getResponseBuffer(38); //hold value of ACTENERY
  data[39] = node.getResponseBuffer(39);

  data[40] = node.getResponseBuffer(40); //hold value of ACTENERB
  data[41] = node.getResponseBuffer(41);
  data[42] = node.getResponseBuffer(42); //hold value of Total Active power
  data[43] = node.getResponseBuffer(43);
  data[44] = node.getResponseBuffer(44);
  data[45] = node.getResponseBuffer(45);
  data[46] = node.getResponseBuffer(46);
  data[47] = node.getResponseBuffer(47);
  data[48] = node.getResponseBuffer(48);
  data[49] = node.getResponseBuffer(49);


  data[50] = node.getResponseBuffer(50);
  data[51] = node.getResponseBuffer(51);
  data[52] = node.getResponseBuffer(52);
  data[53] = node.getResponseBuffer(53);
  data[54] = node.getResponseBuffer(54);
  data[55] = node.getResponseBuffer(55);
  data[56] = node.getResponseBuffer(56);
  data[57] = node.getResponseBuffer(57);
  data[58] = node.getResponseBuffer(58);
  data[59] = node.getResponseBuffer(59);

  for (int i = 0; i < 15; i++) {
    Serial.println(data[i]);
  }
  Serial.println("........................");
  //delay(1000);
  // Read the first line of the request
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("new client");
  while (!client.available()) {
    client_update();
    delay(1);
  }
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("                   ENERGY METER READING                  ");
  client.println("<br><br>");
  client.println("Frequency: ");
  client.println((data[0] / 10.0));
  client.println("<br><br>");
  client.println("Rphase voltage: ");
  client.println((data[2] / 10.0));
  client.println("<br><br>");
  client.println("Yphase voltage: ");
  client.println((data[4] / 10.0));
  client.println("<br><br>");
  client.println("Bphase voltage: ");
  client.println((data[6] / 10.0));
  client.println("<br><br>");
  client.println("VR-VY voltage: ");
  client.println((data[8] / 10.0));
  client.println("<br><br>");
  client.println("VY-VB voltage: ");
  client.println((data[10] / 10.0));
  client.println("<br><br>");
  client.println("VB-VR voltage: ");
  client.println((data[12] / 10.0));
  client.println("<br><br>");

  client.println("IR voltage: ");
  client.println((data[14] / 10.0));
  client.println("<br><br>");
  client.println("IY voltage: ");
  client.println( data[16] / 10.0);
  client.println("<br><br>");
  client.println("IB voltage: ");
  client.println( data[18] / 10.0);
  client.println("<br><br>");

  client.println("PF R phase: ");
  client.println( data[20] / 10.0);
  client.println("<br><br>");
  client.println("PF Y phase: ");
  client.println( data[22] / 10.0);
  client.println("<br><br>");
  client.println("PF B phase: ");
  client.println( data[24] / 10.0);
  client.println("<br><br>");
  client.println("Total PF: ");
  client.println( data[26] / 10.0);
  client.println("<br><br>");
  client.println("<br><br>");
  // tasker.loop();
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}

void client_update() {
  //WiFiClient client = server.available();
}

0 个答案:

没有答案