LoginSignup
4
4

M5ATOM LiteでHTTP POSTするシンプルなサンプルコード

Last updated at Posted at 2021-01-22

はじめに

M5Atom Lite の起動時に 1 度だけ HTTP POST するシンプルなサンプルコードです。
IMG_1063 (2).JPG
コードは tmitsuoka0423/m5atom-lite-http-post-simple-sample で公開しています。
M5Stack などの他の M5 系のデバイスでも同じコードで動くと思います。

動作確認には http://httpbin.org/post を利用します。

準備

サンプルコード

#define FASTLED_INTERNAL

#include <M5Atom.h>
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

void post() {
  HTTPClient http;
  http.begin("http://httpbin.org/post");
  http.addHeader("Content-Type", "application/json");
  http.POST("{\"title\": \"test\"}");
  Serial.println(http.getString());
  http.end();
}

void setup() {
  M5.begin();
  Serial.begin(115200);

  Serial.print("WiFi connecting.");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }

  post();
}

void loop() { /* 起動時のみPOSTするのでここは何も書かない */ }

動作確認

M5ATOM Lite に電源を入れます。

21:21:34.800 -> ets Jun  8 2016 00:22:57
21:21:34.800 -> 
21:21:34.800 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
21:21:34.800 -> configsip: 188777542, SPIWP:0xee
21:21:34.800 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
21:21:34.800 -> mode:DIO, clock div:1
21:21:34.800 -> load:0x3fff0018,len:4
21:21:34.800 -> load:0x3fff001c,len:1044
21:21:34.800 -> load:0x40078000,len:8896
21:21:34.800 -> load:0x40080400,len:5816
21:21:34.800 -> entry 0x400806ac
21:21:35.174 -> WiFi connecting.....{
21:21:39.147 ->   "args": {}, 
21:21:39.147 ->   "data": "{\"title\": \"test\"}", 
21:21:39.147 ->   "files": {}, 
21:21:39.147 ->   "form": {}, 
21:21:39.147 ->   "headers": {
21:21:39.147 ->     "Accept-Encoding": "identity;q=1,chunked;q=0.1,*;q=0", 
21:21:39.147 ->     "Content-Length": "17", 
21:21:39.147 ->     "Content-Type": "application/json", 
21:21:39.147 ->     "Host": "httpbin.org", 
21:21:39.147 ->     "User-Agent": "ESP32HTTPClient", 
21:21:39.147 ->     "X-Amzn-Trace-Id": "Root=1-600ac351-64fc92271caf27324b271307"
21:21:39.194 ->   }, 
21:21:39.194 ->   "json": {
21:21:39.194 ->     "title": "test"   <-- 送ったデータがレスポンスに入ってる!
21:21:39.194 ->   }, 
21:21:39.194 ->   "origin": "153.209.83.152", 
21:21:39.194 ->   "url": "http://httpbin.org/post"
21:21:39.194 -> }

M5ATOM Lite から POST できました!
次は HTTPS の通信にチャレンジします。

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4