WIP - Wemos D1 mini, A6 GSM/GPRS, 1-wire

Hur ser ditt mät- och/eller hemautomations-system ut? Dela med dig av hur just Ditt projekt ser ut och fungerar, eller hur dina planer ser ut för framtiden. Att läsa om andra är bästa inspirationen.

Moderator: elf98

Kategoriregler
Vill du visa bilder i ditt inlägg? Använd funktionen "Ladda upp bilaga" nedanför textrutan!
SGL70
Gillar hemautomation
Inlägg: 37
Blev medlem: 22 apr 2019, 00:07

WIP - Wemos D1 mini, A6 GSM/GPRS, 1-wire

Inlägg av SGL70 » 12 maj 2019, 11:32

Hej!

Jag snickrar på ett system som jag tänkte sätta upp på vår liftstuga
Jag är med i arbetsstyrkan vid den lokala Friluftsfrämjandet och vi har köldgränser som påverkar öppethållandet. För att ge bättre service OCH slippa åka dit för att kolla tempen, tänke jag bygga ett system.

Jag tänkte basera det på en Wemos D1 mini, Ai thinkers A6 GSM/GPRS modul och en (till att börja med) 18B20 one-wire prob.

Så här har jag kopplat ihop hårdvaran:
temp.PNG
temp.PNG (34.09 KiB) Visad 906 gånger
Koden baseras i huvudsak på biblioteket TinyGSM:

Kod: Markera allt

/**************************************************************

   This sketch connects to a website and downloads a page.
   It can be used to perform HTTP/RESTful API calls.

   TinyGSM Getting Started guide:
     https://tiny.cc/tinygsm-readme

 **************************************************************/

// Select your modem:
// #define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM900
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM868
// #define TINY_GSM_MODEM_SIM7000
// #define TINY_GSM_MODEM_UBLOX
// #define TINY_GSM_MODEM_M95
// #define TINY_GSM_MODEM_BG96
#define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_MC60
// #define TINY_GSM_MODEM_MC60E
// #define TINY_GSM_MODEM_ESP8266
// #define TINY_GSM_MODEM_XBEE

// Increase RX buffer if needed
#define TINY_GSM_RX_BUFFER 512

// See the debugging, if wanted
//#define TINY_GSM_DEBUG Serial
//#define LOGGING

// Add a reception delay, if needed
//#define TINY_GSM_YIELD() { delay(1); }

#include <TinyGsmClient.h>

// Uncomment this if you want to see all AT commands
//#define DUMP_AT_COMMANDS

// Uncomment this if you want to use SSL
//#define USE_SSL

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial

// Use Hardware Serial on Mega, Leonardo, Micro
// #define SerialAT Serial1

// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(12, 14); // RX, TX


// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[]  = "online.telia.se";
const char gprsUser[] = "";
const char gprsPass[] = "";
const char wifiSSID[]  = "YourSSID";
const char wifiPass[] = "SSIDpw";
String hash = "ba79c368433558812aca75XXXXXXXXX";

// Server details
const char server[] = "temperatur.nu";
const char resource[] = "/rapportera.php?hash=ba79c368433558812acXXXXXXXXXXXX"; //

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

#ifdef USE_SSL
TinyGsmClientSecure client(modem);
const int  port = 443;
#else
TinyGsmClient client(modem);
const int  port = 80;
#endif


// ******** ONEWIRE *************************************
#define ONE_WIRE_BUS 2 //D4 (Wemos)
#include "OneWire.h"
#include "DallasTemperature.h"
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

String getTemp(int index) {
  sensors.requestTemperatures();
  float retval = sensors.getTempCByIndex(index); //Index pekar på sensor - atm bara en
  //beräkna medelvärde behövs vid flera sensorer.
  return (String)retval;
}
unsigned long previousMillis = 0;
const long interval = 60000;

void setup() {
  // Set console baud rate
  SerialMon.begin(115200);
  delay(10);
  SerialMon.println(F("Wait..."));

  //pinMode(23, OUTPUT);
  //digitalWrite(23, LOW);

  // Set GSM module baud rate
  SerialAT.begin(28800);
  delay(3000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println(F("Initializing modem..."));
  modem.restart();

  String modemInfo = modem.getModemInfo();
  SerialMon.print(F("Modem: "));
  SerialMon.println(modemInfo);

  // Unlock your SIM card with a PIN
  //modem.simUnlock("1234");
}

void loop() {
  
  //unsigned long currentMillis = millis();

  //if (currentMillis - previousMillis >= interval) {
    if (modem.hasWifi()) {
      SerialMon.print(F("Setting SSID/password..."));
      if (!modem.networkConnect(wifiSSID, wifiPass)) {
        SerialMon.println(" fail");
        delay(10000);
        return;
      }
      SerialMon.println(" OK");
    }
    else if (modem.getModemName().indexOf("XBee") >= 0) {
      SerialMon.print(F("Setting APN"));
      if (!modem.gprsConnect(apn)) {
        SerialMon.println(" fail");
        delay(10000);
        return;
      }
      SerialMon.println(" OK");
    }

    SerialMon.print(F("Waiting for network..."));
    if (!modem.waitForNetwork()) {
      SerialMon.println(" fail");
      delay(10000);
      return;
    }
    SerialMon.println(" OK");
    
    if (modem.hasGPRS()) {
      SerialMon.print(F("Connecting to "));
      SerialMon.print(apn);
      if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
        SerialMon.println(" fail");
        delay(10000);
        return;
      }
      SerialMon.println(" OK");
    }

    IPAddress local = modem.localIP();
    SerialMon.print(F("IP: "));
    SerialMon.println(local);

    SerialMon.print(F("Connecting to "));
    SerialMon.print(server);
    if (!client.connect(server, port)) {
      SerialMon.println(" fail");
      delay(10000);
      return;
    }
    SerialMon.println(" OK");



    // Make a HTTP GET request:
    SerialMon.println(F("Sending GET request"));
    String request;
    String temperatur = getTemp(0);

    request = "GET /rapportera.php?hash=";
    request += hash;
    request += "&t=";
    request += temperatur;
    request += " HTTP/1.1";
    request += ("\r\n");
    request += ("Host: temperatur.nu");
    request += ("\r\n");
    request += ("\r\n");

    client.print(request);
    //client.print(String("GET ") + resource + " HTTP/1.1\r\n");
    //client.print(String("Host: ") + server + "\r\n\r\n");
    //client.print("Connection: close\r\n\r\n");

    unsigned long timeout = millis();
    while (client.connected() && millis() - timeout < 10000L) {
      // Print available data
      while (client.available()) {
        char c = client.read();
        SerialMon.print(c);
        timeout = millis();
      }
    }
    SerialMon.println();

    // Shutdown

    client.stop();
    SerialMon.println(F("Server disconnected"));

    if (modem.hasWifi()) {
      modem.networkDisconnect();
      SerialMon.println(F("WiFi disconnected"));
    }
    else {
      modem.gprsDisconnect();
      SerialMon.println(F("GPRS disconnected"));
    }

    // Do nothing forevermore
    // while (true) {
    modem.restart();
    delay(60000);
    //}
  //}
}

Ska kanske sägas att jag inte kodar till yrket, så min "stil" kan nog lämna en del att önska. Dessutom är behöver den lite TLC, för jag får bara rätt svar OK! (temp) kanske i hälften av fallen.

Output:

Kod: Markera allt

11:18:02.064 -> Server disconnected
11:18:02.132 -> GPRS disconnected
11:19:11.980 -> Waiting for network... OK
11:19:12.014 -> Connecting to online.telia.se OK
11:19:16.478 -> IP: 10.213.91.35
11:19:16.478 -> Connecting to temperatur.nu OK
11:19:19.838 -> Sending GET request
11:19:40.075 -> 
11:19:41.086 -> Server disconnected
11:20:41.080 -> GPRS disconnected
11:21:51.336 -> Waiting for network... OK
11:22:39.329 -> Connecting to online.telia.se OK
11:22:39.772 -> IP: 0.0.0.0
11:22:39.772 -> Connecting to temperatur.nu OK
11:22:41.032 -> Sending GET request
11:22:42.028 -> HTTP/1.1 200 OK
11:22:42.028 -> Date: Sun, 12 May 2019 09:22:39 GMT
11:22:42.028 -> Content-Type: text/html; charset=UTF-8
11:22:42.028 -> Transfer-Encoding: chunked
11:22:42.028 -> Connection: keep-alive
11:22:42.063 -> Set-Cookie: __cfduid=dfb563344e27f9b3404a4c2cbb9dc651f1557652959; expires=Mon, 11-May-20 09:22:39 GMT; path=/; domain=.temperatur.nu; HttpOnly
11:22:42.063 -> Server: cloudflare
11:22:42.063 -> CF-RAY: 4d5b54565962cb0c-ARN
11:22:42.063 -> 
11:22:42.063 -> b
11:22:42.063 -> ok! (22.87)
11:22:42.063 -> 0
11:22:42.063 -> 
11:22:52.054 -> 
11:22:52.124 -> Server disconnected
11:22:52.191 -> GPRS disconnected
11:24:02.053 -> Waiting for network... OK
11:24:02.131 -> Connecting to online.telia.se OK
11:24:06.477 -> IP: 10.143.246.113
11:24:06.477 -> Connecting to temperatur.nu OK
11:24:09.819 -> Sending GET request
11:24:20.369 -> 
11:24:20.420 -> Server disconnected
11:24:20.486 -> GPRS disconnected
11:25:30.344 -> Waiting for network... OK
11:25:30.414 -> Connecting to online.telia.se OK
11:25:34.840 -> IP: 10.44.242.123
11:25:34.840 -> Connecting to temperatur.nu OK
11:25:38.176 -> Sending GET request
11:25:39.069 -> HTTP/1.1 200 OK
11:25:39.069 -> Date: Sun, 12 May 2019 09:25:36 GMT
11:25:39.069 -> Content-Type: text/html; charset=UTF-8
11:25:39.069 -> Transfer-Encoding: chunked
11:25:39.069 -> Connection: keep-alive
11:25:39.069 -> Set-Cookie: __cfduid=dad2b1ba1ed6d688db7cdcc84063298931557653136; expires=Mon, 11-May-20 09:25:36 GMT; path=/; domain=.temperatur.nu; HttpOnly
11:25:39.103 -> Server: cloudflare
11:25:39.103 -> CF-RAY: 4d5b58a8fdc9caf0-ARN
11:25:39.103 -> 
11:25:39.103 -> b
11:25:39.103 -> ok! (22.87)
11:25:39.103 -> 0
Jag får komplettera posten med den slutgiltiga formen, om jag når dit...

//Greger