我有一个项目,其中我使用Arduino从两个来源收集传感器数据,然后使用TX-> RX和RX-> TX将它们发送到ESP8266。因为我使用自动连接将ESP连接到WiFi,所以无法使用AT Commands和SoftwareSerial。 通过输入我的wifi名称和密码以及ThingSpeak Write API密钥并将它们存储在ESP的EPROM中,我已经能够使ESP连接到wifi并将信息发送到ThingSpeak。然后,我使用以下代码将传感器信息以字符串形式从Arduino发送到ESP。
const int batteryInPin = A1;
sensorBatValue = analogRead(batteryInPin);
sensorOutValue = map(sensorBatValue,0,1024,0,225);
const int waterInPin = A3;
int waterSensorInValue;//reading our water lever sensor
int waterSensorOutValue;//conversion of water sensor value
waterSensorInValue = analogRead(waterInPin);
waterSensorOutValue = map(waterSensorInValue,0,1024,0,225);
f = String('<')+String("Hi")+String(',')+String(waterSensorOutValue)+String(',')+String(sensorBatValue)+String('>');
Serial.print(f);
然后我使用以下代码在ESP上获取信息,并将其发送给ThingSpeak
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromPC[numChars] = {0};
int floatFromPC = 0;
int floatFromPC2 = 0;
boolean newData = false;
char Password[36]="";
long itt = 500;
long itt2 = 500;
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
long itt =floatFromPC;
long itt2 = floatFromPC2;
long itt =floatFromPC;
long itt2 = floatFromPC2;
EEPROM.begin(512);
Serial.println(""); //Goto next line, as ESP sends some garbage when you reset it
Serial.print(char(EEPROM.read(addr))); //Read from address 0x00
addr++; //Increment address
Serial.print(char(EEPROM.read(addr))); //Read from address 0x01
addr++; //Increment address
Serial.println(char(EEPROM.read(addr))); //Read from address 0x02
//Read string from eeprom
String www;
//Here we dont know how many bytes to read it is better practice to use some terminating character
//Lets do it manually www.circuits4you.com total length is 20 characters
for(int i=0;i<16;i++)
{
www = www + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F
}
Serial.print(www); //Print the text on serial monitor
if (client.connect(defaultHost,80))
{ // "184.106.153.149" or api.thingspeak.com
itt++; //Replace with a sensor reading or something useful
String postStr = www;
postStr +="&field1=";
postStr += String(itt);
postStr +="&field2=";
postStr += String(itt2);
postStr += "\r\n\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+String (www)+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n\n");
client.print(postStr);
Serial.println("% send to Thingspeak");
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
floatFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC2 = atoi(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
Serial.print("Float ");
Serial.println(floatFromPC2);
}
正如我所说,我不使用AT命令将其发送给ThingSpeak。
起初,我在Arduino Uno R3上做得很好,但是由于尺寸和假定的能源效率,我决定选择Arduino Pro Mini。 但是,现在将Arduino Pro Mini以完全相同的代码和相同的布线连接到ESP时,它不起作用。[code] [/ code]
我是否需要使用其他代码才能使其正常工作? ESP确实允许我输入信息并连接到wifi,并且确实在适当的时候将信息发送到ThingSpeak。我不知道是否从Arduino Pro Mini将信息发送到ESP,但是当应该发送f字符串时,我没有看到闪烁的灯光。在ThingSpeak中,发布的水位为1,电池电量为0。
任何想法将不胜感激。