diff --git a/libraries/BayEOSBoards/RF24RouterBoard.h b/libraries/BayEOSBoards/RF24RouterBoard.h index 30e4271..444884c 100644 --- a/libraries/BayEOSBoards/RF24RouterBoard.h +++ b/libraries/BayEOSBoards/RF24RouterBoard.h @@ -125,18 +125,20 @@ RTC_Timer2 myRTC; volatile long rtc_seconds_correct; #endif -#ifdef GPRS_CONFIG + +#if defined(GPRS_CONFIG) #include BayGPRS client(Serial, 0); //No Power Pin -#else -#ifdef WLAN_CONFIG +#elif defined(SIM800_CONFIG) +#include +BaySIM800 client = BaySIM800(Serial); +#elif defined(WLAN_CONFIG) #include BayESP8266 client(Serial, WLAN_PIN); -#else +#else #include BaySerialESP client(Serial,WLAN_PIN); #endif -#endif @@ -301,50 +303,42 @@ void initLCB() { client.setBuffer(myBuffer); pinMode(POWER_PIN, OUTPUT); digitalWrite(POWER_PIN, HIGH); -#ifdef GPRS_CONFIG -#define ESP01 0 +#if defined(GPRS_CONFIG) client.readConfigFromStringPGM(PSTR(GPRS_CONFIG)); -#else -#ifdef WLAN_CONFIG -#define ESP01 1 +#elif defined(SIM800_CONFIG) + client.readConfigFromStringPGM(PSTR(SIM800_CONFIG)); +#elif defined(WLAN_CONFIG) client.readConfigFromStringPGM(PSTR(WLAN_CONFIG)); #else -#define ESP01 1 client.begin(38400); client.powerUp(); while(client.isReady()){ blinkLED(2); delay(1500); } -#ifndef WLAN_CONFIG #ifdef ROUTER_NAME while (tx_res=client.setName(ROUTER_NAME)){ if(tx_res==10+strlen(ROUTER_NAME)) break; blinkLED(tx_res); delay(tx_res*500+2000); } -#endif #endif client.powerDown(); -#endif -// client.begin(9600); -// client.changeIPR(38400); #endif - blinkLED(2); + blinkLED(3); adjust_OSCCAL(); -#ifdef GPRS_CONFIG +#if defined(GPRS_CONFIG) || defined(SIM800_CONFIG) delayLCB(1000); tx_res = client.begin(38400); if (! tx_res) myRTC.adjust(client.now()); -#else -#ifdef WLAN_CONFIG +#elif defined(WLAN_CONFIG) tx_res = client.begin(38400); #else client.powerUp(); tx_res=0; #endif -#endif + blinkLED(tx_res + 1); /* 1 == OK @@ -356,8 +350,10 @@ void initLCB() { 7 == No SIM Card */ delay(2000); - - tx_res = client.sendMessage("Router started"); + client.startFrame(BayEOS_Message); + client.addToPayload("Router started"); + client.writeToBuffer(); + tx_res = client.sendMultiFromBuffer(); blinkLED(tx_res + 1); delay(1000 + tx_res * 500); @@ -632,7 +628,7 @@ void checkAction0(void){ #if ESP01 client.powerUp(); #endif - tx_res = client.sendMultiFromBuffer(1000); + tx_res = client.sendMultiFromBuffer(3000); if(tx_res) tx_error++; else tx_error=0; blinkLED(tx_res + 1); @@ -646,7 +642,7 @@ void checkAction0(void){ while (! tx_res && myBuffer.available() && ! ISSET_ACTION(0)) { handleRF24(); - tx_res = client.sendMultiFromBuffer(1000); + tx_res = client.sendMultiFromBuffer(3000); blinkLED(tx_res + 1); } } else diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest/BoardTest.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest/BoardTest.ino index a61aaba..dd917d0 100644 --- a/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest/BoardTest.ino +++ b/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest/BoardTest.ino @@ -66,8 +66,8 @@ void loop() { digitalWrite(MCPPOWER_PIN, HIGH); client.startDataFrame(); for (uint8_t ch = 0; ch < 16; ch++) { - digitalWrite(A1, ch & 0x8); - digitalWrite(A0, ch & 0x4); + digitalWrite(A0, ch & 0x8); + digitalWrite(A1, ch & 0x4); digitalWrite(A3, ch & 0x2); digitalWrite(A2, ch & 0x1); delay(1); diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest_Serial/BoardTest_Serial.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest_Serial/BoardTest_Serial.ino new file mode 100644 index 0000000..5f1992c --- /dev/null +++ b/libraries/BayEOSBoards/examples/LowCurrentMplex16/BoardTest_Serial/BoardTest_Serial.ino @@ -0,0 +1,85 @@ +/* + Sample Sketch to read resistance using + + MCP, a 16bit Multiplexer and a preresistor + + +*/ +#define PRERESISTOR 14300.0 +const uint8_t rate = 3; //0-3: 12bit ... 18bit +#define MCPPOWER_PIN 6 + +#include +#define POWER_DIVIDER ((100.0 + 100.0) / 100.0) +#include +MCP342x mcp342x(0); +const byte addr = 0; +const uint8_t gain = 0; //0-3: x1, x2, x4, x8 +float span; + +SPIFlash flash(8); +BayEOSBufferSPIFlash myBuffer; + +#include +BayDebug client(Serial); + +#define ACTION_COUNT 1 +#include + +void setup() +{ + client.begin(9600); + Serial.println("Starting Test"); + myBuffer.init(flash); //This will restore old pointers + Serial.print("Flash: "); + Serial.println(flash.getCapacity()); + Serial.flush(); + //myBuffer.reset(); //This will set all pointers to zero + myBuffer.skip(); //This will move read pointer to write pointer + myBuffer.setRTC(myRTC, 0); //Nutze RTC relativ! + client.setBuffer(myBuffer); //use skip! + initLCB(); //init time2 + mcp342x.reset(); + pinMode(POWER_PIN,OUTPUT); + digitalWrite(POWER_PIN,HIGH); + mcp342x.storeConf(1, 0); //14bit,0 gain + batLCB = 3.3*analogRead(A7)/1023*POWER_DIVIDER; + Serial.print("Bat Voltage: "); + Serial.println(batLCB); + //digitalWrite(POWER_PIN,LOW); + + Serial.println("Test done"); + Serial.flush(); + startLCB(); + pinMode(MCPPOWER_PIN, OUTPUT); + pinMode(A0, OUTPUT); + pinMode(A1, OUTPUT); + pinMode(A2, OUTPUT); + pinMode(A3, OUTPUT); + +} + + +void loop() { + digitalWrite(MCPPOWER_PIN, HIGH); + for (uint8_t ch = 0; ch < 16; ch++) { + digitalWrite(A0, ch & 0x8); + digitalWrite(A1, ch & 0x4); + digitalWrite(A3, ch & 0x2); + digitalWrite(A2, ch & 0x1); + delay(1); + mcp342x.runADC(0); + delay(mcp342x.getADCTime()); + span = mcp342x.getData(); + float strom = span / PRERESISTOR; + mcp342x.runADC(1); + delay(mcp342x.getADCTime()); + span = mcp342x.getData(); + Serial.print(span/strom/1000); + Serial.print(" "); + } + digitalWrite(MCPPOWER_PIN, LOW); + Serial.println(); + + +} diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/Logger/Logger.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/Logger/Logger.ino index a073a92..5e5ce5a 100644 --- a/libraries/BayEOSBoards/examples/LowCurrentMplex16/Logger/Logger.ino +++ b/libraries/BayEOSBoards/examples/LowCurrentMplex16/Logger/Logger.ino @@ -84,8 +84,8 @@ void measure() { digitalWrite(MCPPOWER_PIN, HIGH); delayLogger(20); for (uint8_t ch = 0; ch < 16; ch++) { - digitalWrite(A1, ch & 0x8); - digitalWrite(A0, ch & 0x4); + digitalWrite(A0, ch & 0x8); + digitalWrite(A1, ch & 0x4); digitalWrite(A3, ch & 0x2); digitalWrite(A2, ch & 0x1); delayLogger(10); diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/LoggerRF24/LoggerRF24.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/LoggerRF24/LoggerRF24.ino new file mode 100644 index 0000000..2ac6931 --- /dev/null +++ b/libraries/BayEOSBoards/examples/LowCurrentMplex16/LoggerRF24/LoggerRF24.ino @@ -0,0 +1,190 @@ +/* + Logger-Sketch for Read-Out via RF24 + use BaySerialRF24/LoggerConnector as receiver +*/ + +#define SAMPLING_INT 60 +#define PRE_RESISTOR 14300 +/* Factor to NTC10 - e.g. 0.5 for NTC5, 0.3 for NTC3 ...*/ +#define NTC10FACTOR 0.5 +#define MCPPOWER_PIN 6 +#define NRF24_TRYINT 60 +#define BLINK_ON_LOGGING_DISABLED 1 +const uint8_t channel = 0x70; +const uint8_t adr[] = {0x12, 0xae, 0x31, 0xc4, 0x45}; + +#define BAT_WARNING 3800 + + +//channel map and unit map must not exceed 98 characters! +char channel_map[] = "time;bat;T1;T2;T3;T4;T5;T6;T7;T8;T9;T10;T11;T12;T13,T14;T15;T16"; +char unit_map[] = "ms;V;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C"; + +#include +#include +#include +#include +#include +#include +#include + +const byte addr = 0; +const uint8_t gain = 0; //0-3: x1, x2, x4, x8 +const uint8_t rate = 3; //0-3: 12bit ... 18bit +// create an objcet of the class MCP342x +MCP342x mcp342x(addr); +//Configure your resistors on the board! + +float ntc10_R2T(float r) { + float log_r = log(r); + return 440.61073 - 75.69303 * log_r + + 4.20199 * log_r * log_r - 0.09586 * log_r * log_r * log_r; +} + +#define TICKS_PER_SECOND 16 + + +RF24 radio(9, 10); +BaySerialRF24 client(radio, 50, 3); //wait maximum 100ms for ACK +SPIFlash flash(8); //CS-Pin of SPI-Flash +BayEOSBufferSPIFlash myBuffer; +BayEOSLogger myLogger; +#include + +void delayLogger(unsigned long d) { + if (client.connected) { + unsigned long s = millis(); + while ((millis() - s) < d) { + myLogger.handleCommand(); + myLogger.sendBinaryDump(); + } + } else { + delayLCB(d); + } +} + + +//Add your sensor measurements here! +float values[17]; +uint16_t count; +unsigned long last_measurement; + +//Add your sensor measurements here! +uint16_t current_tics, last_tics; +void measure() { + float span, strom, ntc10r; + if (myLogger._logged_flag) { + myLogger._logged_flag = 0; + count = 0; + for (uint8_t i = 0; i < 17; i++) { + values[i] = 0; + } + } + + + + + + + digitalWrite(POWER_PIN, HIGH); + analogReference(INTERNAL); + myLogger._bat = (1.1 * 200 / 100 / 1023 * analogRead(A0)) * 1000; + values[0] += ((float)myLogger._bat) / 1000; + digitalWrite(POWER_PIN, LOW); + digitalWrite(MCPPOWER_PIN, HIGH); + delayLogger(20); + for (uint8_t ch = 0; ch < 16; ch++) { + digitalWrite(A0, ch & 0x8); + digitalWrite(A1, ch & 0x4); + digitalWrite(A3, ch & 0x2); + digitalWrite(A2, ch & 0x1); + delayLogger(10); + mcp342x.runADC(0); + delayLogger(mcp342x.getADCTime()); + strom = span / PRE_RESISTOR; //current in A + + mcp342x.runADC(1); + delayLogger(mcp342x.getADCTime()); + span = mcp342x.getData(); + ntc10r = span / strom / NTC10FACTOR; + + + values[ch + 1] += ntc10_R2T(ntc10r); + } + digitalWrite(MCPPOWER_PIN, LOW); + + + count++; + + client.startDataFrame(0x41); + client.addChannelValue(millis(), 1); + for (uint8_t i = 0; i < 17; i++) { + client.addChannelValue(values[i] / count, i + 2); + } + + + +} + + + + +void setup() { + pinMode(MCPPOWER_PIN, OUTPUT); + pinMode(POWER_PIN, OUTPUT); + myBuffer.init(flash); + myBuffer.setRTC(myRTC); //Nutze RTC absolut! + client.init(channel, adr); + radio.powerDown(); + client.setBuffer(myBuffer); + //register all in BayEOSLogger + myLogger.init(client, myBuffer, myRTC, 120, BAT_WARNING); //min_sampling_int = 120 + //disable logging as RTC has to be set first!! + myLogger._logging_disabled = 1; + myLogger.setChannelMap(channel_map); + myLogger.setUnitMap(unit_map); + Wire.begin(); + mcp342x.reset(); + mcp342x.storeConf(rate, gain); + pinMode(A1, OUTPUT); + pinMode(A2, OUTPUT); + pinMode(A3, OUTPUT); + initLCB(); //init time2 +} + + +unsigned long last_try = -NRF24_TRYINT; + + +void loop() { + if (! myLogger._logging_disabled && (myLogger._mode == LOGGER_MODE_LIVE || + (myRTC.get() - last_measurement) >= SAMPLING_INT)) { + last_measurement = myRTC.get(); + measure(); + } + myLogger.run(client.connected); + + if (! client.connected) { + myLogger._mode = 0; + //sleep until timer2 will wake us up... + Sleep.sleep(TIMER2_ON, SLEEP_MODE_PWR_SAVE); + + //check if receiver is present + if ((myRTC.get() - last_try) > NRF24_TRYINT) { + blinkLED(0); + last_try = myRTC.get(); + client.sendTestByte(); +#if BLINK_ON_LOGGING_DISABLED + if (! client.connected && myLogger._logging_disabled) { + last_try -= (NRF24_TRYINT - 5); + blinkLED(10); + } +#endif + } + } else if ((millis() - client.last_activity) > 30000) { + //check if still connected + last_try = myRTC.get(); + client.sendTestByte(); + } + +} diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/RF24_NTC/RF24_NTC.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/RF24_NTC/RF24_NTC.ino index 70f4069..df4db1b 100644 --- a/libraries/BayEOSBoards/examples/LowCurrentMplex16/RF24_NTC/RF24_NTC.ino +++ b/libraries/BayEOSBoards/examples/LowCurrentMplex16/RF24_NTC/RF24_NTC.ino @@ -106,8 +106,8 @@ void loop() client.sendOrBuffer(); client.startDataFrame(BayEOS_Int16le, WITH_CHECKSUM); } - digitalWrite(A1, ch & 0x8); - digitalWrite(A0, ch & 0x4); + digitalWrite(A0, ch & 0x8); + digitalWrite(A1, ch & 0x4); digitalWrite(A3, ch & 0x2); digitalWrite(A2, ch & 0x1); delayLCB(10); diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/readResistance/readResistance.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/readResistance/readResistance.ino deleted file mode 100644 index 749f311..0000000 --- a/libraries/BayEOSBoards/examples/LowCurrentMplex16/readResistance/readResistance.ino +++ /dev/null @@ -1,89 +0,0 @@ -/* - Sample Sketch to read resistance using - - MCP, a 16bit Multiplexer and a preresistor - - -*/ -#define PRERESISTOR 10000.0 - -//When inividual preresistorvalues are given PRERESITOR is ignored -//#define PRERESISTORS {9955.0, 9964.0, 9956.0, 9966.0, 9955.0, 9972.0, 9975.0, 9972.0, 9945.0, 9962.0, 9988.0, 9957.0, 9957.0, 9964.0, 9950.0, 9954.0 } - -//Define resolution -const uint8_t rate = 2; //0-3: 12bit ... 18bit - -#include -const byte addr = 0; -const uint8_t gain = 0; //0-3: x1, x2, x4, x8 -// create an objcet of the class MCP342x -MCP342x mcp342x(addr); - -float span = 0.0; -char str_buf[50] = " "; - - -#define MCPPOWER_PIN 6 - -#ifdef PRERESISTORS -float preresistors[]=PRERESISTORS; -#endif - -void setup() -{ - Serial.begin(9600); - mcp342x.reset(); - mcp342x.storeConf(rate, gain); - pinMode(MCPPOWER_PIN, OUTPUT); - pinMode(A0, OUTPUT); - pinMode(A1, OUTPUT); - pinMode(A2, OUTPUT); - pinMode(A3, OUTPUT); -} - - - -void loop() -{ - digitalWrite(MCPPOWER_PIN, HIGH); - delay(20); - for (uint8_t ch = 0; ch < 16; ch++) { - digitalWrite(A1, ch & 0x8); - digitalWrite(A0, ch & 0x4); - digitalWrite(A3, ch & 0x2); - digitalWrite(A2, ch & 0x1); - delay(1); - Serial.print(ch); - Serial.print("\t"); - mcp342x.runADC(0); - delay(mcp342x.getADCTime()); - span = mcp342x.getData(); - #ifdef PRERESISTORS - float strom = span / preresistors[ch] * 1000; //current in mA -#else - float strom = span / PRERESISTOR * 1000; //current in mA -#endif - dtostrf(span, 10, 6, str_buf); - Serial.print(str_buf); - Serial.print("\t"); - dtostrf(strom, 10, 6, str_buf); - Serial.print(str_buf); - Serial.print("\t"); - mcp342x.runADC(1); - delay(mcp342x.getADCTime()); - span = mcp342x.getData(); - dtostrf(span, 10, 6, str_buf); - Serial.print(str_buf); - Serial.print("\t"); - dtostrf(span / strom, 10, 6, str_buf); //kOhm - Serial.println(str_buf); - - } - digitalWrite(MCPPOWER_PIN, LOW); - - Serial.println(); - - - // do it every n seconds - delay(2000); -} diff --git a/libraries/BayEOSBoards/examples/LowCurrentMplex16/readTemperature/readTemperature.ino b/libraries/BayEOSBoards/examples/LowCurrentMplex16/readTemperature/readTemperature.ino deleted file mode 100644 index 078bb95..0000000 --- a/libraries/BayEOSBoards/examples/LowCurrentMplex16/readTemperature/readTemperature.ino +++ /dev/null @@ -1,76 +0,0 @@ -/* - Sample Sketch to read resistance using - - MCP, a 16bit Multiplexer and a preresistor - - -*/ -#define PRERESISTOR 14300.0 -#define NTC10FACTOR 0.5 - -//Define resolution -const uint8_t rate = 1; //0-3: 12bit ... 18bit - -#include -#include - -float ntc10_R2T(float r) { - float log_r = log(r); - return 440.61073 - 75.69303 * log_r + - 4.20199 * log_r * log_r - 0.09586 * log_r * log_r * log_r; -} -const byte addr = 0; -const uint8_t gain = 0; //0-3: x1, x2, x4, x8 -// create an objcet of the class MCP342x -MCP342x mcp342x(addr); - -float span = 0.0; -char str_buf[50] = " "; - - -#define MCPPOWER_PIN 6 - - -void setup() -{ - Serial.begin(9600); - mcp342x.reset(); - mcp342x.storeConf(rate, gain); - pinMode(MCPPOWER_PIN, OUTPUT); - pinMode(A0, OUTPUT); - pinMode(A1, OUTPUT); - pinMode(A2, OUTPUT); - pinMode(A3, OUTPUT); -} - - - -void loop() -{ - digitalWrite(MCPPOWER_PIN, HIGH); - delay(20); - for (uint8_t ch = 0; ch < 16; ch++) { - digitalWrite(A1, ch & 0x8); - digitalWrite(A0, ch & 0x4); - digitalWrite(A3, ch & 0x2); - digitalWrite(A2, ch & 0x1); - delay(1); - mcp342x.runADC(0); - delay(mcp342x.getADCTime()); - span = mcp342x.getData(); - float strom = span / PRERESISTOR; //current in mA - mcp342x.runADC(1); - delay(mcp342x.getADCTime()); - float R_mess = mcp342x.getData() / strom / NTC10FACTOR; - dtostrf(ntc10_R2T(R_mess), 10, 2, str_buf); - Serial.print(str_buf); - Serial.print(" "); - } - digitalWrite(MCPPOWER_PIN, LOW); - - Serial.println(); - - - // do it every n seconds - delay(1000); -} diff --git a/libraries/BaySIM800/BaySIM800.cpp b/libraries/BaySIM800/BaySIM800.cpp new file mode 100644 index 0000000..a714979 --- /dev/null +++ b/libraries/BaySIM800/BaySIM800.cpp @@ -0,0 +1,520 @@ +#include "BaySIM800.h" +const char* const BaySIM800::_urlencodedChars = "$&+,/:;=?@ <>#%{}|~[]`"; + + +BaySIM800::BaySIM800(HardwareSerial &serial){ + _serial=&serial; +} + +uint8_t BaySIM800::begin(long baud){ + _baud=baud; + _serial->begin(_baud); + skipChars(); + printlnP_OK("AT",200); //Wake up + printlnP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! + if(wait_forOK(1000)) changeIPR(baud); + return init(); +} + +uint8_t BaySIM800::init(void){ + uint8_t i=0; + skipChars(); + printlnP_OK("AT",200); //Wake up + printlnP("AT"); + if(wait_forOK(1000)) return 1; + //communication ok! + printlnP_OK("ATE0",500); //Command echo off + printlnP_OK("AT+CSCLK=0",500); //Auto-Sleepmode off + //Check PIN + printlnP("AT+CPIN?"); + while(wait_forPGM(PSTR("+CPIN: "),5000,7,_base64buffer)){ + printlnP_OK("AT",200); + printlnP_OK("AT+CFUN=0",10000); + //Disable + printlnP_OK("AT+CFUN=1",10000); + //Enable + printlnP_OK("AT",200); + printlnP("AT+CPIN?"); + i++; + if(i>2) return 6; + } + if(_base64buffer[5]=='U') return 3; //SIM PUK + if(_base64buffer[5]=='I'){ //SIM PIN + printlnP_OK("AT",200); + printP("AT+CPIN=\""); + _serial->print(_pin); + _serial->println("\""); + if(wait_forOK(30000)) { + return 2; //Wrong PIN + } + wait_for("SMS Ready",20000); + } + // Waiting for Modem to Connect + for(i=0;i<127;i++){ + if(isRegistered()) break; + delay(500); + } + if(i==127) return 4; + for(i=0;i<127;i++){ + if(isAttached()) break; + delay(500); + } + if(i==127) return 5; + + printlnP_OK("AT+HTTPTERM",200); + printlnP_OK("AT+SAPBR=0,1",200); + printlnP_OK("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"",200); + printP("AT+SAPBR=3,1,\"APN\",\""); + _serial->print(_apn); + printlnP_OK("\"",200); + wait_forOK(200); + printP("AT+SAPBR=3,1,\"USER\",\""); + _serial->print(_prov_user); + printlnP_OK("\"",200); + printP("AT+SAPBR=3,1,\"PWD\",\""); + _serial->print(_prov_pw); + printlnP_OK("\"",200); + + return 0; +} + + +uint8_t BaySIM800::changeIPR(long baud) { + _baud = baud; + long t_baud[] = { baud, 9600, 38400, 57600 }; + for (uint8_t i = 0; i < 4; i++) { + _serial->begin(t_baud[i]); + skipChars(); + uint8_t tries=5; + do { + printlnP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! + if(!wait_forOK(1000)){ + printP("AT+IPR="); + _serial->println(_baud); + if (!wait_forOK(200)) { + _serial->end(); + _serial->begin(_baud); + return 0; + } + + } + tries--; + } while(tries); + _serial->end(); + + } + return 1; + +} + + +uint8_t BaySIM800::getRSSI(void){ + printlnP_OK("AT",200); + printlnP("AT+CSQ"); + wait_forPGM(PSTR("+CSQ: "),500,2,_base64buffer); + return (uint8_t)114 -(2*(uint8_t)atoi(_base64buffer)); +} + +uint8_t BaySIM800::isRegistered(void){ + printlnP_OK("AT",200); + printlnP("AT+CREG?"); + wait_forPGM(PSTR("+CREG: "),2000,3,_base64buffer); + if(_base64buffer[2]=='1'|| _base64buffer[2]=='5') return 1; //Connected or Roaming + else return 0; +} + +uint8_t BaySIM800::isAttached(void){ + printlnP_OK("AT",200); + printlnP("AT+CGATT?"); + if(! wait_for("+CGATT: 1",2000)) return 1; + return 0; +} + +DateTime BaySIM800::now(void){ + printlnP_OK("AT",200); + uint8_t m,d,hh,mm,ss; + uint16_t y; + DateTime dt; + printlnP("AT+CCLK?"); + if(! wait_forPGM(PSTR("+CCLK: \""),3000,20,_base64buffer)){ //YY/MM/DD,HH:MM:SS+02 +// Serial.print("LocalTime:"); +// Serial.println(_base64buffer); + y = atoi(_base64buffer+0) + 2000; + m = atoi(_base64buffer+3); + d = atoi(_base64buffer+6); + hh = atoi(_base64buffer+9); + mm = atoi(_base64buffer+12); + ss = atoi(_base64buffer+15); + dt=DateTime (y, m, d, hh, mm, ss); + dt=DateTime(dt.get()-(3600L*atoi(_base64buffer+17))); //Adjust for Timezone! + } + return dt; +} + +uint8_t BaySIM800::postHeader(uint16_t size){ + uint8_t res; + res = init(); + if (res) { + _tx_error_count++; + return (res + 5); + } + printlnP_OK("AT+SAPBR=1,1",200); + uint8_t i=0; + for(i=0;i<10;i++){ + delay(300); + printlnP("AT+SAPBR=2,1"); + if(! wait_forPGM(PSTR("1,1,"),1000)) break; + } + if(i>=9) return(2); + printlnP_OK("AT+HTTPINIT",200); + printlnP_OK("AT+HTTPPARA=\"CID\",1",200); + printlnP_OK("AT+HTTPPARA=\"UA\",\"BaySIM800 1.0\"",200); + strcpy(_base64buffer+70, _user); + strcat(_base64buffer+70, ":"); + strcat(_base64buffer+70, _password); + base64_encode(_base64buffer, (char*) _base64buffer+70, strlen(_base64buffer+70)); + printP("AT+HTTPPARA=\"USERDATA\",\"Authorization: Basic "); + _serial->print(_base64buffer); + printlnP_OK("\"",200); + printP("AT+HTTPPARA=\"URL\",\""); + _serial->print(_url); + //wait_forOK(500); + if(printlnP_OK("\"",200)) return(3); + printlnP_OK("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\"",200); + printP("AT+HTTPDATA="); + _serial->print(size); + printlnP(",10000"); + if(wait_forPGM(PSTR("DOWNLOAD"),200)) return(4); + return(0); +} + +uint8_t BaySIM800::post(void){ + if(_url[4]=='s'){ //https + printlnP_OK("AT+HTTPSSL=1",200); + printlnP_OK("AT+SSLOPT=0,1",200); + } + printlnP("AT+HTTPACTION=1"); + if(wait_forPGM(PSTR("+HTTPACTION: 1,200"),5000)){ + _tx_error_count++; + return 1; + } else { + _tx_error_count=0; + return 0; + } +} + +void BaySIM800::readAck(void){ + printlnP("AT+HTTPREAD"); + wait_forPGM(PSTR("+HTTPREAD: "),5000,2,_base64buffer); + uint8_t ack_length=atoi(_base64buffer); + if( ack_length){ + ack_length-=12; + if(ack_length>BaySIM800_BUFFER) ack_length=BaySIM800_BUFFER; + if(! wait_forPGM(PSTR("bayeosframe="),5000,ack_length,_base64buffer)){ + urlDecode(_base64buffer); + _next=base64_decode((char *)_payload, _base64buffer, ack_length); + } + } else _next=0; +} + +uint8_t BaySIM800::sendPayload(void){ + return sendPayloadWithAck(false); +} + +uint8_t BaySIM800::sendPayloadWithAck(bool ack_payload){ + base64_encode(_base64buffer, (char*) _payload, getPacketLength()); + uint8_t size = strlenURLencoded(_base64buffer); + size += 7 + strlenURLencoded(_sender) + 1 + 15; + + uint8_t res=postHeader(size); + if(res) return(res); + //Redo Base64 encoding! buffer is overwritten in postHeader! + base64_encode(_base64buffer, (char*) _payload, getPacketLength()); + printP("sender="); + printURLencoded(_sender); + printP("&bayeosframes[]="); + printURLencoded(_base64buffer); //BASE64 + printlnP_OK("",2000); + res=post(); + + if(ack_payload && ! res) readAck(); + printlnP_OK("AT+HTTPTERM",200); + printlnP_OK("AT+SAPBR=0,1",200); + printlnP_OK("AT+CSCLK=2",500); //Auto-Sleepmode + return res; + +} + +uint8_t BaySIM800::sendMultiFromBuffer(uint16_t maxsize,bool ack_payload) { + if (!_buffer->available()) + return 0; + uint16_t size; + unsigned long readpos = _buffer->readPos(); + + //Guess size for the POST request: + //We need the size of the POST request for Content-Length. However ATMEGA cannot build up a + //several kb POST request in RAM. So we send a guess content length and fill up with dummy content + if (_buffer->available() > ((unsigned long) maxsize)) { + size = maxsize; + } else { + size = _buffer->available(); + size *= 2; + size += 30; //Double size and add 30 - This is just a guess. "bayeosframes[]=base64(frame)" + if (size < 150) + size = 150; //To small sizes may cause a problem because "bayeosframe[]=" does not fit in... + size += 7 + strlenURLencoded(_sender); // + 1 + 9 + strlenURLencoded(_password); + if (size > maxsize) + size = maxsize; + } + + uint8_t res=postHeader(size); + if(res) return(res); + //Send Body - first part (sender) + printP("sender="); + printURLencoded(_sender); + uint16_t postsize = 7 + strlenURLencoded(_sender); //+ 1 + 9 + strlenURLencoded(_password); + + //Send Body - second part (frames) + uint8_t framesize; + while (postsize < (size - 25) && readFromBuffer()) { + base64_encode(_base64buffer, (char*) _payload, getPacketLength()); + framesize = 1 + 15 + strlenURLencoded(_base64buffer); + postsize += framesize; + if (postsize <= size) { //Still space in the POST for the frame + printP("&bayeosframes[]="); + printURLencoded(_base64buffer); + _buffer->next(); + } else { //Frame does not fitt in the POST + postsize -= framesize; + break; + } + } + + //Fill up with dummy content + while (postsize < size) { + _serial->print("&"); + postsize++; + } + printlnP_OK("",2000); + res=post(); + if (res) { + _buffer->seekReadPointer(readpos); + } else { + delay(10); + } + + if(ack_payload && ! res) readAck(); + printlnP_OK("AT+HTTPTERM",200); + printlnP_OK("AT+SAPBR=0,1",200); + printlnP_OK("AT+CSCLK=2",500); //Auto-Sleepmode + + return res; +} + + + +void BaySIM800::printPGM(const char *str) { + skipChars(); + char c; + while (true) { + c = pgm_read_byte(str); + if (!c) + break; + _serial->write(c); + str++; + } +} + +void BaySIM800::printlnPGM(const char *str) { + printPGM(str); + _serial->println(); +} + + +uint8_t BaySIM800::printlnPGM(const char *str,uint16_t timeout) { + printlnPGM(str); + return wait_forOK(timeout); +} + +void BaySIM800::skipChars(void) { + do { + while (_serial->available()) { + _serial->read(); + + } + delay(1); + } while (_serial->available()); + +} + +int BaySIM800::strlenURLencoded(const char *str) { + int count = 0; + while (*str) { + if (strchr(_urlencodedChars, *str)) { + count += 3; + } else + count++; + str++; + + } + return count; +} + +void BaySIM800::urlDecode(char *str){ + char *leader = str; + char *follower = str; + + while (*leader) { + if (*leader == '%') { + leader++; + char high = *leader; + leader++; + char low = *leader; + + if (high > 0x39) high -= 7; + high &= 0x0f; + + if (low > 0x39) low -= 7; + low &= 0x0f; + + *follower = (high << 4) | low; + } else { + *follower = *leader; + } + leader++; + follower++; + } +} + + +void BaySIM800::printURLencoded(const char *str) { + while (*str) { + if (strchr(_urlencodedChars, *str)) { + _serial->print('%'); + _serial->print(*str, HEX); + } else { + _serial->print(*str); + } + str++; + } +} + + +void BaySIM800::readConfigFromStringPGM(const char *string) { + uint8_t offset = 0; + while (offset < BaySIM800_CONFIG_SIZE) { + _config_buffer[offset] = pgm_read_byte(string); + if (!_config_buffer[offset]) + break; + if (_config_buffer[offset] == '|') + _config_buffer[offset] = 0; + offset++; + string++; + } + setConfigPointers(); +} + +char** BaySIM800::getConfigPointer(uint8_t index) { + switch (index) { + case BaySIM800_CONFIG_URL: + return &_url; + break; + case BaySIM800_CONFIG_USER: + return &_user; + break; + case BaySIM800_CONFIG_PASSWORD: + return &_password; + break; + case BaySIM800_CONFIG_SENDER: + return &_sender; + break; + case BaySIM800_CONFIG_APN: + return &_apn; + break; + case BaySIM800_CONFIG_PROVPW: + return &_prov_pw; + break; + case BaySIM800_CONFIG_PROVUSER: + return &_prov_user; + break; + case BaySIM800_CONFIG_PIN: + return &_pin; + break; + } + return 0; + +} + +void BaySIM800::setConfigPointers(void) { + uint8_t offset = 0; + _url = _config_buffer; + char** p; + for (uint8_t i = 1; i < 8; i++) { + if (_config_buffer[offset]) + offset += strlen(_config_buffer + offset) + 1; + else + offset++; + p = getConfigPointer(i); + *p = _config_buffer + offset; + } +} + +uint8_t BaySIM800::wait_for_available(uint16_t* timeout, int bytes) { + while (_serial->available() < bytes) { + delay(1); + (*timeout)--; + if (*timeout == 0) { + return 1; + } + } + return 0; +} + +uint8_t BaySIM800::wait_forOK(uint16_t timeout) { + return wait_forPGM(PSTR("OK"), timeout); +} + +uint8_t BaySIM800::wait_forPGM(const char* str, uint16_t timeout, + uint8_t bytes, char* buffer) { + uint8_t length = 0; + while (true) { + _base64buffer[length] = pgm_read_byte(str); + if (!_base64buffer[length]) + break; + str++; + length++; + } + + if (wait_for_available(&timeout, length)) + return 1; + uint8_t offset = 0; + char c; + while (true) { + if (wait_for_available(&timeout)) + return 1; + c = _serial->read(); + if (offset < length) { + if (c == _base64buffer[offset]) + offset++; + else + offset = 0; + } + + if (offset == length) { + if (bytes && buffer != NULL) { + if (wait_for_available(&timeout, bytes)) + return 1; + offset = 0; + while (offset < bytes) { + buffer[offset] = _serial->read(); + offset++; + } + buffer[offset] = 0; + } + return 0; + } + + } +} + + diff --git a/libraries/BaySIM800/BaySIM800.h b/libraries/BaySIM800/BaySIM800.h new file mode 100644 index 0000000..476f648 --- /dev/null +++ b/libraries/BaySIM800/BaySIM800.h @@ -0,0 +1,140 @@ +#ifndef BaySIM800_h +#define BaySIM800_h + +#define BaySIM800_BUFFER 138 +#define BaySIM800_CONFIG_URL 0 +#define BaySIM800_CONFIG_USER 1 +#define BaySIM800_CONFIG_PASSWORD 2 +#define BaySIM800_CONFIG_SENDER 3 +#define BaySIM800_CONFIG_APN 4 +#define BaySIM800_CONFIG_PROVUSER 5 +#define BaySIM800_CONFIG_PROVPW 6 +#define BaySIM800_CONFIG_PIN 7 +#define BaySIM800_CONFIG_SIZE 140 + + +#define printP(x) printPGM(PSTR(x)) +#define printlnP(x) printlnPGM(PSTR(x)) +#define printlnP_OK(x,y) printlnPGM(PSTR(x),y) +#define wait_for(x,y) wait_forPGM(PSTR(x),y) + + +#include +#include + +class BaySIM800 : public BayEOS { +public: + BaySIM800(HardwareSerial& serial); + + /** + * read config from string - use | as field delimiter + * url|user|password|Sender|APN|APN-USER|APN-PW|PIN| + * + */ + void readConfigFromStringPGM(const char* string); + + + /** + * Send several frames in one post request + * returns 0 for success + * 1 == No HTTP 200 + * 2 == No connect to network + * 3 == no connect to server + * 4 == could not send data to moden + * 6 and more == result code of init()+5 + */ + uint8_t sendMultiFromBuffer(uint16_t maxsize=5000,bool ack_payload=false); + uint8_t sendPayloadWithAck(bool ack_payload=true); + uint8_t sendPayload(void); + + /** + * Switch on GPRS-Modem + * 0 == GPRS-modem is up and responding OK + * 1 == Communication-ERROR + * 2 == PIN failed + * 3 == PIN locked + * 4 == Not CREG + * 5 == Not CGATT + * 6 == No SIM Card + * + * if unlock_only is set, function returns already after unlocking the modem + */ + uint8_t begin(long baud); + uint8_t init(void); + + /* + * Change the baud rate of the Modem + * 0 == OK + * 1 == Failed + */ + uint8_t changeIPR(long baud); + + /** + * Checks if modem is registered to network + */ + uint8_t isRegistered(void); + + /** + * Checks if modem is attached to network + */ + uint8_t isAttached(void); + + /** + * Disconnect from the web + */ + uint8_t getRSSI(void); + + /** + * Get time from RTC + */ + DateTime now(void); + +protected: + void setConfigPointers(void); + /** + * get a pointer to a specific config buffer entry + * e.g Serial.print(*getConfigPointer(APN)); + */ + char** getConfigPointer(uint8_t index); + + /** + * Helper functions for different Post requests + */ + uint8_t postHeader(uint16_t size); + uint8_t post(void); + void readAck(void); + + + + HardwareSerial* _serial; //Pointer to existing serial object!! + long _baud; + + void skipChars(void); + uint8_t wait_forOK(uint16_t timeout); + uint8_t wait_forPGM(const char* str, uint16_t timeout,uint8_t bytes=0, char* buffer=NULL); + uint8_t wait_for_available(uint16_t* timeout,int bytes=1); + void printPGM(const char *str); + void printlnPGM(const char *str); + uint8_t printlnPGM(const char *str, uint16_t timeout); + void urlDecode(char *str); + int strlenURLencoded(const char *str); + void printURLencoded(const char *str); + + char _config_buffer[BaySIM800_CONFIG_SIZE]; + char* _url; //0 + char* _user; //1 + char* _password; //2 + char* _sender; //3 + char* _apn; //4 + char* _prov_user; //5 + char* _prov_pw; //6 + char* _pin; //7 + char _base64buffer[BaySIM800_BUFFER]; + uint8_t _tx_error_count; + uint16_t _mtu; + static const char* const _urlencodedChars; + +}; + + +#endif diff --git a/libraries/BaySIM800/examples/Test/Test.ino b/libraries/BaySIM800/examples/Test/Test.ino new file mode 100644 index 0000000..4a8ae8a --- /dev/null +++ b/libraries/BaySIM800/examples/Test/Test.ino @@ -0,0 +1,45 @@ +#include +#include + +BaySIM800 client = BaySIM800(Serial); +SPIFlash flash(8); +BayEOSBufferSPIFlash myBuffer; + +void blink(uint8_t times) { + pinMode(LED_BUILTIN, OUTPUT); + while (true) { + digitalWrite(LED_BUILTIN, HIGH); + delay(200); + digitalWrite(LED_BUILTIN, LOW); + times--; + if (! times) return; + delay(500); + } +} + +void setup(void) { + Serial.begin(38400); + Serial.println("starting"); + delay(100); + // client.softSwitch(); + pinMode(7, OUTPUT); + digitalWrite(7, HIGH); + client.readConfigFromStringPGM( + PSTR("http://bayeos.bayceer.uni-bayreuth.de/gateway/frame/saveFlat|import@IT|import|SIM800_HTTPS|iot.1nce.net||||")); + myBuffer.init(flash); //This will restore old pointers + //myBuffer.reset(); //This will set all pointers to zero + myBuffer.skip(); //This will move read pointer to write pointer + client.setBuffer(myBuffer); + blink(client.begin(38400) + 1); +} + +void loop(void) { + //Construct DataFrame + client.startDataFrame(BayEOS_Float32le); + client.addChannelValue(millis() / 1000); + client.writeToBuffer(); + blink(client.sendMultiFromBuffer() + 1); + + delay(5000); + +} diff --git a/libraries/BaySIM800/examples/TestAckPayload/TestAckPayload.ino b/libraries/BaySIM800/examples/TestAckPayload/TestAckPayload.ino new file mode 100644 index 0000000..4e3d391 --- /dev/null +++ b/libraries/BaySIM800/examples/TestAckPayload/TestAckPayload.ino @@ -0,0 +1,66 @@ +#include +#include + +BaySIM800 client = BaySIM800(Serial); +SPIFlash flash(8); +BayEOSBufferSPIFlash myBuffer; + +void blink(uint8_t times) { + pinMode(LED_BUILTIN, OUTPUT); + while (true) { + digitalWrite(LED_BUILTIN, HIGH); + delay(200); + digitalWrite(LED_BUILTIN, LOW); + times--; + if (! times) return; + delay(500); + } +} + +void setup(void) { + //Serial.begin(9600); + // client.softSwitch(); + pinMode(7, OUTPUT); + digitalWrite(7, HIGH); + pinMode(A3, OUTPUT); + pinMode(A2, OUTPUT); + digitalWrite(A3, HIGH); + client.readConfigFromStringPGM( + PSTR("http://132.180.112.128/gateway/frame/saveFlat|import@IT|import|SIM800-ACK1|iot.1nce.net||||")); + myBuffer.init(flash); //This will restore old pointers + //myBuffer.reset(); //This will set all pointers to zero + myBuffer.skip(); //This will move read pointer to write pointer + client.setBuffer(myBuffer); + blink(client.begin(38400) + 1); + digitalWrite(A3, LOW); + +} + +void loop(void) { + //Construct DataFrame + client.startDataFrame(BayEOS_Float32le); + client.addChannelValue(millis() / 1000); + client.writeToBuffer(); + blink(client.sendMultiFromBuffer(3000,true) + 1); + + if (client.getPacketLength()) { + delay(2000); + blink(client.getPacketLength()); + //Got a AckPayload + if (client.getPayload(0) == BayEOS_Action) { + uint8_t pin = client.getPayload(1); + uint8_t value = client.getPayload(2); + if (pin < 5 && pin > 1) { + pinMode(pin, OUTPUT); + digitalWrite(pin, value); + client.createActionResponse(pin, BayEOS_ActionSuccess); + } else { + client.createActionResponse(pin, BayEOS_ActionFailed); + client.addToPayload("No valid pin"); //additional error information + } + client.writeToBuffer(); //will be send with next send action. + } + } + delay(5000); + +} diff --git a/libraries/BayTCP/BayTCP.cpp b/libraries/BayTCP/BayTCP.cpp index f79d50c..7d8498e 100644 --- a/libraries/BayTCP/BayTCP.cpp +++ b/libraries/BayTCP/BayTCP.cpp @@ -171,7 +171,7 @@ void BayTCPInterface::printPostHeader(uint16_t size) { strcat(_base64buffer+70, ":"); strcat(_base64buffer+70, _password); base64_encode(_base64buffer, (char*) _base64buffer+70, strlen(_base64buffer+70)); - _base64buffer[base64_enc_len(strlen(_base64buffer+70))] = 0; + //_base64buffer[base64_enc_len(strlen(_base64buffer+70))] = 0; println(_base64buffer); //BASE64 #if BayTCP_DEBUG_INPUT BayTCP_DEBUG_INTERFACE.println(_base64buffer); @@ -216,8 +216,7 @@ uint8_t BayTCPInterface::sendMultiFromBuffer(uint16_t maxsize) { size += 30; //Double size and add 30 - This is just a guess. "bayeosframes[]=base64(frame)" if (size < 150) size = 150; //To small sizes may cause a problem because "bayeosframe[]=" does not fit in... - size += 7 + strlenURLencoded(_sender) + 1 + 9 - + strlenURLencoded(_password); + size += 7 + strlenURLencoded(_sender); // + 1 + 9 + strlenURLencoded(_password); if (size > maxsize) size = maxsize; } @@ -227,10 +226,9 @@ uint8_t BayTCPInterface::sendMultiFromBuffer(uint16_t maxsize) { //Send Body - first part (sender) printP("sender="); printURLencoded(_sender); - printP("&password="); - printURLencoded(_password); - uint16_t postsize = 7 + strlenURLencoded(_sender) + 1 + 9 - + strlenURLencoded(_password); +// printP("&password="); +// printURLencoded(_password); + uint16_t postsize = 7 + strlenURLencoded(_sender); //+ 1 + 9 + strlenURLencoded(_password); uint16_t mtusize = postsize; //Send Body - second part (frames) @@ -241,7 +239,7 @@ uint8_t BayTCPInterface::sendMultiFromBuffer(uint16_t maxsize) { mtusize = 0; } base64_encode(_base64buffer, (char*) _payload, getPacketLength()); - _base64buffer[base64_enc_len(getPacketLength())] = 0; +// _base64buffer[base64_enc_len(getPacketLength())] = 0; framesize = 1 + 15 + strlenURLencoded(_base64buffer); postsize += framesize; if (postsize <= size) { //Still space in the POST for the frame @@ -308,8 +306,7 @@ uint8_t BayTCPInterface::sendMultiFromBufferWithAckPayload(uint16_t maxsize) { size += 30; //Double size and add 30 - This is just a guess. "bayeosframes[]=base64(frame)" if (size < 150) size = 150; //To small sizes may cause a problem because "bayeosframe[]=" does not fit in... - size += 7 + strlenURLencoded(_sender) + 1 + 9 - + strlenURLencoded(_password); + size += 7 + strlenURLencoded(_sender); // + 1 + 9 + strlenURLencoded(_password); if (size > maxsize) size = maxsize; } @@ -319,10 +316,9 @@ uint8_t BayTCPInterface::sendMultiFromBufferWithAckPayload(uint16_t maxsize) { //Send Body - first part (sender) printP("sender="); printURLencoded(_sender); - printP("&password="); - printURLencoded(_password); - uint16_t postsize = 7 + strlenURLencoded(_sender) + 1 + 9 - + strlenURLencoded(_password); +// printP("&password="); +// printURLencoded(_password); + uint16_t postsize = 7 + strlenURLencoded(_sender); // + 1 + 9 + strlenURLencoded(_password); uint16_t mtusize = postsize; //Send Body - second part (frames) @@ -333,7 +329,7 @@ uint8_t BayTCPInterface::sendMultiFromBufferWithAckPayload(uint16_t maxsize) { mtusize = 0; } base64_encode(_base64buffer, (char*) _payload, getPacketLength()); - _base64buffer[base64_enc_len(getPacketLength())] = 0; + //_base64buffer[base64_enc_len(getPacketLength())] = 0; framesize = 1 + 15 + strlenURLencoded(_base64buffer); postsize += framesize; if (postsize <= size) { //Still space in the POST for the frame @@ -406,22 +402,22 @@ uint8_t BayTCPInterface::sendPayload(void) { } base64_encode(_base64buffer, (char*) _payload, getPacketLength()); - _base64buffer[base64_enc_len(getPacketLength())] = 0; + //_base64buffer[base64_enc_len(getPacketLength())] = 0; uint8_t size = strlenURLencoded(_base64buffer); - size += 7 + strlenURLencoded(_sender) + 1 + 9 + strlenURLencoded(_password) + size += 7 + strlenURLencoded(_sender) //+ 1 + 9 + strlenURLencoded(_password) + 1 + 15; printPostHeader(size); //Redo Base64 encoding! buffer is overwritten in printPostHeader! base64_encode(_base64buffer, (char*) _payload, getPacketLength()); - _base64buffer[base64_enc_len(getPacketLength())] = 0; + //_base64buffer[base64_enc_len(getPacketLength())] = 0; printP("sender="); printURLencoded(_sender); - printP("&password="); - printURLencoded(_password); +// printP("&password="); +// printURLencoded(_password); printP("&bayeosframes[]="); printURLencoded(_base64buffer); //BASE64 println(); diff --git a/libraries/BayTCP/BayTCP.h b/libraries/BayTCP/BayTCP.h index 4368e0b..3acb150 100644 --- a/libraries/BayTCP/BayTCP.h +++ b/libraries/BayTCP/BayTCP.h @@ -12,7 +12,7 @@ #ifndef BayTCP_BUFFER -#define BayTCP_BUFFER 137 +#define BayTCP_BUFFER 138 #endif #define BayTCP_CONFIG_SERVER 0 diff --git a/libraries/BayTCPSim900/BayTCPSim900.cpp b/libraries/BayTCPSim900/BayTCPSim900.cpp index 0c4714f..837a82c 100644 --- a/libraries/BayTCPSim900/BayTCPSim900.cpp +++ b/libraries/BayTCPSim900/BayTCPSim900.cpp @@ -21,17 +21,23 @@ uint8_t BayGPRSInterface::changeIPR(long baud) { for (uint8_t i = 0; i < 4; i++) { i_begin(t_baud[i]); skipChars(); - printP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! - delay(100); - println(); - printP("AT+IPR="); - println(_baud); - if (!wait_forOK(200)) { - i_end(); - i_begin(_baud); - return 0; - } + uint8_t tries=5; + do { + printlnP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! + if(!wait_forOK(1000)){ + printP("AT+IPR="); + println(_baud); + if (!wait_forOK(200)) { + i_end(); + i_begin(_baud); + return 0; + } + + } + tries--; + } while(tries); i_end(); + } return 1; @@ -40,77 +46,60 @@ uint8_t BayGPRSInterface::init(uint8_t unlock_only){ uint8_t count=0; init_start: uint8_t i=0; - printP("AT"); //Wake up - delay(200); - println(); - wait_forOK(1000); - printlnP("AT"); //Check communication - if(! wait_forOK(200)){ - //communication ok! - printlnP("ATE0"); //Command echo off - wait_forOK(500); - printlnP("AT+CSCLK=2"); //Auto-Sleepmode - wait_forOK(500); - //Check PIN + skipChars(); + printlnP("AT"); //Wake up + wait_forOK(200); + printlnP("AT"); + if(wait_forOK(1000)) return 1; + //communication ok! + printlnP("ATE0"); //Command echo off + wait_forOK(500); + printlnP("AT+CSCLK=0"); //Auto-Sleepmode + wait_forOK(500); + //Check PIN + printlnP("AT+CPIN?"); + while(wait_forPGM(PSTR("+CPIN: "),5000,7,_base64buffer)){ + printlnP("AT"); + wait_forOK(200); + printlnP("AT+CFUN=0"); + //Disable + wait_forOK(10000); + printlnP("AT+CFUN=1"); + //delay(2000); + //Enable + wait_forOK(10000); + printlnP("AT"); + wait_forOK(200); printlnP("AT+CPIN?"); - while(wait_forPGM(PSTR("+CPIN: "),5000,7,_base64buffer)){ - printlnP("AT"); - wait_forOK(200); - printlnP("AT+CFUN=0"); - //Disable - wait_forOK(10000); - printlnP("AT+CFUN=1"); - //delay(2000); - //Enable - wait_forOK(10000); - printlnP("AT"); - wait_forOK(200); - printlnP("AT+CPIN?"); - i++; - if(i>2) return 6; - } - if(_base64buffer[5]=='U') return 3; //SIM PUK - if(_base64buffer[5]=='I'){ //SIM PIN - printlnP("AT"); - wait_forOK(200); - printP("AT+CPIN=\""); - print(_pin); - println("\""); - if(wait_forOK(30000)) { - return 2; //Wrong PIN - } - wait_for("SMS Ready",(unlock_only?5000:60000)); - } - //Return here - Moden will try to connect and attach in the background! - if(unlock_only) return 0; - // Waiting for Modem to Connect - for(i=0;i<127;i++){ - if(isRegistered()) break; - delay(500); - } - if(i==127) return 4; - for(i=0;i<127;i++){ - if(isAttached()) break; - delay(500); + i++; + if(i>2) return 6; + } + if(_base64buffer[5]=='U') return 3; //SIM PUK + if(_base64buffer[5]=='I'){ //SIM PIN + printlnP("AT"); + wait_forOK(200); + printP("AT+CPIN=\""); + print(_pin); + println("\""); + if(wait_forOK(30000)) { + return 2; //Wrong PIN } - if(i==127) return 5; - return 0; + wait_for("SMS Ready",(unlock_only?5000:60000)); } - - softReset(); - softSwitch(); - i_begin(_baud); - skipChars(); - printP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! - delay(100); - println(); - printP("AT+IPR="); - println(_baud); - wait_forOK(200); - count++; - - if(count>1) return 1; - goto init_start; + //Return here - Moden will try to connect and attach in the background! + if(unlock_only) return 0; + // Waiting for Modem to Connect + for(i=0;i<127;i++){ + if(isRegistered()) break; + delay(500); + } + if(i==127) return 4; + for(i=0;i<127;i++){ + if(isAttached()) break; + delay(500); + } + if(i==127) return 5; + return 0; } void BayGPRSInterface::softSwitch(void){ @@ -294,6 +283,8 @@ void BayGPRSInterface::disconnect(void){ //init(); printlnP("AT+CIPCLOSE"); wait_forOK(2000); + printlnP("AT+CSCLK=2"); //Auto-Sleepmode + wait_forOK(500); // printlnP("AT+CIPSHUT"); // wait_forOK(2000); } @@ -317,12 +308,10 @@ uint8_t BayGPRSInterface::begin(long baud,uint8_t unlock_only){ _baud=baud; i_begin(_baud); skipChars(); - printP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! - delay(100); - println(); - printP("AT+IPR="); - println(_baud); - if(wait_forOK(200)) changeIPR(baud); + printlnP("AT"); //Wake up + wait_forOK(200); + printlnP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode! + if(wait_forOK(1000)) changeIPR(baud); return init(unlock_only); } diff --git a/libraries/HX711Array/examples/calibration/calibration.ino b/libraries/HX711Array/examples/calibration/calibration.ino index 0c690a1..5409750 100644 --- a/libraries/HX711Array/examples/calibration/calibration.ino +++ b/libraries/HX711Array/examples/calibration/calibration.ino @@ -17,6 +17,7 @@ HX711_4PointCal scale; float sw = 3.00; float t[] = { 10.0, 20.0 }; long cal_adc[] = { -68137L, -73803, -378845L, -384611L }; +float t_coef = 0; volatile uint8_t tics; @@ -30,21 +31,21 @@ void setup() { // put your setup code here, to run once: printHelp(); Serial.flush(); - Sleep.setupTimer2(2); //init timer2 to 1/16 sec + Sleep.setupTimer2(2); //init timer2 to 1/16 sec scale.set_no_sleep(1); scale.begin(dout, 1, sck); //start HX711Array with one ADCs scale.power_up(); - } +String s; long adc; -char c=0; +char c = 0; void loop() { Serial.flush(); - char cc=0; - if (Serial.available()){ + char cc = 0; + if (Serial.available()) { cc = Serial.read(); - if ((cc >= 'a' && cc <= 'z') || (cc >= '0' && cc <= '1')) c = cc; + if ((cc >= 'a' && cc <= 'z') || (cc >= 'A' && cc <= 'Z') || (cc >= '0' && cc <= '1')) c = cc; } switch (c) { @@ -69,8 +70,9 @@ void loop() { Serial.flush(); adc = scale.read_average(50); Serial.println(adc); - cal_adc[0] = adc; cal_adc[1] = adc; + cal_adc[0] = cal_adc[1] - t_coef * 10; + c = '0'; break; case 'w': @@ -78,8 +80,9 @@ void loop() { Serial.flush(); adc = scale.read_average(50); Serial.println(adc); - cal_adc[2] = adc; cal_adc[3] = adc; + cal_adc[2] = cal_adc[3] - t_coef * 10; + c = '0'; break; case 'r': @@ -104,13 +107,29 @@ void loop() { } Serial.println("Please enter calibration weight"); while (Serial.available() < 3) {} - String s = Serial.readString(); + s = Serial.readString(); s.trim(); sw = s.toFloat(); Serial.print("Calibration weight: "); Serial.println(sw); c = '0'; break; + case 'T': + while (Serial.available()) { + delay(1); + Serial.read(); + } + Serial.println("Please enter "); + while (Serial.available() < 3) {} + s = Serial.readString(); + s.trim(); + t_coef = s.toFloat(); + Serial.print("Calibration weight: T-coefficient ADC/°C"); + Serial.println(t_coef); + cal_adc[2] = cal_adc[3] - t_coef * 10; + cal_adc[0] = cal_adc[1] - t_coef * 10; + c = '0'; + break; case '0': break; } @@ -122,6 +141,7 @@ void printHelp(void) { Serial.println("z: Zero calibration"); Serial.println("w: Weight calibration"); Serial.println("c: Set calibration weight"); + Serial.println("T: Set T-coefficient ADC/°C"); Serial.println("p: Print calibration"); Serial.println("r: Read calibration from EEPROM"); Serial.println("s: Save calibration to EEPROM");