# Mcu digital input example

**URL:** <https://discuss.blues.com/t/mcu-digital-input-example/311>\
**Category:** Hardware\
**Created:** [June 27, 2021, 11:10pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311 "2021-06-27T23:10:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 27, 2021, 11:10pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/1 "2021-06-27T23:10:47Z")

</div>

Is there an example using digital inputs from the esp32 ? I do not want to use the aux 1-4.  
Thank you fred

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 28, 2021, 5:55pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/2 "2021-06-28T17:55:17Z")

</div>

Hi @fwarden thanks for the post, and welcome to the community!

I would be happy to help, but will need a bit more info from you. Are you looking for examples of how to read from digital inputs on the ESP32 in firmware and separate from any interaction with the Notecard? As in, is this a general ESP32 question and not a Blues Notecard question? Also, is this from esp\_idf or an ESP32 in the Arduino IDE?

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 28, 2021, 6:10pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/3 "2021-06-28T18:10:09Z")

</div>

Brandon,

I’m using the Adafruit feather blues wireless development kit and trying to using the digital inputs of the feather to send info the notehub. So digital input 26 would tell me that a door is open .

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 28, 2021, 6:25pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/4 "2021-06-28T18:25:37Z")

</div>

ok, thanks for the context. If I understand what you are doing correctly, something like this should get you what you need. Note that this is a general sketch and you’ll want to adapt it for your use:

```auto
#include <Notecard.h>
#include <Wire.h>

#define DOOR 26
#define PRODUCT "<your_product_uid>"

Notecard notecard;

void setup() {
  //start serial connection
  Serial.begin(9600);
  delay(2500);

  // Start a Notecard Connection
  Wire.begin();
  notecard.begin();
  
  pinMode(DOOR, INPUT);

  J *req = notecard.newRequest("hub.set");
  JAddStringToObject(req, "product", PRODUCT);
  notecard.sendRequest(req);
}

void loop() {
  int doorVal = digitalRead(DOOR);

  if (doorVal == HIGH) {
    Serial.println("Door is Open...");

    J *req = notecard.newRequest("note.add");
    if (req != NULL) {
      JAddBoolToObject(req, "sync", true);
      J *body = JCreateObject();
      if (body != NULL) {
        JAddStringToObject(body, "door_state", "open");
        JAddItemToObject(req, "body", body);
      }
      notecard.sendRequest(req);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 28, 2021, 6:41pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/5 "2021-06-28T18:41:34Z")

</div>

Brandon,  
I will give this a shot tonight it looks like what i am trying to do.  
Thank you  
Fred

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 6:49pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/6 "2021-06-29T18:49:56Z")

</div>

Brandon,  
I tried the example and it works great on the serial port but does not show up on notehub.  
thank you  
Fred

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 6:58pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/7 "2021-06-29T18:58:04Z")

</div>

Brandon,  
see the attached photo of my screenshot

```auto
![Notehub _ Events|353x500](upload://hGMONGilUXwN7xSJRtXkQvGWGLr.jpeg)

```

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 29, 2021, 6:59pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/8 "2021-06-29T18:59:52Z")

</div>

Hey @fwarden can you re-upload that screenshot? It’s not showing up. Also, please post your Arduino sketch and I’ll take a look.

Brandon

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 7:07pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/9 "2021-06-29T19:07:07Z")

</div>

```auto
#include <Notecard.h>
#include <Wire.h>

#define DOOR 26
#define myProductID "com.xxxxxxxxxxxxxxxxxx"

Notecard notecard;

void setup() {
  //start serial connection
  Serial.begin(9600);
  delay(2500);
  

  // Start a Notecard Connection
  Wire.begin();
  notecard.begin();
  
  pinMode(DOOR, INPUT_PULLUP);

  J *req = notecard.newRequest("hub.set");
  JAddStringToObject(req, "product", myProductID);
  
  JAddStringToObject(req, "mode", "continuous");
  JAddNumberToObject(req, "outbound", 0);
  notecard.sendRequest(req);
}

void loop() {
  int doorVal = digitalRead(DOOR);

  if (doorVal == HIGH) {
   // Serial.println("Door is Open...");

    J *req = notecard.newRequest("note.add");
    if (req != NULL) {
      JAddBoolToObject(req, "sync", true);
      J *body = JCreateObject();
      if (body != NULL) {
        JAddStringToObject(body, "door_state", "open");
        JAddItemToObject(req, "body", body);
      }
      notecard.sendRequest(req);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 7:08pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/10 "2021-06-29T19:08:56Z")

</div>

![Notehub _ Events 1](https://us1.discourse-cdn.com/flex020/uploads/blues/original/1X/f95312a1374b0805cb288ddf3f583881ec44b036.jpeg)

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 29, 2021, 7:13pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/11 "2021-06-29T19:13:53Z")

</div>

Great thanks. Two things:

1. You don’t need to set `outbound` to 0 in the `hub.set` as the `sync:true` in `note.add` will issue an immediate sync, regardless of the outbound rule.
2. Right after `notecard.begin()` can you add the following?

```auto
notecard.setDebugOutputStream(Serial);

```

This will output all of the Notecard requests to USB serial so you can make sure notes are actually being added to the Notecard. Once you’ve done that, re-run with the serial terminal open and let me know what you see.

Thanks,

Brandon

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 7:17pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/12 "2021-06-29T19:17:17Z")

</div>

15:15:39.926 → {“req”:“note.add”,“sync”:true,“body”:{“door\_state”:“open”}}  
15:15:39.992 → {“err”:“error adding note: body fields not found in template: door\_state {template-incompatible}”}

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 29, 2021, 7:21pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/13 "2021-06-29T19:21:26Z")

</div>

Ah, you’re using Note templates. You’ll need to either [define `door_state` in your template](https://dev.blues.io/notecard/notecard-walkthrough/low-bandwidth-design/#modifying-a-template), or [clear it out](https://dev.blues.io/notecard/notecard-walkthrough/low-bandwidth-design/#clearing-a-template).

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 7:37pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/14 "2021-06-29T19:37:48Z")

</div>

Brandon,  
Where did the note template derive from i do not see it in the arduino sketch?

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 8:12pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/15 "2021-06-29T20:12:09Z")

</div>

Brandon,  
i did a reset on the notecard board and re uploaded my sketch and this is what i get , but it does not show up on notehub.

16:10:37.724 → Door is Open…  
16:10:37.757 → Door is Open…  
16:10:37.757 → Door is Open…

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 8:29pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/16 "2021-06-29T20:29:59Z")

</div>

Brandon,  
Using the Example1\_notecard basic i get this  
{“req”:“note.add”,“sync”:true,“body”:{“temp”:29.625,“voltage”:4.92919,“count”:16}}  
{“err”:“error adding note: body fields not found in template: temp voltage count {template-incompatible}”}  
is there an issue with my notecard?

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 29, 2021, 9:00pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/17 "2021-06-29T21:00:24Z")

</div>

@fwarden it’s not an issue with the Notecard per se, but your Notecard is configured to use templates on the default Notefile. Just out of curiosity, did you:

- Get this Notecard from someone else who had used it previously?
- Send API requests to the Notecard from the web serial terminal before starting your ESP32 application?

It’s possible that someone else configured the Notecard to use templates, or you did so previously. Either way, you can clear this by connecting to the Notecard via the [Notecard Playground](https://dev.blues.io/notecard-playground/) and sending the following request:

```auto
{"req":"note.template","file":"data.qo"}

```

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 9:05pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/18 "2021-06-29T21:05:25Z")

</div>

Brandon  
I purchased this direct from blues wireless and I may have done it as you stated I will try your answer  
Thank you  
Fred

---

<div class="post-metadata">

**Author:** ![fwarden](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@fwarden](https://discuss.blues.com/u/fwarden)\
**Post date:** [June 29, 2021, 10:16pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/19 "2021-06-29T22:16:55Z")

</div>

Brandon,  
I am not doing well with this i sent the {“req”:“note.template”,“file”:“data.qo”} and still no matter my sketch will not send data tot the notehub so i tried the example 5 usingTemplates it begins to send data to the notehub and then stops.

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [June 29, 2021, 10:22pm UTC](https://discuss.blues.com/t/mcu-digital-input-example/311/20 "2021-06-29T22:22:54Z")

</div>

Ok, let’s just get the Notecard in a factory fresh state. Try this and then re-run your sketch.

```auto
{"req":"card.restore","delete":true}

```

Make sure to user serial debugging and if it works, you should see a response from the notecard like

```auto
{“total”:1}

```

After a note.add

[Next page](https://discuss.blues.com/t/mcu-digital-input-example/311.md?page=2)
