M5StickCとCO2センサ(SCD30)を用いて気温、湿度、CO2のデータをGoogleスプレッドシートに記録するといったプログラムを作成中です。
いろいろなサイトを参考にして、GASとJsonを使って送信できると思ったのですができませんでした。
GASはデプロイの設定でアプリケーションとして全員がアクセスできるように設定しました。
ArduinoIDEではコンパイルと書き込みは成功するのですが、Googleスプレッドシートはデータが送信されず、シリアルモニターに「connection refused」と出て、解決策がなく困っております。
ご助言していただけると幸いです。
M5StickCPlusプログラムデータ送信部分(ArduinoIDE)
ArduinoIDE
1#include <M5StickC.h> 2#include <Adafruit_SCD30.h> 3#include <WiFi.h> 4#include <HTTPClient.h> 5#include <ArduinoJson.h> 6 7char *ssid = "*****"; // Wi-FiのSSID 8char *password = "*****"; // Wi-Fiのパスワード 9const char* published_url = "https://script.google.com/macros/s/*****/exec"; //googleスプレッドシート→拡張機能→GASでデプロイしたURL 10float temp = 0; 11float humid = 0; 12float C02 = 0; 13Adafruit_SCD30 scd30; //CSD30 を使うのに必要な関数 14 15void setup(void) { 16 M5. begin (); 17 Serial. begin (115200); 18 while (!Serial) delay (10) ; 19 WiFi. begin (ssid, password); 20 while (WiFi.status() != WL_CONNECTED) { 21 delay(1000); 22 M5. Lcd. print("."); 23 Serial.print("."); 24 } 25 M5. Lcd. print ("\r\nWiFi connected\r\nIP address: "); 26 M5. Lcd. println (WiFi. localIP()); 27 Serial.print("WiFi connected\nIP address: "); 28 Serial.println(WiFi. localIP()); 29 delay (2000) ; 30 } 31 32void loop() { 33 M5. update () ; 34 sokutei (); 35 soshin(); 36 datasave () ; 37} 38 39void sokutei (void) { 40 scd30. read() ; 41 temp = scd30. temperature; 42 humid = scd30. relative_humidity; 43 C02 = scd30.CO2; 44} 45 46void datasave (void) { 47 databox_temp [count1] = temp; 48 count1++; 49 databox_humid [count2] = humidityl; 50 count2++; 51 databox_C02 [count3] = C02; 52 count3++; 53} 54 55void soshin (void) { 56 int j = 0; 57 while (1) { 58 StaticJsonDocument<500> doc; 59 char pubMessage[256]; 60 JsonArray nameValues = doc.createNestedArray ("NAME"); 61 nameValues. add ("co2analys"); 62 63 JsonArray tempValues = doc.createNestedArray ("ТEМP"); 64 tempValues. add (databox_temp [n]); 65 JsonArray humidityValues = doc.createNestedArray ("HUMIDITY"); 66 humidityValues. add (databox_humid [n]); 67 JsonArray co2Values = doc.createNestedArray ("C02") ; 68 co2Values. add (databox_C02 [n]); 69 70 serializeJson(doc, pubMessage); 71 n++; 72 HTTPClient http; 73 Serial.print (” HTTP 通信開始/n”); 74 http. begin (published_url); 75 Serial.print (” HTTP 通信 POST ¥n"); 76 int httpCode = http.POST(pubMessage) ; 77 if (httpCode > 0) { 78 M5. Led. printf ("НТТР Response:%dIn", httpCode); 79 if (httpCode == HTTP_CODE_OK) { 80 String payload = http. getString(); 81 Serial. printIn (payload); 82 } 83 } else { 84 M5. Lcd. fillScreen (BLACK); 85 M5. Lcd. setCursor (0, 0) ; 86 M5. Lcd. println ("CAN' T TO SEND"); 87 delay (1000) ; 88 Serial. printf ("HTTP failed, error: %s/n",http.errorToString (httpCode).c_str()); 89 M5.Lcd.printf("HTTP failed, error: %s/n",http.errorToString (httpCode).c_str()); 90 break; 91 } 92 http.end (); 93 i++; 94 if ((databox_temp[n + 1] == 0) || (j == 4)) { 95 break; 96 } 97 } 98 if (databox_temp[n + 1] == 0) { //データボックス初期化 99 for (p = 0; p < 6700 ; p++ ) { 100 databox_temp [p] = 0; 101 databox_humid [p] = 0; 102 databox_C02 [p] = 0; 103 } 104 p = 0; 105 count1 = 0; //カウンタとn初期化 106 count2 = 0; 107 count3 = 0; 108 n = 0; 109 } 110}
Googleスプレッドシートプログラム(GAS)
GAS
1function doPost(e) { 2 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('シート1'); 3 var params = JSON.parse(e.postData.getDataAsString()); 4 var temp = params.TEMP; 5 var humid = params.HUMIDITY; 6 var co2 = params.C02; 7 8 9 sheet.insertRows(2,1); 10 sheet.getRange(2,1).setValue(new Date()); 11 sheet.getRange(2,2).setValue(temp); 12 sheet.getRange(2,3).setValue(humid); 13 sheet.getRange(2,4).setValue(co2); 14}
シリアルモニターの表示
connection refused
よろしくお願いします。
