Quantcast
Channel: ThingSpeak IoT Community - Forum: Arduino
Viewing all articles
Browse latest Browse all 172

ICar on Upload two values to ThingSpeak with a Arduino GSM Module?

$
0
0

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();
  }
}


Viewing all articles
Browse latest Browse all 172

Trending Articles