次のシステムを構築するための検証です。
Grove IoT スターターキット for SORACOM で作るリモートカメラシステム
変換を行う Web 上の API
http_to_mqtt.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# http_to_mqtt.py
#
# Aug/18/2021
#
# ---------------------------------------------------------------------
import os
import sys
import json
import cgi
#
from mqtt_publish import mqtt_publish_proc
# ---------------------------------------------------------------------
if os.environ['REQUEST_METHOD'] == 'POST':
length, _ = cgi.parse_header(os.environ['CONTENT_LENGTH'])
data = sys.stdin.buffer.read(int(length))
#
json_str = data.decode("utf-8")
#
mqtt_publish_proc(json_str)
#
data_aa = {}
str_aa = json.dumps(data_aa)
#
print('Content-Type: text/json; charset=utf-8')
print("Access-Control-Allow-Origin: *\r\n")
print(str_aa)
#
# ---------------------------------------------------------------------
mqtt_publish.py
#
# mqtt_publish.py
#
# Aug/18/2021
# ------------------------------------------------------------------
import sys
import paho.mqtt.client as mqtt
MQTT_HOST = "example.com"
MQTT_TOPIC = "sample/imageTopic"
MQTT_PORT = 1883
MQTT_KEEP_ALIVE = 60
# ------------------------------------------------------------------
def on_connect(mqttc, obj, flags, rc):
sys.stderr.write("rc: " + str(rc) + "\n")
#
# ------------------------------------------------------------------
def mqtt_publish_proc(json_str):
sys.stderr.write("*** 開始 ***\n")
#
mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEP_ALIVE)
# mqttc.loop_start()
sys.stderr.write("%d\n" % len(json_str))
mqttc.publish(MQTT_TOPIC,json_str)
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
WioLTE で 画像を撮って、base64,JSON 変換を行って HTTP POST で送信するプログラム
フォルダー構造
$ tree image_post
image_post
├── camera_direct.ino
├── http_upload.ino
├── image_post.ino
└── setupLTE.ino
image_post.ino
// ---------------------------------------------------------------
/*
image_post.ino
Aug/23/2021 AM 08:00
Adafruit VC0706 TTL Serial Camera
*/
// ---------------------------------------------------------------
#include <WioLTEforArduino.h>
#include <Adafruit_VC0706.h>
#include <stdio.h>
#include <Base64.h>
#include <ArduinoJson.h>
#include <WioLTEClient.h>
#define cameraconnection Serial
// #define URL "http://15.152.30.174/redis_post/redis_post.py"
// #define URL "http://15.152.30.174/mariadb_post/image_upload.py"
#define URL "https://www2.ekzemplaro.org/mqtt/http_to_mqtt/http_to_mqtt.py"
// #define INTERVAL (9000)
#define INTERVAL (3000)
#define DATA_SIZE (512)
WioLTE Wio;
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);
String device = "00000000";
// ---------------------------------------------------------------
// [4]:
void setup()
{
setupLTE();
Wio.PowerSupplyGrove(true);
Wio.LedSetRGB(0, 0, 1);
delay(500);
Wio.LedSetRGB(0, 1, 0);
delay(500);
SerialUSB.println("*** START *** setup *** Aug/23/2021 AM 08:00 ***");
snap_upload_proc();
}
// ---------------------------------------------------------------
// [6]:
void loop() {
char buff[50];
sprintf(buff,"*** Wait Interval. %d sec ***" , INTERVAL / 1000);
SerialUSB.println(buff);
delay(INTERVAL);
snap_upload_proc();
}
// ---------------------------------------------------------------
// [4-4]:
void snap_upload_proc()
{
char str_json[8192];
camera_direct_proc(str_json);
http_upload_proc(URL,str_json);
}
// ---------------------------------------------------------------
camera_direct.ino と http_upload.ino はこちら
リモートカメラシステム (Arduino 部分)
setupLTE.ino はこちら
UDP で温度と湿度を Harvest に送る
テスト風景
こちらのプログラムで画像を表示
MQTT over WebSocket で画像を表示