diff --git a/common/include/espnow_common.h b/common/include/espnow_common.h index a73cac0..5f3a8f8 100644 --- a/common/include/espnow_common.h +++ b/common/include/espnow_common.h @@ -10,6 +10,13 @@ #define WIFI_DEFAULT_CHANNEL 11 +#define SENSOR_TYPE_TEMPERATURE 10 + + +#include +#include +#include + /* * Define the MAC address for our -Gateway- WiFi interface. * @@ -37,12 +44,12 @@ uint8_t cont06_mac[] = { 0x02, 0xFE, 0xED, 0xBE, 0xEF, 0x08 }; // Change me! /* * The gateway and nodes must share an identical data structure. */ -struct __attribute__ ((packed)) SENSOR_DATA { - uint8_t loc_id; - float temp; - float humidity; - float pressure; -} sensorData; +// struct __attribute__ ((packed)) SENSOR_DATA { +// uint8_t loc_id; +// float temp; +// float humidity; +// float pressure; +// } sensorData; #endif diff --git a/common/lib/nanopb/messages.pb b/common/lib/nanopb/messages.pb index 4f9d8b7..7422393 100644 --- a/common/lib/nanopb/messages.pb +++ b/common/lib/nanopb/messages.pb @@ -1,12 +1,10 @@ -¾ -messages.prototobalr"S -Report +„ +messages.proto"V +Report +nodeId (RnodeId -deviceName ( R -deviceName) -reading ( 2.tobalr.ReadingRreading"3 -Reading -name ( Rname -value (RvalueB +sensorType (R +sensorType +value (RvalueB dk.tobalr.espsensornodes \ No newline at end of file diff --git a/common/lib/nanopb/messages.pb.c b/common/lib/nanopb/messages.pb.c index c22309c..6938b1e 100644 --- a/common/lib/nanopb/messages.pb.c +++ b/common/lib/nanopb/messages.pb.c @@ -1,5 +1,5 @@ /* Automatically generated nanopb constant definitions */ -/* Generated by nanopb-0.3.9.2 at Mon Feb 18 20:16:25 2019. */ +/* Generated by nanopb-0.3.9.2 at Tue Feb 19 20:55:37 2019. */ #include "messages.pb.h" @@ -10,15 +10,10 @@ -const pb_field_t tobalr_Report_fields[3] = { - PB_FIELD( 1, STRING , REQUIRED, CALLBACK, FIRST, tobalr_Report, deviceName, deviceName, 0), - PB_FIELD( 2, MESSAGE , REPEATED, CALLBACK, OTHER, tobalr_Report, reading, deviceName, &tobalr_Reading_fields), - PB_LAST_FIELD -}; - -const pb_field_t tobalr_Reading_fields[3] = { - PB_FIELD( 3, STRING , REQUIRED, CALLBACK, FIRST, tobalr_Reading, name, name, 0), - PB_FIELD( 4, FLOAT , REQUIRED, STATIC , OTHER, tobalr_Reading, value, name, 0), +const pb_field_t Report_fields[4] = { + PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, Report, nodeId, nodeId, 0), + PB_FIELD( 2, INT32 , OPTIONAL, STATIC , OTHER, Report, sensorType, nodeId, 0), + PB_FIELD( 3, FLOAT , OPTIONAL, STATIC , OTHER, Report, value, sensorType, 0), PB_LAST_FIELD }; diff --git a/common/lib/nanopb/messages.pb.h b/common/lib/nanopb/messages.pb.h index fb3198d..c72e4ec 100644 --- a/common/lib/nanopb/messages.pb.h +++ b/common/lib/nanopb/messages.pb.h @@ -1,8 +1,8 @@ /* Automatically generated nanopb header */ -/* Generated by nanopb-0.3.9.2 at Mon Feb 18 20:16:25 2019. */ +/* Generated by nanopb-0.3.9.2 at Tue Feb 19 20:55:37 2019. */ -#ifndef PB_TOBALR_MESSAGES_PB_H_INCLUDED -#define PB_TOBALR_MESSAGES_PB_H_INCLUDED +#ifndef PB_MESSAGES_PB_H_INCLUDED +#define PB_MESSAGES_PB_H_INCLUDED #include /* @@protoc_insertion_point(includes) */ @@ -15,39 +15,31 @@ extern "C" { #endif /* Struct definitions */ -typedef struct _tobalr_Report { - pb_callback_t deviceName; - pb_callback_t reading; -/* @@protoc_insertion_point(struct:tobalr_Report) */ -} tobalr_Report; - -typedef struct _tobalr_Reading { - pb_callback_t name; +typedef struct _Report { + int32_t nodeId; + bool has_sensorType; + int32_t sensorType; + bool has_value; float value; -/* @@protoc_insertion_point(struct:tobalr_Reading) */ -} tobalr_Reading; +/* @@protoc_insertion_point(struct:Report) */ +} Report; /* Default values for struct fields */ /* Initializer values for message structs */ -#define tobalr_Report_init_default {{{NULL}, NULL}, {{NULL}, NULL}} -#define tobalr_Reading_init_default {{{NULL}, NULL}, 0} -#define tobalr_Report_init_zero {{{NULL}, NULL}, {{NULL}, NULL}} -#define tobalr_Reading_init_zero {{{NULL}, NULL}, 0} +#define Report_init_default {0, false, 0, false, 0} +#define Report_init_zero {0, false, 0, false, 0} /* Field tags (for use in manual encoding/decoding) */ -#define tobalr_Report_deviceName_tag 1 -#define tobalr_Report_reading_tag 2 -#define tobalr_Reading_name_tag 3 -#define tobalr_Reading_value_tag 4 +#define Report_nodeId_tag 1 +#define Report_sensorType_tag 2 +#define Report_value_tag 3 /* Struct field encoding specification for nanopb */ -extern const pb_field_t tobalr_Report_fields[3]; -extern const pb_field_t tobalr_Reading_fields[3]; +extern const pb_field_t Report_fields[4]; /* Maximum encoded size of messages (where known) */ -/* tobalr_Report_size depends on runtime parameters */ -/* tobalr_Reading_size depends on runtime parameters */ +#define Report_size 27 /* Message IDs (where set with "msgid" option) */ #ifdef PB_MSGID diff --git a/common/nanopb/generator/proto/__init__.pyc b/common/nanopb/generator/proto/__init__.pyc index 1776b38..f76ef29 100644 Binary files a/common/nanopb/generator/proto/__init__.pyc and b/common/nanopb/generator/proto/__init__.pyc differ diff --git a/common/nanopb/generator/proto/nanopb_pb2.pyc b/common/nanopb/generator/proto/nanopb_pb2.pyc index 02862d7..851a033 100644 Binary files a/common/nanopb/generator/proto/nanopb_pb2.pyc and b/common/nanopb/generator/proto/nanopb_pb2.pyc differ diff --git a/common/nanopb/generator/proto/plugin_pb2.pyc b/common/nanopb/generator/proto/plugin_pb2.pyc index 6738cd7..3c3f7cb 100644 Binary files a/common/nanopb/generator/proto/plugin_pb2.pyc and b/common/nanopb/generator/proto/plugin_pb2.pyc differ diff --git a/common/src/common/model/messages.proto b/common/src/common/model/messages.proto index bd21aac..3126efe 100644 --- a/common/src/common/model/messages.proto +++ b/common/src/common/model/messages.proto @@ -1,13 +1,8 @@ syntax = "proto2"; -package tobalr; option java_package = "dk.tobalr.espsensornodes"; message Report { - required string deviceName = 1; - repeated Reading reading = 2; -} - -message Reading { - required string name = 3; - required float value = 4; + required int32 nodeId = 1; + optional int32 sensorType = 2; + optional float value = 3; } diff --git a/gateway/src/espnow_gw.cpp b/gateway/src/espnow_gw.cpp index 8e6d469..4e75c6d 100755 --- a/gateway/src/espnow_gw.cpp +++ b/gateway/src/espnow_gw.cpp @@ -45,6 +45,9 @@ extern "C" { #include "espnow_common.h" #include "espnow_gw.h" } + + uint8_t buffer[128]; + Report report; /* *INDENT-ON* */ @@ -89,7 +92,15 @@ void initEspNow() { /* *INDENT-OFF* */ esp_now_register_recv_cb([](uint8_t * r_mac, uint8_t * r_data, uint8_t len) { memcpy(&rec_MAC, r_mac, sizeof(rec_MAC)); // Save MAC passed in cb. - memcpy(&sensorData, r_data, sizeof(sensorData)); // Save data struct passed in cb. + //memcpy(&sensorData, r_data, sizeof(sensorData)); // Save data struct passed in cb. + report = Report_init_zero; + Serial.write(r_data, len); + pb_istream_t inStream = pb_istream_from_buffer(r_data, len); + if (!pb_decode(&inStream, Report_fields, &report)){ + Serial.println("failed to decode proto"); + Serial.println(PB_GET_ERROR(&inStream)); + return; + } haveReading = true;} ); /* *INDENT-ON* */ @@ -164,7 +175,7 @@ void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect - if (client.connect("espNowGw","test","123456")) { + if (client.connect("espNowGw","tobalr","2Ih8DXlkCkvYZSBhwWt5")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("outTopic", "Hello world!"); @@ -213,6 +224,8 @@ void setup() { Serial.print("Ethernet IP is: "); Serial.println(Ethernet.localIP()); + + } @@ -223,10 +236,16 @@ void loop() { haveReading = false; Serial.print("Connection from: "); printMacAddress(rec_MAC); - Serial.printf(" Id: %i, Temp=%0.1f, Hum=%0.0f%%, pressure=%0.0fmb\r\n", - sensorData.loc_id, sensorData.temp, sensorData.humidity, - sensorData.pressure); - snprintf(buff, MQ_TOPIC_MAX, "%i:%0.2f:%0.0f:%0.0f", sensorData.loc_id, sensorData.temp, sensorData.humidity, sensorData.pressure); + Serial.println(report.nodeId); + Serial.println(report.sensorType); + Serial.println(report.value); + + + + // Serial.printf(" Id: %i, Temp=%0.1f, Hum=%0.0f%%, pressure=%0.0fmb\r\n", + // sensorData.loc_id, sensorData.temp, sensorData.humidity, + // sensorData.pressure); + // snprintf(buff, MQ_TOPIC_MAX, "%i:%0.2f:%0.0f:%0.0f", sensorData.loc_id, sensorData.temp, sensorData.humidity, sensorData.pressure); mq_publish((char *)TOPIC01, buff); } if (!client.connected()) { diff --git a/gateway/src/espnow_gw.h b/gateway/src/espnow_gw.h index 9d91699..3359121 100644 --- a/gateway/src/espnow_gw.h +++ b/gateway/src/espnow_gw.h @@ -32,7 +32,7 @@ IPAddress eth_IP(192, 168, 1, 68); // CHANGE THIS to something relevant for Y IPAddress eth_MASK(255, 255, 255, 0); // Subnet mask. IPAddress eth_DNS(192, 168, 1, 33); // DNS server. IPAddress eth_GW(192, 168, 1, 1); // Gateway (router). -IPAddress mq_server(192, 168, 1, 34); // CHANGE THIS to point at YOUR MQTT broker. +IPAddress mq_server(192, 168, 1, 49 ); // CHANGE THIS to point at YOUR MQTT broker. EthernetClient ethClient; // ETHERNET - The gateway wire to the local network. PubSubClient client(ethClient); // ETHERNET - The MQTT process is attached to the wire, -not- WiFi. diff --git a/sensor/src/espnow_demo_controller.cpp b/sensor/src/espnow_demo_controller.cpp index 7a50a8b..764b4ce 100644 --- a/sensor/src/espnow_demo_controller.cpp +++ b/sensor/src/espnow_demo_controller.cpp @@ -15,7 +15,7 @@ extern "C" { #include } -#define MY_LOC_ID 27 +#define MY_NODE_ID 101 uint8_t mac[] = { 0x02, 0x60, 0x0D, 0xF0, 0x0D, 0x02 }; void printMacAddress(uint8_t * macaddr) { @@ -52,9 +52,7 @@ void setup() { } esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); - esp_now_register_recv_cb([] - (uint8_t * macaddr, uint8_t * data, - uint8_t len) { + esp_now_register_recv_cb([](uint8_t * macaddr, uint8_t * data,uint8_t len) { Serial.println("recv_cb"); Serial.print("mac address: "); printMacAddress(macaddr); @@ -70,18 +68,24 @@ void setup() { Serial.println(status); }); - int res = esp_now_add_peer(wifi_mac, (uint8_t) ESP_NOW_ROLE_SLAVE, - (uint8_t) WIFI_DEFAULT_CHANNEL, NULL, 0); + int res = esp_now_add_peer(wifi_mac, (uint8_t) ESP_NOW_ROLE_SLAVE,(uint8_t) WIFI_DEFAULT_CHANNEL, NULL, 0); + Report report = Report_init_zero; + report.nodeId = 123; + report.sensorType = 45; + report.value = 67; - sensorData.loc_id = MY_LOC_ID; - sensorData.temp = ((uint8_t) RANDOM_REG32); - sensorData.humidity = ((uint8_t) RANDOM_REG32); - sensorData.pressure = ((uint8_t) RANDOM_REG32); + uint8_t buffer[128]; + pb_ostream_t outStream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - uint8_t bs[sizeof(sensorData)]; - memcpy(bs, &sensorData, sizeof(sensorData)); - esp_now_send(wifi_mac, bs, sizeof(sensorData)); + if (!pb_encode(&outStream, Report_fields, &report)) + { + Serial.println("failed to encode proto"); + Serial.println(PB_GET_ERROR(&outStream)); + return; + } + + esp_now_send(wifi_mac, buffer, outStream.bytes_written); ESP.deepSleep(20e6, WAKE_RF_DEFAULT); }