Skip to content

Instantly share code, notes, and snippets.

@ebith
Last active December 31, 2020 13:36
Show Gist options
  • Save ebith/cf93f21fb8bcf465759f3711a5857b3c to your computer and use it in GitHub Desktop.
Save ebith/cf93f21fb8bcf465759f3711a5857b3c to your computer and use it in GitHub Desktop.
M5 ATOM(ESP32)でWiFiに常時接続してHTTP POSTするやつ
#define FASTLED_INTERNAL
#include <Arduino.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <FastLED.h>
#include <ArduinoJson.h>
const int NUM_LEDS = 1;
const int LED_PIN = 27;
static CRGB leds[NUM_LEDS];
const int BUTTON_PIN = 39;
bool BUTTON_STATE = false;
void setup() {
// LED
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(1);
leds[0] = CRGB::Black;
FastLED.show();
// Button
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Serial
Serial.begin(115200);
Serial.flush();
Serial.println("");
}
void post(const char* content) {
DynamicJsonDocument doc(JSON_OBJECT_SIZE(2));
doc["title"] = "ATOM Lite Wifi Test";
doc["content"] = content;
String json;
serializeJson(doc, json);
HTTPClient http;
http.begin("http://httpbin.org/post");
http.addHeader("Content-Type", "application/json");
http.POST(json);
Serial.println(http.getString());
http.end();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (digitalRead(BUTTON_PIN) == LOW && !BUTTON_STATE) {
post("The button was pressed.");
BUTTON_STATE = true;
} else if (digitalRead(BUTTON_PIN) == HIGH && BUTTON_STATE) {
BUTTON_STATE = false;
}
} else {
leds[0] = CRGB::Red;
FastLED.show();
WiFi.begin(WIFI_SSID, WIFI_PASS);
int wifiReconnectCount = 0;
while (WiFi.status() != WL_CONNECTED) {
if (wifiReconnectCount >= 200) { ESP.restart(); }
wifiReconnectCount++;
delay(100);
}
post("It's now online.");
leds[0] = CRGB::Green;
FastLED.show();
}
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:m5stick-c]
platform = espressif32
board = m5stick-c
framework = arduino
monitor_speed = 115200
lib_deps =
bblanchon/ArduinoJson@^6.17.2
fastled/FastLED@^3.3.3
build_flags =
-D WIFI_SSID='"${sysenv.WIFI_SSID}"'
-D WIFI_PASS='"${sysenv.WIFI_PASS}"'
$env:WIFI_SSID = "**********"
$env:WIFI_PASS = "**********"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment