PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
skrawool 0:3954a906acc2 1 #include "ESP8266.h"
skrawool 0:3954a906acc2 2
skrawool 0:3954a906acc2 3 // Constructor
skrawool 0:3954a906acc2 4 ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) {
skrawool 0:3954a906acc2 5 comm.baud(br);
skrawool 0:3954a906acc2 6 }
skrawool 0:3954a906acc2 7
skrawool 0:3954a906acc2 8 // Destructor
skrawool 0:3954a906acc2 9 ESP8266::~ESP8266() { }
skrawool 0:3954a906acc2 10
skrawool 0:3954a906acc2 11 // Add <CR> + <LF> at the end of the string
skrawool 0:3954a906acc2 12 void ESP8266::AddEOL(char * s) {
skrawool 0:3954a906acc2 13 char k;
skrawool 0:3954a906acc2 14 k = strlen(s); // Finds position of NULL character
skrawool 0:3954a906acc2 15 s[k] = 0x0D; // switch NULL for <CR>
skrawool 0:3954a906acc2 16 s[k + 1] = 0x0A; // Add <LF>
skrawool 0:3954a906acc2 17 s[k + 2] = 0; // Add NULL at the end
skrawool 0:3954a906acc2 18 }
skrawool 0:3954a906acc2 19
skrawool 0:3954a906acc2 20 // Add one ASCII character at the end of the string
skrawool 0:3954a906acc2 21 void ESP8266::AddChar(char * s, char c) {
skrawool 0:3954a906acc2 22 char k;
skrawool 0:3954a906acc2 23 k = strlen(s);
skrawool 0:3954a906acc2 24 s[k] = c;
skrawool 0:3954a906acc2 25 s[k + 1] = 0;
skrawool 0:3954a906acc2 26 }
skrawool 0:3954a906acc2 27
skrawool 0:3954a906acc2 28 // Converts integer number to null-terminated string
skrawool 0:3954a906acc2 29 void ESP8266::itoa(int n, char * s) {
skrawool 0:3954a906acc2 30 char k = 0;
skrawool 0:3954a906acc2 31 char r[11];
skrawool 0:3954a906acc2 32
skrawool 0:3954a906acc2 33 if(n == 0) {
skrawool 0:3954a906acc2 34 s[0] = '0';
skrawool 0:3954a906acc2 35 s[1] = 0;
skrawool 0:3954a906acc2 36 } else {
skrawool 0:3954a906acc2 37 while(n != 0) {
skrawool 0:3954a906acc2 38 r[k]= (n % 10) + '0';
skrawool 0:3954a906acc2 39 n = n / 10;
skrawool 0:3954a906acc2 40 k++;
skrawool 0:3954a906acc2 41 }
skrawool 0:3954a906acc2 42 while(k > 0) {
skrawool 0:3954a906acc2 43 s[n] = r[k - 1] + '0';
skrawool 0:3954a906acc2 44 n++;
skrawool 0:3954a906acc2 45 k--;
skrawool 0:3954a906acc2 46 }
skrawool 0:3954a906acc2 47 s[n] = 0;
skrawool 0:3954a906acc2 48 }
skrawool 0:3954a906acc2 49 }
skrawool 0:3954a906acc2 50
skrawool 0:3954a906acc2 51 // Sends command to ESP8266. Receives the command string
skrawool 0:3954a906acc2 52 void ESP8266::SendCMD(char * s) {
skrawool 0:3954a906acc2 53 AddEOL(s);
skrawool 0:3954a906acc2 54 comm.printf("%s", s);
skrawool 0:3954a906acc2 55 }
skrawool 0:3954a906acc2 56
skrawool 0:3954a906acc2 57 // Resets the ESP8266
skrawool 0:3954a906acc2 58 void ESP8266::Reset(void) {
skrawool 0:3954a906acc2 59 char rs[10];
skrawool 0:3954a906acc2 60 strcpy(rs, "AT+RST");
skrawool 0:3954a906acc2 61 SendCMD(rs);
skrawool 0:3954a906acc2 62 }
skrawool 0:3954a906acc2 63
skrawool 0:3954a906acc2 64 // Receive reply until no character is received after a given timeout in miliseconds
skrawool 0:3954a906acc2 65 void ESP8266::RcvReply(char * r, int to) {
skrawool 0:3954a906acc2 66 Timer t;
skrawool 0:3954a906acc2 67 bool ended = 0;
skrawool 0:3954a906acc2 68 char c;
skrawool 0:3954a906acc2 69
skrawool 0:3954a906acc2 70 strcpy(r, "");
skrawool 0:3954a906acc2 71 t.start();
skrawool 0:3954a906acc2 72 while(!ended) {
skrawool 0:3954a906acc2 73 if(comm.readable()) {
skrawool 0:3954a906acc2 74 c = comm.getc();
skrawool 0:3954a906acc2 75 AddChar(r, c);
skrawool 0:3954a906acc2 76 t.start();
skrawool 0:3954a906acc2 77 }
skrawool 0:3954a906acc2 78 if(t.read_ms() > to) {
skrawool 0:3954a906acc2 79 ended = 1;
skrawool 0:3954a906acc2 80 }
skrawool 0:3954a906acc2 81 }
skrawool 0:3954a906acc2 82 AddChar(r, 0x00);
skrawool 0:3954a906acc2 83 }
skrawool 0:3954a906acc2 84
skrawool 0:3954a906acc2 85 // Gets the AP list. Parameter: the string to receive the list
skrawool 0:3954a906acc2 86 void ESP8266::GetList(char * l) {
skrawool 0:3954a906acc2 87 char rs[15];
skrawool 0:3954a906acc2 88 strcpy(rs, "AT+CWLAP");
skrawool 0:3954a906acc2 89 SendCMD(rs);
skrawool 0:3954a906acc2 90 RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
skrawool 0:3954a906acc2 91 }
skrawool 0:3954a906acc2 92
skrawool 0:3954a906acc2 93 // Joins a Wifi AP. Parameters: SSID and Password (strings)
skrawool 0:3954a906acc2 94 void ESP8266::Join(char * id, char * pwd) {
skrawool 0:3954a906acc2 95 char cmd[255];
skrawool 0:3954a906acc2 96 strcpy(cmd, "AT+CWJAP=");
skrawool 0:3954a906acc2 97 AddChar(cmd, 0x22);
skrawool 0:3954a906acc2 98 strcat(cmd, id);
skrawool 0:3954a906acc2 99 AddChar(cmd, 0x22);
skrawool 0:3954a906acc2 100 AddChar(cmd, 0x2C);
skrawool 0:3954a906acc2 101 AddChar(cmd, 0x22);
skrawool 0:3954a906acc2 102 strcat(cmd, pwd);
skrawool 0:3954a906acc2 103 AddChar(cmd, 0x22);
skrawool 0:3954a906acc2 104 SendCMD(cmd);
skrawool 0:3954a906acc2 105 }
skrawool 0:3954a906acc2 106
skrawool 0:3954a906acc2 107 // Gets ESP IP. Parameter: string to contain IP
skrawool 0:3954a906acc2 108 void ESP8266::GetIP(char * ip) {
skrawool 0:3954a906acc2 109 char cmd[15];
skrawool 0:3954a906acc2 110 strcpy(cmd, "AT+CIFSR");
skrawool 0:3954a906acc2 111 SendCMD(cmd);
skrawool 0:3954a906acc2 112 RcvReply(ip, 2000);
skrawool 0:3954a906acc2 113 }
skrawool 0:3954a906acc2 114
skrawool 0:3954a906acc2 115 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both
skrawool 0:3954a906acc2 116 void ESP8266::SetMode(char mode) {
skrawool 0:3954a906acc2 117 char cmd[15];
skrawool 0:3954a906acc2 118 strcpy(cmd, "AT+CWMODE=");
skrawool 0:3954a906acc2 119 mode = mode + 0x30; // Converts number into corresponding ASCII character
skrawool 0:3954a906acc2 120 AddChar(cmd, mode); // Completes command string
skrawool 0:3954a906acc2 121 SendCMD(cmd);
skrawool 0:3954a906acc2 122 }
skrawool 0:3954a906acc2 123
skrawool 0:3954a906acc2 124 // Quits the AP
skrawool 0:3954a906acc2 125 void ESP8266::Quit(void) {
skrawool 0:3954a906acc2 126 char cmd[15];
skrawool 0:3954a906acc2 127 strcpy(cmd, "AT+CWQAP");
skrawool 0:3954a906acc2 128 SendCMD(cmd);
skrawool 0:3954a906acc2 129 }
skrawool 0:3954a906acc2 130
skrawool 0:3954a906acc2 131 // Sets single connection
skrawool 0:3954a906acc2 132 void ESP8266::SetSingle(void) {
skrawool 0:3954a906acc2 133 char cmd[15];
skrawool 0:3954a906acc2 134 strcpy(cmd, "AT+CIPMUX=0");
skrawool 0:3954a906acc2 135 SendCMD(cmd);
skrawool 0:3954a906acc2 136 }
skrawool 0:3954a906acc2 137
skrawool 0:3954a906acc2 138 // Sets multiple connection
skrawool 0:3954a906acc2 139 void ESP8266::SetMultiple(void) {
skrawool 0:3954a906acc2 140 char rs[15];
skrawool 0:3954a906acc2 141 strcpy(rs, "AT+CIPMUX=1");
skrawool 0:3954a906acc2 142 SendCMD(rs);
skrawool 0:3954a906acc2 143 }
skrawool 0:3954a906acc2 144
skrawool 0:3954a906acc2 145 // Gets connection status. Parameter: string to contain status
skrawool 0:3954a906acc2 146 void ESP8266::GetConnStatus(char * st) {
skrawool 0:3954a906acc2 147 char cmd[15];
skrawool 0:3954a906acc2 148 strcpy(cmd, "AT+CIPSTATUS");
skrawool 0:3954a906acc2 149 SendCMD(cmd);
skrawool 0:3954a906acc2 150 RcvReply(st, 2000);
skrawool 0:3954a906acc2 151 }
skrawool 0:3954a906acc2 152
skrawool 0:3954a906acc2 153 // Starts server mode. Parameter: port to be used
skrawool 0:3954a906acc2 154 void ESP8266::StartServerMode(int port) {
skrawool 0:3954a906acc2 155 char rs[25];
skrawool 0:3954a906acc2 156 char t[4];
skrawool 0:3954a906acc2 157 strcpy(rs, "AT+CIPSERVER=1,");
skrawool 0:3954a906acc2 158 itoa(port, t);
skrawool 0:3954a906acc2 159 strcat(rs, t);
skrawool 0:3954a906acc2 160 SendCMD(rs);
skrawool 0:3954a906acc2 161 }
skrawool 0:3954a906acc2 162
skrawool 0:3954a906acc2 163 // Close server mode.
skrawool 0:3954a906acc2 164 void ESP8266::CloseServerMode(void) {
skrawool 0:3954a906acc2 165 char rs[20];
skrawool 0:3954a906acc2 166 strcpy(rs, "AT+CIPSERVER=0");
skrawool 0:3954a906acc2 167 SendCMD(rs);
skrawool 0:3954a906acc2 168 }