North East Blog Directory

June 17, 2017

Transitlab

Hall effect sensor in the ESP32 with Arduino ide

Learning about the esp32 again. When looking at the adc documentation at this interested me

It is also possible to read the internal hall effect sensor via ADC1.

Digging in the code for the arduino-esp32 we find that we can read it using hallRead() from arduino-esp32/cores/esp32/esp32-hal-adc.h

Might be useful to someone.

//Simple sketch to access the internal hall effect detector on the esp32.
//values can be quite low. 
//Brian Degger 

int val = 0;

void setup() {
  Serial.begin(9600);
    }

void loop() {
  // put your main code here, to run repeatedly:
  val = hallRead();
  // print the results to the serial monitor:
  //Serial.print("sensor = ");
  Serial.println(val);//to graph
}

Strong magnets are needed to get a good response.

by Brian at June 17, 2017 03:05 PM

ESP32 + analogRead() on Arduino

It works fine. I had it confused with analogWrite(). You can find the pins for analogRead in https://esp-idf.readthedocs.io/en/v2.0/api/peripherals/adc.html

On the esp32 devkit v1  GPIO’s 32, 33, 34, 35 are broken out.

/*
  Analog input, serial output on esp32

 Reads an analog input pin, prints the results to the serial monitor.

Cut down from AnalogInOutSerial by Tom Igoe for Arduino.
Modified for use with esp32 
This example code is in the public domain.
 */
// These constants won't change.  They're used to give names
// to the pins used:
const int analogPin = 32;  // Analog input pin , ours is connected to YL-83

int sensorValue = 0;        // value read from the adc
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogPin);
  // print the results to the serial monitor:
  //Serial.print("sensor = ");
  Serial.println(sensorValue);


  // wait 1 seconds before the next loop to minimise corrosion on the rain detector.
  delay(1000);
}

 

by Brian at June 17, 2017 01:05 PM

June 16, 2017

Transitlab

Mongoose OS on esp32

Heard about mongoose OS from http://tech.scargill.net/mongoose-os/ . Scargills blog is always worth a read if you are interested in sbc, iot, home automation and node-red.

Mongoose OS is an open source Operating System for the IOT, supporting ESP32, ESP8266, STM32, TI CC3200 (Amazon AWS IoT integrated). Code in C or JavaScript.

This sounded interesting, and seemed a middle way between the arduino and esp32-IDF routes of programing the esp32. Also as it is built with IOT in mind is a good place to learn a bit more.

Installed following https://mongoose-os.com/software.html

Tried

~/.mos/bin/mos

This launches a gui in a webpage. I could select the usb port that the esp32 was on, but when i tried to flash mongoose onto it I got:

E0614 19:52:33.535553   15390 ui.go:453] Calling: FS.ListExt
Server Error: {"error":"Device is not connected"}

The solution is to flash from the command line:

~/.mos/bin/mos flash mos-esp32

Much happier:

Fetching https://mongoose-os.com/downloads/mos-esp32.zip...
Loaded mjs_base/esp32 version 1.0 (20170614-125632/???)
Using port /dev/ttyUSB0
Opening /dev/ttyUSB0...
Connecting to ESP32 ROM, attempt 1 of 10...
  Connected
Running flasher @ 460800...
  Flasher is running
Flash size: 4194304, params: 0x0220 (dio,32m,40m)
Deduping...
   16320 @ 0x1000 -> 12288
   16384 @ 0x9000 -> 8192
  819824 @ 0x10000 -> 126976
  131072 @ 0x190000 -> 16384
Writing...
   12288 @ 0x1000
    4096 @ 0x8000
    8192 @ 0x9000
    8192 @ 0xd000
    4096 @ 0x10000
  122880 @ 0x20000
   16384 @ 0x1a2000
Wrote 176128 bytes in 3.93 seconds (349.73 KBit/sec)
Verifying...
   16320 @ 0x1000
    3072 @ 0x8000
   16384 @ 0x9000
    8192 @ 0xd000
  819824 @ 0x10000
  131072 @ 0x190000
Booting firmware...
All done!

Then go back to the gui using

~/.mos/bin/mos

for final configuration (like the ssid and password).

One of the examples is a mqtt example that publishes the sys.free ram to a mqtt feed when the boot button is pressed on the esp32. With a bit of digging I found ‘api_adc.js’ that acts as a wrapper for the adc api, so I could use the YL-83 rain sensor on pin 32.

load('api_config.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_sys.js');
load('api_adc.js');
load('api_grove.js');

GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
  let topic = Cfg.get('mqtt.user') + '/feeds/rain';
  //let message = JSON.stringify(Sys.free_ram());
  let message = JSON.stringify(ADC.read(32));
  let ok = MQTT.pub(topic, message, 1);
  print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message);
}, null);

You can see the feed at after testing with drops of water.

https://io.adafruit.com/BrianDegger/feeds/rain

 

 

by Brian at June 16, 2017 04:25 PM

June 07, 2017

Transitlab

ESP32 + analogRead()

The ESP32 is the new chip-on-the-block for Internet of Things, having both wi-fi and bluetooth. I have a breakout board from doit.am. They are coming down in price and can be found for under £7. This breaks out many(but not all) of the pins. I wanted to use a temperature sensor TMP36 and use analogRead() like in https://makerhacks.com/thermometer-esp8266-arduino/. That example was for the ESP8266.

However, being relatively new, the ESP32 does not support reading an analog input using that simple analogRead() that you find in the arduino ide.

Actually, I just worked out it dosen’t support analogWrite() at  the moment, analogRead() works fine. However, it gave me a chance to learn about other environments for the esp32.

So it’s off to download and install the “Espressif IoT Development Framework” https://github.com/espressif/esp-idf
esp32

Read the docs on ADC http://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/adc.html

The ADC driver API currently only supports ADC1 (9 channels, attached to GPIOs 32-39).

The doit.am only goes to GPIO 35, so we have 4 to play with. Compiling and flashing the adc1_test, in /examples/peripherals/adc/main/adc1_test.c
ADC1_CHANNEL_4 is used, from the docs ADC1 channel 4 is GPIO32

<pre class="lang:c decode:true">/* ADC1 Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>;
#include <string.h>;
#include <stdlib.h>;
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "driver/adc.h"

#define ADC1_TEST_CHANNEL (4)

void adc1task(void* arg)
{
    // initialize ADC
    adc1_config_width(ADC_WIDTH_12Bit);
    adc1_config_channel_atten(ADC1_TEST_CHANNEL,ADC_ATTEN_11db);
    while(1){
        printf("%d\n",adc1_get_voltage(ADC1_TEST_CHANNEL));
        vTaskDelay(1000/portTICK_PERIOD_MS);
    }
}

void app_main()
{
    xTaskCreate(adc1task, "adc1task", 1024*3, NULL, 10, NULL);
}

The analog read worked, but the temperature sensor was a bit funny. So I tested it with a rain-detector, which seemed to give reasonable results.

 

 

by Brian at June 07, 2017 06:23 PM

# This site is managed by the team at SuperMondays, is hosted by the team at Consilience and is maintained on github.