/* ---------------------------- UPSCALE ESP-NOW SENSOR NODE ----------------------------- -------------------- UP-SCALES.COM ROOM VALUES FOR THE SCALE OVER ESP-NOW --------------- ---------------------------------------------------------------------------------------- AN ESP32 NEXT TO YOUR SCALE SENDS ROOM VALUES (TEMPERATURE, HUMIDITY, AIR PRESSURE, TWO OF YOUR OWN) TO AN UP↑SCALE SCALE OVER ESP-NOW - NO WIFI NETWORK, NO BROKER, NO SERVER. THE SCALE USES THEM FOR ROOM LIMITS, THE AIR BUOYANCY CORRECTION AND THE "env" OF ITS RECORDS. BUILT ON THE MASTERBLASTER (THE SAME KEY, ENCRYPTION AND COUNTER - PUT BOTH INTO ONE DEVICE IF YOU LIKE). ---------------------------------------------------------------------------------------- ^^^ A WORKING EXAMPLE - COPY IT AND CHANGE IT AS NEEDED! ---------------------------------------------------------------------------------------- SENSORS OF THE SCALE: https://up-scales.com/manual.html#WiFiSensors ESP-NOW DEVICES OF THE SCALE: https://up-scales.com/manual.html#WiFiPeripherals THE MASTERBLASTER (LIVE DATA, COMMANDS): https://up-scales.com/info/upscale-masterblaster.ino ALL EXAMPLES FOR YOUR IT: https://up-scales.com/hardware/integration-lims-webhook-api-examples.html#examples ---------------------------------------------------------------------------------------- (C) Johann Anderson, 2025-2026 http://farm23.es ---------------------------------------------------------------------------------------- MIT License - Copyright (c) 2025-2026 Johann Anderson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------------------------- This sketch will: Read your sensor every SEND_EVERY_MS (read_the_sensor() below). Seal the values with the key of this device (AES-128-GCM) and send them to the scale as one ESP-NOW frame of 47 bytes. Learn the address of the scale from its first frame (until then: broadcast). ---------------------------------------------------------------------------------------- 1st: Your scale needs: ---------------------------------------------------------------------------------------- 1.) ENTERPRISE (the sensors of the scale), and ESP-NOW ON with the MAC address of this device in its list (app: ESP-NOW page) - this sketch prints its MAC address at the start. Then KEY below that address: copy the line and put it in place of the UPSCALE_KEY line below (the same as for the MasterBlaster, see there). 2.) In the app: LINK -> 🌡 Sensors of the scale -> a source of the kind "ESP-NOW node", the device (1-5 = its place in the ESP-NOW list), the values you send (the same as SENSOR_MASK below) and how long a value counts. 3.) WIFI_CHANNEL below = the channel of the scale: in the WiFi network the channel of your access point (the app shows it on the ESP-NOW page and in the WiFi card; the status page of the scale says "channel"), over Bluetooth the channel of the ESP-NOW page. ---------------------------------------------------------------------------------------- The frame (to write it for another device): ---------------------------------------------------------------------------------------- [0] 0xE5 | [1-8] counter (uint64, little endian, only goes up - shared with the commands of this device) | AES-128-GCM of 22 bytes: [0] 0x53 [1] the mask of the values (bit 0 °C, 1 % RH, 2 hPa, 3 value 4, 4 value 5) [2-21] 5 x float, little endian | tag (16 bytes). Nonce (12 bytes) = 'D' 0 0 0 + counter (8); extra data (AAD) = 0xE5 'D'. The scale drops values outside -40..125 °C, 0..100 % RH and 300..1200 hPa. ---------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- */ #include #include #include #include #include "mbedtls/gcm.h" // THE KEY OF THIS DEVICE (see 1.) above): replace this line with the one the app shows const uint8_t UPSCALE_KEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // the channel of the scale (see 3.) above) #define WIFI_CHANNEL 6 // how often a frame goes out (the source on the scale: "a value counts for" longer than this) #define SEND_EVERY_MS 10000 // the values this node sends: bit 0 temperature °C | bit 1 humidity % RH | bit 2 air pressure hPa | bit 3 value 4 | bit 4 value 5 // (the example: value 4 = the temperature INSIDE the ESP32 chip - not the room! Wire a real sensor and send 0x07) #define SENSOR_MASK 0x08 // the address of the scale, if you know it (else learned from its first frame; until then: broadcast) uint8_t upscaleMAC[6] = {0, 0, 0, 0, 0, 0}; // ----------------------------------- // Your sensor // ----------------------------------- // values[0] °C, values[1] % RH, values[2] hPa, values[3] value 4, values[4] value 5 - only the ones in SENSOR_MASK count. // Return false when the sensor gave no value (nothing is sent then). // A BME280 / SHT31 / DS18B20 ...: install the library of your choice for it and read it here, e.g. with a BME280 library: // values[0] = bme.readTemperature(); values[1] = bme.readHumidity(); values[2] = bme.readPressure() / 100.0f; bool read_the_sensor(float *values) { values[3] = temperatureRead(); // (Arduino-ESP32: the chip temperature in °C) return !isnan(values[3]); } // ----------------------------------- // Encryption (the same as the MasterBlaster and the scale: AES-128-GCM, a counter that only goes up) // ----------------------------------- #define UPSCALE_WIRE 0xE5 #define UPSCALE_HEAD 9 #define UPSCALE_TAG 16 #define UPSCALE_SENSOR 0x53 #define SENSOR_PLAIN 22 static uint64_t UPSCALE_TX_COUNTER = 0; // the last counter of a frame to the scale static uint64_t UPSCALE_TX_RESERVED = 0; // saved in the flash: the counter may go up to here static uint64_t UPSCALE_RX_LAST = 0; // the last counter of the scale (to learn its address) Preferences upscalePrefs; esp_now_peer_info_t peerInfo; const uint8_t BROADCAST[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; volatile bool upscaleLearned = false; static void upscale_nonce(uint8_t *nonce, uint8_t direction, uint64_t counter) { memset(nonce, 0, 12); nonce[0] = direction; memcpy(&nonce[4], &counter, 8); } // the counter starts after everything an earlier run could have used (the scale refuses a counter it saw already) // (namespace "upscale" as in the MasterBlaster: one counter per device, also when both run in one sketch) void upscale_counter_begin() { upscalePrefs.begin("upscale", false); UPSCALE_TX_COUNTER = upscalePrefs.getULong64("ctr", 0); UPSCALE_TX_RESERVED = UPSCALE_TX_COUNTER + 1024; upscalePrefs.putULong64("ctr", UPSCALE_TX_RESERVED); upscalePrefs.end(); } // 22 bytes -> the frame for the scale (47 bytes); false = not now bool upscale_seal(const uint8_t *plain, size_t length, uint8_t *wire) { if(UPSCALE_TX_COUNTER + 1 >= UPSCALE_TX_RESERVED) { upscalePrefs.begin("upscale", false); UPSCALE_TX_RESERVED = UPSCALE_TX_COUNTER + 1024; upscalePrefs.putULong64("ctr", UPSCALE_TX_RESERVED); upscalePrefs.end(); } UPSCALE_TX_COUNTER++; uint8_t nonce[12], aad[2] = {UPSCALE_WIRE, 'D'}; upscale_nonce(nonce, 'D', UPSCALE_TX_COUNTER); wire[0] = UPSCALE_WIRE; memcpy(&wire[1], &UPSCALE_TX_COUNTER, 8); mbedtls_gcm_context gcm; mbedtls_gcm_init(&gcm); int ret = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, UPSCALE_KEY, 128); if(ret == 0) {ret = mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, length, nonce, 12, aad, 2, plain, &wire[UPSCALE_HEAD], UPSCALE_TAG, &wire[UPSCALE_HEAD + length]);} mbedtls_gcm_free(&gcm); return ret == 0; } // a frame of the scale that opens with our key -> true (only to learn its address; the MasterBlaster reads the data) bool upscale_from_scale(const uint8_t *wire, int len) { if(len <= UPSCALE_HEAD + UPSCALE_TAG || len > 300 || wire[0] != UPSCALE_WIRE) {return false;} uint64_t counter; memcpy(&counter, &wire[1], 8); if(counter <= UPSCALE_RX_LAST) {return false;} int length = len - UPSCALE_HEAD - UPSCALE_TAG; uint8_t nonce[12], aad[2] = {UPSCALE_WIRE, 'S'}; static uint8_t plain[300]; upscale_nonce(nonce, 'S', counter); mbedtls_gcm_context gcm; mbedtls_gcm_init(&gcm); int ret = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, UPSCALE_KEY, 128); if(ret == 0) {ret = mbedtls_gcm_auth_decrypt(&gcm, length, nonce, 12, aad, 2, &wire[UPSCALE_HEAD + length], UPSCALE_TAG, &wire[UPSCALE_HEAD], plain);} mbedtls_gcm_free(&gcm); if(ret != 0) {return false;} UPSCALE_RX_LAST = counter; return true; } bool add_peer(const uint8_t *mac) { memset(&peerInfo, 0, sizeof(peerInfo)); memcpy(peerInfo.peer_addr, mac, 6); peerInfo.channel = WIFI_CHANNEL; peerInfo.encrypt = false; // (the frame is sealed with the key of the device - not the ESP-NOW encryption) if(esp_now_is_peer_exist(mac)) {return true;} return esp_now_add_peer(&peerInfo) == ESP_OK; } // ----------------------------------- // Callbacks // ----------------------------------- void OnDataSent(const esp_now_send_info_t* tx_info, esp_now_send_status_t status) { Serial.println(status == ESP_NOW_SEND_SUCCESS ? " sent" : " not delivered (scale off? wrong channel?)"); } void OnDataRecv(const esp_now_recv_info_t* info, const unsigned char* data, int len) { // the first frame of the scale that opens with our key: from now on send to it directly if(!upscaleLearned && upscale_from_scale(data, len)) { memcpy(upscaleMAC, info->src_addr, 6); upscaleLearned = true; } } // ----------------------------------- // Setup + loop // ----------------------------------- void setup() { Serial.begin(115200); Serial.println("***** UPSCALE ESP-NOW sensor node *****"); upscale_counter_begin(); bool keySet = false; for(uint8_t i = 0; i < 16; i++) {if(UPSCALE_KEY[i] != 0) {keySet = true;}} if(!keySet) {Serial.println("!!! NO KEY YET: paste the UPSCALE_KEY line of this device from the app (see 1. at the top) !!!");} WiFi.mode(WIFI_STA); WiFi.disconnect(); esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE); delay(100); Serial.print("The MAC address of this device (into the ESP-NOW list of the scale): "); Serial.println(WiFi.macAddress()); if(esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } esp_now_register_send_cb(OnDataSent); esp_now_register_recv_cb(OnDataRecv); bool known = false; for(uint8_t i = 0; i < 6; i++) {if(upscaleMAC[i] != 0) {known = true;}} if(known) {upscaleLearned = true; add_peer(upscaleMAC);} add_peer(BROADCAST); } void loop() { static uint32_t last = 0; if(last != 0 && millis() - last < SEND_EVERY_MS) {delay(20); return;} last = millis() | 1; float values[5] = {0, 0, 0, 0, 0}; if(!read_the_sensor(values)) {Serial.println("no value from the sensor"); return;} // the plain frame: 0x53, the mask, 5 floats (little endian - the ESP32 is little endian) uint8_t plain[SENSOR_PLAIN]; plain[0] = UPSCALE_SENSOR; plain[1] = SENSOR_MASK; memcpy(&plain[2], values, sizeof(values)); uint8_t wire[UPSCALE_HEAD + SENSOR_PLAIN + UPSCALE_TAG]; if(!upscale_seal(plain, SENSOR_PLAIN, wire)) {Serial.println("seal failed"); return;} const uint8_t *to = BROADCAST; if(upscaleLearned) {add_peer(upscaleMAC); to = upscaleMAC;} Serial.printf("values %.2f %.2f %.2f %.2f %.2f (mask 0x%02X) -> %s\n", values[0], values[1], values[2], values[3], values[4], SENSOR_MASK, upscaleLearned ? "the scale" : "broadcast (the scale is not known yet)"); esp_now_send(to, wire, sizeof(wire)); }