Hello everyone!
First of all, thank you for building MySensors, it's really an awesome project!
I had been searching a few months ago for a low-cost (and DIY) alternative to Zigbee / ZWave / Enocean... I'm glad I found it!
This is my first post here ; I'm currently struggling to make encryption and signing work on a barebone ATMEGA328P, using the internal 8MHz clock.
First, a bit of context:
I am working on a project to control my heaters via MySensors and HomeAssistant.
Basically, the idea is to send a (part) of the mains signal to the heater so that it enters the right mode: Comfort, Eco, Frost protection or Off mode.
The circuit behind that is really simple to build: a 3.3V power adapter, two optotriacs, two diodes, a microcontroller, a few resistors and a radio basically.
On the gateway side, HomeAssistant is running on a Raspberry Pi 1 and I attached to that board an RFM69W radio in the "serial gateway" configuration.
It has been built with the right configuration options: encryption enabled, rmf69w radio defined, etc.
I also defined a random AES and HMAC key in the configuration file used by the gateway.
On the "actuator" side if I can say so, I first tested the setup I'm trying to build with an Arduino Uno, another RFM69W, the right level converter between the radio and the Arduino Uno, and finally the controlling circuit.
I followed the procedure to make signing work: run the SecurityPersonalizer sketch with the keys defined and then uploaded my pilot wire code.
This first prototype using the Arduino Uno worked well: I could see nonces being exchanged in the debug view of the gateway, the commands sent / received and the state changing on the Arduino side (thanks to diodes). I am now planning to make a second more "realistic" prototype.
My goal is to produce PCBs with the minimum number of components ; I'll therefore replace the Arduino Uno by an ATMEGA328P with the internal 8MHz clock, two capacitors between (A)Vin and GND, and a resistor near the "not RESET" pin.
An important note before talking about the issue: I bought the ATMEGAs directly from Microchip (RIP Atmel :P) and the RFM69W radios directly from HopeRF.
So, these are genuine components!
Now, the issue:
I uploaded the SecurityPersonalizer sketch to the ATMEGA328P, made sure it was executed correctly and then I pushed my "pilot wire" sketch.
For an unknown reason, the node fails to register and I can't see any message on the gateway side.
Here are the "actuator" logs:
__ __ ____
| \/ |_ _/ ___| ___ _ __ ___ ___ _ __ ___
| |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
| | | | |_| |___| | __/ | | \__ \ _ | | \__ \
|_| |_|\__, |____/ \___|_| |_|___/\___/|_| |___/
|___/ 2.3.1
16 MCO:BGN:INIT NODE,CP=RPNNAS-X,REL=255,VER=2.3.1
63 TSM:INIT
63 TSF:WUR:MS=0
67 TSM:INIT:TSP OK
67 TSM:FPAR
75 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
2390 TSF:MSG:READ,51-180-176,s=199,c=3,t=129,pt=3,l=23,sg=1:0
2398 !TSF:MSG:LEN=7,EXP=32
2400 !TSM:FPAR:NO REPLY
2402 TSM:FPAR
2410 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
4278 TSF:MSG:READ,51-180-176,s=199,c=3,t=129,pt=3,l=23,sg=1:0
4286 !TSF:MSG:LEN=7,EXP=32
4419 !TSM:FPAR:NO REPLY
4421 TSM:FPAR
4427 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
6436 !TSM:FPAR:NO REPLY
6438 TSM:FPAR
6445 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
8454 !TSM:FPAR:FAIL
8456 TSM:FAIL:CNT=1
8458 TSM:FAIL:DIS
8460 TSF:TDI:TSL
18462 TSM:FAIL:RE-INIT
18464 TSM:INIT
18466 TSM:INIT:TSP OK
18468 TSM:FPAR
18477 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
20486 !TSM:FPAR:NO REPLY
20488 TSM:FPAR
20494 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
22503 !TSM:FPAR:NO REPLY
22505 TSM:FPAR
22511 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
24520 !TSM:FPAR:NO REPLY
24522 TSM:FPAR
24528 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
26540 !TSM:FPAR:FAIL
26542 TSM:FAIL:CNT=2
26544 TSM:FAIL:DIS
26546 TSF:TDI:TSL
36550 TSM:FAIL:RE-INIT
(etc)
When this problem appeared, I followed the pieces of advice given on this forum: I ran the "clear EEPROM" sketch, re-run the SecurityPersonalizer sketch and re-pushed my pilot wire sketch. Still no luck
I started to believe the problem could be at the hardware level.
I disabled encryption on the gateway (after rebuilding it) and in my sketch... and the actuator managed to register:
__ __ ____
| \/ |_ _/ ___| ___ _ __ ___ ___ _ __ ___
| |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
| | | | |_| |___| | __/ | | \__ \ _ | | \__ \
|_| |_|\__, |____/ \___|_| |_|___/\___/|_| |___/
|___/ 2.3.1
16 MCO:BGN:INIT NODE,CP=RPNNA---,REL=255,VER=2.3.1
28 TSM:INIT
28 TSF:WUR:MS=0
30 TSM:INIT:TSP OK
32 TSM:FPAR
38 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
440 TSF:MSG:READ,0-0-255,s=255,c=3,t=8,pt=1,l=1,sg=0:0
446 TSF:MSG:FPAR OK,ID=0,D=1
2048 TSM:FPAR:OK
2048 TSM:ID
2050 TSM:ID:REQ
2066 TSF:MSG:SEND,255-255-0-0,s=2,c=3,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2574 TSF:MSG:READ,0-0-255,s=2,c=3,t=4,pt=0,l=1,sg=0:1
2580 TSF:SID:OK,ID=1
2584 TSM:ID:OK
2584 TSM:UPL
2600 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
2820 TSF:MSG:READ,0-0-1,s=255,c=3,t=25,pt=1,l=1,sg=0:1
2824 TSF:MSG:PONG RECV,HP=1
2828 TSM:UPL:OK
2830 TSM:READY:ID=1,PAR=0,DIS=1
2844 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
3055 TSF:MSG:READ,0-0-1,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
3080 TSF:MSG:SEND,1-1-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1
3602 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
3649 TSF:MSG:READ,0-0-1,s=255,c=3,t=6,pt=0,l=1,sg=0:M
3674 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=11,pt=0,l=10,sg=0,ft=0,st=OK:Fil pilote
4196 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
4229 TSF:MSG:SEND,1-1-0-0,s=0,c=0,t=4,pt=0,l=0,sg=0,ft=0,st=OK:
4237 MCO:REG:REQ
4751 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
4968 TSF:MSG:READ,0-0-1,s=255,c=3,t=27,pt=1,l=1,sg=0:1
4974 MCO:PIM:NODE REG=1
4978 MCO:BGN:STP
4978 MCO:BGN:INIT OK,TSP=1
4995 TSF:MSG:SEND,1-1-0-0,s=0,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:1
5521 TSF:MSG:SEND,1-1-0-0,s=0,c=1,t=3,pt=2,l=2,sg=0,ft=0,st=OK:2
589813 TSF:MSG:READ,0-0-1,s=0,c=1,t=3,pt=0,l=1,sg=0:6
599988 TSF:MSG:READ,0-0-1,s=0,c=1,t=3,pt=0,l=1,sg=0:1
611840 TSF:MSG:READ,0-0-1,s=0,c=1,t=3,pt=0,l=1,sg=0:3
I could of course see messages being received / send on the gateway side too.
To sum up
Communication is successfully working between an Arduino Uno and the Serial Gateway with encryption + signing enabled but fails to work on a barebone ATMEGA328P.
Here are a few questions / ideas I have regarding the issue:
- Could the problem be the bootloader I used to flash the ATMEGA328P? I used the " breadboard-1-6-x.zip" found on the official Arduino website: https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard. Maybe the fuses / EEPROM address it set / uses conflicts with what the SecurityPersonalizer sketch does?
- It seems the encrypted packet is just ignored with encryption enabled since I can't see any logs on the gateway side. I was wondering: are the messages dropped at the radio level or at the software level?
Thanks in advance for your help!
Appendix -- Pilot wire code
#define MY_DEBUG
// MySensors configuration
#define MY_RADIO_RFM69
#define MY_RFM69_NEW_DRIVER
#define MY_SIGNING_SOFT
#define MY_SIGNING_SOFT_RANDOMSEED_PIN 7
#define MY_SIGNING_REQUEST_SIGNATURES
#define MY_RFM69_ENABLE_ENCRYPTION
//Β Include the libraries
#include <MySensors.h>
// Global settings
#define CHILD_ID 0
#define INITIAL_MODE 2
#define MOC_P 3
#define MOC_N 4
//Β Global variables
bool first_msg_sent = false;
MyMessage status_msg(CHILD_ID, V_STATUS);
MyMessage mode_msg(CHILD_ID, V_PERCENTAGE);
void set_mode(int16_t mode) {
digitalWrite(MOC_N, mode & 1);
digitalWrite(MOC_P, mode & 2);
}
void presentation() {
sendSketchInfo("Fil pilote", "1.0");
present(CHILD_ID, S_DIMMER);
}
void setup() {
// Make the MOC pins outputs
pinMode(MOC_P, OUTPUT);
pinMode(MOC_N, OUTPUT);
// Set the initial mode (2 = OFF)
set_mode(INITIAL_MODE);
}
void loop() {
if (!first_msg_sent) {
send(status_msg.set((int16_t) 1));
send(mode_msg.set((int16_t) INITIAL_MODE));
first_msg_sent = true;
}
}
void receive(const MyMessage &message) {
if(message.type == V_PERCENTAGE) {
set_mode(message.getInt());
}
}