Part Number:ENERGIA
I am working on code related to sending an email through cc3100booster and msp432p401r. I am having an issue in connectivity of wifi in the module when the wifi connection code is provided in void setup(). I am providing mobile's hotspot and it gets connected and disconnected periodically. I require an continuous connection.
When the wifi connection code is written inside loop(), the connection problem is solved but a newer issue is arised i,e, the program is running for email but not for the server [In server program i am sending temperature and pulse count value to thingspeak.com to get a graphical representation of my data.]
moreover, despite of being present in loop, it executes only once. its a complete arcane issue for me. I am linking code below. please help me out with this.
#define TEMBOO_ACCOUNT "xxxxxxx" // your Temboo account name
#define TEMBOO_APP_KEY_NAME "xxxxxxx" // your Temboo app name
#define TEMBOO_APP_KEY "xxxxxxxx" // your Temboo app key
#define WIFI_SSID "xxxxxx" //hotspot name
#define WIFI_PASSWORD "xxxxxxx" //hotspot password
#ifndef __CC3200R1M1RGC__
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <Temboo.h>
#endif
const
String GMAIL_USER_NAME =
"xxxxxxx"
;
//sender's email
const
String GMAIL_APP_PASSWORD =
"xxxxxx"
;
//sender's app password
const
String TO_EMAIL_ADDRESS =
"xxxxxxx"
;
//receiver's email
boolean attempted =
false
;
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,33,12,24,5,25,6,26,27,8,28);
float
temp;
float
volt;
int
sensorValue1;
int
sensorValue2;
int
count;
WiFiClient client;
char
server[] =
"api.thingspeak.com"
;
unsigned
long
lastConnectionTime = 0;
// last time you connected to the server, in milliseconds
const
unsigned
long
postingInterval = 10L * 1000L;
// delay between updates, in milliseconds
void
checkconnection()
{
int
wifiStatus = WL_IDLE_STATUS;
// determine if the WiFi Shield is present.
Serial.print(
"\n\nShield:"
);
if
(WiFi.status() == WL_NO_SHIELD) {
Serial.println(
"FAIL"
);
// if there's no WiFi shield, stop here.
while
(
true
);
}
while
(wifiStatus != WL_CONNECTED) {
Serial.print(
"WiFi:"
);
wifiStatus = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
if
(wifiStatus == WL_CONNECTED) {
Serial.println(
"OK"
);
}
else
{
Serial.println(
"FAIL"
);
}
delay(1000);
}
Serial.println(
"Waiting for an ip address"
);
while
(WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(
"."
);
delay(300);
}
Serial.println(
"\nIP Address obtained"
);
// We are connected and have an IP address.
// Print the WiFi status.
printWifiStatus();
}
void
serverprog()
{
while
(client.available()) {
char
c = client.read();
Serial.write(c);
}
if
(millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
void
httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if
(client.connect(server, 80)) {
Serial.println(
"connecting..."
);
char
msgg[200];
sprintf(msgg,
"GET /update?api_key=xxxxxxxxxxxxxxxx&field1=%f&field2=%d"
,temp,count);
//have to make account on thingspeak.com and create channel then create 2 fields. then copy the WRITE API key in place of xxxxxxxxx
// send the HTTP PUT request:
client.println(msgg);
client.println(
"Host: api.thingspeak.com"
);
client.println(
"Connection: close"
);
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else
{
// if you couldn't make a connection:
Serial.println(
"connection failed"
);
}
}
void
printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print(
"SSID: "
);
Serial.println(WiFi.SSID());
// print your WiFi IP address:
IPAddress ip = WiFi.localIP();
Serial.print(
"IP Address: "
);
Serial.println(ip);
// print the received signal strength:
long
rssi = WiFi.RSSI();
Serial.print(
"signal strength (RSSI):"
);
Serial.print(rssi);
Serial.println(
" dBm"
);
}
void
setup() {
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println(
"processing......"
);
}
void
loop() {
int
sensorValue1 = analogRead(A0);
lcd.clear();
lcd.setCursor(0,2);
lcd.print(
"wait.."
);
/* lcd.setCursor(0,0);
lcd.print("temperature =");
lcd.setCursor(14,0);
lcd.print(temp);*/
Serial.println(sensorValue1);
volt=sensorValue1*3.22;
temp=volt/10;
Serial.println(volt);
Serial.println(
"temp="
);
Serial.println(temp);
delay(10);
int
sensorValue2;
unsigned
long
start=millis();
analogReadResolution(10);
unsigned
long
currentValue;
float
x=0;
int
y;
count=0;
delay(3000);
lcd.clear();
lcd.setCursor(0,2);
lcd.print(
"detecting......."
);
do
{
int
sensorValue2 = analogRead(A1);
if
(sensorValue2 > x)
{
x=sensorValue2;
y=1;
}
else
if
(sensorValue2 < x)
{
x=sensorValue2;
if
(y==1)
{
count++;
y=0;
}
}
currentValue=millis();
}
while
(currentValue-start<=30000);
count=(count*2)/10000;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(
"temperature ="
);
lcd.setCursor(14,0);
lcd.print(temp);
lcd.setCursor(0,2);
lcd.print(
"pulse count ="
);
lcd.setCursor(14,1);
lcd.print(count);
delay(1000);
Serial.println(sensorValue2);
Serial.println(
"pulse count="
);
Serial.println(count);
checkconnection();
delay(3000);
if
(!attempted) {
Serial.println(
"Running SendAnEmail..."
);
TembooChoreo SendEmailChoreo(client);
SendEmailChoreo.begin();
SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);
SendEmailChoreo.setChoreo(
"/Library/Google/Gmail/SendEmail"
);
SendEmailChoreo.addInput(
"Username"
, GMAIL_USER_NAME);
SendEmailChoreo.addInput(
"Password"
, GMAIL_APP_PASSWORD);
SendEmailChoreo.addInput(
"ToAddress"
, TO_EMAIL_ADDRESS);
SendEmailChoreo.addInput(
"Subject"
,
"Recieved An Email???"
);
char
msg[100];
sprintf (msg,
" Body Temperature=%f, Pulse count=%d"
,temp,count);
SendEmailChoreo.addInput(
"MessageBody"
, msg);
unsigned
int
returnCode = SendEmailChoreo.run();
if
(returnCode == 0) {
Serial.println(
"Success! Email sent!"
);
}
else
{
while
(SendEmailChoreo.available()) {
char
c = SendEmailChoreo.read();
Serial.print(c);
}
}
SendEmailChoreo.close();
}
serverprog();
lcd.clear();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | #define TEMBOO_ACCOUNT "xxxxxxx" // your Temboo account name
#define TEMBOO_APP_KEY_NAME "xxxxxxx" // your Temboo app name
#define TEMBOO_APP_KEY "xxxxxxxx" // your Temboo app key
#define WIFI_SSID "xxxxxx" //hotspot name
#define WIFI_PASSWORD "xxxxxxx" //hotspot password
#ifndef __CC3200R1M1RGC__
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <Temboo.h>
#endif
const String GMAIL_USER_NAME = "xxxxxxx" ; //sender's email
const String GMAIL_APP_PASSWORD = "xxxxxx" ; //sender's app password
const String TO_EMAIL_ADDRESS = "xxxxxxx" ; //receiver's email
boolean attempted = false ;
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,33,12,24,5,25,6,26,27,8,28);
float temp; float volt; int sensorValue1; int sensorValue2; int count; WiFiClient client;
char server[] = "api.thingspeak.com" ; unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
void checkconnection() { int wifiStatus = WL_IDLE_STATUS;
// determine if the WiFi Shield is present.
Serial.print( "\n\nShield:" ); Serial.println( "qqqqq" );
if (WiFi.status() == WL_NO_SHIELD) { Serial.println( "xxxxx" );
Serial.println( "FAIL" ); Serial.println( "yyyyy" );
// if there's no WiFi shield, stop here.
while ( true ); } Serial.println( "zzzzzzz" );
while (wifiStatus != WL_CONNECTED) { Serial.print( "WiFi:" ); wifiStatus = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
if (wifiStatus == WL_CONNECTED) { Serial.println( "OK" ); } else { Serial.println( "FAIL" ); } delay(1000); } Serial.println( "Waiting for an ip address" );
while (WiFi.localIP() == INADDR_NONE) { // print dots while we wait for an ip addresss
Serial.print( "." ); delay(300); }
Serial.println( "\nIP Address obtained" ); // We are connected and have an IP address.
// Print the WiFi status.
printWifiStatus(); }
void serverprog() { while (client.available()) { char c = client.read(); Serial.write(c); } if (millis() - lastConnectionTime > postingInterval) { httpRequest(); } }
void httpRequest() { // close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) { Serial.println( "connecting..." ); char msgg[200]; sprintf(msgg, "GET /update?api_key=xxxxxxxxxxxxxxxx&field1=%f&field2=%d" ,temp,count); //have to make account on thingspeak.com and create channel then create 2 fields. then copy the WRITE API key in place of xxxxxxxxx
// send the HTTP PUT request:
client.println(msgg); client.println( "Host: api.thingspeak.com" ); client.println( "Connection: close" ); client.println();
// note the time that the connection was made:
lastConnectionTime = millis(); } else { // if you couldn't make a connection:
Serial.println( "connection failed" ); } }
void printWifiStatus() { // print the SSID of the network you're attached to:
Serial.print( "SSID: " ); Serial.println(WiFi.SSID());
// print your WiFi IP address:
IPAddress ip = WiFi.localIP(); Serial.print( "IP Address: " ); Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI(); Serial.print( "signal strength (RSSI):" ); Serial.print(rssi); Serial.println( " dBm" ); }
void setup() { lcd.begin(16, 2);
Serial.begin(9600); Serial.println( "processing......" );
}
void loop() { int sensorValue1 = analogRead(A0); lcd.clear(); lcd.setCursor(0,2); lcd.print( "wait.." ); /* lcd.setCursor(0,0); lcd.print("temperature ="); lcd.setCursor(14,0); lcd.print(temp);*/
Serial.println(sensorValue1); volt=sensorValue1*3.22; temp=volt/10; Serial.println(volt); Serial.println( "temp=" ); Serial.println(temp); delay(10);
int sensorValue2; unsigned long start=millis(); analogReadResolution(10); unsigned long currentValue; float x=0; int y; count=0; delay(3000); lcd.clear();
lcd.setCursor(0,2); lcd.print( "detecting......." ); do
{ int sensorValue2 = analogRead(A1); if (sensorValue2 > x)
{ x=sensorValue2; y=1;
} else if (sensorValue2 < x)
{ x=sensorValue2; if (y==1) { count++; y=0; } }
currentValue=millis(); } while (currentValue-start<=30000); count=(count*2)/10000; lcd.clear(); lcd.setCursor(0,0); lcd.print( "temperature =" ); lcd.setCursor(14,0); lcd.print(temp); lcd.setCursor(0,2); lcd.print( "pulse count =" ); lcd.setCursor(14,1); lcd.print(count); delay(1000);
Serial.println(sensorValue2); Serial.println( "pulse count=" ); Serial.println(count); checkconnection(); delay(3000);
if (!attempted) {
Serial.println( "Running SendAnEmail..." );
TembooChoreo SendEmailChoreo(client);
SendEmailChoreo.begin();
SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT); SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); SendEmailChoreo.setAppKey(TEMBOO_APP_KEY); SendEmailChoreo.setChoreo( "/Library/Google/Gmail/SendEmail" );
SendEmailChoreo.addInput( "Username" , GMAIL_USER_NAME);
SendEmailChoreo.addInput( "Password" , GMAIL_APP_PASSWORD); SendEmailChoreo.addInput( "ToAddress" , TO_EMAIL_ADDRESS);
SendEmailChoreo.addInput( "Subject" , "Recieved An Email???" ); char msg[100]; sprintf (msg, " Body Temperature=%f, Pulse count=%d" ,temp,count); SendEmailChoreo.addInput( "MessageBody" , msg);
unsigned int returnCode = SendEmailChoreo.run();
if (returnCode == 0) { Serial.println( "Success! Email sent!" ); } else { while (SendEmailChoreo.available()) { char c = SendEmailChoreo.read(); Serial.print(c); } } SendEmailChoreo.close();
} serverprog(); lcd.clear();
} |