summaryrefslogtreecommitdiff
path: root/flowerpot-device.ino
blob: 3071d73af8df96171d6eea87dc387a4b9c4a29e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * Flowerpot Device Firmware
 *
 * Copyright © 2021 Jesse Morgan <jesse@jesterpm.net>
 * 
 * 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.
 */

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <DHT.h>

#include "Seeed_mbedtls.h"

#include "config.h"


// IO Pin Configuration
#define BATTERY_SWITCH D6
#define MEASURE_SWITCH D8
#define DHT_PIN        D9
#define DHT_TYPE       DHT11

// DHT Sensor
#ifdef HAS_DHT
  DHT_Unified dht(DHT_PIN, DHT_TYPE);
#endif

// Wifi
ESP8266WiFiMulti WiFiMulti;

// HMAC Context
mbedtls_md_context_t ctx;
const mbedtls_md_type_t MD_TYPE = MBEDTLS_MD_SHA256;
#define HMAC_SIZE_BYTES 32
const char* HEX_BYTE = "0123456789ABCDEF";

// The length of the secret key defined in config.h
#define SECRET_KEY_LEN sizeof(SECRET_KEY) - sizeof(SECRET_KEY[0])

// Generate an HMAC signature for a payload.
const char* generate_signature(const char* payload, size_t len) {
  byte hmacResult[HMAC_SIZE_BYTES];
  static char hexstring[2 * HMAC_SIZE_BYTES + 1];

  // Generate the HMAC
  mbedtls_md_hmac_starts(&ctx, (const unsigned char *) SECRET_KEY, SECRET_KEY_LEN);
  mbedtls_md_hmac_update(&ctx, (const unsigned char *) payload, len);
  mbedtls_md_hmac_finish(&ctx, hmacResult);

  // Encode as a hex string
  for (int i = 0; i < HMAC_SIZE_BYTES; i++) {
    hexstring[2 * i]     = HEX_BYTE[(hmacResult[i] >> 4) & 0x0F];
    hexstring[2 * i + 1] = HEX_BYTE[hmacResult[i] & 0xF];
  }
  hexstring[2 * HMAC_SIZE_BYTES] = 0;
  
  return hexstring;
}

// Read the ADC and return a value between 0 and 1.
float read_value(int pin) {
  pinMode(A0, INPUT);
  
  // Connect load to the ADC
  digitalWrite(pin, HIGH);

  // Give things a second to normalize
  delay(100);
  
  int raw;
  // The ADC will return a value from 0 to 1024,
  // corresponding to a voltage range between 0 and 1 volts.
  raw = analogRead(A0);

  // Disconnect load from the ADC
  digitalWrite(pin, LOW);

  return raw / 1024.0;
}

  
void setup() {
  Serial.begin(115200);
  Serial.println("\n\n");
  
  // Initialize the output variables as outputs
  pinMode(LED_BUILTIN, OUTPUT);
  #ifdef HAS_BATTERY
    pinMode(BATTERY_SWITCH, OUTPUT);
  #endif
  pinMode(MEASURE_SWITCH, OUTPUT);
  
  // Set outputs to LOW
  digitalWrite(LED_BUILTIN, LOW);
  #ifdef HAS_BATTERY
    digitalWrite(BATTERY_SWITCH, LOW);
  #endif
  digitalWrite(MEASURE_SWITCH, LOW);

  #ifdef HAS_DHT
    // Initialize DHT sensor
    dht.begin();
  #endif

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  Serial.printf("Hello, my name is %x, you killed my father...\n", system_get_chip_id());

  Serial.printf("Key len %d\n", SECRET_KEY_LEN);

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(WIFI_NETWORK, WIFI_PASSWORD);

  // Prepare the HMAC context
  mbedtls_md_init(&ctx);
  mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MD_TYPE), 1);
}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    #ifdef USE_TLS
      WiFiClientSecure client;
      client.setFingerprint(FINGERPRINT);
    #else
      WiFiClient client;
    #endif
   
    HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    digitalWrite(LED_BUILTIN, LOW);
    if (http.begin(client, ENDPOINT)) {  // HTTP
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");

      Serial.print("[HTTP] PUT...\n");
      // start connection and send HTTP header

      #ifdef HAS_BATTERY
        // Get current battery state.
        Serial.println("Reading battery...");
        float battery_value = read_value(BATTERY_SWITCH);
      #else
        float battery_value = 1.0;
      #endif

      float value, r;

      #ifdef TEST_SENSOR
      while (1) {
      #endif

      // Get the current resistance
      Serial.println("Reading R...");
      value = read_value(MEASURE_SWITCH);
      Serial.printf("Raw measure value = %f\n", value);
      value = value * 1024.0 / 938.0;
      Serial.printf("Calibrated measure value = %f\n", value);
      r = 320000.0 / value - 320000.0;
      Serial.printf("R = %f\n", r);

      #ifdef TEST_SENSOR
      delay(1000);
      }
      #endif

      #ifdef HAS_DHT
        sensors_event_t event;
        
        dht.temperature().getEvent(&event);
        float temp = event.temperature;
        
        dht.humidity().getEvent(&event);
        float relative_humidity = event.relative_humidity;
      #endif

      // Payload
      const size_t payload_length = 1024;
      char payload[payload_length];
      int len = snprintf(
          payload,
          payload_length,
          "battery_value=%0.2f&"
            "device_id=%08x&"
            "value=%0.2f&"
            #ifdef HAS_DHT
            "temperature_value=%0.2f&"
            "relative_humidity_value=%0.2f&"
            #endif
            "signature=",
          battery_value,
          system_get_chip_id(),
          r
          #ifdef HAS_DHT
          , temp
          , relative_humidity
          #endif
          );

      if (len + 65 <= payload_length) {
        const char* signature = generate_signature(payload, len - 11);
        strncpy(payload + len, signature, 65);
      } else {
        Serial.printf("Overflow! '%s'\n", payload);
      }
        
      Serial.printf("Putting payload '%s'\n", payload);
      
      int httpCode = http.PUT(payload);

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] PUT... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] PUT... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.printf("Nighty night.\n");
    #ifdef HAS_BATTERY
      ESP.deepSleep(3600 * 1000000);
    #else
      delay(900000);
    #endif
  }
}