« Arduinoで作ろう(47) GPSモジュールで現在地の緯度・経度を取得 | トップページ | Arduinoで作ろう(49) BLEでリモートLチカ »

2020年4月22日 (水)

Arduinoで作ろう(48) GPSのデータをAmbientに投げる

20200422

 前回の装置をテザリングでスマホにつないで,GPSモジュールで得られた緯度・経度のデータを1分おきにAmbientに投げてみました。
上のがクルマに積んで近所を一周してできた地図。Ambientはこんなこともできるんですね。おもしろいなあ。

//ESP32 + GPSモジュール NEO-6M → Ambient

#include <WiFi.h>
#include "Ambient.h"
#include "TinyGPS++.h" 
#include "HardwareSerial.h"
#include "Wire.h"
#include "SSD1306.h"
const char* ssid = "無線LANのSSID";
const char* password = "パスワード";
WiFiClient client;
unsigned int channelId = 20050; // あなたのチャネルID
const char* writeKey = "123456789abcde";//あなたのライトキー
Ambient ambient;
SSD1306 display(0x3c, 21, 22); //I2Cアドレス0x3c,SDA->GPIO21,SCL->GPIO22
TinyGPSPlus gps;
HardwareSerial serial2(2); //ESP32_GPIO16->GPS_TX , ESP32_GPIO17->GPS_RX

void setup() {
  display.init();
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  Serial.begin(9600);
  Serial.println("Start GPS... ");
  display.clear();
  display.drawString(0,0,"Start GPS... ");
  display.display();
  serial2.begin(9600);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  ambient.begin(channelId, writeKey, &client);
}

void loop() {
  if (serial2.available()){
  char c = serial2.read();
  gps.encode(c);
    if (gps.location.isUpdated()) {
     digitalWrite(2, HIGH);
     Serial.print("LAT: "); Serial.println(gps.location.lat(), 9);
     Serial.print("LONG: "); Serial.println(gps.location.lng(), 9);
     display.clear();
     display.drawString(0,0,String(gps.time.hour()+9)+":"+String(gps.time.minute())+":"+String(gps.time.second())+" JST");
     display.drawString(0,20,"LAT : "+String(gps.location.lat(),9));
     display.drawString(0,35,"LNG : "+String(gps.location.lng(),9));
     display.drawString(0,50,"ALT : "+String(gps.altitude.meters())+"m");
     display.display();
    } else{
      digitalWrite(2, LOW);
   }
 }
 if (int(millis()/1000%60) == 0){    //1分ごとに
  char buf[16];
  ambient.set(1, gps.altitude.meters());  // 標高をデータ1に
  dtostrf(gps.location.lat(), 12, 8, buf);
  ambient.set(9, buf);  // 緯度をデータ9に
  dtostrf(gps.location.lng(), 12, 8, buf);
  ambient.set(10, buf);  //経度をデータ10に
  ambient.send(); 
  }
}

| |

« Arduinoで作ろう(47) GPSモジュールで現在地の緯度・経度を取得 | トップページ | Arduinoで作ろう(49) BLEでリモートLチカ »

Arduino」カテゴリの記事

ESP8266 , ESP32」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)


コメントは記事投稿者が公開するまで表示されません。



« Arduinoで作ろう(47) GPSモジュールで現在地の緯度・経度を取得 | トップページ | Arduinoで作ろう(49) BLEでリモートLチカ »