Last active
February 17, 2017 13:16
-
-
Save Kaniiisek/b0959512d83da99d6511ba0ae10a485f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "TD1208.h" | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#define SIGFOXMOD_TX (2) | |
#define SIGFOXMOD_RX (3) | |
#define ONE_WIRE_BUS 7 | |
//#define SENSOR_RESOLUTION 9 | |
//#define SENSOR_INDEX 0 | |
//int DS18S20_Pin = 7; | |
//OneWire ds(DS18S20_Pin); | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature sensors(&oneWire); | |
//DeviceAddress insideThermometer; | |
SoftwareSerial SigFoxSerial(SIGFOXMOD_RX, SIGFOXMOD_TX); | |
TD1208 td1208(&SigFoxSerial); | |
int TempInC; | |
void setup() | |
{ | |
Serial.begin(9600); | |
sensors.begin(); | |
if (td1208.sendAT()) | |
{ | |
Serial.println("TD1207 OK"); | |
} | |
else | |
{ | |
Serial.println("TD1207 KO"); | |
//while (1); | |
} | |
} | |
void loop() | |
{ | |
sensors.requestTemperatures(); | |
sensors.getTempCByIndex(0); | |
TempInC = sensors.getTempCByIndex(0); | |
float temperatureInCelsius = sensors.getTempCByIndex(0); | |
Serial.print("Teplota: "); | |
Serial.print(temperatureInCelsius, 0); | |
Serial.print(" Celsius, "); | |
int temperature = 0; | |
if (td1208.getTemperature(&temperature)) | |
{ | |
if (td1208.sendPayload((String)TempInC)) | |
{ | |
Serial.println("Poslano"); | |
} | |
else | |
{ | |
Serial.println("Neposlano"); | |
} | |
} | |
else | |
{ | |
Serial.println("Temperature KO"); | |
} | |
while (1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "TD1208.h" | |
#define ATOK "OK" | |
#define ATCOMMAND "AT" | |
#define ATSIGFOXTX "AT$SS=" | |
#define ATTEMPERATURE "ATI26" | |
#define ATCOMMAND_TIMEOUT (3000) | |
#define ATSIGFOXTX_TIMEOUT (30000) | |
TD1208::TD1208(SoftwareSerial* serialPort) | |
{ | |
TD1208::serialPort = serialPort; | |
} | |
bool TD1208::sendAT() | |
{ | |
return sendATCommand(ATCOMMAND, ATCOMMAND_TIMEOUT, nullptr); | |
} | |
bool TD1208::sendPayload(const String payload) | |
{ | |
String message = (String) ATSIGFOXTX + payload; | |
String data = ""; | |
if (sendATCommand(message, ATSIGFOXTX_TIMEOUT, &data)) | |
{ | |
Serial.println(data); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
bool TD1208::getTemperature(int *temperature) | |
{ | |
String data = ""; | |
if (sendATCommand(ATTEMPERATURE, ATCOMMAND_TIMEOUT, &data)) | |
{ | |
*temperature = data.toInt(); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
bool TD1208::sendATCommand(const String command, const int timeout, String *dataOut) | |
{ | |
// Start serial interface | |
serialPort->begin(9600); | |
delay(200); | |
serialPort->flush(); | |
serialPort->listen(); | |
// Add CRLF to the command | |
String ATCommand = ""; | |
ATCommand.concat(command); | |
ATCommand.concat("\r\n"); | |
// Print echo | |
Serial.println((String)">>" + ATCommand); | |
// Send the command : need to write/read char by char because of echo | |
for (int i = 0; i < ATCommand.length(); ++i) | |
{ | |
serialPort->print(ATCommand.c_str()[i]); | |
serialPort->read(); | |
} | |
Serial.print("<<"); | |
// Read response | |
String response = ""; | |
unsigned int startTime = millis(); | |
volatile unsigned int currentTime = millis(); | |
volatile char rxChar = '\0'; | |
// Manage rx : two ways for loop break: | |
// - Timeout | |
// - Receive OK | |
do | |
{ | |
if (serialPort->available() > 0) | |
{ | |
rxChar = (char)serialPort->read(); | |
response.concat(rxChar); | |
} | |
currentTime = millis(); | |
}while(((currentTime - startTime) < timeout) && response.endsWith(ATOK) == false); | |
serialPort->end(); | |
Serial.println(response); | |
// Split the response | |
int index = 0; | |
String firstData = ""; | |
String secondData = ""; | |
if (response.length() != 0) | |
{ | |
// Split CRLF | |
do | |
{ | |
// Save previous index | |
int previous = index; | |
// Get next index | |
index = response.indexOf("\r\n", index); | |
// Check that index change | |
if (previous != index) | |
{ | |
// Get the chunk | |
String chunk = response.substring(previous+1, index); | |
if (chunk.length() > 0) | |
{ | |
// Remove \r\n | |
chunk.replace("\r\n", ""); | |
if (firstData != "") | |
{ | |
secondData = chunk; | |
} | |
else if (firstData == "" && secondData == "") | |
{ | |
firstData = chunk; | |
} | |
else | |
{ | |
Serial.println("ERROR on rx frame"); | |
return false; | |
} | |
} | |
} | |
// Increment index | |
if (index >= 0) | |
{ | |
index++; | |
} | |
} while (index < response.length() && index >= 0); | |
} | |
else | |
{ | |
return false; | |
} | |
//Serial.println((String) "firstData : " + firstData); | |
//Serial.println((String) "secondData : " + secondData); | |
// Check if we have data on the first string and OK on second = data + OK | |
if (firstData != "" && secondData == ATOK) | |
{ | |
*dataOut = firstData; | |
return true; | |
} | |
// Check if we have only an OK | |
else if (firstData == ATOK && secondData == "") | |
{ | |
return true; | |
} | |
else | |
{ | |
Serial.println("Wrong AT response"); | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TD1208_H | |
#define TD1208_H | |
#include <Arduino.h> | |
#include <SoftwareSerial.h> | |
class TD1208 | |
{ | |
public: | |
TD1208(SoftwareSerial* serialPort); | |
bool sendPayload(const String payload); | |
bool sendAT(); | |
bool getTemperature(int *temperature); | |
private: | |
bool sendATCommand(const String command, const int timeout, String *dataOut); | |
SoftwareSerial* serialPort; | |
}; | |
#endif // TD1208_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment