Hi,
I’m using the Wifi shied and a Mega. I’m adding in LEDs (each of my LEDs need three pins one each for RGB) via the output pins, this prevents my code making post requests to a server. The post worked without LEDs. As I understand it, the Wifi shield uses pins 4,7,10,11,12 and 13. I was using some of these and connection attempts were flickering the LEDs so I changed pins. Still I get no response from the server. When I use the code with no pins at all the server responds as expected.
I would think the pin setting is affecting the shield but as far as I know it should not.
I’d appreciate any suggestions.
Code would not fit in message length. Here it is
#include <WiFi.h>
boolean isWPA=!true; //if requires authentication on WPA/WPA2
char ssid[] = "MrNetwork"; // your network SSID (name)
int windData[24];//init wind array
char pass[] = "pass";
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "www.energyelephant.com";
const int redT = 3; // Analog output pin that the red LED is attached to
const int greenT = 6;
const int blueT = 5;
const int auxVHigh=8;//extra 5 v pin
int rValT=0;//value to set led red to
int gValT=0;
int bValT=0;
boolean isOffT=false;//if trafic led is currently flashing
const int redF = 0; // Analog output pin that the red LED is attached to
const int greenF = 1;
const int blueF = 2;
int rValF=0;//value to set led red to
int gValF=0;
int bValF=0;
boolean isOffF=false;//if F led is currently flashing
const int redFlashThreshold=0;//% at which red flashes is about to change
const int redThreshold=10;
const int yellowThreshold=20;
const int greenThreshold=40;
const int updateTime=32000;//how often poll wifi for new info
const int flashFrequency=2000;//how long should led be on/off when flashing
boolean isFlashing=false;//if on of the leds is flashing needs to poll loop faster
int pollCount=0;//so tjhat wifi only every few loops
void setup() {
digitalWrite(auxVHigh, HIGH);
for(int a=0;a<24;a++)
{
windData[a]=0;
}
Serial.begin(9600);
while (!Serial) {
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
// Connect to network:
if(isWPA)
{
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
//if a wpa network suply password
status = WiFi.begin(ssid, pass);
}
else
{
Serial.print("Attempting to connect to unsecure SSID: ");
Serial.println(ssid);
//if not wpa assume no encryption
status = WiFi.begin(ssid);
}
// wait 10 seconds for connection:
delay(10000);
}
}
void loop() {
boolean tempIsFlashing=false;//see if either should be flashing
if(pollCount%50==0)
{//only do this every few as it seems to flicker leds on 8,9,10,11
for(int a=0;a<24;a++)
{
windData[a]=-1;
}
while(windData[0]==-1)
{//while not connected
parsePostData(getWindDataFromServer());//get the data and break it up in to int[]
}
pollCount=0;
}
pollCount++;
//windData[0]=-1;//test override
//windData[1]=41;//test override
//calculate the colour of current wind
if (windData[0]<=redFlashThreshold)
{
tempIsFlashing=true;
//Serial.println("red flash set for T");
if(isOffT)
{//if is off turn on
isOffT=!isOffT;
//should be red
rValT=255;
gValT=0;
bValT=0;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
}
else
{//turn off
isOffT=!isOffT;
//should be red
rValT=0;
gValT=0;
bValT=0;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
}
}
else if(windData[0]<=redThreshold)
{//should be red
rValT=255;
gValT=0;
bValT=0;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
//Serial.println("red set for T");
//delay(updateTime);
}
else if(windData[0]<=yellowThreshold)
{//should be yellow
rValT=255;
gValT=125;
bValT=5;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
// Serial.println("yellow set for T");
//delay(updateTime);
}
else if(windData[0]<=greenThreshold)
{//should be green
rValT=0;
gValT=255;
bValT=0;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
//Serial.println("green set for T");
//delay(updateTime);
}
else
{
tempIsFlashing=true;
//Serial.println("green flash set for T");
if(isOffT)
{//if is off turn on
isOffT=!isOffT;
//should be green
rValT=0;
gValT=255;
bValT=0;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
}
else
{//turn off
isOffT=!isOffT;
//should be green
rValT=0;
gValT=0;
bValT=0;
analogWrite(redT, rValT);
analogWrite(greenT, gValT);
analogWrite(blueT, bValT);
}
}
//calculate the colour of next wind
if(windData[1]<=redFlashThreshold)
{
tempIsFlashing=true;
//Serial.println("green flash set for F");
if(isOffF)
{//if is off turn on
isOffF=!isOffF;
//should be green
rValF=0;
gValF=255;
bValF=0;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
}
else
{//turn off
isOffF=!isOffF;
//should be green
rValF=0;
gValF=0;
bValF=0;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
}
}
else if(windData[1]<=redThreshold)
{//should be red
rValF=255;
gValF=0;
bValF=0;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
Serial.println("red set for F");
//delay(updateTime);
}
else if(windData[1]<=yellowThreshold)
{//should be yellow
rValF=255;
gValF=125;
bValF=5;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
///Serial.println("yellow set for F");
//delay(updateTime);
}
else if(windData[1]<=greenThreshold)
{//should be green
rValF=0;
gValF=255;
bValF=0;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
//Serial.println("green set for F");
//delay(updateTime);
}
else
{
tempIsFlashing=true;
//Serial.println("green flash set for F");
if(isOffF)
{//if is off turn on
isOffF=!isOffF;
//should be green
rValF=0;
gValF=255;
bValF=0;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
}
else
{//turn off
isOffF=!isOffF;
//should be green
rValF=0;
gValF=0;
bValF=0;
analogWrite(redF, rValF);
analogWrite(greenF, gValF);
analogWrite(blueF, bValF);
}
}
//see how long to wait
isFlashing=tempIsFlashing;
if(isFlashing)
{//if an led is flashing need to update soon enough to flash it so
delay(flashFrequency);
}
else
{//wait max otherwise
delay(updateTime);
}
}
/*
*get the wind data from server
*/
String getWindDataFromServer()
{
WiFiClient client;
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server posting:");
// Make a HTTP request: ////
String PostData="action=tabledata&date=0&task=wind&thumb=0";
Serial.println(PostData);
client.println("POST /wp-admin/admin-ajax.php");
client.println("Host: www.energyelephant.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
////
//client.println("Connection: close");
}
else {
Serial.println("connection failed");
}
String response="";
//delay(1000);//wait for response
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
response=response+c;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
return response;
}