Skip to content

Commit

Permalink
Now almost working sample with protobuf
Browse files Browse the repository at this point in the history
First value is transfered. Suspect that message is getting capped.
  • Loading branch information
tfatrifork committed Mar 4, 2019
1 parent b578cbb commit 275b8f5
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 78 deletions.
19 changes: 13 additions & 6 deletions common/include/espnow_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

#define WIFI_DEFAULT_CHANNEL 11

#define SENSOR_TYPE_TEMPERATURE 10


#include <messages.pb.h>
#include <pb_encode.h>
#include <pb_decode.h>

/*
* Define the MAC address for our -Gateway- WiFi interface.
*
Expand Down Expand Up @@ -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
16 changes: 7 additions & 9 deletions common/lib/nanopb/messages.pb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 5 additions & 10 deletions common/lib/nanopb/messages.pb.c
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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
};

Expand Down
42 changes: 17 additions & 25 deletions common/lib/nanopb/messages.pb.h
Original file line number Diff line number Diff line change
@@ -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 <pb.h>

/* @@protoc_insertion_point(includes) */
Expand All @@ -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
Expand Down
Binary file modified common/nanopb/generator/proto/__init__.pyc
Binary file not shown.
Binary file modified common/nanopb/generator/proto/nanopb_pb2.pyc
Binary file not shown.
Binary file modified common/nanopb/generator/proto/plugin_pb2.pyc
Binary file not shown.
11 changes: 3 additions & 8 deletions common/src/common/model/messages.proto
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 25 additions & 6 deletions gateway/src/espnow_gw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ extern "C" {
#include "espnow_common.h"
#include "espnow_gw.h"
}

uint8_t buffer[128];
Report report;
/* *INDENT-ON* */


Expand Down Expand Up @@ -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* */
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -213,6 +224,8 @@ void setup() {

Serial.print("Ethernet IP is: ");
Serial.println(Ethernet.localIP());


}


Expand All @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion gateway/src/espnow_gw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 17 additions & 13 deletions sensor/src/espnow_demo_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C" {
#include <espnow_common.h>
}

#define MY_LOC_ID 27
#define MY_NODE_ID 101
uint8_t mac[] = { 0x02, 0x60, 0x0D, 0xF0, 0x0D, 0x02 };

void printMacAddress(uint8_t * macaddr) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit 275b8f5

Please sign in to comment.