我正在尝试使用Edison更新Thingspeak,它只更新数据一次。
这是我的代码:
#include <WiFi.h>
#include <SPI.h>
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com"; //TS
String writeAPIKey = "**************"; //TS
const int updateThingSpeakInterval = 10 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
char ssid[] = "*******"; // your network SSID (name)
char pass[] = "********"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// initialize the library instance:
WiFiClient client;
String stringVal = "";
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
int a;
//char dtostrfbuffer[20];
float tempC;
float temp =3;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// printWifiStatus();
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
// printCurrentNet();
// printWifiData();
}
void loop() {
a = analogRead(temp);
tempC = (5.0 * a * 1000)/1024.0;
float cel = tempC/10;
// converts temp to string
stringVal += String(int(cel))+ "."+String(getDecimal(cel));
//char buf[16];
//String strTemp = floatToString(16 , cel , 5);
//Serial.println(strTemp);
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop(); }
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1="+ stringVal);
Serial.print( stringVal);
Serial.println("C");
delay(1000);
Serial.println();
}
lastConnected = client.connected();
}
//function to extract decimal part of float
long getDecimal(float val)
{
int intPart = int(val);
long decPart = 100*(val-intPart); //I am multiplying by 100 assuming that the foat values will have a maximum of 3 decimal places
//Change to match the number of decimal places you need
if(decPart>0)return(decPart); //return the decimal part of float number if it is available
else if(decPart<0)return((-1)*decPart); //if negative, multiply by -1
else if(decPart=0)return(00); //return 0 if decimal part of float number is not available
}
void updateThingSpeak(String stringVal)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print( stringVal.length());
client.print("\n\n");
client.print( stringVal);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
为什么它只更新一次数据?
答案 0 :(得分:0)
我使用Edison遇到了同样的问题,我通过更改:
修复了示例github代码if (!client.connected() && lastConnected)
对此:(不再是&#39;!&#39;)
if (client.connected() && lastConnected)
我发现整个样本的设计非常奇怪,我调整了很多。 我有一个工作示例和一篇文章here