iot - I am trying to update data on Thingspeak,but it updates only at once using Edison -
i trying update thingspeak using edison, updates data once.
here code:
#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 update thingspeak (number of seconds * 1000 = interval) char ssid[] = "*******"; // network ssid (name) char pass[] = "********"; // network password int status = wl_idle_status; // wifi radio's status // initialize 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 , wait port open: serial.begin(115200); while (!serial) { ; // wait serial port connect. needed leonardo } // printwifistatus(); // check presence of shield: if (wifi.status() == wl_no_shield) { serial.println("wifi shield not present"); // don't continue: while(true); } // attempt connect wifi network: while ( status != wl_connected) { serial.print("attempting connect wpa ssid: "); serial.println(ssid); // connect wpa/wpa2 network: status = wifi.begin(ssid, pass); // wait 10 seconds connection: delay(10000); } // you're connected now, print out data: serial.print("you're connected network"); // printcurrentnet(); // printwifidata(); } void loop() { = analogread(temp); tempc = (5.0 * * 1000)/1024.0; float cel = tempc/10; // converts temp string stringval += string(int(cel))+ "."+string(getdecimal(cel)); //char buf[16]; //string strtemp = floattostring(16 , cel , 5); //serial.println(strtemp); // print update response serial monitor if (client.available()) { char c = client.read(); serial.print(c); } // disconnect 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 extract decimal part of float long getdecimal(float val) { int intpart = int(val); long decpart = 100*(val-intpart); //i multiplying 100 assuming foat values have maximum of 3 decimal places //change match number of decimal places need if(decpart>0)return(decpart); //return decimal part of float number if available else if(decpart<0)return((-1)*decpart); //if negative, multiply -1 else if(decpart=0)return(00); //return 0 if decimal part of float number 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 thingspeak..."); serial.println(); failedcounter = 0; } else { failedcounter++; serial.println("connection thingspeak failed ("+string(failedcounter, dec)+")"); serial.println(); } } else { failedcounter++; serial.println("connection thingspeak failed ("+string(failedcounter, dec)+")"); serial.println(); lastconnectiontime = millis(); } }
why update data once?
i ran same issue using edison , fixed example github code changing:
if (!client.connected() && lastconnected)
to this: (no more '!')
if (client.connected() && lastconnected)
i found whole sample oddly designed , tweaked lot. have working example , article here
Comments
Post a Comment