test program for SIM808 GPRS + JPEG Camera library for LinkSprite LS-Y201 camera +MPU9150

Dependencies:   JPEGCamera MPU9150_DMP_Nucleo QuaternionMath mbed

Fork of JPEGCamera_SIM808_STM32F401RE by 涂 桂旺

Committer:
tgw
Date:
Wed Jun 21 08:30:26 2017 +0000
Revision:
3:f307a059affe
Parent:
2:9ad661c031a4
Child:
4:c4c42e7c2298
LSY201+SIM808

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thesane 0:0c9cdf074ba1 1
thesane 0:0c9cdf074ba1 2 #include "mbed.h"
tgw 3:f307a059affe 3 #include <string>
tgw 3:f307a059affe 4
thesane 0:0c9cdf074ba1 5 #include "JPEGCamera.h"
thesane 0:0c9cdf074ba1 6
tgw 3:f307a059affe 7 #define buf_max 256
tgw 3:f307a059affe 8
tgw 3:f307a059affe 9 Serial pc(SERIAL_TX, SERIAL_RX); // SERIAL_TX(D1), SERIAL_RX(D0) read from bluetooth module VERIFIED
tgw 3:f307a059affe 10 Serial sim808(PA_9, PA_10); // TX(D8), RX(D2) to camera
tgw 3:f307a059affe 11 JPEGCamera camera(PA_11, PA_12); // TX6, RX6 to camera
tgw 3:f307a059affe 12
tgw 3:f307a059affe 13 //I2C_SCL--PB_8(D15)
tgw 3:f307a059affe 14 //I2C_SDA--PB_9(D14)
tgw 3:f307a059affe 15
tgw 3:f307a059affe 16 DigitalIn mybutton(USER_BUTTON);//PC_13
tgw 3:f307a059affe 17 DigitalOut myled(LED1);//PA_5(D13)
tgw 3:f307a059affe 18 DigitalOut sim_power(D4);//PB_5 //复位SIM808
thesane 0:0c9cdf074ba1 19
thesane 0:0c9cdf074ba1 20 //Camera time
thesane 0:0c9cdf074ba1 21 Timer timer;
thesane 0:0c9cdf074ba1 22
tgw 3:f307a059affe 23 int rec_cnt = 0; //receive count
tgw 3:f307a059affe 24 char result[buf_max];
tgw 3:f307a059affe 25
tgw 3:f307a059affe 26 void sim_callback()
tgw 3:f307a059affe 27 {
tgw 3:f307a059affe 28 char x;
tgw 3:f307a059affe 29 x = sim808.getc();
tgw 3:f307a059affe 30 result[rec_cnt++]= x;
tgw 3:f307a059affe 31 pc.putc(x);
tgw 3:f307a059affe 32 if(rec_cnt>=buf_max-1)rec_cnt=0;
tgw 3:f307a059affe 33 }
tgw 3:f307a059affe 34 bool sim_wait(void)
tgw 3:f307a059affe 35 {
tgw 3:f307a059affe 36 char *p;
tgw 3:f307a059affe 37 int x=20;
tgw 3:f307a059affe 38 rec_cnt = 0;
tgw 3:f307a059affe 39 memset(result,0,buf_max*sizeof(char));
tgw 3:f307a059affe 40 while(x)
tgw 3:f307a059affe 41 {
tgw 3:f307a059affe 42 wait(1);
tgw 3:f307a059affe 43 p=strstr(result,"OK");
tgw 3:f307a059affe 44 if(p)
tgw 3:f307a059affe 45 {
tgw 3:f307a059affe 46 return true;
tgw 3:f307a059affe 47 }
tgw 3:f307a059affe 48 x--;
tgw 3:f307a059affe 49 }
tgw 3:f307a059affe 50 return false;
tgw 3:f307a059affe 51 }
tgw 3:f307a059affe 52
tgw 3:f307a059affe 53 void sim_gprs_updata(string s)
tgw 3:f307a059affe 54 {
tgw 3:f307a059affe 55 pc.printf("%s",s); //发送内容
tgw 3:f307a059affe 56 pc.putc(0x1a);//CtrlZ
tgw 3:f307a059affe 57
tgw 3:f307a059affe 58 sim808.printf("%s",s); //发送内容
tgw 3:f307a059affe 59 sim808.putc(0x1a);
tgw 3:f307a059affe 60
tgw 3:f307a059affe 61 if(sim_wait()) {
tgw 3:f307a059affe 62 pc.printf("TCP send OK\r\n");
tgw 3:f307a059affe 63 } else {
tgw 3:f307a059affe 64 pc.printf("TCP send Field\r\n");
tgw 3:f307a059affe 65 }
tgw 3:f307a059affe 66 }
tgw 3:f307a059affe 67
tgw 3:f307a059affe 68 void sim_gprs_init(void)
tgw 3:f307a059affe 69 {
tgw 3:f307a059affe 70
tgw 3:f307a059affe 71 pc.printf("AT\r\n");
tgw 3:f307a059affe 72 sim808.printf("AT\r\n");
tgw 3:f307a059affe 73
tgw 3:f307a059affe 74 if(sim_wait()) {
tgw 3:f307a059affe 75 pc.printf("SIM AT OK.\r\n");//
tgw 3:f307a059affe 76 }
tgw 3:f307a059affe 77 else{
tgw 3:f307a059affe 78 pc.printf("Reset SIM808.\r\n");//
tgw 3:f307a059affe 79 sim_power.write(0);
tgw 3:f307a059affe 80 wait(3);
tgw 3:f307a059affe 81 sim_power.write(1);
tgw 3:f307a059affe 82 wait(19);
tgw 3:f307a059affe 83 }
tgw 3:f307a059affe 84
tgw 3:f307a059affe 85 pc.printf("ATE0\r\n");//关闭回显
tgw 3:f307a059affe 86 sim808.printf("ATE0\r\n");//关闭回显
tgw 3:f307a059affe 87 wait(1);
tgw 3:f307a059affe 88
tgw 3:f307a059affe 89 pc.printf("AT+CPIN?\r\n");
tgw 3:f307a059affe 90 sim808.printf("AT+CPIN?\r\n");//SIM卡状态
tgw 3:f307a059affe 91 if(sim_wait()) {
tgw 3:f307a059affe 92 pc.printf("SIM CARD CPIN OK\r\n");
tgw 3:f307a059affe 93 } else {
tgw 3:f307a059affe 94 pc.printf("SIM CARD CPIN Field\r\n");
tgw 3:f307a059affe 95 }
tgw 3:f307a059affe 96
tgw 3:f307a059affe 97 pc.printf("AT+CSQ\r\n");
tgw 3:f307a059affe 98 sim808.printf("AT+CSQ\r\n");//信号状态
tgw 3:f307a059affe 99 if(sim_wait()) {
tgw 3:f307a059affe 100 pc.printf("SIM CARD signal OK\r\n");
tgw 3:f307a059affe 101 } else {
tgw 3:f307a059affe 102 pc.printf("SIM CARD signal Field\r\n");
tgw 3:f307a059affe 103 }
tgw 3:f307a059affe 104
tgw 3:f307a059affe 105 pc.printf("AT+CREG?\r\n"); //判断SIM卡网络是否注册成功
tgw 3:f307a059affe 106 sim808.printf("AT+CREG?\r\n"); //判断SIM卡网络是否注册成功
tgw 3:f307a059affe 107 if(sim_wait()) {
tgw 3:f307a059affe 108 pc.printf("SIM CARD REG OK\r\n");
tgw 3:f307a059affe 109 } else {
tgw 3:f307a059affe 110 pc.printf("SIM CARD REG Field\r\n");
tgw 3:f307a059affe 111 }
tgw 3:f307a059affe 112
tgw 3:f307a059affe 113 pc.printf("AT+CGATT?\r\n"); //GPRS附着状态
tgw 3:f307a059affe 114 sim808.printf("AT+CGATT?\r\n"); //GPRS附着状态
tgw 3:f307a059affe 115
tgw 3:f307a059affe 116 if(sim_wait()) {
tgw 3:f307a059affe 117 pc.printf("SIM CGATT OK\r\n");
tgw 3:f307a059affe 118 } else {
tgw 3:f307a059affe 119 pc.printf("SIM CGATT Field\r\n");
tgw 3:f307a059affe 120 }
tgw 3:f307a059affe 121
tgw 3:f307a059affe 122 //-----------------------组合命令------------------------------------------------------------
tgw 3:f307a059affe 123
tgw 3:f307a059affe 124 pc.printf("AT+CSTT=\"CMNET\"\r\n"); //set APN
tgw 3:f307a059affe 125 sim808.printf("AT+CSTT=\"CMNET\"\r\n"); //set APN
tgw 3:f307a059affe 126
tgw 3:f307a059affe 127 if(sim_wait()) {
tgw 3:f307a059affe 128 pc.printf("SIM set APN OK\r\n");
tgw 3:f307a059affe 129 }
tgw 3:f307a059affe 130 else
tgw 3:f307a059affe 131 {
tgw 3:f307a059affe 132 pc.printf("SIM set APN Field\r\n");
tgw 3:f307a059affe 133 return ;
tgw 3:f307a059affe 134 }
tgw 3:f307a059affe 135
tgw 3:f307a059affe 136 pc.printf("AT+CIICR\r\n"); //wireless link 建立无线链路
tgw 3:f307a059affe 137 sim808.printf("AT+CIICR\r\n"); //wireless link 建立无线链路
tgw 3:f307a059affe 138
tgw 3:f307a059affe 139 if(sim_wait()) {
tgw 3:f307a059affe 140 pc.printf("SIM wireless link OK\r\n");
tgw 3:f307a059affe 141 } else {
tgw 3:f307a059affe 142 pc.printf("SIM wireless link Field\r\n");
tgw 3:f307a059affe 143 }
tgw 3:f307a059affe 144
tgw 3:f307a059affe 145 wait(15);//等待建立无线链路
tgw 3:f307a059affe 146
tgw 3:f307a059affe 147 pc.printf("AT+CIFSR\r\n"); //获取本地IP
tgw 3:f307a059affe 148 sim808.printf("AT+CIFSR\r\n"); //获取本地IP
tgw 3:f307a059affe 149
tgw 3:f307a059affe 150 pc.printf("AT+CIPSTART=\"TCP\",\"122.5.17.146\",\"9980\"\r\n"); //TCP set 建立TCP连接
tgw 3:f307a059affe 151 sim808.printf("AT+CIPSTART=\"TCP\",\"122.5.17.146\",\"9980\"\r\n"); //TCP set 建立TCP连接
tgw 3:f307a059affe 152
tgw 3:f307a059affe 153 if(sim_wait())
tgw 3:f307a059affe 154 {
tgw 3:f307a059affe 155 pc.printf("SIM TCP set OK\r\n");
tgw 3:f307a059affe 156
tgw 3:f307a059affe 157 wait(10);//等待建立TCP链路
tgw 3:f307a059affe 158
tgw 3:f307a059affe 159 pc.printf("AT+CIPSEND\r\n"); //send direction
tgw 3:f307a059affe 160 sim808.printf("AT+CIPSEND\r\n"); //send direction
tgw 3:f307a059affe 161
tgw 3:f307a059affe 162 wait(1);
tgw 3:f307a059affe 163 sim_gprs_updata("TPC SEND:hello,sim808 tcp test ok!");
tgw 3:f307a059affe 164 } else
tgw 3:f307a059affe 165 {
tgw 3:f307a059affe 166 pc.printf("SIM TCP set Field\r\n");
tgw 3:f307a059affe 167 }
tgw 3:f307a059affe 168
tgw 3:f307a059affe 169 }
tgw 3:f307a059affe 170 void sim_gprs_relink(void)
tgw 3:f307a059affe 171 {
tgw 3:f307a059affe 172 pc.printf("AT+CIPCLOSE\r\n");
tgw 3:f307a059affe 173 sim808.printf("AT+CIPCLOSE\r\n");//关闭连接
tgw 3:f307a059affe 174 if(sim_wait())
tgw 3:f307a059affe 175 {
tgw 3:f307a059affe 176 pc.printf("SIM TCP CLOSE OK\r\n");
tgw 3:f307a059affe 177 } else
tgw 3:f307a059affe 178 {
tgw 3:f307a059affe 179 pc.printf("SIM TCP CLOSE Field\r\n");
tgw 3:f307a059affe 180 }
tgw 3:f307a059affe 181 pc.printf("AT+CIPSTART=\"TCP\",\"122.5.17.146\",\"9980\"\r\n"); //TCP set 建立TCP连接
tgw 3:f307a059affe 182 sim808.printf("AT+CIPSTART=\"TCP\",\"122.5.17.146\",\"9980\"\r\n"); //TCP set 建立TCP连接
tgw 3:f307a059affe 183
tgw 3:f307a059affe 184 if(sim_wait())
tgw 3:f307a059affe 185 {
tgw 3:f307a059affe 186 pc.printf("SIM TCP set OK\r\n");
tgw 3:f307a059affe 187
tgw 3:f307a059affe 188 wait(5);//等待建立TCP链路
tgw 3:f307a059affe 189
tgw 3:f307a059affe 190 pc.printf("AT+CIPSEND\r\n"); //send direction
tgw 3:f307a059affe 191 sim808.printf("AT+CIPSEND\r\n"); //send direction
tgw 3:f307a059affe 192
tgw 3:f307a059affe 193 wait(1);
tgw 3:f307a059affe 194 sim_gprs_updata("TPC SEND:hello,sim808 tcp test ok!");
tgw 3:f307a059affe 195 } else
tgw 3:f307a059affe 196 {
tgw 3:f307a059affe 197 pc.printf("SIM TCP set Field\r\n");
tgw 3:f307a059affe 198 pc.printf("Reset SIM808.\r\n");//
tgw 3:f307a059affe 199 sim_power.write(0);
tgw 3:f307a059affe 200 wait(3);
tgw 3:f307a059affe 201 sim_power.write(1);
tgw 3:f307a059affe 202 wait(19);
tgw 3:f307a059affe 203 sim_gprs_init();
tgw 3:f307a059affe 204 }
tgw 3:f307a059affe 205 }
tgw 3:f307a059affe 206
thesane 0:0c9cdf074ba1 207 int main()
thesane 0:0c9cdf074ba1 208 {
thesane 0:0c9cdf074ba1 209 int message = 0;
thesane 0:0c9cdf074ba1 210
tgw 3:f307a059affe 211 pc.baud(115200);
tgw 3:f307a059affe 212 sim808.baud(115200);
tgw 3:f307a059affe 213 sim808.attach(&sim_callback,SerialBase::RxIrq); //serial interrupt attach
thesane 0:0c9cdf074ba1 214
tgw 3:f307a059affe 215 sim_power.write(1);
thesane 0:0c9cdf074ba1 216 //Camera Initialization and picture sizing
thesane 0:0c9cdf074ba1 217 timer.start();
tgw 3:f307a059affe 218 wait(1);
tgw 2:9ad661c031a4 219 if(camera.setPictureSize(JPEGCamera::SIZE160x120))
tgw 2:9ad661c031a4 220 {
tgw 3:f307a059affe 221 pc.printf("camera setPictureSize ok!\r\n");
tgw 2:9ad661c031a4 222 }
tgw 2:9ad661c031a4 223 else
tgw 2:9ad661c031a4 224 {
tgw 3:f307a059affe 225 pc.printf("camera setPictureSize fail!\r\n");
tgw 2:9ad661c031a4 226 }
thesane 0:0c9cdf074ba1 227 wait(3);
thesane 0:0c9cdf074ba1 228 //Continuously wait for commands
tgw 3:f307a059affe 229 sim_gprs_init();
thesane 0:0c9cdf074ba1 230 while(1)
thesane 0:0c9cdf074ba1 231 {
thesane 0:0c9cdf074ba1 232 //Check for bytes over serial from bluetooth/xbee
tgw 3:f307a059affe 233 if(pc.readable())
thesane 0:0c9cdf074ba1 234 {
tgw 3:f307a059affe 235 message=pc.getc();
thesane 0:0c9cdf074ba1 236 //Take a picture (note that this takes a while to complete and will only only work for 999 pictures
tgw 3:f307a059affe 237 if (message==210)//0xd2
thesane 0:0c9cdf074ba1 238 {
thesane 0:0c9cdf074ba1 239 if (camera.isReady())
thesane 0:0c9cdf074ba1 240 {
thesane 0:0c9cdf074ba1 241 if (camera.takePicture())
thesane 0:0c9cdf074ba1 242 {
thesane 0:0c9cdf074ba1 243 int image_size = camera.getImageSize();
thesane 0:0c9cdf074ba1 244 while (camera.isProcessing())
thesane 0:0c9cdf074ba1 245 {
tgw 3:f307a059affe 246 camera.processPicture(pc);
thesane 0:0c9cdf074ba1 247 }
thesane 0:0c9cdf074ba1 248 }
thesane 0:0c9cdf074ba1 249 else
thesane 0:0c9cdf074ba1 250 {
tgw 3:f307a059affe 251 pc.printf("camera takePicture fail!\r\n");
thesane 0:0c9cdf074ba1 252 }
thesane 0:0c9cdf074ba1 253 }
thesane 0:0c9cdf074ba1 254 else
thesane 0:0c9cdf074ba1 255 {
tgw 3:f307a059affe 256 pc.printf("camera isReady fail!\r\n");
thesane 0:0c9cdf074ba1 257 }
thesane 0:0c9cdf074ba1 258 }
thesane 0:0c9cdf074ba1 259 }
tgw 3:f307a059affe 260
tgw 3:f307a059affe 261 if (!mybutton)
tgw 3:f307a059affe 262 {
tgw 3:f307a059affe 263 // time_t seconds = time(NULL);
tgw 3:f307a059affe 264 // //pc.printf("%s", ctime(&seconds));
tgw 3:f307a059affe 265 // char msg[100];
tgw 3:f307a059affe 266 // sprintf(msg,"seconds:%d", ctime(&seconds));
tgw 3:f307a059affe 267 // sim_gprs_updata(msg);
tgw 3:f307a059affe 268 sim_gprs_relink();
tgw 3:f307a059affe 269 }
thesane 0:0c9cdf074ba1 270 }
thesane 0:0c9cdf074ba1 271 }
tgw 3:f307a059affe 272