add PubSubClient MQTT library
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
#include "WiModbusGateway.h"
|
||||
|
||||
#include <SPI.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiUDP.h>
|
||||
@ -8,6 +10,7 @@
|
||||
#include "ModbusApp.h"
|
||||
#include "PString.h"
|
||||
#include "Streaming.h"
|
||||
#include "PubSubClient.h"
|
||||
|
||||
|
||||
|
||||
@ -16,18 +19,26 @@
|
||||
LiquidCrystal lcd = LiquidCrystal(A0, A1, A2, A3, A4, A5);
|
||||
|
||||
// Mudbus Mb;
|
||||
WiFiUDP udpSock;
|
||||
// WiFiUDP udpSock;
|
||||
WiFiClient wifiClient;
|
||||
byte server[] = { 192, 168, 87, 100 };
|
||||
PubSubClient client(server, 1883, callback, wifiClient);
|
||||
|
||||
|
||||
//char ssid[] = "Kinderland"; // your network SSID (name)
|
||||
//char pass[] = "test1234"; // your network password
|
||||
char ssid[] = "MessWLAN";
|
||||
char pass[] = "UNVmpwbr6heQnMQ7ykXT";
|
||||
char ssid[] = "Kinderland"; // your network SSID (name)
|
||||
char pass[] = "test1234"; // your network password
|
||||
//char ssid[] = "MessWLAN";
|
||||
//char pass[] = "UNVmpwbr6heQnMQ7ykXT";
|
||||
|
||||
Metro tick = Metro(10000);
|
||||
Metro second = Metro(1000);
|
||||
uint32_t uptime;
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// handle message arrived
|
||||
}
|
||||
|
||||
|
||||
void printWifiStatus() {
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
@ -89,10 +100,14 @@ void setup() {
|
||||
lcd.setCursor(0, 2);
|
||||
lcd.print("Connected.");
|
||||
|
||||
|
||||
|
||||
|
||||
printWifiStatus();
|
||||
|
||||
|
||||
delay(10000);
|
||||
|
||||
delay(1000);
|
||||
lcd.home();
|
||||
lcd.clear();
|
||||
|
||||
@ -105,6 +120,7 @@ void setup() {
|
||||
|
||||
// modbusAppBegin(&Mb);
|
||||
modbusAppBegin();
|
||||
client.connect("arduinoClient");
|
||||
}
|
||||
|
||||
|
||||
@ -149,6 +165,8 @@ void updateDisplay() {
|
||||
void loop() {
|
||||
modbusAppExec();
|
||||
// Mb.Run();
|
||||
client.loop();
|
||||
|
||||
|
||||
if (second.check() == 1) {
|
||||
uptime++;
|
||||
@ -156,45 +174,22 @@ void loop() {
|
||||
char strbuf[256];
|
||||
memset(strbuf, sizeof(strbuf), 0);
|
||||
PString buf = PString(strbuf, sizeof(strbuf));
|
||||
|
||||
udpSock.beginPacket("172.16.2.16", 9999);
|
||||
|
||||
buf << "{ \"metadata\": { \"table\": \"WiFiPowerMeter\" }, \"data\": {";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "\"voltage\": " << getVoltage() << ", ";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "\"current\": " << getCurrent() << ", ";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "\"frequency\": " << getFrequency() << ", ";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "\"power\": " << getPower() << ", ";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "\"energy\": " << getEnergy() << ", ";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "\"uptime\": " << uptime;
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "}";
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
buf.begin();
|
||||
buf << "}" << endl;
|
||||
udpSock.write(strbuf, buf.length());
|
||||
|
||||
udpSock.endPacket();
|
||||
// uint8_t x = uptime % 128;
|
||||
// for(uint8_t y = 1; y < x; y++) {
|
||||
// buf << "a";
|
||||
// }
|
||||
// Serial.print("buflen: "); Serial.println(buf.length());
|
||||
client.publish("outTopic", strbuf);
|
||||
// client.publish("outTopic", "tick");
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user