Skip to content

Commit 0e0a933

Browse files
linusnielsenLinus Nielsenskotopes
authored
WS: add protocol EMOS E601x (#107)
Co-authored-by: Linus Nielsen <[email protected]> Co-authored-by: あく <[email protected]>
1 parent 847ef94 commit 0e0a933

File tree

7 files changed

+362
-1
lines changed

7 files changed

+362
-1
lines changed

weather_station/.catalog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ TX8300
2424
Wendox W6276
2525
Auriol AHFL
2626
Kedsum-TH/Conrad-S3318P
27+
EMOS E601x

weather_station/.catalog/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 1.4
2+
- Add protocol EMOS E601x
13
## 1.3
24
- Add protocol Kedsum-TH/Conrad-S3318P
35
## 1.2

weather_station/application.fam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ App(
77
requires=["gui"],
88
stack_size=4 * 1024,
99
fap_description="Receive weather data from a wide range of supported Sub-1GHz remote sensor",
10-
fap_version="1.3",
10+
fap_version="1.4",
1111
fap_icon="weather_station_10px.png",
1212
fap_category="Sub-GHz",
1313
fap_icon_assets="images",
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
#include "emos_e601x.h"
2+
3+
#define TAG "WSProtocolEmosE601x"
4+
5+
/*
6+
* Help
7+
* https://github.com/merbanan/rtl_433/blob/master/src/devices/emos_e6016.c
8+
*
9+
* Data Layout:
10+
*
11+
* PP PP PP II ?K KK KK KK CT TT HH SS DF XX RR
12+
*
13+
* - P: (24 bit) preamble
14+
* - I: (8 bit) ID
15+
* - ?: (2 bit) unknown
16+
* - K: (32 bit) datetime, fields are 6d-4d-5d 5d:6d:6d
17+
* - C: (2 bit) channel
18+
* - T: (12 bit) temperature, signed, scale 10
19+
* - H: (8 bit) humidity
20+
* - S: (8 bit) wind speed
21+
* - D: (4 bit) wind direction
22+
* - F: (4 bit) flags of (?B??), B is battery good indication
23+
* - X: (8 bit) checksum
24+
* - R: (8 bit) repeat counter
25+
*/
26+
27+
static const SubGhzBlockConst ws_protocol_emose601x_const = {
28+
.te_short = 260,
29+
.te_long = 800,
30+
.te_delta = 100,
31+
.min_count_bit_for_found = 24,
32+
};
33+
34+
#define MAGIC_HEADER 0xaaa583
35+
36+
struct WSProtocolDecoderEmosE601x {
37+
SubGhzProtocolDecoderBase base;
38+
39+
SubGhzBlockDecoder decoder;
40+
WSBlockGeneric generic;
41+
uint64_t upper_decode_data;
42+
};
43+
44+
struct WSProtocolEncoderEmosE601x {
45+
SubGhzProtocolEncoderBase base;
46+
47+
SubGhzProtocolBlockEncoder encoder;
48+
WSBlockGeneric generic;
49+
};
50+
51+
typedef enum {
52+
EmosE601xDecoderStepReset = 0,
53+
EmosE601xDecoderStepCheckPreamble,
54+
EmosE601xDecoderStepSaveDuration,
55+
EmosE601xDecoderStepCheckDuration,
56+
} EmosE601xDecoderStep;
57+
58+
const SubGhzProtocolDecoder ws_protocol_emose601x_decoder = {
59+
.alloc = ws_protocol_decoder_emose601x_alloc,
60+
.free = ws_protocol_decoder_emose601x_free,
61+
62+
.feed = ws_protocol_decoder_emose601x_feed,
63+
.reset = ws_protocol_decoder_emose601x_reset,
64+
65+
.get_hash_data = ws_protocol_decoder_emose601x_get_hash_data,
66+
.serialize = ws_protocol_decoder_emose601x_serialize,
67+
.deserialize = ws_protocol_decoder_emose601x_deserialize,
68+
.get_string = ws_protocol_decoder_emose601x_get_string,
69+
};
70+
71+
const SubGhzProtocolEncoder ws_protocol_emose601x_encoder = {
72+
.alloc = NULL,
73+
.free = NULL,
74+
75+
.deserialize = NULL,
76+
.stop = NULL,
77+
.yield = NULL,
78+
};
79+
80+
const SubGhzProtocol ws_protocol_emose601x = {
81+
.name = WS_PROTOCOL_EMOSE601X_NAME,
82+
.type = SubGhzProtocolWeatherStation,
83+
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
84+
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
85+
86+
.decoder = &ws_protocol_emose601x_decoder,
87+
.encoder = &ws_protocol_emose601x_encoder,
88+
};
89+
90+
void* ws_protocol_decoder_emose601x_alloc(SubGhzEnvironment* environment) {
91+
UNUSED(environment);
92+
WSProtocolDecoderEmosE601x* instance = malloc(sizeof(WSProtocolDecoderEmosE601x));
93+
instance->base.protocol = &ws_protocol_emose601x;
94+
instance->generic.protocol_name = instance->base.protocol->name;
95+
return instance;
96+
}
97+
98+
void ws_protocol_decoder_emose601x_free(void* context) {
99+
furi_assert(context);
100+
WSProtocolDecoderEmosE601x* instance = context;
101+
free(instance);
102+
}
103+
104+
void ws_protocol_decoder_emose601x_reset(void* context) {
105+
furi_assert(context);
106+
WSProtocolDecoderEmosE601x* instance = context;
107+
instance->decoder.parser_step = EmosE601xDecoderStepReset;
108+
}
109+
110+
static bool ws_protocol_emose601x_check(WSProtocolDecoderEmosE601x* instance) {
111+
uint8_t msg[] = {
112+
instance->upper_decode_data >> 48,
113+
instance->upper_decode_data >> 40,
114+
instance->upper_decode_data >> 32,
115+
instance->upper_decode_data >> 24,
116+
instance->upper_decode_data >> 16,
117+
instance->upper_decode_data >> 8,
118+
instance->upper_decode_data,
119+
instance->decoder.decode_data >> 56,
120+
instance->decoder.decode_data >> 48,
121+
instance->decoder.decode_data >> 40,
122+
instance->decoder.decode_data >> 32,
123+
instance->decoder.decode_data >> 24,
124+
instance->decoder.decode_data >> 16};
125+
126+
uint8_t sum = subghz_protocol_blocks_add_bytes(msg, 13);
127+
return (sum == ((instance->decoder.decode_data >> 8) & 0xff));
128+
}
129+
130+
/**
131+
* Analysis of received data
132+
* @param instance Pointer to a WSBlockGeneric* instance
133+
*/
134+
static void ws_protocol_emose601x_extract_data(WSProtocolDecoderEmosE601x* instance) {
135+
instance->generic.id = (instance->upper_decode_data >> 24) & 0xff;
136+
instance->generic.battery_low = (instance->decoder.decode_data >> 10) & 1;
137+
instance->generic.btn = WS_NO_BTN;
138+
int16_t temp = (instance->decoder.decode_data >> 40) & 0xfff;
139+
/* Handle signed data */
140+
if(temp & 0x800) {
141+
temp |= 0xf000;
142+
}
143+
instance->generic.temp = (float)temp / 10.0;
144+
instance->generic.humidity = (instance->decoder.decode_data >> 32) & 0xff;
145+
instance->generic.channel = (instance->decoder.decode_data >> 52) & 0x03;
146+
instance->generic.data = (instance->decoder.decode_data >> 16);
147+
}
148+
149+
void ws_protocol_decoder_emose601x_feed(void* context, bool level, uint32_t duration) {
150+
furi_assert(context);
151+
WSProtocolDecoderEmosE601x* instance = context;
152+
153+
switch(instance->decoder.parser_step) {
154+
case EmosE601xDecoderStepReset:
155+
if((level) && (DURATION_DIFF(duration, ws_protocol_emose601x_const.te_short * 7) <
156+
ws_protocol_emose601x_const.te_delta * 2)) {
157+
instance->decoder.parser_step = EmosE601xDecoderStepCheckPreamble;
158+
instance->decoder.te_last = duration;
159+
}
160+
break;
161+
162+
case EmosE601xDecoderStepCheckPreamble:
163+
if(level) {
164+
instance->decoder.te_last = duration;
165+
} else {
166+
if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_emose601x_const.te_short * 7) <
167+
ws_protocol_emose601x_const.te_delta * 2) &&
168+
(DURATION_DIFF(duration, ws_protocol_emose601x_const.te_short) <
169+
ws_protocol_emose601x_const.te_delta)) {
170+
//Found preamble
171+
instance->decoder.parser_step = EmosE601xDecoderStepSaveDuration;
172+
instance->decoder.decode_data = 0;
173+
instance->decoder.decode_count_bit = 0;
174+
} else {
175+
instance->decoder.parser_step = EmosE601xDecoderStepReset;
176+
}
177+
}
178+
break;
179+
180+
case EmosE601xDecoderStepSaveDuration:
181+
if(level) {
182+
instance->decoder.te_last = duration;
183+
instance->decoder.parser_step = EmosE601xDecoderStepCheckDuration;
184+
} else {
185+
instance->decoder.parser_step = EmosE601xDecoderStepReset;
186+
}
187+
break;
188+
189+
case EmosE601xDecoderStepCheckDuration:
190+
if(!level) {
191+
if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_emose601x_const.te_short) <
192+
ws_protocol_emose601x_const.te_delta) &&
193+
(DURATION_DIFF(duration, ws_protocol_emose601x_const.te_long) <
194+
ws_protocol_emose601x_const.te_delta)) {
195+
subghz_protocol_blocks_add_to_128_bit(
196+
&instance->decoder, 0, &instance->upper_decode_data);
197+
instance->decoder.parser_step = EmosE601xDecoderStepSaveDuration;
198+
} else if(
199+
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_emose601x_const.te_long) <
200+
ws_protocol_emose601x_const.te_delta) &&
201+
(DURATION_DIFF(duration, ws_protocol_emose601x_const.te_short) <
202+
ws_protocol_emose601x_const.te_delta)) {
203+
subghz_protocol_blocks_add_to_128_bit(
204+
&instance->decoder, 1, &instance->upper_decode_data);
205+
instance->decoder.parser_step = EmosE601xDecoderStepSaveDuration;
206+
} else {
207+
instance->decoder.parser_step = EmosE601xDecoderStepReset;
208+
break;
209+
}
210+
211+
/* Bail out if the header doesn't match */
212+
if(instance->decoder.decode_count_bit ==
213+
ws_protocol_emose601x_const.min_count_bit_for_found) {
214+
if(instance->decoder.decode_data != MAGIC_HEADER) {
215+
instance->decoder.parser_step = EmosE601xDecoderStepReset;
216+
break;
217+
}
218+
}
219+
220+
if(instance->decoder.decode_count_bit == 120) {
221+
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
222+
if(ws_protocol_emose601x_check(instance)) {
223+
ws_protocol_emose601x_extract_data(instance);
224+
if(instance->base.callback)
225+
instance->base.callback(&instance->base, instance->base.context);
226+
}
227+
break;
228+
}
229+
} else {
230+
instance->decoder.parser_step = EmosE601xDecoderStepReset;
231+
}
232+
break;
233+
}
234+
}
235+
236+
uint8_t ws_protocol_decoder_emose601x_get_hash_data(void* context) {
237+
furi_assert(context);
238+
WSProtocolDecoderEmosE601x* instance = context;
239+
return subghz_protocol_blocks_get_hash_data(
240+
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
241+
}
242+
243+
SubGhzProtocolStatus ws_protocol_decoder_emose601x_serialize(
244+
void* context,
245+
FlipperFormat* flipper_format,
246+
SubGhzRadioPreset* preset) {
247+
furi_assert(context);
248+
WSProtocolDecoderEmosE601x* instance = context;
249+
return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
250+
}
251+
252+
SubGhzProtocolStatus
253+
ws_protocol_decoder_emose601x_deserialize(void* context, FlipperFormat* flipper_format) {
254+
furi_assert(context);
255+
WSProtocolDecoderEmosE601x* instance = context;
256+
return ws_block_generic_deserialize_check_count_bit(
257+
&instance->generic, flipper_format, ws_protocol_emose601x_const.min_count_bit_for_found);
258+
}
259+
260+
void ws_protocol_decoder_emose601x_get_string(void* context, FuriString* output) {
261+
furi_assert(context);
262+
WSProtocolDecoderEmosE601x* instance = context;
263+
furi_string_printf(
264+
output,
265+
"%s %dbit\r\n"
266+
"Code:0x%ld\r\n"
267+
"Ch:%d Bat:%d\r\n"
268+
"Temp:%3.1f C Hum:%d%%",
269+
instance->generic.protocol_name,
270+
instance->generic.data_count_bit,
271+
instance->generic.id,
272+
instance->generic.channel,
273+
instance->generic.battery_low,
274+
(double)instance->generic.temp,
275+
instance->generic.humidity);
276+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma once
2+
3+
#include <lib/subghz/protocols/base.h>
4+
5+
#include <lib/subghz/blocks/const.h>
6+
#include <lib/subghz/blocks/decoder.h>
7+
#include <lib/subghz/blocks/encoder.h>
8+
#include "ws_generic.h"
9+
#include <lib/subghz/blocks/math.h>
10+
11+
#define WS_PROTOCOL_EMOSE601X_NAME "EMOS E601x"
12+
13+
typedef struct WSProtocolDecoderEmosE601x WSProtocolDecoderEmosE601x;
14+
typedef struct WSProtocolEncoderEmosE601x WSProtocolEncoderEmosE601x;
15+
16+
extern const SubGhzProtocolDecoder ws_protocol_emose601x_decoder;
17+
extern const SubGhzProtocolEncoder ws_protocol_emose601x_encoder;
18+
extern const SubGhzProtocol ws_protocol_emose601x;
19+
20+
/**
21+
* Allocate WSProtocolDecoderEmosE601x.
22+
* @param environment Pointer to a SubGhzEnvironment instance
23+
* @return WSProtocolDecoderEmosE601x* pointer to a WSProtocolDecoderEmosE601x instance
24+
*/
25+
void* ws_protocol_decoder_emose601x_alloc(SubGhzEnvironment* environment);
26+
27+
/**
28+
* Free WSProtocolDecoderEmosE601x.
29+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
30+
*/
31+
void ws_protocol_decoder_emose601x_free(void* context);
32+
33+
/**
34+
* Reset decoder WSProtocolDecoderEmosE601x.
35+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
36+
*/
37+
void ws_protocol_decoder_emose601x_reset(void* context);
38+
39+
/**
40+
* Parse a raw sequence of levels and durations received from the air.
41+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
42+
* @param level Signal level true-high false-low
43+
* @param duration Duration of this level in, us
44+
*/
45+
void ws_protocol_decoder_emose601x_feed(void* context, bool level, uint32_t duration);
46+
47+
/**
48+
* Getting the hash sum of the last randomly received parcel.
49+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
50+
* @return hash Hash sum
51+
*/
52+
uint8_t ws_protocol_decoder_emose601x_get_hash_data(void* context);
53+
54+
/**
55+
* Serialize data WSProtocolDecoderEmosE601x.
56+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
57+
* @param flipper_format Pointer to a FlipperFormat instance
58+
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
59+
* @return status
60+
*/
61+
SubGhzProtocolStatus ws_protocol_decoder_emose601x_serialize(
62+
void* context,
63+
FlipperFormat* flipper_format,
64+
SubGhzRadioPreset* preset);
65+
66+
/**
67+
* Deserialize data WSProtocolDecoderEmosE601x.
68+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
69+
* @param flipper_format Pointer to a FlipperFormat instance
70+
* @return status
71+
*/
72+
SubGhzProtocolStatus
73+
ws_protocol_decoder_emose601x_deserialize(void* context, FlipperFormat* flipper_format);
74+
75+
/**
76+
* Getting a textual representation of the received data.
77+
* @param context Pointer to a WSProtocolDecoderEmosE601x instance
78+
* @param output Resulting text
79+
*/
80+
void ws_protocol_decoder_emose601x_get_string(void* context, FuriString* output);

weather_station/protocols/protocol_items.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const SubGhzProtocol* weather_station_protocol_registry_items[] = {
1111
&ws_protocol_auriol_th, &ws_protocol_oregon_v1,
1212
&ws_protocol_tx_8300, &ws_protocol_wendox_w6726,
1313
&ws_protocol_auriol_ahfl, &ws_protocol_kedsum_th,
14+
&ws_protocol_emose601x,
1415
};
1516

1617
const SubGhzProtocolRegistry weather_station_protocol_registry = {

weather_station/protocols/protocol_items.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
#include "wendox_w6726.h"
2222
#include "auriol_ahfl.h"
2323
#include "kedsum_th.h"
24+
#include "emos_e601x.h"
2425

2526
extern const SubGhzProtocolRegistry weather_station_protocol_registry;

0 commit comments

Comments
 (0)