add PubSubClient MQTT library

This commit is contained in:
hg
2015-05-06 19:08:30 +02:00
parent 0501794eb7
commit 67632ef43e
6 changed files with 450 additions and 40 deletions

View File

@ -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");
}