Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Pulse SDFileSystem TinyJpgDec mbed-rtos mbed
Revision 0:214db1fdfc95, committed 2015-06-12
- Comitter:
- metabi814
- Date:
- Fri Jun 12 10:07:59 2015 +0000
- Commit message:
- sim900 socket and websocket project
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Pulse.lib Fri Jun 12 10:07:59 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/NickRyder/code/Pulse/#fb79a4637a64
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Fri Jun 12 10:07:59 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/neilt6/code/SDFileSystem/#c2c1f0b16380
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TinyJpgDec.lib Fri Jun 12 10:07:59 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/humlet/code/TinyJpgDec/#b6f284347a66
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Jun 12 10:07:59 2015 +0000
@@ -0,0 +1,314 @@
+#include "mbed.h"
+#include "modem.h"
+#include "rtos.h"
+#include "math.h"
+/* DEFINICION DE PUERTOS */
+DigitalOut led1(LED1);
+DigitalOut power(PA_13);
+DigitalOut sound(PA_14);
+/* */
+Serial pc(USBTX,USBRX);
+char res[32];
+Modem modem(PA_11,PA_12,19200);
+/*Sortie Digital */
+DigitalOut ch1(PC_3);
+DigitalOut ch2(PC_2);
+DigitalOut ch3(PH_1);
+DigitalOut ch4(PH_0);
+DigitalOut ch5(PC_15);
+DigitalOut ch6(PC_14);
+DigitalOut ch7(PC_13);
+DigitalOut ch8(PB_7);
+/*Entre analogic */
+AnalogIn tmp1(PC_0);
+AnalogIn tmp2(PC_1);
+AnalogIn tmp3(PB_0);
+AnalogIn tmp4(PA_4);
+AnalogIn tmp5(PA_1);
+AnalogIn light(PA_0);
+/*variables globales */
+int st[8]= {0}; // state de relay
+//double ADC[6]= {0}; // sensor temperature et 1 sensor lumiere ADC[5]
+
+int x;
+class sim900
+{
+
+public:
+ int on(void);
+ int off(void);
+ void speak(int tmp);
+ int relay(int rel,int state,int seg); //1 gsm , 2 gprs
+ float read_Temp(int n_sensor);
+};
+
+class gprs
+{
+public:
+};
+
+class sms:sim900
+{
+public:
+ int send_m(char *num,char *text);
+ int read_m(void);
+ int sms_cmd(void);
+};
+
+/*definicion de clases*/
+int sim900::on()
+{
+ int i=3;
+ i=modem.sendCmdAndWaitForResp("AT\r", "OK", DEFAULT_TIMEOUT,CMD);
+ if(i==0) {
+ led1=1;
+ speak(2);
+ return 0; //ok encendido
+ } else {
+ power=1;
+ wait(0.5);
+ power=0;
+ wait(3);
+ i=modem.sendCmdAndWaitForResp("AT\r", "OK", DEFAULT_TIMEOUT,CMD);
+ if(i==0) {
+ led1=1;
+ speak(2);
+ } else {
+ led1=0;
+ speak(4);
+ return -1;
+ }
+ return 0;
+ }
+
+}
+int sim900::off()
+{
+ int i=3;
+ i=modem.sendCmdAndWaitForResp("AT\r", "OK", DEFAULT_TIMEOUT,CMD);
+ if(i==0) {
+ speak(4);
+ power=1;
+ wait(1);
+ power=0;
+ led1=0;
+ return 0; //ok encendido
+ } else {
+ i=modem.sendCmdAndWaitForResp("AT\r", "OK", DEFAULT_TIMEOUT,CMD);
+ if(i==0) {
+ speak(4);
+ power=1;
+ wait(1);
+ power=0;
+ led1=0;
+ return 0; //ok encendido
+ } else {
+ led1=0;
+ speak(4);
+ }
+ return -1; // no apagado
+ }
+
+}
+
+void sim900::speak(int tmp)
+{
+ for(int i=0; i<tmp; i++) {
+ sound=1;
+ wait(0.3);
+ sound=0;
+ wait(0.3);
+ }
+}
+
+int sim900::relay(int rel,int state,int seg)
+{
+ switch (rel) {
+ case 1:
+ ch1=state;
+ break;
+ case 2:
+ ch2=state;
+ break;
+ case 3:
+ ch3=state;
+ break;
+ case 4:
+ ch4=state;
+ break;
+ case 5:
+ ch5=state;
+ break;
+ case 6:
+ ch6=state;
+ break;
+ case 7:
+ ch7=state;
+ break;
+ case 8:
+ ch8=state;
+ break;
+ }
+ wait(1);
+ ch1=0;
+ ch2=0;
+ ch3=0;
+ ch4=0;
+ return 0;
+}
+
+float sim900::read_Temp(int n_sensor)
+{
+ double tr,y,temp;
+ uint16_t meas=0;
+ float Valim=3300;
+ float ADCres=65535;
+ double Result_V = 0;
+ double MinStepRes = 0;
+
+ meas=tmp1.read_u16();
+
+ switch (n_sensor) {
+ case 1:
+ meas=tmp1.read_u16();
+ break;
+ case 2:
+ meas=tmp2.read_u16();
+ break;
+ case 3:
+ meas=tmp3.read_u16();
+ break;
+ case 4:
+ meas=tmp4.read_u16();
+ break;
+ case 5:
+ meas=tmp5.read_u16();
+ break;
+ case 6:
+ meas=light.read_u16();
+ break;
+ }
+
+ MinStepRes = (Valim / ADCres);
+ Result_V = ((MinStepRes * meas))/1000.0;
+ tr=(Result_V*47000.0)/(3.3-Result_V);
+ y=log(tr/47000.0);
+ y=(1.0/298.15)+(y*(1.0/4450.0));
+ temp=1.0/y;
+ temp=temp-273.15;
+ pc.printf("Temp=%f\r\n",temp);
+ return temp;
+}
+
+int sms::send_m(char *num,char *text)
+{
+ char aux_string[22]= {0};
+ int answer;
+ modem.sendCmd("AT+CMGF=1\r");
+ wait(1);
+ sprintf(aux_string,"AT+CMGS=\"%s\"\r",num);
+ modem.sendCmd(aux_string);
+ wait(1);
+ modem.sendCmd(text);
+ wait(1);
+ answer=modem.sendCmdAndWaitForResp("\x1A", "OK", DEFAULT_TIMEOUT,CMD);
+ if(answer==0) {
+ pc.printf("message envoye");
+ } else {
+ pc.printf("erreur ctrl z");
+ }
+ return 0;
+}
+
+
+
+int sms::read_m()
+{
+ char m[200]= {0};
+ char *p;
+ char CH1[5]= {0},CH2[5]= {0},CH3[5]= {0},CH4[5]= {0},CH5[5]= {0},CH6[5]= {0},CH7[5]= {0},CH8[1]= {0};
+ char phone_number[]="+33000000000";
+ int r;
+ r=modem.sendCmdAndWaitForResp("AT+CMGR=1\r","+CMGR:",DEFAULT_TIMEOUT,CMD);
+ if(r==0) {
+ modem.readBuffer(m,200,1); // leer buffer y guardar en array m[].
+ if(NULL!=(p=strstr(m,"+33"))) {
+ strncpy(phone_number,p,12);
+ pc.puts(phone_number);
+ }
+
+ if(NULL!=(p=strstr(m,"CH1="))) {
+ strncpy(CH1,p+4,1);
+ st[0]=atoi(CH1);
+ relay(1,st[0],1);
+ pc.puts(CH1);
+ }
+
+ if(NULL!=(p=strstr(m,"CH2="))) {
+ strncpy(CH2,p+4,1);
+ st[1]=atoi(CH2);
+ relay(2,st[1],1);
+ pc.puts(CH2);
+ }
+ if(NULL!=(p=strstr(m,"CH3="))) {
+ strncpy(CH3,p+4,1);
+ st[2]=atoi(CH3);
+ relay(3,st[2],1);
+ pc.puts(CH3);
+ }
+ if(NULL!=(p=strstr(m,"CH4="))) {
+ strncpy(CH4,p+4,1);
+ st[3]=atoi(CH4);
+ relay(4,st[3],1);
+ pc.puts(CH4);
+ }
+ if(NULL!=(p=strstr(m,"CH5="))) {
+ strncpy(CH5,p+4,1);
+ st[4]=atoi(CH5);
+ relay(5,st[0],1);
+ pc.puts(CH5);
+ }
+ if(NULL!=(p=strstr(m,"CH6="))) {
+ strncpy(CH6,p+4,1);
+ st[5]=atoi(CH6);
+ relay(6,st[5],1);
+ pc.puts(CH6);
+ }
+ if(NULL!=(p=strstr(m,"CH7="))) {
+ strncpy(CH7,p+4,1);
+ st[6]=atoi(CH7);
+ relay(7,st[6],1);
+ pc.puts(CH7);
+ }
+ if(NULL!=(p=strstr(m,"CH8="))) {
+ strncpy(CH8,p+4,1);
+ st[7]=atoi(CH8);
+ relay(8,st[7],1);
+ pc.puts(CH8);
+ }
+ modem.sendCmd("AT+CMGD=1\r");
+ send_m(phone_number,"comando recibido\r");
+ modem.cleanBuffer(m,200);
+ }
+ //pc.printf("%s",m);
+ return 0;
+}
+
+int main()
+{
+ sim900 sim;
+ // float temperatura;
+ sms sms1;
+ modem.sendCmd("AT+CNMI=3,1,0,0\r");
+ modem.sendCmd("AT+ECHO\r");
+ x=sim.on();
+ pc.printf("%d",x);
+ while(true) {
+ sms1.read_m();
+ sim.read_Temp(2);
+ wait(1);
+ }
+ //x=sim.off();
+ //pc.printf("%d",x);
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Fri Jun 12 10:07:59 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#85a52b7ef44b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jun 12 10:07:59 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/modem/modem.cpp Fri Jun 12 10:07:59 2015 +0000
@@ -0,0 +1,129 @@
+/*
+ modem.cpp
+ 2014 Copyright (c) Seeed Technology Inc. All right reserved.
+
+ Author:lawliet zou(lawliet.zou@gmail.com)
+ 2014-2-24
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "modem.h"
+
+char Modem::readByte(void)
+{
+ return serialModem.getc();
+}
+
+bool Modem::readable()
+{
+ return serialModem.readable();
+}
+
+int Modem::readBuffer(char *buffer,int count, unsigned int timeOut)
+{
+ int i = 0;
+ timeCnt.start();
+ while(1) {
+ while (serialModem.readable()) {
+ char c = serialModem.getc();
+ buffer[i++] = c;
+ if(i >= count)break;
+ }
+ if(i >= count)break;
+ if(timeCnt.read() > timeOut) {
+ timeCnt.stop();
+ timeCnt.reset();
+ break;
+ }
+ }
+ return 0;
+}
+
+void Modem::cleanBuffer(char *buffer, int count)
+{
+ for(int i=0; i < count; i++) {
+ buffer[i] = '\0';
+ }
+}
+
+void Modem::sendCmd(const char* cmd)
+{
+ serialModem.puts(cmd);
+}
+
+void Modem::sendATTest(void)
+{
+ sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD);
+}
+
+bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout)
+{
+ int sum=0;
+ timeCnt.start();
+
+ while(1) {
+ if(serialModem.readable()) {
+ char c = serialModem.getc();
+ sum = (c==resp[sum]) ? sum+1 : 0;
+ if(sum == len)break;
+ }
+ if(timeCnt.read() > timeout) {
+ timeCnt.stop();
+ timeCnt.reset();
+ return false;
+ }
+ }
+ timeCnt.stop();
+ timeCnt.reset();
+
+ return true;
+}
+
+int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type)
+{
+ int len = strlen(resp);
+ int sum=0;
+ timeCnt.start();
+
+ while(1) {
+ if(serialModem.readable()) {
+ char c = serialModem.getc();
+ sum = (c==resp[sum]) ? sum+1 : 0;
+ if(sum == len)break;
+ }
+ if(timeCnt.read() > timeout) {
+ timeCnt.stop();
+ timeCnt.reset();
+ return -1;
+ }
+ }
+ timeCnt.stop();
+ timeCnt.reset();
+
+ if(type == CMD) {
+ while(serialModem.readable()) {
+ char c = serialModem.getc();
+ }
+ }
+
+ return 0;
+}
+
+int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type)
+{
+ sendCmd(data);
+ return waitForResp(resp,timeout,type);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/modem/modem.h Fri Jun 12 10:07:59 2015 +0000
@@ -0,0 +1,128 @@
+/*
+ modem.h
+ 2014 Copyright (c) Seeed Technology Inc. All right reserved.
+
+ Author:lawliet zou(lawliet.zou@gmail.com)
+ 2014-2-24
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef __MODEM_H__
+#define __MODEM_H__
+
+#include "mbed.h"
+
+#define DEFAULT_TIMEOUT 5
+
+enum DataType {
+ CMD = 0,
+ DATA = 1,
+};
+
+/** Modem class.
+ * Used for Modem communication. attention that Modem module communicate with MCU in serial protocol
+ */
+class Modem
+{
+
+public:
+ /** Create Modem Instance
+ * @param tx uart transmit pin to communicate with Modem
+ * @param rx uart receive pin to communicate with Modem
+ * @param baudRate baud rate of uart communication
+ */
+ Modem(PinName tx, PinName rx, int baudRate) : serialModem(tx, rx) {
+ serialModem.baud(baudRate);
+ };
+
+ Serial serialModem;
+
+ /** Power on Modem
+ */
+ void preInit(void);
+
+ /** check serialModem is readable or not
+ * @returns
+ * true on readable
+ * false on not readable
+ */
+ bool readable();
+
+ /** read one byte from serialModem
+ * @returns
+ * one byte read from serialModem
+ */
+ char readByte(void);
+
+ /** read from Modem module and save to buffer array
+ * @param buffer buffer array to save what read from Modem module
+ * @param count the maximal bytes number read from Modem module
+ * @param timeOut time to wait for reading from Modem module
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
+ int readBuffer(char* buffer,int count, unsigned int timeOut);
+
+
+ /** clean Buffer
+ * @param buffer buffer to clean
+ * @param count number of bytes to clean
+ */
+ void cleanBuffer(char* buffer, int count);
+
+ /** send AT command to Modem module
+ * @param cmd command array which will be send to GPRS module
+ */
+ void sendCmd(const char* cmd);
+
+ /**send "AT" to Modem module
+ */
+ void sendATTest(void);
+
+ /** compare the response from GPRS module with a string
+ * @param resp buffer to be compared
+ * @param len length that will be compared
+ * @param timeout waiting seconds till timeout
+ */
+ bool respCmp(const char *resp, unsigned int len, unsigned int timeout);
+
+ /** check Modem module response before time out
+ * @param *resp correct response which Modem module will return
+ * @param *timeout waiting seconds till timeout
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
+ int waitForResp(const char *resp, unsigned int timeout,DataType type);
+
+ /** send AT command to GPRS module and wait for correct response
+ * @param *cmd AT command which will be send to GPRS module
+ * @param *resp correct response which GPRS module will return
+ * @param *timeout waiting seconds till timeout
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
+ int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type);
+
+ Timer timeCnt;
+
+private:
+
+};
+
+#endif