Project to use SF weather shield over cell link
Dependencies: MPL3115A2_for_weather_shield htu21d_for_weather_shield mbed
Revision 0:924cb994fc16, committed 2016-10-20
- Comitter:
- isaackod
- Date:
- Thu Oct 20 16:49:21 2016 +0000
- Commit message:
- First commit. Cell not fully implemented yet
Changed in this revision
diff -r 000000000000 -r 924cb994fc16 CELL_CTRL/cell_ctrl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CELL_CTRL/cell_ctrl.cpp Thu Oct 20 16:49:21 2016 +0000 @@ -0,0 +1,100 @@ + +#include "cell_ctrl.h" + +//These are functions to connect the modem to a port, and to reset the modem. + +void cell_ctrl_init(Serial* io, Serial pc, char* buffer){ + // Enable echo + io->printf("ATE1\r"); + collectResponseString(io, buffer, BUFFER_SIZE, TIMEOUT_MS, 0); + pc.printf("%s", buffer); + + // Disable the forwarding of +++ to the actual data connection + io->printf("AT#SKIPESC=1\r"); + collectResponseString(io, buffer, BUFFER_SIZE, TIMEOUT_MS, 0); + pc.printf("%s", buffer); + + // Flow control (none) + //io->printf("AT+IFC=2,2\r"); + io->printf("AT+IFC=0,0\r"); + collectResponseString(io, buffer, BUFFER_SIZE, TIMEOUT_MS, 0); + pc.printf("%s", buffer); + + io->printf("AT+CSQ\r"); + collectResponseString(io, buffer, BUFFER_SIZE, TIMEOUT_MS, 0); + pc.printf("%s", buffer); + + io->printf("AT+CDV*22899\r"); + collectResponseString(io, buffer, BUFFER_SIZE, TIMEOUT_MS, 0); + pc.printf("%s", buffer); + + // Config socket + io->printf("AT#SCFG=1,1,%i,%i,%i,1\r", + PACKET_SIZE, + int(INACTIVITY_TIMEOUT), + int(CONNECTION_TIMEOUT)); + collectResponseString(io, buffer, BUFFER_SIZE, TIMEOUT_MS, 0); + pc.printf("%s", buffer); + + // Set context (ppp?) + io->printf("AT#SGACT=1,1\r"); + collectResponseString(io, buffer, BUFFER_SIZE, 30000, 0); + pc.printf("%s", buffer); + + // Connect to TCP server + io->printf("AT#SD=1,0,%i,\"%s\",0,1,0\r", PORT, ADDRESS); + collectResponseString(io, buffer, BUFFER_SIZE, 30000, '\n'); + pc.printf("%s", buffer); + + pc.printf("Entering message loop. Now in data mode.\r\n"); + +} + +void cell_ctrl_rst(Serial* io, Serial pc, char* buffer){ + // Send escape sequence + wait(1.5); + io->printf("+++"); + wait(1.5); + collectResponseString(io, buffer, BUFFER_SIZE, 15000, 0); + pc.printf("%s", buffer); + + // Get socket status + io->printf("AT#SS\r"); + collectResponseString(io, buffer, BUFFER_SIZE, 1000, 0); + pc.printf("%s", buffer); + + // Reboot the modem + pc.printf("Performing hard reset of the modem and waiting 10 seconds. \r\n"); + io->printf("AT#REBOOT\r"); + collectResponseString(io, buffer, BUFFER_SIZE, 1000, 0); + pc.printf("%s", buffer); + + wait(10.0); + + pc.printf("Restarting the program...\r\n"); + +} + +bool collectResponseString(Serial* io, char* buffer, int size, int timeout_ms, char terminator) { + mbed::Timer tmr; + tmr.start(); + int i = 0; + char byte; + bool notimeout = true; + while (i < size-1) { + if (tmr.read_ms() > timeout_ms) { + notimeout = false; + break; + } + if (io->readable()) { + byte = io->getc(); + buffer[i] = byte; + i++; + if (byte == terminator) { + break; + } + } + } + buffer[i] = '\0'; + return notimeout; +} \ No newline at end of file
diff -r 000000000000 -r 924cb994fc16 CELL_CTRL/cell_ctrl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CELL_CTRL/cell_ctrl.h Thu Oct 20 16:49:21 2016 +0000 @@ -0,0 +1,20 @@ +#ifndef CELL_H +#define CELL_H + +#include "mbed.h" + +#define BUFFER_SIZE 1024 +#define TIMEOUT_MS 1000 +#define PORT 9873 +#define INACTIVITY_TIMEOUT 20.0 +#define CONNECTION_TIMEOUT 20.0 +#define PACKET_SIZE 1428 +#define ADDRESS "52.36.17.98" + +//io connects with the modem, pc is for debugging +void cell_ctrl_init(Serial* io, Serial pc); +bool collectResponseString(Serial* io,char* buffer, int size, int timeout_ms, char terminator); +void cell_ctrl_rst(Serial* io, Serial pc, char* buffer); + + +#endif \ No newline at end of file
diff -r 000000000000 -r 924cb994fc16 MPL3115A2.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPL3115A2.lib Thu Oct 20 16:49:21 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/isaackod/code/MPL3115A2_for_weather_shield/#9ba8fde79093
diff -r 000000000000 -r 924cb994fc16 htu21d.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htu21d.lib Thu Oct 20 16:49:21 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/isaackod/code/htu21d_for_weather_shield/#fe060047cc24
diff -r 000000000000 -r 924cb994fc16 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 20 16:49:21 2016 +0000 @@ -0,0 +1,86 @@ +#include "mbed.h" +#include "MPL3115A2.h" +#include "htu21d.h" + + +I2C i2c(I2C_SDA, I2C_SCL); // sda, scl +Serial pc(USBTX, USBRX); // tx, rx +MPL3115A2 mpl(&i2c, &pc); +htu21d htu(i2c); +DigitalOut bled(PTC3); +DigitalOut gled(PTC12); +AnalogIn light(A1); +AnalogIn lightref(A3); + +float readLightSensor(AnalogIn sig, AnalogIn ref); + +int main() { + pc.baud(115200); + wait_ms(300); + gled.write(1); + bled.write(1); + + + pc.printf("** Temperature Shield **\r\n"); + + + // Initialize the MPL3115A2 pressure and temp. + Temperature t; + Pressure p; + + mpl.init(); + pc.printf("MPL3115A2 ID: 0x%X\r\n", mpl.whoAmI()); + // Offsets for Dacula, GA + mpl.setOffsetTemperature(20); + mpl.setOffsetPressure(-32); + + + //initialize the HTU21D humidity and temp. + float H21Temp = 0.0; //Temperture from HTU21D + float H21Hum = 0.0; //Humidity from HTU21D + int htu21 = htu.softReset(); + if(htu21 == 0){ + pc.printf(" - HTU21D broken...\r\n"); + } + else{ + uint8_t HTU21DuserReg = htu.getUserReg(); + pc.printf("HTU21D UserReg: 0x%02x SN: 0x%04x %08x %04x\r\n", + HTU21DuserReg, htu.HTU21sn.HTU21D_sna, htu.HTU21sn.HTU21D_snb, htu.HTU21sn.HTU21D_snc); + } + + //main loop + while(1) + { + //read mpl + bled = 0; + wait(1); + mpl.readTemperature(&t); + mpl.readPressure(&p); + bled = 1; + pc.printf("Temp: %s C, Pressure: %sPa \r\n", t.print(), p.print()); + + //read htu + gled = 0; + wait(1); + if(htu21 == 1) { //if HTU21D didn't initialize, don't access HTU21D anymore + H21Hum = htu.getHum(); + if((double)H21Hum == 255.0) pc.printf("\r\n*** HTU21D Hum error!!\r\n"); + H21Temp = htu.getTemp(); + if((double)H21Temp == 255.0) pc.printf("\r\n*** HTU21D Temp error!!\r\n"); + } + gled = 1; + pc.printf("Temp: %7.2f C Hum: %4.1f %% \r\n ", H21Temp, H21Hum); + + //read photoresistor + wait(1); + float lightLvl = readLightSensor(light,lightref); + pc.printf("Light: %3.2f \r\n", lightLvl); + + } +} + +float readLightSensor(AnalogIn sig, AnalogIn ref){ + float opVoltage = 3.3/ref.read(); + float lightSen = opVoltage *sig.read(); + return lightSen; + } \ No newline at end of file
diff -r 000000000000 -r 924cb994fc16 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Oct 20 16:49:21 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file