Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Akshayan Sinha
Published © LGPL

Getting Started with ESP32

Got yourself a new ESP32? This article will help you get started with Arduino, MicroPython and ESP-IDF

BeginnerProtip2 hours4,013
Getting Started with ESP32

Things used in this project

Hardware components

Adafruit HUZZAH32 – ESP32 Feather Board
Adafruit HUZZAH32 – ESP32 Feather Board
×1

Software apps and online services

Arduino IDE
Arduino IDE
MicroPython
MicroPython
VS Code
Microsoft VS Code
Espressif ESP-IDF
Thonny

Story

Read more

Code

Mini Project 1 (Arduino)

C/C++
#define T5 12
#define T6 14
#define T7 27

int A, B, C;
const int buzzer = 32;
const int freq = 3000;
const int channel = 0;
const int res = 8;

void setup() {
ledcSetup(channel, freq, res);
ledcAttachPin(buzzer, channel);
ledcAttachPin(buzzer, channel);
pinMode(12, INPUT);
pinMode(14, INPUT);
pinMode(27, INPUT);
Serial.begin(115200);
}

void loop() {
A = touchRead(T5);
B = touchRead(T6);
C = touchRead(T7);
if (A < 30)
{
ledcWriteTone(channel, 500);
Serial.print("A: ");
Serial.println(A);
delay(500);
}
if (B < 30)
{
ledcWriteTone(channel, 1500);
Serial.print("B: ");
Serial.println(B);
delay(500);
}
if (C < 30)
{
ledcWriteTone(channel, 4000);
Serial.print("C: ");
Serial.println(C);
delay(500);
}
}

Mini Project 2 (Arduino)

C/C++
#include <WiFi.h>
#include "HTTPClient.h"
#include <ArduinoJson.h>
const char* ssid = "wifi_name";
const char* password = "wifi_password";

String serverName = "https://uselessfacts.jsph.pl/random.json?language=en";
String oldTEXT="null";

void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
//-------------------- WIFI INIT --------------------------------
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
//--------------------------------------------------------------
}

void loop() {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName;
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
//Serial.print("HTTP Response code: ");
//Serial.println(httpResponseCode);
String payload = http.getString();
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
String TEXT = doc["text"];
digitalWrite(2, HIGH);
if (TEXT != oldTEXT){
Serial.println(TEXT);
}
oldTEXT = TEXT;
digitalWrite(2, LOW);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(2000);
}
else {
Serial.println("WiFi Disconnected");
}
}

Mini Project 3 (MicroPython)

MicroPython
import esp32,time
tempF = esp32.raw_temperature()
tempC = (tempF - 32)*0.5555
while (True):
print("Current Temp of ESP32 is", tempC)
time.sleep(3)

Mini Project 4 (MicroPython)

MicroPython
import network
import urequests, time
import ujson
from machine import Pin
p0 = Pin(18, Pin.OUT)
p1 = Pin(22, Pin.OUT)
wlan = network.WLAN(network.STA_IF)
def do_connect():
    wlan.active(True)
    if not wlan.isconnected():
        print('Connecting to Network...')
        wlan.connect('wifi_name', 'wifi_password')
        while not wlan.isconnected():
            p1.off()
            p1.on()
            pass
    p1.off()
    p0.off()
    time.sleep(0.2)
    p0.on()
    print('Connected (Local IP , Subnet, Gateway):')
    print(wlan.ifconfig())

do_connect()

while wlan.isconnected():
    try:
        wlan.connect('wifi_name', 'wifi_password')
        p0.off()
        time.sleep(1)
        res = urequests.get(url='https://dog-facts-api.herokuapp.com/api/v1/resources/dogs?number=1')
        fact = ujson.loads(res.text)
        print("Fact:",fact[0]['fact'])
        #print(res.text)
        p1.off()
        p0.on()
        time.sleep(5)
    except Exception as e:
        print("Error Occured")
        time.sleep(5)
        p0.off()


        p1.on()
        print(e)  

Credits

Akshayan Sinha

Akshayan Sinha

28 projects β€’ 27 followers
Robotics Software Engineer with a makermind.

Comments