● Проект 34: Организация подключения к сети Интернет с помощью модуля Ai-Thinker A6
#define OK 1
#define NOTOK 2
#define TIMEOUT 3
#define RST 2
#define A6board Serial1
#define A6baud 115200
#define SERIALTIMEOUT 3000
char end_c[2];
void setup() {
A6board.begin(A6baud); // порт Serial1
Serial.begin(115200); // порт Serial
// ctrlZ
end_c[0] = 0x1a;
end_c[1] = '\0';
Serial.println("Start");
}
void loop()
{
Serial.println("Waiting for command");
if (Serial.available())
switch (Serial.read())
{ // проверка команды на отправку данных
case 's':
Serial.println("-Post value to Sparkfun-");
// получение аналоговых данных A0
float batt = (float)analogRead(A0)*5.165/594.0;
// вызов процедуры отправки данных
sendSparkfunGSM(1, batt);
break;
}
if ( A6board.available())
Serial.write( A6board.read());
delay(2000);
}
///отправка данных на sparkfun()///
bool sendSparkfunGSM(byte sparkfunType, float value1) {
String host = "data.sparkfun.com";
String publicKey = "9b1nm53N68cDAOvVEJgK";
String privateKey = "xv9NwElKY7ibJ57Vdaje";
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
A6command("AT+CGATT?", "OK", "yy", 20000, 2);
A6command("AT+CGATT=1", "OK", "yy", 20000, 2);
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
A6command("AT+CSTT=\"internet.mts.ru\",\"mts\",\"mts\"", "OK", "yy", 20000, 2); //bring up wireless connection
A6command("AT+CGDCONT=1,\"IP\",\"internet.mts.ru\"", "OK", "yy", 20000, 2);
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
A6command("AT+CGACT=1,1", "OK", "yy", 10000, 2);
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
A6command("AT+CIFSR", "OK", "yy", 20000, 2); //get IP adress
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
A6command("AT+CIPSTART=\"TCP\",\"" + host + "\",80", "CONNECT OK", "yy", 25000, 2); //start up the connection
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
// отправка данных на сервер
A6command("AT+CIPSEND", ">", "yy", 10000, 1);
delay(500);
A6board.print("GET /input/");
A6board.print(publicKey);
A6board.print("?private_key=");
A6board.print(privateKey);
A6board.print("&battery=");
A6board.print(value1, 2);
A6board.print(" HTTP/1.1");
A6board.print("\r\n");
A6board.print("HOST: ");
A6board.print(host);
A6board.print("\r\n");
A6board.print("\r\n");
Serial.print("GET /input/");
Serial.print(publicKey);
Serial.print("?private_key=");
Serial.print(privateKey);
Serial.print("&battery=");
Serial.print(value1, 2);
Serial.print(" HTTP/1.1");
Serial.print("\r\n");
Serial.print("HOST: ");
Serial.print(host);
Serial.print("\r\n");
Serial.print("\r\n");
A6command(end_c, "HTTP/1.1", "yy", 30000, 1);
A6board.println(end_c); //sending ctrlZ
unsigned long entry = millis();
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
A6command("AT+CIPCLOSE", "OK", "yy", 15000, 1); //sending
A6command("AT+CIPSTATUS", "OK", "yy", 10000, 2);
delay(100);
Serial.println("-End-");
}
byte A6waitFor(String response1, String response2, int timeOut) {
unsigned long entry = millis();
int count = 0;
String reply = A6read();
byte retVal = 99;
do {
reply = A6read();
if (reply != "") {
Serial.print((millis() - entry));
Serial.print(" ms ");
Serial.println(reply);
}
} while ((reply.indexOf(response1) + reply.indexOf(response2) == -2) && millis() - entry < timeOut );
if ((millis() - entry) >= timeOut) {
retVal = TIMEOUT;
} else {
if (reply.indexOf(response1) + reply.indexOf(response2) > -2) retVal = OK;
else retVal = NOTOK;
}
return retVal;
}
byte A6command(String command, String response1, String response2, int timeOut, int repetitions) {
byte returnValue = NOTOK;
byte count = 0;
while (count < repetitions && returnValue != OK) {
A6board.println(command);
Serial.print("Command: ");
Serial.println(command);
if (A6waitFor(response1, response2, timeOut) == OK) {
returnValue = OK;
} else returnValue = NOTOK;
count++;
}
return returnValue;
}
bool A6begin() {
A6board.println("AT+CREG?");
byte hi = A6waitFor("1,", "5,", 1500);
while ( hi != OK) {
A6board.println("AT+CREG?");
hi = A6waitFor("1,", "5,", 1500);
}
if (A6command("AT&F0", "OK", "yy", 5000, 2) == OK) {
if (A6command("ATE0", "OK", "yy", 5000, 2) == OK) {
if (A6command("AT+CMEE=2", "OK", "yy", 5000, 2) == OK)
return OK;
else return NOTOK;
}
}
}
void ShowSerialData()
{
unsigned long entry = millis();
while ( A6board.available() != 0 && millis() - entry < SERIALTIMEOUT)
Serial.println(A6board.readStringUntil('\n'));
}
String A6read() {
String reply = "";
if (A6board.available()) {
reply = A6board.readString();
}
return reply;
}
#include <ESP8266WiFi.h>
const char* ssid = "my_point ";
const char* password = "my_point pass ";
const char* host = "data.sparkfun.com";
const char* streamId = "---------------------";
const char* privateKey = "---------------------";
void setup() {
Serial.begin(115200);
delay(10000);
// подсоединение к WiFi точке доступа
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
float value = 0;
void loop() {
delay(10000);
Serial.print("connecting to ");
Serial.println(host);
// создаем TCP соединение
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// создаем строку URL
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&battery=";
value=3.3*analogRead(A0)/1024;
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// отправить данные на сервер
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// ответ сервера
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}