Upload two values to ThingSpeak with a Arduino GSM Module? | Arduino | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Upload two values to ThingSpeak with a Arduino GSM Module?
Avatar
ICar
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
1
June 2, 2017 - 4:05 am
sp_Permalink sp_Print

Halo everyone,

I want to upload 2 values to ThingSpeak in feld1 and feld2. I use an Arduino uno and a GSM module for that. I have already work out some code that work but not as good as I want. I am working with the ThingSpeak.h but I don’t think the library good for the use with the GSM Module. The problem with the code below is that die code stops working after the two values are sended to ThingSpeak. So the loop function just stop there. I don’t know why.  Even when after the function writeFields() some more function came. The getLastReadStatus () doesn’t work also. Do you have any idea how to fix this problem? The loop function and the values should send some values over and over again.

Or do you know other methods to upload more very values to ThingSpeak with the GSM module from Arduino?

thank you in advance.

 

//------------------------------------Bibliotheken--------------------------------------------------------------------------------------------------------------
#include <SPI.h>
#include <GSM.h>
#include "ThingSpeak.h"

//------------------------------------Definitionen für SIM-Karte------------------------------------------------------------------------------------------------
#define PINNUMBER "XXX"
#define GPRS_APN "internet.t-mobile"
#define GPRS_LOGIN "t-mobile"
#define GPRS_PASSWORD "XXX"

//------------------------------------Variablen für SIM-Karte---------------------------------------------------------------------------------------------------
GSMClient client;
GPRS gprs;
GSM gsmAccess;

uint16_t reset = 0;
int Wert1 = 80; /
int Wertalt;
boolean neuerWert = true;
int Wert2 =5;

long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
//------------------------------------Variablen für Datencloud--------------------------------------------------------------------------------------------------

const char * myWriteAPIKey = "XXX";
unsigned long myChannelNumber = 000;

//Zählervariablen für neunstart
int seconds =0;

//CanAktiv erkennen
boolean CANschlaf=false;
boolean GSMaus=true;

 

void setup() {
Serial.begin(9600); // Initialiesiere Serial port (Baudrate:9600)
ThingSpeak.begin(client);
pinMode(5, INPUT); //die Pins 9,6 und 3 sind durch die ThingSpeak lib schon beleckt und werden zum ansteuern einer RGB genutzt. Solten nicht verwendet werden.
digitalWrite(5,HIGH);
pinMode(2, INPUT);
CANschlaf=digitalRead(5);

startGSM();
GSMaus=false;

}

 

void loop() {

// Update ThingSpeak

if(neuerWert== true && Wert1 > 0)
{
if(GSMaus==true)
{
startGSM();
GSMaus=false;
}
ThingSpeak.setField(1,Wert1);
ThingSpeak.setField(2,Wert2);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
int resultCode = ThingSpeak.getLastReadStatus();
if(resultCode == 200)
{
Serial.print("OK");
}
else
{
Serial.print("Error reading message. Status was: ");
Serial.println(resultCode);
}

neuerWert == false;
delay(15000);
Wert1--;

}

// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 )
{
startGSM();
GSMaus=false;
}

lastConnected = client.connected();

}

void startGSM() {
boolean notConnected = true;
Serial.println("try to connect to GSM...");
while(notConnected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) & (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
notConnected = false;
else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("Connected");
}

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
2
June 3, 2017 - 1:22 am
sp_Permalink sp_Print

Does startGSM() claim to succeed?  - Do you get the "connected" message printed out..

Perhaps you can try calling Thingspeak.begin()  only after you connect to GSM rather than before?

If that doesnt work you can avoid the thingspeak library altogether and just construct the URL yourself (its possible that the thingspeak library isnt tested for GSM since the source only refers to WiFi and Ethernet)

Sample code for a web client is https://www.arduino.cc/en/Tutorial/GSMExamplesWebClient

The URL you need can be found on the "Data Import/Export" tab of your channel page on the thingspeak website.

Avatar
ICar
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
3
June 3, 2017 - 5:51 am
sp_Permalink sp_Print

Thang you for your tip. Yes the startGSM() works fine and I get the Massage connected. I try your idea to change the position of Thingspeak.begin() after the startGSM() but the code still stops working after uploading the two values, like bevor. I think your right I should avoid the ThingSpeak.h library. I already working on come code for that. Down below is my code. With this code I am able to upload one value to feld1 on ThingSpeak but not two values. It looks easier than it is. When I call the function updateThingSpeak("field1="+analogValue) again for the fild2 I get a Bad request error.

Any ideas?

//------------------------------------Bibliotheken--------------------------------------------------------------------------------------------------------------           
#include <SPI.h>                           
#include <GSM.h>
//------------------------------------Definitionen für SIM-Karte------------------------------------------------------------------------------------------------           
#define PINNUMBER "XXX"                   
#define GPRS_APN "internet.t-mobile"
#define GPRS_LOGIN "t-mobile"
#define GPRS_PASSWORD "XX"

//------------------------------------Variablen für SIM-Karte---------------------------------------------------------------------------------------------------           
GSMClient client;
GPRS gprs;
GSM gsmAccess;

uint16_t reset = 0;     
int Wert1 = 0;                          //Startwert auf 0 gesetzt
int WEert1_alt;
boolean neuWert = true;

long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
//------------------------------------Variablen für Datencloud--------------------------------------------------------------------------------------------------         

char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "XXX";
const int updateThingSpeakInterval = 16 * 1000;

void setup() {

  Serial.begin(9600);                  // Initialiesiere Serial port (Baudrate:9600)                         
  pinMode(9, OUTPUT);
  pinMode(2, INPUT);
  startGSM();
 
}

void loop() {
 
  String analogValue = String(Wert1, DEC);   
 
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
   
  }
 
  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected)
  {
    Serial.print(Wert1);
    Serial.println("...disconnected");
    Serial.println();
    client.stop();
    neuWert = false;
  }
 
  // Update ThingSpeak
  if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval) && neuWert == true && Wert1 > 0)
  {
    updateThingSpeak("field1="+analogValue);
  }
 
  // Check if Arduino Ethernet needs to be restarted
  if (failedCounter > 3 )
  {
    startGSM();
    }
 
  lastConnected = client.connected();
}                         

void startGSM() {
    boolean notConnected = true;
    Serial.println("try to connect to GSM...");
    while(notConnected) {
        if ((gsmAccess.begin(PINNUMBER) == GSM_READY) & (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
            notConnected = false;
        else {
            Serial.println("Not connected");
            delay(1000);
        }
    }
    Serial.println("Connected");
}

void updateThingSpeak(String tsData)
{
  if (client.connect(thingSpeakAddress, 80))
  {         
    client.print("POST /update HTTP/1.1
");
    client.print("Host: api.thingspeak.com
");
    client.print("Connection: close
");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
    client.print("Content-Type: application/x-www-form-urlencoded
");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("

");

    client.print(tsData);
   
    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();
  }
}

Avatar
piajola

Gold
Forum Posts: 75
sp_UserOfflineSmall Offline
4
June 3, 2017 - 8:19 pm
sp_Permalink sp_Print

Hi ICar,

You say

int Wert1 = 0; //Startwert auf 0 gesetzt

google helped me saying you say start with value=0
but you never made it greater than 0
and you check

if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval) && neuWert == true && Wert1 > 0){

to run your routine void updateThingSpeak(String tsData) that will never run

 

Also you can do (semi pseudo-code)

analogValue = "field1=" + str(Wert1) + "field2=" + str(Wert2)
...
...
updateThingSpeak(analogValue);

 

Hope this is some helpWink

Good luck (&& | ask again)

Avatar
ICar
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
5
June 4, 2017 - 6:19 am
sp_Permalink sp_Print

Hi piajola,

You are absolutely right with the “int Wert1=0”. That was a volatile mistake from me.

I am still working on the problem to upload two variables to ThingSpeak. The code example you give me (analogValue = "field1=" + str(Wert1) + "field2=" + str(Wert2) )looks good but how is the right implementation for that. I try a lot variation of that but still no idea. What did you mean with str?

Thank you very much for your help.

Avatar
piajola

Gold
Forum Posts: 75
sp_UserOfflineSmall Offline
6
June 4, 2017 - 5:56 pm
sp_Permalink sp_Print

Hi ICar,

str is "string" the idea is concatenate one string from 2 strings plus 2 numbers. (convert the number to string)

It depends in the "type" of number int, float, etc.

My particular case is I have 2 numbers from my sensor

      float valHumidity;
      float valTemperature;

and thingspeak wants to receive one string that my "internet device" must compose. So my string is

    altSer.print(F("GET /update?api_key=")); // 20 chars
    altSer.print(apiKey);                            // 16 chars
    altSer.print(F("&field1="));                   // 8 chars
    altSer.print(String(valTemperature));      // 5 chars
    altSer.print(F("&field2="));                   // 8 chars
    altSer.print(String(valHumidity));            // 5 chars
    altSer.print(F("

"));                    // 4 chars

because also I must say how many characters I am sending. Capital F so arduino put the fixed strings in flash and use less RAM

      "&field1="+String(valTemperature))+"&field2="+String(valHumidity))

is the same string, but as I did is cleaner to see 2 years after to repeat

My device get the string and do whatever needed to send "really to Internet"

I do not know how to use your device but I think it is "more or less" the same thing

 

Hope still making sense Wink

Avatar
ICar
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
7
June 5, 2017 - 7:18 am
sp_Permalink sp_Print

Hi piajola,

Thank you very much for your help. It works now! Great idea to put both variables in one string. The solution is:

   String analogValue = String(Wert1, DEC);

   String analogValue2 = String(Wert2, DEC); 

senden="&field1="+analogValue+"&field2="+ analogValue2; 

and after this you can just call the “updateThingSpeak(senden);” function.

I am really happy now. Thank you again and great job.

Avatar
piajola

Gold
Forum Posts: 75
sp_UserOfflineSmall Offline
8
June 5, 2017 - 5:06 pm
sp_Permalink sp_Print

Hi ICar,

Glad to know your status ... Cool

Welcome to the exclusive club of Iot(ers) I hope for a long membership

And help someone you know you can help

Smile

Avatar
Mahathirz
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
9
November 13, 2017 - 9:20 am
sp_Permalink sp_Print

Hi Icar, 

Can I have your email cause I wanna do the same project like you but know nothing. Seems like your coding used some Deutsch language which I kinda not understood. Confused

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: cstapels
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

yeseniara11

RomanFug

JamesPes

RobertBak

ScottSmiva

Amourhotte

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee