Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed WIZnet_Library Watchdog DHT MQTT DS1820
Diff: main.cpp
- Revision:
- 5:b2ae1ed8a30e
- Parent:
- 4:ebaf1973d008
- Child:
- 6:4d30bc5a8076
diff -r ebaf1973d008 -r b2ae1ed8a30e main.cpp
--- a/main.cpp Sun Feb 23 18:36:18 2020 +0000
+++ b/main.cpp Mon Feb 24 00:40:29 2020 +0000
@@ -56,12 +56,9 @@
// ================= *************** ==================
#define NODE_NAME "controller03" // TODO just define node number
-uint8_t mac_addr[6]={0x00, 0x00, 0x00, 0xBE, 0xEF, 0x03}; // TODO make last byte dynamic
-const char* mqtt_broker = "192.168.1.99";
-const int mqtt_port = 1883;
-static Timer g_timer; // TODO remove me
+Ticker tick_5sec;
+Ticker tick_1sec;
-WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, PB_10); // SPI1 with D29 (reset)
typedef MQTT::Client<MQTTSocket,Countdown> MClient;
const char* ONOFF[] = {"OFF", "ON"};
@@ -73,6 +70,12 @@
DigitalOut led(LED_GREEN);
DigitalOut output0(OUTPUTPIN0);
+uint8_t mac_addr[6]={0x00, 0x00, 0x00, 0xBE, 0xEF, 0x03}; // TODO make last byte dynamic
+const char* mqtt_broker = "192.168.1.99";
+const int mqtt_port = 1883;
+unsigned long uptime_sec = 0;
+
+
const char* inputs[2] = {
"button",
NULL
@@ -110,6 +113,7 @@
void read_inputs()
{
+
}
@@ -171,10 +175,13 @@
return publish(client, "stat", topic, buf, strlen(buf), true);
}
-
-int networking_init(MQTTSocket &sock, MClient &client) {
+WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, PB_10); // SPI1 with D29 (reset)
+MQTTSocket sock;
+MClient client(sock);
+int connected = -1;
+
+int networking_init(MQTTSocket &sock, MClient &client, WIZnetInterface &wiz) {
int ret = 0;
- g_timer.start();
pc.printf("\n\nNode: %s\r\n", NODE_NAME);
pc.printf("%s attempting ethernet connection...\r\n", NODE_NAME);
wiz.init(mac_addr); // resets the w5500
@@ -183,18 +190,14 @@
}
pc.printf("IP: %s\r\n", wiz.getIPAddress());
-
- srand(rand()^g_timer.read_us()); // what is this doing?
-
+
ret = sock.connect((char*)mqtt_broker,mqtt_port);
if(ret != 0){
pc.printf("failed to connect to TCP server\r\n");
return 1;
}
pc.printf("sock.connect()=%d\r\n",ret);
-
- srand(rand()^g_timer.read_us()); // what is this doing?
-
+
if(client.connect() != 0){
pc.printf("MQTT connect failed\r\n");
return -1;
@@ -206,38 +209,43 @@
pc.printf("client.subscribe()=%d\r\n", ret);
// Node online message
- publish(client, "stat","online");
+ publish_value(client, "alive","ON");
+ publish_value(client, "IPAddress", wiz.getIPAddress());
pc.printf("Initialization done.\r\n");
return 0;
+}
+
+void periodic_announcement() {
+ // announce statuses
+ char uptime_sec_str[12];
+ sprintf(uptime_sec_str, "%d", uptime_sec);
+ connected = publish_value(client,"uptime",uptime_sec_str);
}
+void every_second() {
+ uptime_sec++;
+}
int main()
{
- MQTTSocket sock;
- MClient client(sock);
+ tick_1sec.attach(&every_second, 1.0);
+ pc.printf("\n\nNode: %s\r\n", NODE_NAME);
- int connected = networking_init(sock, client);
-
- bool btn = 0;
+ connected = networking_init(sock, client, wiz);
+
+ tick_5sec.attach(&periodic_announcement, 5.0);
while(1) {
set_outputs();
read_inputs();
- // replace this hacky mess with read_inputs!
- bool newBTN = button;
- if(newBTN != btn) {
- publish_value(client,"input0",OPENCLOSED[newBTN]);
- btn = newBTN;
- } else {
- client.yield(1000);
- connected = publish_value(client,"stat","hello world");
- if(connected != 0) {
- pc.printf("Restarting network....\r\n");
- networking_init(sock, client);
- }
+
+ if(connected != 0) {
+ pc.printf("Restarting network....\r\n");
+ networking_init(sock, client, wiz);
}
+ // pause a while, yawn......
+// client.yield(1000);
}
}