torsdag den 8. december 2016

Using C# .NET for auto-responding to SurveyMonkey surveys


If you need to do auto-responding to SurveyMonkey surveys, you can perchance use the below C# code as a source of inspiration.

I used it for testing a survey that we had running internally. Please bear in mind that your company's SurveyMonkey-subscription might put a cap on the number of survey-responses.

Basically it works by utilizing the Selenium library (download it care of Nuget) for web-page testing. I used the Chrome web-driver because it didn't store cookies or history for the browser session. I also included a unique temporary value for when accessing the survey, or I would get the "you've already responded"-message.

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 58; i++)
            {
                RespondToSurvey();
                Console.WriteLine(i);
            }
        }

        private static void RespondToSurvey()
        {
            OpenQA.Selenium.Chrome.ChromeDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver();

            string baseUrl = $@"https://da.surveymonkey.com/r/?tempValue=" + DateTime.Now.Ticks;

            driver.Navigate().GoToUrl(baseUrl + "/");

            Actions actions = new Actions(driver);
            IWebElement radioBtn = driver.FindElementById("72374030_573987619");
            actions.MoveToElement(radioBtn).Click().Perform();

            var element = driver.FindElement(By.Name("surveyForm"));
            element.Submit();

            driver.Close();
        }
    }

søndag den 4. december 2016

Connecting an ESP8266-12 to a DS1820 thermometer and do a http post data to the internet.

Here's how to connect an ESP8266-12 to a DS1820 thermometer and perform a

Pre-requisites! I'll be using these components:

@ A standard ESP8266-12 on a breakout-board such as the one shown.
@ Three 4.7k resistors.
@ A 3v battery (I use a CR123a) for powering the ESP826612.
@ A DS1820 (i use the DO-92 packaged version below)

The following requirements should be met:

@ All connections, including those on the The ESP 8266-12 on the breakout-board, are solid and conduct power as they should.
@ The battery holds at least 2.8v of power.
@ The ESP8266-12 has been programmed with the following sketch:



#include OneWire.h
#include DallasTemperature.h
#include ESP8266WiFi.h
#include ESP8266WiFiMulti.h
#include ESP8266HTTPClient.h

#define ONEWIRE_PIN 13

OneWire oneWire(ONEWIRE_PIN);
DallasTemperature sensors(&oneWire);

boolean TempSensorAvailable = false;
DeviceAddress TempSensor;
float tempCtry1;
float tempCtry2;

//AP definitions
#define AP_SSID "your wifi-network name here"
#define AP_PASSWORD "wifi password here"

void setup() {

//enable this to test from the arduino serial monitor
Serial.begin(74880);

sensors.begin();

Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" OneWire device(s).");

// report parasite power requirements
Serial.print("Parasite power: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");

if (!sensors.getAddress(TempSensor, 0)) {
Serial.println("No OneWire Device Found");
} else {
TempSensorAvailable = true;
Serial.println("OneWire Device Found");
sensors.setResolution(TempSensor, 12);
}
}

void loop() {

wifiConnect();
postTemperature();


delay(60 * 1000);
}

void postTemperature()
{
sensors.requestTemperatures(); // Get temprature
tempCtry1 = sensors.getTempC(TempSensor); // save temprature
sensors.requestTemperatures(); // Get temprature
tempCtry2 = sensors.getTempC(TempSensor); // save temprature

HTTPClient http;
http.begin("http://");
http.addHeader("Content-Type", "application/json");
String postMessagePart1 = String("{ 'sensorId' : 'L15-Out1', 'temperature' : '");
String postMessagePart2 = String("', 'postAttempts' : '");
String postMessagePart3 = String("', 'batteryVoltage' : '");
String postMessagePart4 = String("' }");
String postMessage = postMessagePart1 + ((tempCtry1+tempCtry2)/2) + postMessagePart2 + retries + postMessagePart3 + vdd + postMessagePart4 ;
int httpCode = http.POST(postMessage);
Serial.print("http result:");
Serial.println(httpCode);

http.writeToStream(&Serial);
http.end();

if ( httpCode != 200 && httpCode != 201)
{
delay(1000);
postTemperature();
}
}

void wifiConnect()
{
Serial.print("Connecting to AP");
WiFi.begin(AP_SSID, AP_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
}



Don't forget to import the libraries into the arduino IDE environment.
Given the above is in order, go ahead and connect the components as per the following pictures:



- The RESET and GPIO16 pins should be connected - this enables the ESP 8266-12 to wake up from deep sleep mode.
- The GPIO0 and GPIO2 should be connected to VCC with a 4.7k resistor in the middle. This is to prevent a so-called 'zombie-mode', in which the ESP8266-12 has trouble waking up from deep sleep.
- The data-line and the VCC line of the DS1820 should be joined by a 4.7k resistor, or the temperature will not be read.
- The data-line of the DS1820 should be connected to the GPIO13-pin of the stand-alone ESP8266-12, as this corresponds with the "#define ONEWIRE_PIN 13" statement of the code.


With the ESP connected like so, my ESP8266-12 happily does a http post to my web-service every 60 seconds, before repeating the cycle.

You should put the ESP8266-12 into deep sleep mode if you power your thermometer via battery.