Archiv der Kategorie: BitBastelei

[ICStation.com] BitBastelei #261 – GPS-Tracker mit Neo-6/7-Module und ESP8266

[ICStation.com] BitBastelei #261 - GPS-Tracker mit Neo-6/7-Module und ESP8266

(111.1 MB) 00:40:34

2017-11-05 11:00 🛈

In der letzten Woche haben wir uns angesehen, wie Satellitennavigationssysteme wie GPS funktionieren, nun wollen wir diese auch in der Praxis nutzen. Da die Berechnungen komplex sind ist es auch hier eine gute Idee spezialisierte ICs zu verwenden, welche eine einfache Schnittstelle für Mikrocontroller bereitstellen. ICStation.com bietet hierzu ein Modul auf Basis des mächtigen U-Blox Neo M6/M7 an, welches über UART angesprochen werden kann. Schauen wir mal auf das Modul, welche Optionen die Konfigurationssoftware bietet, den Aufbau des NMEA-Protokolls und bauen am Ende einen GPS-Trakcer auf Basis des ESP8266.

Links

Code

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <NMEAGPS.h>
#include <SoftwareSerial.h>

/**
 * Required libs (see manager):
 *   - SoftwareSerial
 *   - NeoGPS
 */

#define RTCMEMORYSTART 65

extern "C" {
#include "user_interface.h"
}

//WiFi
const char* ssid = "freifunk-myk.de";
const char* pass = "";

//URL of ownTracks Recorder
const char* owntracksURL = "http://adlerweb-vm-owntracks.ffmyk:8083/pub";

//User and Device for ownTracks
const char* owntracksUser   = "esp8266-1";
const char* owntracksDevice = "esp8266-1";

//Sleep between GNSS checks in minutes
//const unsigned int sleep_off = 5;
const unsigned int sleep_off = 1;

//Time for GNSS warm-up in seconds
//const unsigned int sleep_GNSS = 30;
const unsigned int sleep_GNSS = 10;

//Time to wait for 3D-Fix before reverting to 2D in seconds
const unsigned int sleep_3d  = 30;

//Time to wait for 2D-Fix before reverting to RF in seconds
const unsigned int sleep_2d  = 60;

//RF Geolocation Todo https://github.com/m0xpd/ESP8266-GeoLocation/blob/master/ESP8266_GeoLocate_2.ino

//Geo-deviation in deg-fract(?)
const unsigned int fence = 5000;

//Force update after x minutes
const unsigned int rf_force = 720;

//Time to wait for RF-Connect before giving up in seconds
const unsigned int rf_timeout = 60;

//GNSS Power Switch
#define GNSS_PWR_PIN D4
#define GNSS_PWR_ON  LOW

//Software UART for GNSS (RX, TX)
SoftwareSerial GNSSPort(D7, D8);
#define GNSS_BAUD 115200

//Hardware UART for Debug
#define DEBUG_PORT Serial
#define DEBUG_BAUD 115200

//Write all data when received
#define DEBUG_DUMP 0

NMEAGPS GNSS;
gps_fix fix;
int32_t lon=0, lat=0;
float vel=0,alt=0,cog=0;

bool delayGNSS(uint32_t dly) {
  return delayGNSS(dly, false);
}

bool delayGNSS(int32_t dly, bool direct) {
  unsigned long mstop = millis()+dly;
  bool ret = false;

  if(DEBUG_DUMP > 1) DEBUG_PORT.print(F("\nS:"));
  if(DEBUG_DUMP > 1) DEBUG_PORT.print(dly);

  do {
    if (GNSS.available( GNSSPort )) {
      fix = GNSS.read();
      ret = true;
      if(DEBUG_DUMP > 1) DEBUG_PORT.print('!');
    }
  }while(!ret && mstop > millis());

  if (fix.valid.heading) cog = fix.heading();
  if (fix.valid.speed) vel = fix.speed_kph();
  if (fix.valid.altitude) alt = fix.altitude();
  if (fix.valid.location) {
    lon = fix.longitudeL();
    lat = fix.latitudeL();
  }
  

  if(!ret) return false;
  if(!direct) {
    if(DEBUG_DUMP > 1) DEBUG_PORT.print('+');
    if(DEBUG_DUMP > 1) DEBUG_PORT.print(mstop-millis());
    delay((mstop-millis()));
  }
  return true;
}

bool getGNSS(void) {
  bool ret;
  if(delayGNSS(100, true)) {
    if(DEBUG_DUMP > 0) debugDump();
    return true;
  }
  return false;
}

bool wait_3d(void) {
  DEBUG_PORT.print(F("Waiting 3D Fix..."));
  for (unsigned int sec = 0; sec < sleep_3d; sec++) {
    if(getGNSS() && lon != 0 && lat != 0 && alt != 0) {
      DEBUG_PORT.println(F("OK"));
      return true;
    }
    Serial.print('.');
    yield();
    delayGNSS(1000);
  }
  DEBUG_PORT.println(F("NOPE"));
  return false;
}

bool wait_2d(void) {
  DEBUG_PORT.print(F("Waiting 2D Fix..."));
  for (unsigned int sec = 0; sec < sleep_2d; sec++) {
    if(getGNSS() && lon != 0 && lat != 0) {
      DEBUG_PORT.println(F("OK"));
      return true;
    }
    Serial.print('.');
    yield();
    delayGNSS(1000);
  }
  DEBUG_PORT.println(F("NOPE"));
  return false;
}

byte getBattery(void) {
  //@TODO not yet implemented
  return 100;
}

String getGeoDecimal(int32_t location) {
  unsigned long tmp1, tmp2;
  String out = "";

  if (location < 0) {
    out += '-';
    location = 0 - location;
  }

  tmp1 = location / 10000000;
  tmp2 = location - (tmp1 * 10000000);

  out += tmp1;
  out += ".";
  out += tmp2;

  return out;
}

bool getFence(void) {
  int32_t check = 0;
  DEBUG_PORT.print(F("FENCE - "));

  //Lat
  DEBUG_PORT.print(F("LAT:"));
  system_rtc_mem_read(RTCMEMORYSTART + 1, (int32_t *)check, 4);
  DEBUG_PORT.print(check);
  DEBUG_PORT.print('/');
  DEBUG_PORT.print(lat);
  if (check + fence > lat || check - fence < lat) {
    DEBUG_PORT.println('!');
    return true;
  }
  //Lon
  DEBUG_PORT.print(F(" - LON:"));
  system_rtc_mem_read(RTCMEMORYSTART + 2, (int32_t *)check, 4);
  DEBUG_PORT.print(check);
  DEBUG_PORT.print('/');
  DEBUG_PORT.print(lon);
  if (check + fence > lon || check - fence < lon) {
    DEBUG_PORT.println('!');
    return true;
  }

  DEBUG_PORT.println('_');
  return false;
}

NeoGPS::clock_t getTimestamp(void) {
  if (!fix.valid.date || !fix.valid.time) return 0;

  //This contains the seconds starting from the start of this century
  NeoGPS::clock_t seconds = fix.dateTime;

  //Guessing we're still 20xx this is the unix timestamp for 01.01.2000 00:00:00
  seconds += 946684800;

  return seconds;
}

bool rfConnect(void) {
  unsigned int rftimer;
  
  if(WiFi.status() == WL_CONNECTED) return true;
  
  DEBUG_PORT.print(F("connecting to "));
  DEBUG_PORT.println(ssid);
  if(pass == "") {
    WiFi.begin(ssid);
  }else{
    WiFi.begin(ssid, pass);
  }
  while (WiFi.status() != WL_CONNECTED && rftimer < rf_timeout) {
    delay(1000);
    DEBUG_PORT.print(".");
    rftimer++;
    yield();
  }
  if (WiFi.status() != WL_CONNECTED) {
    DEBUG_PORT.println(F("failed"));
    return false;
  }
  DEBUG_PORT.println(F("connected"));
  DEBUG_PORT.print(F("IP address: "));
  DEBUG_PORT.println(WiFi.localIP());
  return true;
}

void rfDisconnect(void) {
  /*WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();*/
}

bool rfSend(void) {
  String data;
  HTTPClient http;

  if (!rfConnect()) return false;
  if (lat == 0 || lon == 0) return false;

  http.begin(owntracksURL);

  http.addHeader(F("X-Limit-U"), owntracksUser);
  http.addHeader(F("X-Limit-D"), owntracksDevice);
  http.addHeader(F("User-Agent"), F("Adlerweb-ESP-Tracker"));
  http.addHeader(F("Content-Type"), F("application/json"));

  data  = "{\"_type\":\"location\",\"tid\":\"01\",\"conn\":\"m\",\"_cp\":true,\"batt\":";
  data += getBattery();
  data += ",\"lat\":";

  data += getGeoDecimal(lat);
  data += ",\"lon\":";
  data += getGeoDecimal(lon);

  if (cog != 0) {
    data += ",\"cog\":";
    data += cog;
  }

  if (vel != 0) {
    data += ",\"vel\":";
    data += vel;
  }

  if (alt != 0) {
    data += ",\"alt\":";
    data += alt;
  }

  data += ",\"tst\":";
  data += getTimestamp();
  data += "}";

  DEBUG_PORT.println(F("---"));
  DEBUG_PORT.println(data);
  DEBUG_PORT.println(F("---"));
  DEBUG_PORT.println(F("Send"));
  DEBUG_PORT.flush();
  http.POST(data);
  DEBUG_PORT.println(".");
  DEBUG_PORT.flush();
  http.writeToStream(&Serial);
  DEBUG_PORT.println(".");
  DEBUG_PORT.flush();
  http.end();
  DEBUG_PORT.println(F("OK"));
  DEBUG_PORT.flush();

  rfDisconnect();

  return true;
}

void debugDump() {
  DEBUG_PORT.print(F("Status: "));
  switch (fix.status) {
    case 1:
      DEBUG_PORT.println(F("Nährungswert"));
      break;
    case 2:
      DEBUG_PORT.println(F("Nur Zeit"));
      break;
    case 3:
      DEBUG_PORT.println(F("GNSS-Fix"));
      break;
    case 4:
      DEBUG_PORT.println(F("DGNSS-Fix"));
      break;
    default:
      DEBUG_PORT.println(F("Keiner"));
  }
  DEBUG_PORT.print(F("UTC: "));
  DEBUG_PORT.print(fix.dateTime.year);
  DEBUG_PORT.print("-");
  DEBUG_PORT.print(fix.dateTime.month);
  DEBUG_PORT.print("-");
  DEBUG_PORT.print(fix.dateTime.date);
  DEBUG_PORT.print(" ");
  DEBUG_PORT.print(fix.dateTime.hours);
  DEBUG_PORT.print(":");
  DEBUG_PORT.print(fix.dateTime.minutes);
  DEBUG_PORT.print(":");
  DEBUG_PORT.print(fix.dateTime.seconds);
  DEBUG_PORT.print(" - ");
  DEBUG_PORT.print(getTimestamp());
  DEBUG_PORT.print("/");
  //DEBUG_PORT.println(lasttime);

  DEBUG_PORT.print(F("Satellites: "));
  DEBUG_PORT.println(fix.satellites);

  DEBUG_PORT.print(F("Speed: "));
  DEBUG_PORT.println(vel);

  DEBUG_PORT.print(F("Heading: "));
  DEBUG_PORT.println(cog);

  DEBUG_PORT.print(F("Altitude: "));
  DEBUG_PORT.println(alt);

  DEBUG_PORT.print(F("Postition: "));
  DEBUG_PORT.print(lat);
  DEBUG_PORT.print(",");
  DEBUG_PORT.println(lon);

  DEBUG_PORT.print(getGeoDecimal(lat));
  DEBUG_PORT.print(",");
  DEBUG_PORT.println(getGeoDecimal(lon));
}

byte getBootMode(void) {
  byte bootMode = 0;
  system_rtc_mem_read(RTCMEMORYSTART, &bootMode, 1);
  DEBUG_PORT.print("BootMode: ");
  DEBUG_PORT.println(bootMode);
  return bootMode;
}


/////////////


void setup() {
  int32_t dummy = 0;

  pinMode(GNSS_PWR_PIN, OUTPUT);

  DEBUG_PORT.begin(DEBUG_BAUD);
  GNSSPort.begin(GNSS_BAUD);

  delay(2000);

  DEBUG_PORT.println( F("Adlerweb GNSS Tracker v0.1.1") );
  DEBUG_PORT.flush();

  byte bootMode = getBootMode();

  if(bootMode == 0 || bootMode > 5) { //Unknown or first boot
    DEBUG_PORT.println(F("Enabling GNSS Power"));
    digitalWrite(GNSS_PWR_PIN, GNSS_PWR_ON);
    bootMode=1;
    system_rtc_mem_write(RTCMEMORYSTART, &bootMode, 1);
    DEBUG_PORT.print(F("Sleeping for "));
    DEBUG_PORT.print(sleep_GNSS);
    DEBUG_PORT.println(F(" Seconds"));
    DEBUG_PORT.flush();
    ESP.deepSleep(sleep_GNSS * 1e6);
  }

  if(bootMode == 1) { //Waiting for GNSS 3D Fix
    if (wait_3d()) {
      bootMode = 4;
    } else {
      bootMode++;
    }
  }

  if(bootMode == 2) {//Waiting for GNSS 2D Fix
    if (wait_2d()) {
      bootMode = 4;
    } else {
      bootMode++;
    }
  }
  
  if(bootMode == 3) { //Waiting for RF Fix
    //@TODO Not implemented
    bootMode = 5;
  }

  if(bootMode == 4) { //Fix Aquired - sending
    system_rtc_mem_read(RTCMEMORYSTART + 3, &dummy, 4);
    if (getFence() || dummy + (rf_force * 60) <= getTimestamp()) {
      bootMode = 5;
      if (rfSend()) {
        system_rtc_mem_write(RTCMEMORYSTART + 1, &lat, 4);
        system_rtc_mem_write(RTCMEMORYSTART + 2, &lon, 4);
        dummy = getTimestamp();
        system_rtc_mem_write(RTCMEMORYSTART + 3, &dummy, 4);
        bootMode = 0;
      } else {
        DEBUG_PORT.println(F("Transfer failed"));
        bootMode = 0;
      }
    }else{
      Serial.println(F("Position unchanged, no transfer"));
      bootMode = 0;
    }
  }

  if(bootMode == 5) { //No fix
    debugDump();
    bootMode = 0;
  }
  
  system_rtc_mem_write(RTCMEMORYSTART, &bootMode, 1);
}

void loop() {
  DEBUG_PORT.println(F("Disabling GNSS Power"));
  digitalWrite(GNSS_PWR_PIN, !GNSS_PWR_ON);
  DEBUG_PORT.print(F("Sleeping for "));
  DEBUG_PORT.print(sleep_off);
  DEBUG_PORT.println(F(" Minutes"));
  DEBUG_PORT.flush();
  ESP.deepSleep(sleep_off * 60e6);
}

Inhalt

  • 00:35 Das Modul
  • 09:07 Das NMEA-Protokoll
  • 13:33 Test & Konfiguration per U-Center
  • 18:02 GPS mit ESP8266 und Arduino
  • 19:05 Test mit SoftwareSerial
  • 20:47 GPS/WiFi-Tracking mit OwnTracks

Hinweise:

Das GPS-Modul wurde mir von ICStation.com für dieses Video kostenfrei zur Verfügung gestellt.

BitBasics – Satellitennavigationssysteme (GPS, GLONASS, etc)

BitBasics - Satellitennavigationssysteme (GPS, GLONASS, etc)

(43.4 MB) 00:13:24

2017-10-29 11:00 🛈

Wer wissen will wo er ist, der Fragt meist sein Smartphone – aber wie kann der kleine Kasten diese Frage beantworten? In dieser Folge schauen wir auch die Techniken hinter GPS & Co.

BitBastelei #260 – TFT-LED-Steuerung

BitBastelei #260 - TFT-LED-Steuerung

(159.9 MB) 00:26:24

2017-10-22 10:00 🛈

Mal etwas aus der „schauen wir halt mal“-Ecke: Ein alter, kaputter Tablet-LCD aus der Restekiste. In diesen steckt auch eine LED-Beleuchtung, welche sich eventuell als Lampe eignen könnte. Schauen wir mal, ob wie die LEDs entnehmen und die Ansteuerung verstehen können.

[ICStation.com] BitBastelei #259 – Audio-Frequenzweiche: Soundsystem-Recycling

[ICStation.com] BitBastelei #259 - Audio-Frequenzweiche: Soundsystem-Recycling

(84.4 MB) 00:18:17

2017-10-15 10:00 🛈

Es ist Laubbläserzeit – wohl dem, der Soundtechnisch dagegen halten kann. Leider sind meine Eigenbau-Mediacenter da etwas dünn aufgestellt, allerdings liegt noch ein altes, analoges 5.1-System in der Kiste. Könnte man aus dem Stereo-Ausgang des Raspberry Pi nur ein Bass-Signal für den Subwoofer erzeugen. Kann man natürlich, und eine dafür notwendige Frequenzweiche bietet ICStation.com als Bausatz an.

Links

Inhalt

00:00 Warum das Ganze?
02:26 Der Bausatz
05:36 Die Schaltung
07:10 Simulation
09:45 Erste Messung
11:46 Das fertige Modul
12:00 Blick in das Audiosystem
15:05 Frequenzweiche an der Box
16:38 Ozi-Auswertung mit Last

Hinweise

Der Bausatz wurde mir von ICStation.com für dieses Video kostenfrei zur Verfügung gestellt.

BitBastelei #258 – LED in der Steckdose? Geht das?

BitBastelei #258 - LED in der Steckdose? Geht das?

(60.5 MB) 00:21:41

2017-10-08 10:00 🛈

Eine LED in die Steckdose stecken klingt nicht sonderlich klug – aber seit wann lasse ich mich davon abhalten. Schauen wir mal was passiert, warum man nicht wie sonst einen Vorwiderstand nutzen sollte und was ein Kondensator damit zu tun hat.

Links zum Thema

[ICStation.com] BitBastelei #257 – Singende Tesla-Spule

[ICStation.com] BitBastelei #257 - Singende Tesla-Spule

(214.2 MB) 00:26:45

2017-10-01 10:00 🛈

Teslaspulen sind immer faszinierend – und das nicht nur in alten Computerspielen. Durch hochfrequente Wechselspannung und ein großes Übersetzungsverhältnis wird eine Hochspannung generiert, welche optisch interessante Plasmablitze erzeugt. Klingt nach Raketentechnik? Nicht ganz – für ein paar Euro verspricht dieser Bausatz von ICStation.com nicht nur einen einsteigerfreundlichen Aufbau, sondern auch gleich noch eine Musikfunktion.

Achtung: Hochspannung ist gefährlich. Nicht ohne Schutzmaßnahmen betreiben.

Links

Hinweise

Der Bausatz wurde mir von ICStation.com für dieses Video kostenfrei zur Verfügung gestellt

BitBastelei #256 – Analog-Multimeter Fehlersuche

BitBastelei #256 - Analog-Multimeter Fehlersuche

(94.3 MB) 00:10:21

2017-09-24 10:00 🛈

Ein alter Bekannter auf dem Basteltisch: Das Elavi 15n aus den 70ern war bereits in meinem alten Intro zu sehen. Nun kam es mit einer wenig aussagekräftigen Fehlermeldung in die Reparaturkiste: Bei Widerstandsmessung geht der Zeiger ins Negative. Schaun wir mal.

BitNotice #126 – Micro-USB Verlängerung

BitNotice #126 - Micro-USB Verlängerung

(12.4 MB) 00:02:11

2017-09-21 12:00 🛈

Mailbag-Nachschub: Um meine Seek-Wärmebildkamera nicht nur als Selfie-Kamera verwenden zu können hatte ich bisher selbstgebastelte Kabel verwendet. Mal schauen, ob ein fertiges Kabel auch funktioniert und weniger Menschen nervös macht.

1m Micro-USB-Extension, ca. 1€

[ICStation.com] BitBastelei #255 – Geomagnetic Parking Detection Sensor Module

[ICStation.com] BitBastelei #255 - Geomagnetic Parking Detection Sensor Module

(111.3 MB) 00:37:22

2017-09-17 10:00 🛈

Dieses mal einen – mehr oder weniger – Mystery-Sensor: Ein „Geomagnetic Parking Detection Sensor Module“. Außer dem Namen und einigen technischen Daten ist nicht viel zu finden, also schauen wir mal

Inhalt

00:28 Produktbeschreibung
01:05 Zweckraten
02:09 Ansteuerraten
16:03 Testaufbau
17:20 Protokoll-Decoding
28:45 Echttest

Links:

Modul

Ergänzungen und Korrekturen:

00:24 Zur Klarstellung: Es ging nur um die Reihenfolge – die anderen Teile kommen natürlich auch noch dran

Hinweise

Der Bausatz wurde mit von ICStation.com für dieses Video kostenfrei zur Verfügung gestellt

BitBastelei #254 – LED-Deckenpanel

BitBastelei #254 - LED-Deckenpanel

(244.8 MB) 00:22:18

2017-09-10 10:00 🛈

LED-Deckenlampe mit 40W? Was kann da schon schief gehen? Nunja, das deutsche Logistiksystem z.B., welches selbst Lampen aus Aluwinkeln beim Transport zerstört bekommt. Nunja, machen wir das beste draus, schauen uns den Aufbau an und bauen die Lampe gleich auf 12V um.

01:54 Technische Daten
03:24 Erster Test
06:15 Blick ins Innere
09:44 Original LED-Treiber
16:22 12V LED-Treiber